Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary commands #31

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,29 @@ Commands:
vers List available versions
ls List installed versions
install <version> <opt...> Install and use a <version> of lua
install-lj <version> <opt...> Install and use a <version> of luajit
install-rocks <version> Install and use a <version> of lurocks in
current lua environment
use <version> Use a <version> of lua
use-lj <version> Use a <version> of luajit
use-rocks <version> Use a <version> of luajit
uninstall <version> Uninstall a <version> of lua

Note:
The <version> 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 <version> 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 <version> Uninstall a <version> of lua
uninstall-lj <version> Uninstall a <version> of luajit
uninstall-rocks <version> Uninstall a <version> 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)
}
50 changes: 21 additions & 29 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 :<luarocks-version>
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:])
}
}
93 changes: 54 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ver>` or `lenv use-lj <ver>` before installing or uninstalling luarocks", CurrentDir)
fatalf("%q does not exist.\nplease run `lenv use <ver>` 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 :<luarocks-version>
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 {
Expand Down Expand Up @@ -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])
Expand Down
54 changes: 25 additions & 29 deletions uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
38 changes: 8 additions & 30 deletions use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 :<luarocks-version>
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)
}
}
10 changes: 8 additions & 2 deletions vers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading