From 42d8720d6fa336245e5d873a3bfe20208a6a3f7e Mon Sep 17 00:00:00 2001 From: mah0x211 Date: Fri, 5 Apr 2024 09:30:15 +0900 Subject: [PATCH 1/4] Remove install-lj and install-rocks commands --- help.go | 19 +++++++------- install.go | 50 ++++++++++++++++--------------------- main.go | 73 ++++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 81 insertions(+), 61 deletions(-) diff --git a/help.go b/help.go index 06f50f3..9dffac9 100644 --- a/help.go +++ b/help.go @@ -44,9 +44,6 @@ Commands: vers List available versions ls List installed versions install Install and use a of lua - install-lj Install and use a of luajit - install-rocks Install and use a of lurocks in - current lua environment use Use a of lua use-lj Use a of luajit use-rocks Use a of luajit @@ -54,16 +51,18 @@ Commands: Note: The specifier of the above commands can be specified as follows; - lenv install latest ; that picks the latest version - lenv install 5 ; that picks the latest minor version and patch version - lenv install 5.4 ; that picks the latest patch version - - In addition, the install and install-lj commands can be used to install - luarocks at the same time with the following specifier; + lenv install latest ; that picks the latest version + lenv install 5 ; that picks the latest minor version and patch version + lenv install 5.4 ; that picks the latest patch version + lenv install lj-v2.1 ; that picks the version of luajit lenv install latest:latest ; that picks the the latest version of lua and - ; luarocks + ; luarocks in current lua environment lenv install :latest ; that picks the the latest version of luarocks + in current lua environment + + If a version of luarocks is specified, the operation will target the lua + environment currently in use. uninstall Uninstall a of lua uninstall-lj Uninstall a of luajit diff --git a/install.go b/install.go index deb4653..3c3dc89 100644 --- a/install.go +++ b/install.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "path/filepath" + "runtime" "strings" ) @@ -239,6 +240,20 @@ func installRocks(instdir string, cfg *TargetConfig) error { } func installLuaJit(instdir string, opts []string) error { + if runtime.GOOS == "darwin" { + // append MACOSX_DEPLOYMENT_TARGET=10.8 by default on macOS platform + found := false + for _, opt := range opts { + found = strings.HasPrefix(opt, "MACOSX_DEPLOYMENT_TARGET") + if found { + break + } + } + if !found { + opts = append(opts, "MACOSX_DEPLOYMENT_TARGET=10.8") + } + } + // clean up working directory if err := DoExec("make", append([]string{"clean"}, opts...)...); err != nil { return err @@ -422,37 +437,14 @@ func doInstall(cfg *TargetConfig, item *VerItem, opts []string) { UseInstalledVersion(cfg, item.Ver) } -func CmdInstall(cfg *TargetConfig, opts []string) { - // check target version - if len(opts) == 0 || (cfg != LuaRocksCfg && opts[0] == ":") { - CmdHelp(1, "no version specified") - } - ver := opts[0] - - // check : - var rocksVer string - if cfg != LuaRocksCfg { - if delim := strings.Index(ver, ":"); delim != -1 { - rocksVer = ver[delim+1:] - ver = ver[:delim] - } - } - - var verItem *VerItem - if len(ver) > 0 { - verItem = PickTargetVersionItem(cfg, ver) - } - var rocksItem *VerItem - if len(rocksVer) > 0 { - rocksItem = PickTargetVersionItem(LuaRocksCfg, rocksVer) - } - - if verItem != nil { - doInstall(cfg, verItem, opts[1:]) +func CmdInstall(opts []string) { + target := PickTargetVersion(opts[0]) + if target.Lua != nil { + doInstall(target.Lua.Config, target.Lua.Version, opts[1:]) } - if rocksItem != nil { + if target.LuaRocks != nil { ResolveCurrentDir() CheckLuaRocksRootDir() - doInstall(LuaRocksCfg, rocksItem, opts) + doInstall(target.LuaRocks.Config, target.LuaRocks.Version, opts[1:]) } } diff --git a/main.go b/main.go index f6f743f..be2c716 100644 --- a/main.go +++ b/main.go @@ -263,6 +263,56 @@ func CheckLuaRocksRootDir() { } } +type Target struct { + Config *TargetConfig + Version *VerItem +} + +type TargetVersion struct { + Lua *Target + LuaRocks *Target +} + +func PickTargetVersion(vers string) *TargetVersion { + // check target version + if len(vers) == 0 || vers == ":" { + CmdHelp(1, "no version specified") + } + + // check : + var rocksVer string + if delim := strings.Index(vers, ":"); delim != -1 { + rocksVer = vers[delim+1:] + vers = vers[:delim] + } + + target := &TargetVersion{} + if len(vers) > 0 { + if strings.HasPrefix(vers, "lj-") { + // if `lj-' prefix is specified, then the target is LuaJIT version + target.Lua = &Target{ + Config: LuaJitCfg, + Version: PickTargetVersionItem(LuaJitCfg, vers[3:]), + } + } else { + // otherwise the target is Lua version. + target.Lua = &Target{ + Config: LuaCfg, + Version: PickTargetVersionItem(LuaCfg, vers), + } + } + } + + if len(rocksVer) > 0 { + target.LuaRocks = &Target{ + Config: LuaRocksCfg, + Version: PickTargetVersionItem(LuaRocksCfg, rocksVer), + } + } + + return target +} + func start() { argv := os.Args[1:] if len(argv) > 0 { @@ -296,28 +346,7 @@ func start() { CmdList() case "install": - CmdInstall(LuaCfg, argv[1:]) - - case "install-lj": - argv = argv[1:] - if runtime.GOOS == "darwin" { - // set MACOSX_DEPLOYMENT_TARGET=10.8 by default - found := false - for _, arg := range argv { - found = strings.HasPrefix(arg, "MACOSX_DEPLOYMENT_TARGET") - if found { - break - } - } - if !found { - argv = append(argv, "MACOSX_DEPLOYMENT_TARGET=10.8") - } - } - CmdInstall(LuaJitCfg, argv) - - case "install-rocks": - CheckLuaRocksRootDir() - CmdInstall(LuaRocksCfg, argv[1:]) + CmdInstall(argv[1:]) case "uninstall": CmdUninstall(LuaCfg, argv[1:]) From 3718fe431c9ed4ad7dd5daa104ff31127eab17e0 Mon Sep 17 00:00:00 2001 From: mah0x211 Date: Fri, 5 Apr 2024 09:42:06 +0900 Subject: [PATCH 2/4] Remove use-lj and use-rocks commands --- help.go | 2 -- main.go | 11 ++--------- use.go | 38 ++++++++------------------------------ 3 files changed, 10 insertions(+), 41 deletions(-) diff --git a/help.go b/help.go index 9dffac9..a2ae29e 100644 --- a/help.go +++ b/help.go @@ -45,8 +45,6 @@ Commands: ls List installed versions install Install and use a of lua use Use a of lua - use-lj Use a of luajit - use-rocks Use a of luajit Note: The specifier of the above commands can be specified as follows; diff --git a/main.go b/main.go index be2c716..45356c9 100644 --- a/main.go +++ b/main.go @@ -259,7 +259,7 @@ ERROR: the required directory does not exists. func CheckLuaRocksRootDir() { if LuaRocksCfg.RootDir == "" { - fatalf("%q does not exist.\nplease run `lenv use ` or `lenv use-lj ` before installing or uninstalling luarocks", CurrentDir) + fatalf("%q does not exist.\nplease run `lenv use ` before installing or uninstalling luarocks", CurrentDir) } } @@ -359,14 +359,7 @@ func start() { CmdUninstall(LuaRocksCfg, argv[1:]) case "use": - CmdUse(LuaCfg, argv[1:]) - - case "use-lj": - CmdUse(LuaJitCfg, argv[1:]) - - case "use-rocks": - CheckLuaRocksRootDir() - CmdUse(LuaRocksCfg, argv[1:]) + CmdUse(argv[1:]) default: CmdHelp(1, "unknown command %q", argv[0]) diff --git a/use.go b/use.go index ad77245..eb95121 100644 --- a/use.go +++ b/use.go @@ -48,35 +48,13 @@ func UseInstalledVersion(cfg *TargetConfig, ver string) { fatalf("%s version %q is not installed", cfg.Name, ver) } -func CmdUse(cfg *TargetConfig, opts []string) { - // check target version - if len(opts) == 0 || (cfg != LuaRocksCfg && opts[0] == ":") { - CmdHelp(1, "no version specified") - } - ver := opts[0] - - // check : - var rocksVer string - if cfg != LuaRocksCfg { - if delim := strings.Index(ver, ":"); delim != -1 { - rocksVer = ver[delim+1:] - ver = ver[:delim] - } - } - - var verItem *VerItem - if len(ver) > 0 { - verItem = PickTargetVersionItem(cfg, ver) - } - var rocksItem *VerItem - if len(rocksVer) > 0 { - rocksItem = PickTargetVersionItem(LuaRocksCfg, rocksVer) - } - - if verItem != nil { - UseInstalledVersion(cfg, verItem.Ver) - } - if rocksItem != nil { - UseInstalledVersion(LuaRocksCfg, rocksItem.Ver) +func CmdUse(opts []string) { + target := PickTargetVersion(opts[0]) + if target.Lua != nil { + UseInstalledVersion(target.Lua.Config, target.Lua.Version.Ver) + } + if target.LuaRocks != nil { + CheckLuaRocksRootDir() + UseInstalledVersion(target.LuaRocks.Config, target.LuaRocks.Version.Ver) } } From d8a500d5bf715e5730f87f47615b9b9448e85ad4 Mon Sep 17 00:00:00 2001 From: mah0x211 Date: Fri, 5 Apr 2024 10:35:07 +0900 Subject: [PATCH 3/4] Remove uninstall-lj and uninstall-rocks commands --- help.go | 10 ++++------ main.go | 9 +-------- uninstall.go | 54 ++++++++++++++++++++++++---------------------------- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/help.go b/help.go index a2ae29e..ec56982 100644 --- a/help.go +++ b/help.go @@ -45,6 +45,7 @@ Commands: ls List installed versions install Install and use a of lua use Use a of lua + uninstall Uninstall a of lua Note: The specifier of the above commands can be specified as follows; @@ -59,12 +60,9 @@ Commands: lenv install :latest ; that picks the the latest version of luarocks in current lua environment - If a version of luarocks is specified, the operation will target the lua - environment currently in use. - - uninstall Uninstall a of lua - uninstall-lj Uninstall a of luajit - uninstall-rocks Uninstall a of luarocks + If the version of luarocks is specified along with the version of lua, the + operation will target the specified version of the lua environment. + Otherwise, the operation will target the current lua environment. `) osExit(rc) } diff --git a/main.go b/main.go index 45356c9..eadf402 100644 --- a/main.go +++ b/main.go @@ -349,14 +349,7 @@ func start() { CmdInstall(argv[1:]) case "uninstall": - CmdUninstall(LuaCfg, argv[1:]) - - case "uninstall-lj": - CmdUninstall(LuaJitCfg, argv[1:]) - - case "uninstall-rocks": - CheckLuaRocksRootDir() - CmdUninstall(LuaRocksCfg, argv[1:]) + CmdUninstall(argv[1:]) case "use": CmdUse(argv[1:]) diff --git a/uninstall.go b/uninstall.go index 646a74d..6648d07 100644 --- a/uninstall.go +++ b/uninstall.go @@ -5,40 +5,36 @@ import ( "path/filepath" ) -func CmdUninstall(cfg *TargetConfig, opts []string) { - // check target version - if len(opts) == 0 { - CmdHelp(1, "no version specified") - } - - vers, err := NewVersionsFromFile(cfg.VersionFile) +func uninstall(t *Target) { + dir := filepath.Join(t.Config.RootDir, t.Version.Ver) + stat, err := os.Stat(dir) if err != nil { - fatalf("failed to read version file %q: %v", cfg.VersionFile, err) + if !os.IsNotExist(err) { + fatalf("failed to stat %q: %v", dir, err) + } + fatalf("%s version %s is not installed.", t.Config.Name, t.Version.Ver) } - - ver := opts[0] - item := vers.GetItem(ver) - if item == nil { - fatalf("%s version %q does not defined in %q", cfg.Name, ver, cfg.VersionFile) + if !stat.IsDir() { + fatalf("found %s %s (%q) but it is not a directory.\nplease remove it yourself.", t.Config.Name, t.Version.Ver, dir) + } else if err = os.RemoveAll(dir); err != nil { + fatalf("failed to uninstall version %s: %v", t.Version.Ver, err) } + printf("%s version %s (%q) has been uninstalled.", t.Config.Name, t.Version.Ver, dir) +} - infos, err := os.ReadDir(cfg.RootDir) - if err != nil { - fatalf("failed to readdir: %v", err) - } +func CmdUninstall(opts []string) { + target := PickTargetVersion(opts[0]) - for _, info := range infos { - if info.Name() == ver { - dir := filepath.Join(cfg.RootDir, ver) - if !info.IsDir() { - fatalf("found %s %s (%q) but it is not a directory.\nplease remove it yourself.", cfg.Name, ver, dir) - } else if err = os.RemoveAll(dir); err != nil { - fatalf("failed to uninstall version %s: %v", ver, err) - } - printf("%s version %s (%q) has been uninstalled.", cfg.Name, ver, dir) - return - } + // uninstall the specified version of lua + if target.Lua != nil { + uninstall(target.Lua) + // it is remove all the versions of luarocks + return } - fatalf("%s version %q is not installed", cfg.Name, ver) + // uninstall the specified version of luarocks + if target.LuaRocks != nil { + CheckLuaRocksRootDir() + uninstall(target.LuaRocks) + } } From 54a9b7850cd977774302e5796b0242c2dbfff17c Mon Sep 17 00:00:00 2001 From: mah0x211 Date: Fri, 5 Apr 2024 13:56:59 +0900 Subject: [PATCH 4/4] Remove uninstall-lj and uninstall-rocks commands --- help.go | 4 ++++ install.go | 2 +- main.go | 8 ++++---- uninstall.go | 2 +- use.go | 2 +- vers.go | 10 ++++++++-- 6 files changed, 19 insertions(+), 9 deletions(-) diff --git a/help.go b/help.go index ec56982..8a936b3 100644 --- a/help.go +++ b/help.go @@ -63,6 +63,10 @@ Commands: If the version of luarocks is specified along with the version of lua, the operation will target the specified version of the lua environment. Otherwise, the operation will target the current lua environment. + + In the case of the 'uninstall' command, the version specifier must match the + target version exactly. Also, if the version of luarocks is specified along + with the version of lua, the version specifier of luarocks is ignored. `) osExit(rc) } diff --git a/install.go b/install.go index 3c3dc89..9b76db2 100644 --- a/install.go +++ b/install.go @@ -438,7 +438,7 @@ func doInstall(cfg *TargetConfig, item *VerItem, opts []string) { } func CmdInstall(opts []string) { - target := PickTargetVersion(opts[0]) + target := PickTargetVersion(opts[0], false) if target.Lua != nil { doInstall(target.Lua.Config, target.Lua.Version, opts[1:]) } diff --git a/main.go b/main.go index eadf402..e953ce0 100644 --- a/main.go +++ b/main.go @@ -273,7 +273,7 @@ type TargetVersion struct { LuaRocks *Target } -func PickTargetVersion(vers string) *TargetVersion { +func PickTargetVersion(vers string, exactMatch bool) *TargetVersion { // check target version if len(vers) == 0 || vers == ":" { CmdHelp(1, "no version specified") @@ -292,13 +292,13 @@ func PickTargetVersion(vers string) *TargetVersion { // if `lj-' prefix is specified, then the target is LuaJIT version target.Lua = &Target{ Config: LuaJitCfg, - Version: PickTargetVersionItem(LuaJitCfg, vers[3:]), + Version: PickTargetVersionItem(LuaJitCfg, vers[3:], exactMatch), } } else { // otherwise the target is Lua version. target.Lua = &Target{ Config: LuaCfg, - Version: PickTargetVersionItem(LuaCfg, vers), + Version: PickTargetVersionItem(LuaCfg, vers, exactMatch), } } } @@ -306,7 +306,7 @@ func PickTargetVersion(vers string) *TargetVersion { if len(rocksVer) > 0 { target.LuaRocks = &Target{ Config: LuaRocksCfg, - Version: PickTargetVersionItem(LuaRocksCfg, rocksVer), + Version: PickTargetVersionItem(LuaRocksCfg, rocksVer, exactMatch), } } diff --git a/uninstall.go b/uninstall.go index 6648d07..e8c5757 100644 --- a/uninstall.go +++ b/uninstall.go @@ -23,7 +23,7 @@ func uninstall(t *Target) { } func CmdUninstall(opts []string) { - target := PickTargetVersion(opts[0]) + target := PickTargetVersion(opts[0], true) // uninstall the specified version of lua if target.Lua != nil { diff --git a/use.go b/use.go index eb95121..ca6c101 100644 --- a/use.go +++ b/use.go @@ -49,7 +49,7 @@ func UseInstalledVersion(cfg *TargetConfig, ver string) { } func CmdUse(opts []string) { - target := PickTargetVersion(opts[0]) + target := PickTargetVersion(opts[0], false) if target.Lua != nil { UseInstalledVersion(target.Lua.Config, target.Lua.Version.Ver) } diff --git a/vers.go b/vers.go index d5932f8..1a12de1 100644 --- a/vers.go +++ b/vers.go @@ -282,14 +282,20 @@ func ListTargetVersions(cfg *TargetConfig) string { return b.String() } -func PickTargetVersionItem(cfg *TargetConfig, ver string) *VerItem { +func PickTargetVersionItem(cfg *TargetConfig, ver string, exactMatch bool) *VerItem { print("check %s version %q definition ... ", cfg.Name, ver) vers, err := NewVersionsFromFile(cfg.VersionFile) if err != nil { fatalf("failed to read version file %q: %v", cfg.VersionFile, err) } - item := vers.PickItem(ver) + var item *VerItem + if exactMatch { + item = vers.GetItem(ver) + } else { + item = vers.PickItem(ver) + } + if item == nil { printf("not found") fatalf("%s version %q does not defined in %q\n%s", cfg.Name, ver, cfg.VersionFile, ListTargetVersions(cfg))