Skip to content

Commit

Permalink
Merge pull request #26 from mah0x211/change-the-luajit-installation
Browse files Browse the repository at this point in the history
Change the luajit installation instructions
  • Loading branch information
mah0x211 authored Sep 25, 2023
2 parents 97404a3 + 4b3562f commit df9c91d
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 91 deletions.
19 changes: 19 additions & 0 deletions exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"io"
"os"
"os/exec"
)

func DoExecEx(name string, stdout, stderr io.Writer, argv ...string) error {
cmd := exec.Command(name, argv...)
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Env = os.Environ()
return cmd.Run()
}

func DoExec(name string, argv ...string) error {
return DoExecEx(name, os.Stdout, os.Stderr, argv...)
}
131 changes: 99 additions & 32 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
)

type ParseFunc func(body []byte) *Versions
type ParseFunc func(v interface{}) *Versions

var (
ReLuaRocksVer = regexp.MustCompile(`(?si)href="(luarocks-([^"]+?)\.tar\.gz).*?href="(luarocks-[^"]+?\.tar\.gz\.asc)"`)
ReLuaJitVer = regexp.MustCompile(`(?i)([0-9a-f]+?)\s+(LuaJIT-([^\s]+?)\.tar\.gz)`)
ReLuaVer = regexp.MustCompile(`(?si)<tr>\s*<td class="name"><a href="(lua-([^"]+?)\.tar\.gz).+?class="sum">([0-9a-f]+)</td>\s*</tr>`)
)

