diff options
| author | Xe Iaso <me@xeiaso.net> | 2025-04-01 20:21:08 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2025-04-01 20:21:08 -0400 |
| commit | 6e622796547d01f59694687decf157a9f09dd51f (patch) | |
| tree | bbbcdbef2b144141b01f72668077061e464de450 /cmd | |
| parent | 45b660e400c72c7a5a641889386d445ffe1ce202 (diff) | |
| download | x-6e622796547d01f59694687decf157a9f09dd51f.tar.xz x-6e622796547d01f59694687decf157a9f09dd51f.zip | |
cmd/yeet: modernize build syntax, nix nix
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/yeet/README.md | 84 | ||||
| -rw-r--r-- | cmd/yeet/internal/git.go | 2 | ||||
| -rw-r--r-- | cmd/yeet/internal/mkdeb/mkdeb.go | 23 | ||||
| -rw-r--r-- | cmd/yeet/internal/mkrpm/mkrpm.go | 22 | ||||
| -rw-r--r-- | cmd/yeet/internal/pkgmeta/package.go | 20 | ||||
| -rw-r--r-- | cmd/yeet/main.go | 91 | ||||
| -rw-r--r-- | cmd/yeet/yeetfile.js | 54 |
7 files changed, 110 insertions, 186 deletions
diff --git a/cmd/yeet/README.md b/cmd/yeet/README.md index 4e61d6f..f03973a 100644 --- a/cmd/yeet/README.md +++ b/cmd/yeet/README.md @@ -17,6 +17,16 @@ Yeet uses [goja](https://pkg.go.dev/github.com/dop251/goja#section-readme) to ex To make it useful, yeet exposes a bunch of helper objects full of tools. These tools fall in a few categories, each has its own section. +### `$` + +`$` lets you construct shell commands using [tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals). This lets you build whatever shell commands you want by mixing Go and JavaScript values freely. + +Example: + +```js +$`CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -s -w -extldflags "-static" -X "within.website/x.Version=${git.tag()}"`; +``` + ### `docker` Aliases for `docker` commands. @@ -176,80 +186,6 @@ Usage: go.install(); ``` -### `nix` - -Automation for running Nix ecosystem tooling. - -#### `nix.build` - -Runs `nix build` against a given flakeref. - -Usage: - -`nix.build(flakeref);` - -```js -nix.build(".#docker"); -docker.load("./result"); -``` - -#### `nix.eval` - -A tagged template that helps you build Nix expressions safely from JavaScript and then evaluates them. See my [nixexpr blogpost](https://xeiaso.net/blog/nixexpr/) for more information about how this works. - -Usage: - -```js -const glibcPath = nix.eval`let pkgs = import <nixpkgs>; in pkgs.glibc`; -``` - -#### `nix.expr` - -A tagged template that helps you build Nix expressions safely from JavaScript. See my [nixexpr blogpost](https://xeiaso.net/blog/nixexpr/) for more information about how this works. - -Usage: - -```js -go.build(); -const fname = slug.build("todayinmarch2020"); - -const url = slug.push(fname); -const hash = nix.hashURL(url); - -const expr = nix.expr`{ stdenv }: - -stdenv.mkDerivation { - name = "todayinmarch2020"; - src = builtins.fetchurl { - url = ${url}; - sha256 = ${hash}; - }; - - phases = "installPhase"; - - installPhase = '' - tar xf $src - mkdir -p $out/bin - cp bin/main $out/bin/todayinmarch2020 - ''; -} -`; - -file.write(`${repoRoot}/pkgs/x/todayinmarch2020.nix`, expr); -``` - -#### `nix.hashURL` - -Hashes the contents of a given URL and returns the `sha256` SRI form. Useful when composing Nix expressions with the `nix.expr` tagged template. - -Usage: - -`nix.hashURL(url);` - -```js -const hash = nix.hashURL("https://whatever.com/some_file.tgz"); -``` - ### `rpm` Helpers for building RPM packages and docker images out of a constellation of RPM packages. diff --git a/cmd/yeet/internal/git.go b/cmd/yeet/internal/git.go index fb8ae26..6ed6127 100644 --- a/cmd/yeet/internal/git.go +++ b/cmd/yeet/internal/git.go @@ -20,7 +20,7 @@ var ( const ( fallbackName = "Mimi Yasomi" - fallbackEmail = "mimi@xeserv.us" + fallbackEmail = "mimi@techaro.lol" ) func gpgKeyFileLocation() string { diff --git a/cmd/yeet/internal/mkdeb/mkdeb.go b/cmd/yeet/internal/mkdeb/mkdeb.go index 8f9279a..35d8c06 100644 --- a/cmd/yeet/internal/mkdeb/mkdeb.go +++ b/cmd/yeet/internal/mkdeb/mkdeb.go @@ -49,7 +49,14 @@ func Build(p pkgmeta.Package) (foutpath string, err error) { os.Setenv("GOARCH", p.Goarch) os.Setenv("GOOS", "linux") - p.Build(dir) + p.Build(pkgmeta.BuildInput{ + Output: dir, + Bin: filepath.Join(dir, "usr", "bin"), + Doc: filepath.Join(dir, "usr", "share", "doc"), + Etc: filepath.Join(dir, "etc", p.Name), + Man: filepath.Join(dir, "usr", "share", "man"), + Systemd: filepath.Join(dir, "usr", "lib", "systemd", "system"), + }) var contents files.Contents @@ -62,7 +69,11 @@ func Build(p pkgmeta.Package) (foutpath string, err error) { } for repoPath, osPath := range p.ConfigFiles { - contents = append(contents, &files.Content{Type: files.TypeConfig, Source: repoPath, Destination: osPath}) + contents = append(contents, &files.Content{ + Type: files.TypeConfig, + Source: repoPath, + Destination: osPath, + }) } for repoPath, rpmPath := range p.Documentation { @@ -73,6 +84,14 @@ func Build(p pkgmeta.Package) (foutpath string, err error) { }) } + for repoPath, rpmPath := range p.Files { + contents = append(contents, &files.Content{ + Type: files.TypeFile, + Source: repoPath, + Destination: rpmPath, + }) + } + if err := filepath.Walk(dir, func(path string, stat os.FileInfo, err error) error { if err != nil { return err diff --git a/cmd/yeet/internal/mkrpm/mkrpm.go b/cmd/yeet/internal/mkrpm/mkrpm.go index 33b7937..679e2b3 100644 --- a/cmd/yeet/internal/mkrpm/mkrpm.go +++ b/cmd/yeet/internal/mkrpm/mkrpm.go @@ -42,14 +42,24 @@ func Build(p pkgmeta.Package) (foutpath string, err error) { defer os.RemoveAll(dir) os.MkdirAll(dir, 0755) + cgoEnabled := os.Getenv("CGO_ENABLED") defer func() { os.Setenv("GOARCH", runtime.GOARCH) os.Setenv("GOOS", runtime.GOOS) + os.Setenv("CGO_ENABLED", cgoEnabled) }() os.Setenv("GOARCH", p.Goarch) os.Setenv("GOOS", "linux") - - p.Build(dir) + os.Setenv("CGO_ENABLED", "0") + + p.Build(pkgmeta.BuildInput{ + Output: dir, + Bin: filepath.Join(dir, "usr", "bin"), + Doc: filepath.Join(dir, "usr", "share", "doc"), + Etc: filepath.Join(dir, "etc", p.Name), + Man: filepath.Join(dir, "usr", "share", "man"), + Systemd: filepath.Join(dir, "usr", "lib", "systemd", "system"), + }) var contents files.Contents @@ -77,6 +87,14 @@ func Build(p pkgmeta.Package) (foutpath string, err error) { }) } + for repoPath, rpmPath := range p.Files { + contents = append(contents, &files.Content{ + Type: files.TypeFile, + Source: repoPath, + Destination: rpmPath, + }) + } + if err := filepath.Walk(dir, func(path string, stat os.FileInfo, err error) error { if err != nil { return err diff --git a/cmd/yeet/internal/pkgmeta/package.go b/cmd/yeet/internal/pkgmeta/package.go index 056af3b..1aaa5a7 100644 --- a/cmd/yeet/internal/pkgmeta/package.go +++ b/cmd/yeet/internal/pkgmeta/package.go @@ -13,8 +13,22 @@ type Package struct { Recommends []string `json:"recommends"` EmptyDirs []string `json:"emptyDirs"` // rpm destination path - ConfigFiles map[string]string `json:"configFiles"` // repo-relative source path, rpm destination path - Documentation map[string]string `json:"documentation"` // repo-relative source path, file in /usr/share/doc/$Name + ConfigFiles map[string]string `json:"configFiles"` // pwd-relative source path, rpm destination path + Documentation map[string]string `json:"documentation"` // pwd-relative source path, file in /usr/share/doc/$Name + Files map[string]string `json:"files"` // pwd-relative source path, rpm destination path - Build func(out string) `json:"build"` + Build func(BuildInput) `json:"build"` +} + +type BuildInput struct { + Output string `json:"out"` + Bin string `json:"bin"` + Doc string `json:"doc"` + Etc string `json:"etc"` + Man string `json:"man"` + Systemd string `json:"systemd"` +} + +func (b BuildInput) String() string { + return b.Output } diff --git a/cmd/yeet/main.go b/cmd/yeet/main.go index a97d9a1..2ad7985 100644 --- a/cmd/yeet/main.go +++ b/cmd/yeet/main.go @@ -2,19 +2,18 @@ package main import ( "context" - "encoding/json" "flag" "fmt" - "io" "log" "log/slog" "os" + "os/exec" "path/filepath" "runtime" "runtime/debug" - "strconv" "strings" + "al.essio.dev/pkg/shellescape" "github.com/dop251/goja" "within.website/x/cmd/yeet/internal/mkdeb" "within.website/x/cmd/yeet/internal/mkrpm" @@ -117,28 +116,33 @@ func slugpush(fname string) string { return pubURL } -func buildNixExpr(literals []string, exprs ...any) string { - result := "" +func buildShellCommand(literals []string, exprs ...any) string { + var sb strings.Builder for i, value := range exprs { - formattedValue, _ := json.Marshal(value) - formattedValue = []byte(fmt.Sprintf(`(builtins.fromJSON %s)`, strconv.Quote(string(formattedValue)))) - result += literals[i] + string(formattedValue) + sb.WriteString(literals[i]) + sb.WriteString(shellescape.Quote(fmt.Sprint(value))) } - result += literals[len(literals)-1] + sb.WriteString(literals[len(literals)-1]) - return result + return sb.String() } -func evalNixExpr(literals []string, exprs ...any) any { - expr := buildNixExpr(literals, exprs...) - data := []byte(runcmd("nix", "eval", "--json", "--expr", expr)) - var result any - if err := json.Unmarshal(data, &result); err != nil { +func runShellCommand(literals []string, exprs ...any) string { + shPath, err := exec.LookPath("sh") + if err != nil { panic(err) } - return result + cmd := buildShellCommand(literals, exprs...) + + slog.Debug("running command", "cmd", cmd) + output, err := yeet.Output(context.Background(), shPath, "-c", cmd) + if err != nil { + panic(err) + } + + return output } func hostname() string { @@ -178,6 +182,8 @@ func main() { lg := log.New(writer.LineSplitting(writer.PrefixWriter("[yeet] ", os.Stdout)), "", 0) + vm.Set("$", runShellCommand) + vm.Set("deb", map[string]any{ "build": func(p pkgmeta.Package) string { foutpath, err := mkdeb.Build(p) @@ -194,52 +200,6 @@ func main() { "push": dockerpush, }) - vm.Set("file", map[string]any{ - "read": func(fname string) string { - data, err := os.ReadFile(fname) - if err != nil { - panic(err) - } - return string(data) - }, - "write": func(fname, data string) { - if err := os.WriteFile(fname, []byte(data), 0660); err != nil { - panic(err) - } - }, - "copy": func(from, to string) { - st, err := os.Stat(from) - if err != nil { - panic(err) - } - - fin, err := os.Open(from) - if err != nil { - panic(err) - } - defer fin.Close() - - dir := filepath.Dir(to) - os.MkdirAll(dir, 0777) - - fout, err := os.OpenFile(to, os.O_CREATE, st.Mode()) - if err != nil { - panic(err) - } - defer fout.Close() - - n, err := io.Copy(fout, fin) - if err != nil { - panic(err) - } - - if n != st.Size() { - slog.Error("wrong number of bytes written", "from", from, "to", to, "want", st.Size(), "got", n) - panic("copy failed") - } - }, - }) - vm.Set("fly", map[string]any{ "deploy": flydeploy, }) @@ -264,13 +224,6 @@ func main() { "println": fmt.Println, }) - vm.Set("nix", map[string]any{ - "build": nixbuild, - "eval": evalNixExpr, - "expr": buildNixExpr, - "hashURL": func(fileURL string) string { return strings.TrimSpace(runcmd("nix-prefetch-url", fileURL)) }, - }) - vm.Set("rpm", map[string]any{ "build": func(p pkgmeta.Package) string { foutpath, err := mkrpm.Build(p) diff --git a/cmd/yeet/yeetfile.js b/cmd/yeet/yeetfile.js index 7969ef7..b8468d5 100644 --- a/cmd/yeet/yeetfile.js +++ b/cmd/yeet/yeetfile.js @@ -1,41 +1,25 @@ go.install(); yeet.setenv("GOARM", "7"); +yeet.setenv("CGO_ENABLED", "0"); -[ - // "386", - "amd64", - // "arm", - "arm64", - // "loong64", - // "mips", - // "mips64", - // "mips64le", - // "mipsle", - "riscv64", - // "ppc64", - // "ppc64le", - // "s390x", -] - .forEach(goarch => { - [ - deb, - rpm, - ] - .forEach(method => method.build({ - name: "yeet", - description: "Yeet out actions with maximum haste!", - homepage: "https://within.website", - license: "CC0", - goarch, +$`CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -o ./var/yeet -ldflags '-s -w -extldflags "-static" -X "within.website/x.Version=${git.tag()}"'`; - documentation: { - "README.md": "README.md", - "../../LICENSE": "LICENSE", - }, +["amd64", "arm64"].forEach(goarch => { + [deb, rpm].forEach(method => method.build({ + name: "yeet", + description: "Yeet out actions with maximum haste!", + homepage: "https://within.website", + license: "CC0", + goarch, - build: (out) => { - go.build("-o", `${out}/usr/bin/yeet`); - }, - })); - });
\ No newline at end of file + documentation: { + "README.md": "README.md", + "../../LICENSE": "LICENSE", + }, + + build: ({ bin }) => { + $`CGO_ENABLED=0 go build -o ${bin}/yeet -ldflags '-s -w -extldflags "-static" -X "within.website/x.Version=${git.tag()}"'` + }, + })) +})
\ No newline at end of file |
