diff --git a/help.go b/help.go index 06f50f3..8a936b3 100644 --- a/help.go +++ b/help.go @@ -44,30 +44,29 @@ 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 + uninstall Uninstall a of lua 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 - 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. + + 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 deb4653..9b76db2 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], false) + 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..e953ce0 100644 --- a/main.go +++ b/main.go @@ -259,10 +259,60 @@ 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) } } +type Target struct { + Config *TargetConfig + Version *VerItem +} + +type TargetVersion struct { + Lua *Target + LuaRocks *Target +} + +func PickTargetVersion(vers string, exactMatch bool) *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:], exactMatch), + } + } else { + // otherwise the target is Lua version. + target.Lua = &Target{ + Config: LuaCfg, + Version: PickTargetVersionItem(LuaCfg, vers, exactMatch), + } + } + } + + if len(rocksVer) > 0 { + target.LuaRocks = &Target{ + Config: LuaRocksCfg, + Version: PickTargetVersionItem(LuaRocksCfg, rocksVer, exactMatch), + } + } + + return target +} + func start() { argv := os.Args[1:] if len(argv) > 0 { @@ -296,48 +346,13 @@ 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:]) - - case "uninstall-lj": - CmdUninstall(LuaJitCfg, argv[1:]) - - case "uninstall-rocks": - CheckLuaRocksRootDir() - CmdUninstall(LuaRocksCfg, argv[1:]) + CmdUninstall(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/uninstall.go b/uninstall.go index 646a74d..e8c5757 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], true) - 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) + } } diff --git a/use.go b/use.go index ad77245..ca6c101 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], false) + 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) } } 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))