From fe8535c1c7370c21d3a88644d354ce875c9bcfd9 Mon Sep 17 00:00:00 2001 From: Kray-G Date: Mon, 16 Aug 2021 22:03:31 +0900 Subject: [PATCH] #232: updated for Package Manager. --- include/ir.h | 1 - lib/exec/kip.kx | 158 +++++++++++++++++++++++++++++------------ lib/std/kxfile.kx | 3 + lib/std/pkg/Develop.kx | 44 ++++++++---- src/lexer.c | 14 ---- src/main_kxcmd.c | 9 +-- src/mainlib.c | 12 ---- 7 files changed, 148 insertions(+), 93 deletions(-) diff --git a/include/ir.h b/include/ir.h index 40ccfa4ad..1baac7db3 100644 --- a/include/ir.h +++ b/include/ir.h @@ -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; diff --git a/lib/exec/kip.kx b/lib/exec/kip.kx index ba61f9c61..d579b426d 100644 --- a/lib/exec/kip.kx +++ b/lib/exec/kip.kx @@ -310,6 +310,7 @@ class KinxPackageRepositoryManager(cfgMgr_) { class KinxPackageLibraryManager(repoMgr_) { + const DEVELOP_FILE_NAME = "development.kip"; var cfgMgr_; var installed_; @@ -406,6 +407,7 @@ class KinxPackageLibraryManager(repoMgr_) { l.push({ latest: packdef && packdef == ver, version: ver, + development: File.exists(verpath / DEVELOP_FILE_NAME), }); }; } @@ -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()) + }); } } }; @@ -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()); } }; @@ -549,45 +554,8 @@ 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; @@ -595,6 +563,10 @@ class KinxPackageLibraryManager(repoMgr_) { 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()); @@ -602,7 +574,7 @@ class KinxPackageLibraryManager(repoMgr_) { 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()); @@ -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. @@ -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 { @@ -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; } @@ -743,7 +806,8 @@ class KinxPackageManager { Kip.info(" install [] Installs a specified package."); Kip.info(" uninstall |all Uninstalls a specified package."); # Kip.info(" update [] 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."); } diff --git a/lib/std/kxfile.kx b/lib/std/kxfile.kx index 771cd1d4b..33b5b366a 100644 --- a/lib/std/kxfile.kx +++ b/lib/std/kxfile.kx @@ -80,3 +80,6 @@ File.open = _function(name, mode, func) { } } }; +File.touch = _function(name) { + File.open(name, File.TEXT|File.WRITE, &() => {}); +}; diff --git a/lib/std/pkg/Develop.kx b/lib/std/pkg/Develop.kx index 6de3b6b82..49a9c59e1 100644 --- a/lib/std/pkg/Develop.kx +++ b/lib/std/pkg/Develop.kx @@ -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) { @@ -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()}"); } @@ -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); } @@ -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(); @@ -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); } @@ -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); diff --git a/src/lexer.c b/src/lexer.c index 653d95860..f4c6d2d57 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -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); @@ -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; diff --git a/src/main_kxcmd.c b/src/main_kxcmd.c index 12319e55b..840e0530f 100644 --- a/src/main_kxcmd.c +++ b/src/main_kxcmd.c @@ -44,13 +44,8 @@ char *make_exec_name(const char *av0) int start = 0, end = 0, len = 0; MAKE_START_END_LEN(av0, start, end, len); char *cmdname = kx_calloc(len + (7/* --exec: */ + 2), sizeof(char)); - if (ends_with(av0, "-dev") || ends_with(av0, "-dev.exe")) { - strcpy(cmdname, "--dev:"); - strncpy(cmdname + 6, av0 + start, len - 4); - } else { - strcpy(cmdname, "--exec:"); - strncpy(cmdname + 7, av0 + start, len); - } + strcpy(cmdname, "--exec:"); + strncpy(cmdname + 7, av0 + start, len); return cmdname; } diff --git a/src/mainlib.c b/src/mainlib.c index 9638ab405..13a29acb1 100644 --- a/src/mainlib.c +++ b/src/mainlib.c @@ -271,11 +271,6 @@ const char *search_exec_file(kx_context_t *ctx, const char *execname) for (khint_t k = 0; k < kh_end(g_packages); ++k) { if (kh_exist(g_packages, k)) { const char *pkgname = kh_key(g_packages, k); - if (ctx->options.dev_mode) { - if (strstr(pkgname, "-dev") == NULL) { - continue; - } - } package_t *p = kh_value(g_packages, k); ks_clear(ksv); ks_appendf(ksv, "%s%clib%cpackage%c%s%c%s%cbin%c%s.kx", get_kinx_path(), PATH_DELCH, PATH_DELCH, PATH_DELCH, pkgname, PATH_DELCH, p->vers, PATH_DELCH, PATH_DELCH, execname); @@ -378,13 +373,6 @@ DllExport int do_main(int ac, char **av) goto END_OF_OPT; } break; - } else if (!strcmp(lname, "dev")) { - ctx->options.dev_mode = 1; - if (param[0]) { - execname = param; - goto END_OF_OPT; - } - break; } break; case 'q':