Skip to content

Commit c99ba20

Browse files
authored
Merge pull request #31 from mah0x211/remove-unnecessary-commands
Remove unnecessary commands
2 parents 70068c8 + 54a9b78 commit c99ba20

File tree

6 files changed

+130
-144
lines changed

6 files changed

+130
-144
lines changed

help.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,29 @@ Commands:
4444
vers List available versions
4545
ls List installed versions
4646
install <version> <opt...> Install and use a <version> of lua
47-
install-lj <version> <opt...> Install and use a <version> of luajit
48-
install-rocks <version> Install and use a <version> of lurocks in
49-
current lua environment
5047
use <version> Use a <version> of lua
51-
use-lj <version> Use a <version> of luajit
52-
use-rocks <version> Use a <version> of luajit
48+
uninstall <version> Uninstall a <version> of lua
5349
5450
Note:
5551
The <version> specifier of the above commands can be specified as follows;
5652
57-
lenv install latest ; that picks the latest version
58-
lenv install 5 ; that picks the latest minor version and patch version
59-
lenv install 5.4 ; that picks the latest patch version
60-
61-
In addition, the install and install-lj commands can be used to install
62-
luarocks at the same time with the following <version> specifier;
53+
lenv install latest ; that picks the latest version
54+
lenv install 5 ; that picks the latest minor version and patch version
55+
lenv install 5.4 ; that picks the latest patch version
56+
lenv install lj-v2.1 ; that picks the version of luajit
6357
6458
lenv install latest:latest ; that picks the the latest version of lua and
65-
; luarocks
59+
; luarocks in current lua environment
6660
lenv install :latest ; that picks the the latest version of luarocks
61+
in current lua environment
6762
68-
uninstall <version> Uninstall a <version> of lua
69-
uninstall-lj <version> Uninstall a <version> of luajit
70-
uninstall-rocks <version> Uninstall a <version> of luarocks
63+
If the version of luarocks is specified along with the version of lua, the
64+
operation will target the specified version of the lua environment.
65+
Otherwise, the operation will target the current lua environment.
66+
67+
In the case of the 'uninstall' command, the version specifier must match the
68+
target version exactly. Also, if the version of luarocks is specified along
69+
with the version of lua, the version specifier of luarocks is ignored.
7170
`)
7271
osExit(rc)
7372
}

install.go

+21-29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"os"
1212
"path/filepath"
13+
"runtime"
1314
"strings"
1415
)
1516

@@ -239,6 +240,20 @@ func installRocks(instdir string, cfg *TargetConfig) error {
239240
}
240241

241242
func installLuaJit(instdir string, opts []string) error {
243+
if runtime.GOOS == "darwin" {
244+
// append MACOSX_DEPLOYMENT_TARGET=10.8 by default on macOS platform
245+
found := false
246+
for _, opt := range opts {
247+
found = strings.HasPrefix(opt, "MACOSX_DEPLOYMENT_TARGET")
248+
if found {
249+
break
250+
}
251+
}
252+
if !found {
253+
opts = append(opts, "MACOSX_DEPLOYMENT_TARGET=10.8")
254+
}
255+
}
256+
242257
// clean up working directory
243258
if err := DoExec("make", append([]string{"clean"}, opts...)...); err != nil {
244259
return err
@@ -422,37 +437,14 @@ func doInstall(cfg *TargetConfig, item *VerItem, opts []string) {
422437
UseInstalledVersion(cfg, item.Ver)
423438
}
424439

425-
func CmdInstall(cfg *TargetConfig, opts []string) {
426-
// check target version
427-
if len(opts) == 0 || (cfg != LuaRocksCfg && opts[0] == ":") {
428-
CmdHelp(1, "no version specified")
429-
}
430-
ver := opts[0]
431-
432-
// check :<luarocks-version>
433-
var rocksVer string
434-
if cfg != LuaRocksCfg {
435-
if delim := strings.Index(ver, ":"); delim != -1 {
436-
rocksVer = ver[delim+1:]
437-
ver = ver[:delim]
438-
}
439-
}
440-
441-
var verItem *VerItem
442-
if len(ver) > 0 {
443-
verItem = PickTargetVersionItem(cfg, ver)
444-
}
445-
var rocksItem *VerItem
446-
if len(rocksVer) > 0 {
447-
rocksItem = PickTargetVersionItem(LuaRocksCfg, rocksVer)
448-
}
449-
450-
if verItem != nil {
451-
doInstall(cfg, verItem, opts[1:])
440+
func CmdInstall(opts []string) {
441+
target := PickTargetVersion(opts[0], false)
442+
if target.Lua != nil {
443+
doInstall(target.Lua.Config, target.Lua.Version, opts[1:])
452444
}
453-
if rocksItem != nil {
445+
if target.LuaRocks != nil {
454446
ResolveCurrentDir()
455447
CheckLuaRocksRootDir()
456-
doInstall(LuaRocksCfg, rocksItem, opts)
448+
doInstall(target.LuaRocks.Config, target.LuaRocks.Version, opts[1:])
457449
}
458450
}

main.go

+54-39
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,60 @@ ERROR: the required directory does not exists.
259259

260260
func CheckLuaRocksRootDir() {
261261
if LuaRocksCfg.RootDir == "" {
262-
fatalf("%q does not exist.\nplease run `lenv use <ver>` or `lenv use-lj <ver>` before installing or uninstalling luarocks", CurrentDir)
262+
fatalf("%q does not exist.\nplease run `lenv use <ver>` before installing or uninstalling luarocks", CurrentDir)
263263
}
264264
}
265265

266+
type Target struct {
267+
Config *TargetConfig
268+
Version *VerItem
269+
}
270+
271+
type TargetVersion struct {
272+
Lua *Target
273+
LuaRocks *Target
274+
}
275+
276+
func PickTargetVersion(vers string, exactMatch bool) *TargetVersion {
277+
// check target version
278+
if len(vers) == 0 || vers == ":" {
279+
CmdHelp(1, "no version specified")
280+
}
281+
282+
// check :<luarocks-version>
283+
var rocksVer string
284+
if delim := strings.Index(vers, ":"); delim != -1 {
285+
rocksVer = vers[delim+1:]
286+
vers = vers[:delim]
287+
}
288+
289+
target := &TargetVersion{}
290+
if len(vers) > 0 {
291+
if strings.HasPrefix(vers, "lj-") {
292+
// if `lj-' prefix is specified, then the target is LuaJIT version
293+
target.Lua = &Target{
294+
Config: LuaJitCfg,
295+
Version: PickTargetVersionItem(LuaJitCfg, vers[3:], exactMatch),
296+
}
297+
} else {
298+
// otherwise the target is Lua version.
299+
target.Lua = &Target{
300+
Config: LuaCfg,
301+
Version: PickTargetVersionItem(LuaCfg, vers, exactMatch),
302+
}
303+
}
304+
}
305+
306+
if len(rocksVer) > 0 {
307+
target.LuaRocks = &Target{
308+
Config: LuaRocksCfg,
309+
Version: PickTargetVersionItem(LuaRocksCfg, rocksVer, exactMatch),
310+
}
311+
}
312+
313+
return target
314+
}
315+
266316
func start() {
267317
argv := os.Args[1:]
268318
if len(argv) > 0 {
@@ -296,48 +346,13 @@ func start() {
296346
CmdList()
297347

298348
case "install":
299-
CmdInstall(LuaCfg, argv[1:])
300-
301-
case "install-lj":
302-
argv = argv[1:]
303-
if runtime.GOOS == "darwin" {
304-
// set MACOSX_DEPLOYMENT_TARGET=10.8 by default
305-
found := false
306-
for _, arg := range argv {
307-
found = strings.HasPrefix(arg, "MACOSX_DEPLOYMENT_TARGET")
308-
if found {
309-
break
310-
}
311-
}
312-
if !found {
313-
argv = append(argv, "MACOSX_DEPLOYMENT_TARGET=10.8")
314-
}
315-
}
316-
CmdInstall(LuaJitCfg, argv)
317-
318-
case "install-rocks":
319-
CheckLuaRocksRootDir()
320-
CmdInstall(LuaRocksCfg, argv[1:])
349+
CmdInstall(argv[1:])
321350

322351
case "uninstall":
323-
CmdUninstall(LuaCfg, argv[1:])
324-
325-
case "uninstall-lj":
326-
CmdUninstall(LuaJitCfg, argv[1:])
327-
328-
case "uninstall-rocks":
329-
CheckLuaRocksRootDir()
330-
CmdUninstall(LuaRocksCfg, argv[1:])
352+
CmdUninstall(argv[1:])
331353

332354
case "use":
333-
CmdUse(LuaCfg, argv[1:])
334-
335-
case "use-lj":
336-
CmdUse(LuaJitCfg, argv[1:])
337-
338-
case "use-rocks":
339-
CheckLuaRocksRootDir()
340-
CmdUse(LuaRocksCfg, argv[1:])
355+
CmdUse(argv[1:])
341356

342357
default:
343358
CmdHelp(1, "unknown command %q", argv[0])

uninstall.go

+25-29
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,36 @@ import (
55
"path/filepath"
66
)
77

8-
func CmdUninstall(cfg *TargetConfig, opts []string) {
9-
// check target version
10-
if len(opts) == 0 {
11-
CmdHelp(1, "no version specified")
12-
}
13-
14-
vers, err := NewVersionsFromFile(cfg.VersionFile)
8+
func uninstall(t *Target) {
9+
dir := filepath.Join(t.Config.RootDir, t.Version.Ver)
10+
stat, err := os.Stat(dir)
1511
if err != nil {
16-
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
12+
if !os.IsNotExist(err) {
13+
fatalf("failed to stat %q: %v", dir, err)
14+
}
15+
fatalf("%s version %s is not installed.", t.Config.Name, t.Version.Ver)
1716
}
18-
19-
ver := opts[0]
20-
item := vers.GetItem(ver)
21-
if item == nil {
22-
fatalf("%s version %q does not defined in %q", cfg.Name, ver, cfg.VersionFile)
17+
if !stat.IsDir() {
18+
fatalf("found %s %s (%q) but it is not a directory.\nplease remove it yourself.", t.Config.Name, t.Version.Ver, dir)
19+
} else if err = os.RemoveAll(dir); err != nil {
20+
fatalf("failed to uninstall version %s: %v", t.Version.Ver, err)
2321
}
22+
printf("%s version %s (%q) has been uninstalled.", t.Config.Name, t.Version.Ver, dir)
23+
}
2424

25-
infos, err := os.ReadDir(cfg.RootDir)
26-
if err != nil {
27-
fatalf("failed to readdir: %v", err)
28-
}
25+
func CmdUninstall(opts []string) {
26+
target := PickTargetVersion(opts[0], true)
2927

30-
for _, info := range infos {
31-
if info.Name() == ver {
32-
dir := filepath.Join(cfg.RootDir, ver)
33-
if !info.IsDir() {
34-
fatalf("found %s %s (%q) but it is not a directory.\nplease remove it yourself.", cfg.Name, ver, dir)
35-
} else if err = os.RemoveAll(dir); err != nil {
36-
fatalf("failed to uninstall version %s: %v", ver, err)
37-
}
38-
printf("%s version %s (%q) has been uninstalled.", cfg.Name, ver, dir)
39-
return
40-
}
28+
// uninstall the specified version of lua
29+
if target.Lua != nil {
30+
uninstall(target.Lua)
31+
// it is remove all the versions of luarocks
32+
return
4133
}
4234

43-
fatalf("%s version %q is not installed", cfg.Name, ver)
35+
// uninstall the specified version of luarocks
36+
if target.LuaRocks != nil {
37+
CheckLuaRocksRootDir()
38+
uninstall(target.LuaRocks)
39+
}
4440
}

use.go

+8-30
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,13 @@ func UseInstalledVersion(cfg *TargetConfig, ver string) {
4848
fatalf("%s version %q is not installed", cfg.Name, ver)
4949
}
5050

51-
func CmdUse(cfg *TargetConfig, opts []string) {
52-
// check target version
53-
if len(opts) == 0 || (cfg != LuaRocksCfg && opts[0] == ":") {
54-
CmdHelp(1, "no version specified")
55-
}
56-
ver := opts[0]
57-
58-
// check :<luarocks-version>
59-
var rocksVer string
60-
if cfg != LuaRocksCfg {
61-
if delim := strings.Index(ver, ":"); delim != -1 {
62-
rocksVer = ver[delim+1:]
63-
ver = ver[:delim]
64-
}
65-
}
66-
67-
var verItem *VerItem
68-
if len(ver) > 0 {
69-
verItem = PickTargetVersionItem(cfg, ver)
70-
}
71-
var rocksItem *VerItem
72-
if len(rocksVer) > 0 {
73-
rocksItem = PickTargetVersionItem(LuaRocksCfg, rocksVer)
74-
}
75-
76-
if verItem != nil {
77-
UseInstalledVersion(cfg, verItem.Ver)
78-
}
79-
if rocksItem != nil {
80-
UseInstalledVersion(LuaRocksCfg, rocksItem.Ver)
51+
func CmdUse(opts []string) {
52+
target := PickTargetVersion(opts[0], false)
53+
if target.Lua != nil {
54+
UseInstalledVersion(target.Lua.Config, target.Lua.Version.Ver)
55+
}
56+
if target.LuaRocks != nil {
57+
CheckLuaRocksRootDir()
58+
UseInstalledVersion(target.LuaRocks.Config, target.LuaRocks.Version.Ver)
8159
}
8260
}

vers.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,20 @@ func ListTargetVersions(cfg *TargetConfig) string {
282282
return b.String()
283283
}
284284

285-
func PickTargetVersionItem(cfg *TargetConfig, ver string) *VerItem {
285+
func PickTargetVersionItem(cfg *TargetConfig, ver string, exactMatch bool) *VerItem {
286286
print("check %s version %q definition ... ", cfg.Name, ver)
287287
vers, err := NewVersionsFromFile(cfg.VersionFile)
288288
if err != nil {
289289
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
290290
}
291291

292-
item := vers.PickItem(ver)
292+
var item *VerItem
293+
if exactMatch {
294+
item = vers.GetItem(ver)
295+
} else {
296+
item = vers.PickItem(ver)
297+
}
298+
293299
if item == nil {
294300
printf("not found")
295301
fatalf("%s version %q does not defined in %q\n%s", cfg.Name, ver, cfg.VersionFile, ListTargetVersions(cfg))

0 commit comments

Comments
 (0)