From 1ddb10cc366e47a471ef5e288339a1727c8541b8 Mon Sep 17 00:00:00 2001 From: sammy-ette Date: Fri, 13 Jun 2025 20:15:08 -0400 Subject: [PATCH] build: remove taskfile and use go-written program to build and install hilbish --- README.md | 9 +- Taskfile.yaml | 47 ---------- cmd/build/build.go | 210 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 52 deletions(-) delete mode 100644 Taskfile.yaml create mode 100644 cmd/build/build.go diff --git a/README.md b/README.md index aed2dfce..d1f5d597 100644 --- a/README.md +++ b/README.md @@ -43,8 +43,7 @@ on the website for distributed binaries from GitHub or other package repositorie Otherwise, continue reading for steps on compiling. ## Prerequisites -- [Go 1.22+](https://go.dev) -- [Task](https://taskfile.dev/installation/) (**Go on the hyperlink here to see Task's install method for your OS.**) +- [Go 1.23+](https://go.dev) ## Build First, clone Hilbish. The recursive is required, as some Lua libraries @@ -57,16 +56,16 @@ go get -d ./... To build, run: ``` -task +go run cmd/build/build.go ``` Or, if you want a stable branch, run these commands: ``` git checkout $(git describe --tags `git rev-list --tags --max-count=1`) -task build +go run cmd/build/build.go stable ``` -After you did all that, run `sudo task install` to install Hilbish globally. +After you did all that, run `sudo go run cmd/build/build.go install` to install Hilbish globally. # Contributing Any kind of contributions are welcome! Hilbish is very easy to contribute to. diff --git a/Taskfile.yaml b/Taskfile.yaml deleted file mode 100644 index 311d7d76..00000000 --- a/Taskfile.yaml +++ /dev/null @@ -1,47 +0,0 @@ -# https://taskfile.dev - -version: '3' - -vars: - PREFIX: '{{default "/usr/local" .PREFIX}}' - bindir__: '{{.PREFIX}}/bin' - BINDIR: '{{default .bindir__ .BINDIR}}' - libdir__: '' - LIBDIR: '{{default .libdir__ .LIBDIR}}' - goflags__: '-ldflags "-s -w -X main.dataDir={{.LIBDIR}}"' - GOFLAGS: '{{default .goflags__ .GOFLAGS}}' - -tasks: - default: - cmds: - - go build {{.GOFLAGS}} - vars: - GOFLAGS: '-ldflags "-s -w -X main.dataDir={{.LIBDIR}} -X main.gitCommit=$(git rev-parse --short HEAD) -X main.gitBranch=$(git rev-parse --abbrev-ref HEAD)"' - - default-nocgo: - cmds: - - CGO_ENABLED=0 go build {{.GOFLAGS}} - vars: - GOFLAGS: '-ldflags "-s -w -X main.dataDir={{.LIBDIR}} -X main.gitCommit=$(git rev-parse --short HEAD) -X main.gitBranch=$(git rev-parse --abbrev-ref HEAD)"' - - build: - cmds: - - go build {{.GOFLAGS}} - - build-nocgo: - cmds: - - CGO_ENABLED=0 go build {{.GOFLAGS}} - - install: - cmds: - - install -v -d "{{.DESTDIR}}{{.BINDIR}}/" && install -m 0755 -v hilbish "{{.DESTDIR}}{{.BINDIR}}/hilbish" - - mkdir -p "{{.DESTDIR}}{{.LIBDIR}}" - - cp -r libs docs emmyLuaDocs nature .hilbishrc.lua {{.DESTDIR}}{{.LIBDIR}} - - grep -qxF "{{.DESTDIR}}{{.BINDIR}}/hilbish" /etc/shells || echo "{{.DESTDIR}}{{.BINDIR}}/hilbish" >> /etc/shells - - uninstall: - cmds: - - rm -vrf - "{{.DESTDIR}}{{.BINDIR}}/hilbish" - "{{.DESTDIR}}{{.LIBDIR}}" - - grep -v 'hilbish' /etc/shells > /tmp/shells.hilbish_uninstall && mv /tmp/shells.hilbish_uninstall /etc/shells diff --git a/cmd/build/build.go b/cmd/build/build.go new file mode 100644 index 00000000..26a96f8b --- /dev/null +++ b/cmd/build/build.go @@ -0,0 +1,210 @@ +package main + +import ( + "bytes" + "fmt" + "io" + "io/fs" + "os" + "os/exec" + "path/filepath" + "runtime" +) + +var PREFIX string +var BINDIR string +var LIBDIR string + +func initEnvs(local bool) { + if pfx := os.Getenv("PREFIX"); pfx != "" { + PREFIX = pfx + } else { + switch runtime.GOOS { + case "windows": + // do nothing, idk + default: + PREFIX = "/usr/local" + } + } + + if ldr := os.Getenv("LIBDIR"); ldr != "" { + LIBDIR = ldr + } else { + switch runtime.GOOS { + case "windows": + // is this right? + LIBDIR = filepath.Join(os.Getenv("APPDATA"), "hilbish") + default: + LIBDIR = filepath.Join(PREFIX, "share", "hilbish") + } + } + + if bdr := os.Getenv("BINDIR"); bdr != "" { + BINDIR = bdr + } else { + switch runtime.GOOS { + case "windows": + // make it the same as libdir, because yes + BINDIR = LIBDIR + default: + BINDIR = filepath.Join(PREFIX, "bin") + } + } +} + +func main() { + local := false + if len(os.Args) >= 3 { + if os.Args[2] == "local" { + local = true + } + } + initEnvs(local) + + if len(os.Args) == 1 { + buildGit() + } else if os.Args[1] == "stable" { + buildStable() + } else if os.Args[1] == "install" { + install() + } +} + +func buildGit() { + var libdir string + switch runtime.GOOS { + case "linux": + if ldr := os.Getenv("LIBDIR"); ldr != "" { + libdir = ldr + } else { + libdir = LIBDIR + } + default: + libdir = LIBDIR + } + + sh, _ := exec.LookPath("sh") + cmd := exec.Command(sh, "-c", fmt.Sprintf(`go build -ldflags "-s -w -X main.dataDir=%s -X main.gitCommit=$(git rev-parse --short HEAD) -X main.gitBranch=$(git rev-parse --abbrev-ref HEAD)"`, libdir)) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + err := cmd.Run() + if err != nil { + fmt.Println(err) + } +} + +func buildStable() { + gopath, _ := exec.LookPath("go") + cmd := exec.Command(gopath, "build") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + err := cmd.Run() + if err != nil { + fmt.Println(err) + } +} + +func install() { + fmt.Println("bindir", BINDIR) + os.MkdirAll(BINDIR, 0755) + + hilbishPath := filepath.Join(BINDIR, "hilbish") + err := copyFile("hilbish", hilbishPath) + if err != nil { + panic(err) + } + + fmt.Println("libdir", LIBDIR) + err = os.MkdirAll(LIBDIR, 0755) + if err != nil { + panic(err) + } + + dirs := []string{"libs", "docs", "emmyLuaDocs", "nature"} + for _, dir := range dirs { + err := copyFS(filepath.Join(LIBDIR, dir), os.DirFS(dir)) + if err != nil { + panic(err) + } + } + + err = copyFile(".hilbishrc.lua", filepath.Join(LIBDIR, ".hilbishrc.lua")) + if err != nil { + panic(err) + } + + // i guess checking if not windows makes more sense.. + if runtime.GOOS != "windows" { + shells, err := os.ReadFile("/etc/shells") + if err != nil { + // pass, i guess + return + } + + if !bytes.Contains(shells, []byte(hilbishPath)) { + f, err := os.OpenFile("/etc/shells", os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + panic(err) + } + + f.Write([]byte(hilbishPath)) + f.Write([]byte("\n")) + } + } +} + +// https://pkg.go.dev/os#CopyFS that doesnt error if a file already exists +func copyFS(dir string, fsys fs.FS) error { + return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + fpath, err := filepath.Localize(path) + if err != nil { + return err + } + newPath := filepath.Join(dir, fpath) + if d.IsDir() { + return os.MkdirAll(newPath, 0777) + } + + // TODO(panjf2000): handle symlinks with the help of fs.ReadLinkFS + // once https://go.dev/issue/49580 is done. + // we also need filepathlite.IsLocal from https://go.dev/cl/564295. + if !d.Type().IsRegular() { + return &os.PathError{Op: "CopyFS", Path: path, Err: os.ErrInvalid} + } + + r, err := fsys.Open(path) + if err != nil { + return err + } + defer r.Close() + info, err := r.Stat() + if err != nil { + return err + } + w, err := os.OpenFile(newPath, os.O_CREATE|os.O_WRONLY, 0666|info.Mode()&0777) + if err != nil { + return err + } + + if _, err := io.Copy(w, r); err != nil { + w.Close() + return &os.PathError{Op: "Copy", Path: newPath, Err: err} + } + return w.Close() + }) +} + +func copyFile(from, to string) error { + exe, err := os.ReadFile(from) + if err != nil { + return err + } + + return os.WriteFile(to, exe, 0755) +}