func parseLuaRocksVers(body []byte) *Versions {
func parseLuaRocksVers(v interface{}) *Versions {
body := v.([]byte)
vers := NewVersions()
for _, m := range ReLuaRocksVer.FindAllSubmatch(body, -1) {
if !bytes.HasPrefix(m[3], m[1]) {
Expand All @@ -30,25 +33,50 @@ func parseLuaRocksVers(body []byte) *Versions {
printf("ignore unsupported version: %q", name)
}
}

return vers
}

func parseLuaJitVers(body []byte) *Versions {
func parseLuaJitVers(v interface{}) *Versions {
repodir := v.(string)
err := os.Chdir(repodir)
if err != nil {
eprintf("failed to chdir: %v", err)
return nil
}
defer os.Chdir(CWD)

// get list of remote branches
out := &bytes.Buffer{}
err = DoExecEx("git", out, os.Stderr, "branch", "-r")
if err != nil {
eprintf("failed to get branches: %v", err)
return nil
}

// parse branches
vers := NewVersions()
for _, m := range ReLuaJitVer.FindAllSubmatch(body, -1) {
sum := "sha256:" + string(m[1])
name := string(m[2])
ver := string(m[3])
if !vers.Add(name, ver, sum, ".tar.gz") {
printf("ignore unsupported version: %q", name)
for _, line := range strings.Split(out.String(), "\n") {
line = strings.TrimSpace(line)
if strings.Contains(line, " ") {
// ignore unsupported branch
continue
}

// split line to remote and branch
// e.g. origin/HEAD -> "origin", "master"
origin := strings.Split(line, "/")
if len(origin) != 2 {
// ignore unsupported branch
continue
}
vers.AddBranch(origin[1], origin[0])
}

return vers
}

func parseLuaVers(body []byte) *Versions {
func parseLuaVers(v interface{}) *Versions {
body := v.([]byte)
vers := NewVersions()
for _, m := range ReLuaVer.FindAllSubmatch(body, -1) {
name := string(m[1])
Expand All @@ -58,10 +86,28 @@ func parseLuaVers(body []byte) *Versions {
printf("ignore unsupported version: %q", name)
}
}

return vers
}

func makeVerFile(cfg *TargetConfig, vers *Versions) {
if err := vers.WriteFile(cfg.VersionFile); err != nil {
eprintf("failed to write version file: %v\n", err)
return
}

items, maxlen := vers.GetList()
for _, item := range items {
if cfg.RepoDir != "" {
format := fmt.Sprintf("%%-%ds %%s/%%s %%s", maxlen)
printf(format, item.Name, item.Remote, item.Name, cfg.ReleaseURL)
continue
}
format := fmt.Sprintf("%%-%ds %%s", maxlen)
url := cfg.DownloadURL + filepath.Clean(item.Name)
printf(format, item.Ver, url)
}
}

func CmdFetch() {
for _, target := range []struct {
cfg *TargetConfig
Expand All @@ -72,34 +118,55 @@ func CmdFetch() {
{LuaRocksCfg, parseLuaRocksVers},
} {
printf(
"fetch list of %q versions from %q...",
"\nfetch list of %q versions from %q...",
target.cfg.Name, target.cfg.ReleaseURL,
)
rsp, err := http.Get(target.cfg.ReleaseURL)
if err != nil {
eprintf("failed to download %q: %v", target.cfg.ReleaseURL, err)
continue
}
defer rsp.Body.Close()

b, err := io.ReadAll(rsp.Body)
if err != nil {
eprintf("failed to read body: %v", err)
}
var vers *Versions

vers := target.parse(b)
if err = vers.WriteFile(target.cfg.VersionFile); err != nil {
eprintf("failed to write version file: %v", err)
}
if target.cfg.RepoDir != "" {
// make temp dir
tmpdir, err := os.MkdirTemp(os.TempDir(), "lenv-")
if err != nil {
eprintf("failed to create temp dir: %v\n", err)
continue
}
defer os.RemoveAll(tmpdir)

items, maxlen := vers.GetList()
format := fmt.Sprintf("%%-%ds %%s", maxlen)
for _, item := range items {
url := target.cfg.DownloadURL + filepath.Clean(item.Name)
printf(format, item.Ver, url)
// shallow clone with all branches
err = DoExec("git", "clone", "--depth", "1", "--no-single-branch", target.cfg.ReleaseURL, tmpdir)
if err != nil {
eprintf("failed to clone %q: %v\n", target.cfg.ReleaseURL, err)
continue
}
os.RemoveAll(target.cfg.RepoDir)
if err = os.Rename(tmpdir, target.cfg.RepoDir); err != nil {
eprintf("failed to rename %q to %q: %v\n", tmpdir, target.cfg.RepoDir, err)
continue
}
vers = target.parse(target.cfg.RepoDir)

} else {
rsp, err := http.Get(target.cfg.ReleaseURL)
if err != nil {
eprintf("failed to download %q: %v\n", target.cfg.ReleaseURL, err)
continue
}
defer rsp.Body.Close()

b, err := io.ReadAll(rsp.Body)
if err != nil {
eprintf("failed to read body: %v\n", err)
continue
}
vers = target.parse(b)
}

if vers != nil {
makeVerFile(target.cfg, vers)
}
printf("")
}
printf("")
printf("done")
}
13 changes: 0 additions & 13 deletions fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ func Test_parseLuaRocksVers(t *testing.T) {
assert.Equal(t, exp, names)
}

func Test_parseLuaJitVers(t *testing.T) {
exp := []string{"LuaJIT-2.1.0-beta3.tar.gz", "LuaJIT-2.0.5.tar.gz", "LuaJIT-1.1.8.tar.gz", "LuaJIT-1.0.3.tar.gz"}
names := []string{}

list, _ := parseLuaJitVers([]byte(LuaJitHTML)).GetList()
for _, item := range list {
names = append(names, item.Name)
}
sort.Strings(exp)
sort.Strings(names)
assert.Equal(t, exp, names)
}

func Test_parseLuaVers(t *testing.T) {
exp := []string{"lua-5.4.4.tar.gz", "lua-5.4.3.tar.gz", "lua-5.4.2.tar.gz", "lua-5.4.1.tar.gz", "lua-5.4.0.tar.gz", "lua-5.3.6.tar.gz", "lua-5.3.5.tar.gz", "lua-5.3.4.tar.gz", "lua-5.3.3.tar.gz", "lua-5.3.2.tar.gz", "lua-5.3.1.tar.gz", "lua-5.3.0.tar.gz", "lua-5.2.4.tar.gz", "lua-5.2.3.tar.gz", "lua-5.2.2.tar.gz", "lua-5.2.1.tar.gz", "lua-5.2.0.tar.gz", "lua-5.1.5.tar.gz", "lua-5.1.4.tar.gz", "lua-5.1.3.tar.gz", "lua-5.1.2.tar.gz", "lua-5.1.1.tar.gz", "lua-5.1.tar.gz", "lua-5.0.3.tar.gz", "lua-5.0.2.tar.gz", "lua-5.0.1.tar.gz", "lua-5.0.tar.gz", "lua-4.0.1.tar.gz", "lua-4.0.tar.gz", "lua-3.2.2.tar.gz", "lua-3.2.1.tar.gz", "lua-3.2.tar.gz", "lua-3.1.tar.gz", "lua-3.0.tar.gz", "lua-2.5.tar.gz", "lua-2.4.tar.gz", "lua-2.2.tar.gz", "lua-2.1.tar.gz", "lua-1.1.tar.gz", "lua-1.0.tar.gz"}
names := []string{}
Expand Down
Loading

0 comments on commit df9c91d

Please sign in to comment.