Skip to content

Commit

Permalink
#232: updated for Package Manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kray-G committed Aug 16, 2021
1 parent cdf31f1 commit fe8535c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 93 deletions.
1 change: 0 additions & 1 deletion include/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,6 @@ typedef struct kx_options_ {
int exception_detail_info:1;
int debug_mode:1;
int debug_step:1;
int dev_mode:1;
uint16_t case_threshold;
uint16_t max_call_depth;
} kx_options_t;
Expand Down
158 changes: 111 additions & 47 deletions lib/exec/kip.kx
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ class KinxPackageRepositoryManager(cfgMgr_) {

class KinxPackageLibraryManager(repoMgr_) {

const DEVELOP_FILE_NAME = "development.kip";
var cfgMgr_;
var installed_;

Expand Down Expand Up @@ -406,6 +407,7 @@ class KinxPackageLibraryManager(repoMgr_) {
l.push({
latest: packdef && packdef == ver,
version: ver,
development: File.exists(verpath / DEVELOP_FILE_NAME),
});
};
}
Expand All @@ -419,7 +421,10 @@ class KinxPackageLibraryManager(repoMgr_) {
if (pkgname.isUndefined || pkgname == name) {
var l = getVersionList(name);
System.print(name, ": ");
Kip.info(l.map { => _1.latest ? _1.version.bold() : _1.version.yellow() });
Kip.info(l.map { => _1.development
? (_1.latest ? _1.version.green().bold() : _1.version.green())
: (_1.latest ? _1.version.bold() : _1.version.yellow())
});
}
}
};
Expand Down Expand Up @@ -499,7 +504,7 @@ class KinxPackageLibraryManager(repoMgr_) {
// Auto detaction of exec candidate.
var candidates = [];
Directory.walk(packagedir / "bin") { &(f)
if (f.extension() == ".kx" && !f.stem().endsWith("-dev")) {
if (f.extension() == ".kx") {
candidates.push(f.filename());
}
};
Expand Down Expand Up @@ -549,60 +554,27 @@ class KinxPackageLibraryManager(repoMgr_) {
return success;
}

private uninstallAll(name) {
var isDevelopmentMode = name.endsWith("-dev");
var exe = File.exists($kinxpath / "kxrepl.exe");
var packagebase = $pkgpath / name;
Directory.walk(packagebase) { &(packagedir)
Directory.walk(packagedir / "bin") { &(f)
if (f.extension() == ".kx" && !f.stem().endsWith("-dev")) {
var dst = $kinxpath / f.stem() + (isDevelopmentMode ? "-dev" : "") + (exe ? ".exe" : "");
if (File.exists(dst)) {
if (removeFile(dst)) {
Kip.progressln("Removed an executable of %s" % dst.filename());
}
}
}
};
};
cfgMgr_.setPackageDef(name);
if (removeDir(packagebase)) {
Kip.progressln("All versions of %s is successfully uninstalled" % name);
}
return true;
}

public uninstall(name, version) {
if (name.isUndefined) {
Kip.errorln("No package name specified");
return false;
}
if (version.isUndefined) {
Kip.errorln("No version for %{name.bold()} specified");
Kip.errorln("If you want to uninstall all versions, specify 'all'");
return false;
}
if (version == "all") {
return uninstallAll(name);
}

public uninstallVersion(name, version, isDevOnly) {
// Remove a specific binary.
var isDevelopmentMode = name.endsWith("-dev");
var exe = File.exists($kinxpath / "kxrepl.exe");
var packagebase = $pkgpath / name;
var packagedir = packagebase / version;
if (!File.exists(packagedir)) {
Kip.errorln("No package of %s(%s)" % name % version);
return false;
}
if (isDevOnly && !File.exists(packagedir / DEVELOP_FILE_NAME)) {
Kip.errorln("Not a development package of %s(%s)" % name % version);
return false;
}
var vers = [];
Directory.walk(packagebase) { &(ver)
vers.push(ver.filename());
};
if (vers.length() == 1 && vers[0] == version) {
Directory.walk(packagedir / "bin") { &(f)
if (f.extension() == ".kx") {
var dst = $kinxpath / f.stem() + (isDevelopmentMode ? "-dev" : "") + (exe ? ".exe" : "");
var dst = $kinxpath / f.stem() + (exe ? ".exe" : "");
if (File.exists(dst)) {
if (removeFile(dst)) {
Kip.progressln("Removed an executable of %s" % dst.filename());
Expand All @@ -627,6 +599,45 @@ class KinxPackageLibraryManager(repoMgr_) {
return true;
}

private uninstallAll(name) {
var exe = File.exists($kinxpath / "kxrepl.exe");
var packagebase = $pkgpath / name;
Directory.walk(packagebase) { &(packagedir)
Directory.walk(packagedir / "bin") { &(f)
if (f.extension() == ".kx") {
var dst = $kinxpath / f.stem() + (exe ? ".exe" : "");
if (File.exists(dst)) {
if (removeFile(dst)) {
Kip.progressln("Removed an executable of %s" % dst.filename());
}
}
}
};
};
cfgMgr_.setPackageDef(name);
if (removeDir(packagebase)) {
Kip.progressln("All versions of %s is successfully uninstalled" % name);
}
return true;
}

public uninstall(name, version) {
if (name.isUndefined) {
Kip.errorln("No package name specified");
return false;
}
if (version.isUndefined) {
Kip.errorln("No version for %{name.bold()} specified");
Kip.errorln("If you want to uninstall all versions, specify 'all'");
return false;
}
if (version == "all") {
return uninstallAll(name);
}

return uninstallVersion(name, version);
}

public update(name) {
if (name) {
// Install the latest version of the package.
Expand All @@ -639,17 +650,66 @@ class KinxPackageLibraryManager(repoMgr_) {
return false;
}

public devinstall() {
private flushProgressText(buf) {
if (buf.find("\n") >= 0) {
var txts = buf.split("\n");
buf = txts.pop();
txts.each {
if (_1 =~ /(?i)error/) {
Kip.errorln(_1.replace(/(?i)error/, &(g) => g[0].string.bold().red()));
} else if (_1 =~ /(?i)(warning|warn)/) {
Kip.warningln(_1.replace(/(?i)(warning|warn)/, &(g) => g[0].string.bold().yellow()));
} else {
Kip.progressln(_1);
}
};
}
return buf;
}

private devinstimpl(buildopt, output) {
var buildkx = $pwd / "build/build.kx";
if (!File.exists(buildkx)) {
Kip.errorln("Not found: build/build.kx to install a developing package");
return false;
return;
}
var kinx = $kinxpath / "kinx";
var p = new Process([kinx, buildkx, "--dev"], { out: $stdout, err: $stderr }).run();
p.wait();
var buf;
var [r1, w1] = new Pipe();
var p1 = new Process([kinx, buildkx, buildopt], { out: w1 }).run();
w1.close();
while (p1.isAlive() || r1.peek() > 0) {
buf += r1.read();
if (output && buf.length() > 0) {
buf = flushProgressText(buf);
}
}
if (output) {
flushProgressText(buf);
}
return buf;
}

public devinstall() {
var r = devinstimpl("--inst", true);
if (r.isUndefined) {
return false;
}
return true;
}

public devuninstall() {
var namever = devinstimpl("--version");
if (namever.isUndefined) {
return false;
}
var [name, version] = namever.trim().split('\t');
if (name.isString && version.isString) {
Kip.progressln("Removing the versions(%s) of %s" % version % name);
return uninstallVersion(name, version, true);
}
return false;
}
}

class KinxPackageManager {
Expand Down Expand Up @@ -716,9 +776,12 @@ class KinxPackageManager {
case "search": case "s":
mgr.search(args[2]);
return true;
case "devinstall":
case "devinst":
mgr.devinstall();
return true;
case "devuninst":
mgr.devuninstall();
return true;
}
return false;
}
Expand All @@ -743,7 +806,8 @@ class KinxPackageManager {
Kip.info(" install <key> [<ver>] Installs a specified package.");
Kip.info(" uninstall <key> <ver>|all Uninstalls a specified package.");
# Kip.info(" update [<key>] Installs the latest version.");
Kip.info(" devinstall Installs a develping package.");
Kip.info(" devinst Installs a develpment package.");
Kip.info(" devuninst Uninstalls a develpment package.");
Kip.info(" list Lists up installed packages.");
}

Expand Down
3 changes: 3 additions & 0 deletions lib/std/kxfile.kx
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ File.open = _function(name, mode, func) {
}
}
};
File.touch = _function(name) {
File.open(name, File.TEXT|File.WRITE, &() => {});
};
44 changes: 32 additions & 12 deletions lib/std/pkg/Develop.kx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@

class DevelopPackageUpdater(pkgname, version) {
class DevelopPackageUpdater(pkgname, version, options_) {

const DEVELOP_FILE_NAME = "development.kip";
const DEF_FILE_NAME = "kxpackage.def";
var defFile_, root_, candidates_;

private initialize() {
pkgname += "-dev";
defFile_ = $pkgpath / DEF_FILE_NAME;
root_ = $pkgpath / pkgname / version;
@root = root_;
System.println("$PKGPATH: ", root_);
}

private saveDef(data) {
Expand Down Expand Up @@ -95,7 +94,7 @@ class DevelopPackageUpdater(pkgname, version) {
if (candidates_.isDefined) {
var exe = File.exists($kinxpath / "kxrepl.exe");
candidates_.each {
var dst = $kinxpath / _1.stem() + "-dev" + (exe ? ".exe" : "");
var dst = $kinxpath / _1.stem() + (exe ? ".exe" : "");
if (File.exists(dst) && removeFile(dst)) {
System.println("Removed an executable of %{dst.filename().bold().yellow()}");
}
Expand All @@ -117,7 +116,7 @@ class DevelopPackageUpdater(pkgname, version) {
var exe = exefile.extension() == ".exe";
if (File.exists(src)) {
candidates_.each {
var dst = $kinxpath / _1.stem() + "-dev" + (exe ? ".exe" : "");
var dst = $kinxpath / _1.stem() + (exe ? ".exe" : "");
if (File.exists(dst)) {
removeFile(dst);
}
Expand All @@ -131,11 +130,24 @@ class DevelopPackageUpdater(pkgname, version) {
}

public update(code) {
if (options_.version) {
System.println(pkgname + "\t" + version + "\n");
return;
}

if (!options_.force && File.exists(root_) && !File.exists(root_ / DEVELOP_FILE_NAME)) {
System.println("Found the directory for the release version.");
System.println("ERROR: Cancelled the installation.".bold().red());
return;
}

System.println("$PKGPATH: ", root_);
removeExecutableCandidates();
if (File.exists(root_)) {
removeDir(root_);
}
code(this);
File.touch(root_ / DEVELOP_FILE_NAME);
makeDevExecutable();

var def = loadDef();
Expand All @@ -150,7 +162,7 @@ class ReleasePackageArchiver(pkgname, version) {
var zip_ = new Zip($pwd / "package.zip", File.WRITE);

public dircopy(file1, file2) {
if (file1 != "dev" && File.isDirectory(file1)) {
if (File.isDirectory(file1)) {
System.println("Adding a directory : ", file1);
zip_.addFile(file1);
}
Expand All @@ -164,17 +176,25 @@ class ReleasePackageArchiver(pkgname, version) {

class PackageUpdater(args, pkgname, version) {

var opt, config;
while (opt = System.getopt(args, "d", { dev: 'd' })) {
var opt, options = { release: true };
while (opt = System.getopt(args, "iv", { inst: 'i', version: 'v' })) {
switch (opt.type) {
case 'd':
config.dev = true;
case 'i':
options.devinst = true;
options.release = false;
break;
case 'v':
options.version = true;
options.release = false;
break;
case 'f':
options.force = true;
break;
}
}

if (config.dev) {
return new DevelopPackageUpdater(pkgname, version);
if (!options.release) {
return new DevelopPackageUpdater(pkgname, version, options);
}

return new ReleasePackageArchiver(pkgname, version);
Expand Down
14 changes: 0 additions & 14 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,6 @@ static void pop_package_version(const char *key)
}
}

static int set_package_devmode(const char *pkgname, int pos)
{
if (g_parse_ctx && g_parse_ctx->options.dev_mode) {
kx_strbuf[pos++] = '-';
kx_strbuf[pos++] = 'd';
kx_strbuf[pos++] = 'e';
kx_strbuf[pos++] = 'v';
}
kx_strbuf[pos] = 0;
return pos;
}

static int set_package_version(const char *pkgname, int pos)
{
khint_t k = kh_get(package, g_packages, pkgname);
Expand Down Expand Up @@ -308,12 +296,10 @@ static int process_using(void)
while (pos < POSMAX && (kx_is_filechar(kx_lexinfo) || kx_lexinfo.ch == '*')) {
if (is_package) {
if (is_package_version == 0 && kx_lexinfo.ch == '.') {
pos = set_package_devmode(pkgname, pos);
pkgname = kx_const_str(g_parse_ctx, kx_strbuf + is_package_namepos);
pos = set_package_version(pkgname, pos);
is_package_version = 2;
} else if (is_package_version == 0 && kx_lexinfo.ch == '(') {
pos = set_package_devmode(pkgname, pos);
pkgname = kx_const_str(g_parse_ctx, kx_strbuf + is_package_namepos);
is_package_version = 1;
kx_strbuf[pos++] = PATH_DELCH;
Expand Down
Loading

0 comments on commit fe8535c

Please sign in to comment.