Skip to content

Commit

Permalink
Change the install command to automatically use specified version
Browse files Browse the repository at this point in the history
  • Loading branch information
mah0x211 committed Mar 3, 2023
1 parent 213aa64 commit 6ed650d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ Usage:
lenv fetch Fetch remote versions
lenv vers List available versions
lenv ls List installed versions
lenv install <version> <opt...> Install a version <version> of lua
lenv install-lj <version> <opt...> Install a version <version> of luajit
lenv install-rocks <version> Install a version <version> of lurocks in
lenv install <version> <opt...> Install and use a <version> of lua
lenv install-lj <version> <opt...> Install and use a <version> of luajit
lenv install-rocks <version> Install and use a <version> of lurocks in
current lua environment
lenv uninstall <version> Uninstall a version <version> of lua
lenv uninstall-lj <version> Uninstall a version <version> of luajit
Expand All @@ -66,8 +66,6 @@ the following example are installing the Lua 5.1.5.
```sh
$ lenv install 5.1.5 macosx
...snip...
$ lenv use 5.1.5
use lua version 5.1.5 ("lua/5.1.5")
$ lua -v
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
```
Expand All @@ -76,7 +74,7 @@ the following example are installing the LuaJIT 2.0.4.

```sh
$ lenv install-lj 2.0.4
$ lenv use-lj 2.0.4
...snip...
$ lua -v
LuaJIT 2.0.4 -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/
```
Expand All @@ -88,8 +86,6 @@ $ lenv use 5.1.5
use lua version 5.1.5 ("lua/5.1.5")
$ lenv install-rocks 3.5.0
...snip...
$ lenv use-rocks 3.5.0
use luarocks version 3.5.0 ("luarocks/3.5.0/lua_modules")
$ luarocks version
/Users/mah/.lenv/current/lua_modules/bin/luarocks 3.5.0
LuaRocks main command-line interface
Expand Down
6 changes: 3 additions & 3 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ Usage:
lenv fetch Fetch remote versions
lenv vers List available versions
lenv ls List installed versions
lenv install <version> <opt...> Install a version <version> of lua
lenv install-lj <version> <opt...> Install a version <version> of luajit
lenv install-rocks <version> Install a version <version> of lurocks in
lenv install <version> <opt...> Install and use a <version> of lua
lenv install-lj <version> <opt...> Install and use a <version> of luajit
lenv install-rocks <version> Install and use a <version> of lurocks in
current lua environment
lenv uninstall <version> Uninstall a version <version> of lua
lenv uninstall-lj <version> Uninstall a version <version> of luajit
Expand Down
3 changes: 3 additions & 0 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,7 @@ func CmdInstall(cfg *TargetConfig, opts []string) {

printf("")
printf("%s version %s (%q) has been installed.", cfg.Name, ver, instdir)

// automatically use the installed version
UseInstalledVersion(cfg, ver)
}
40 changes: 22 additions & 18 deletions use.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,7 @@ import (
"strings"
)

func CmdUse(cfg *TargetConfig, opts []string) {
// check target version
if len(opts) == 0 {
CmdHelp(1, "no version specified")
}

vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
}

ver := opts[0]
item := vers.GetItem(ver)
if item == nil {
fatalf("%s version %q does not defined in %q", cfg.Name, ver, cfg.VersionFile)
}

func UseInstalledVersion(cfg *TargetConfig, ver string) {
// change workdir
wd := LenvDir
dst := CurrentDir
Expand All @@ -33,7 +17,8 @@ func CmdUse(cfg *TargetConfig, opts []string) {
dst = filepath.Join(wd, "lua_modules")
suffix = "lua_modules"
}
if err = os.Chdir(wd); err != nil {

if err := os.Chdir(wd); err != nil {
fatalf("failed to chdir: %v", err)
}

Expand Down Expand Up @@ -63,3 +48,22 @@ func CmdUse(cfg *TargetConfig, opts []string) {

fatalf("%s version %q is not installed", cfg.Name, ver)
}

func CmdUse(cfg *TargetConfig, opts []string) {
// check target version
if len(opts) == 0 {
CmdHelp(1, "no version specified")
}

vers, err := NewVersionsFromFile(cfg.VersionFile)
if err != nil {
fatalf("failed to read version file %q: %v", cfg.VersionFile, err)
}

ver := opts[0]
if vers.GetItem(ver) == nil {
fatalf("%s version %q does not defined in %q", cfg.Name, ver, cfg.VersionFile)
}

UseInstalledVersion(cfg, ver)
}

0 comments on commit 6ed650d

Please sign in to comment.