Skip to content

Commit

Permalink
Merge pull request #130 from WillAbides/redist
Browse files Browse the repository at this point in the history
update bootstrapper build
  • Loading branch information
WillAbides authored Apr 1, 2023
2 parents a681cf1 + 948abef commit ce4cec7
Show file tree
Hide file tree
Showing 14 changed files with 893 additions and 81 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ jobs:
platform: [ ubuntu-20.04, macos-11 ]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: WillAbides/setup-go-faster@v1
id: setup-go
with:
go-version: '1.20.x'
- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: |
${{ steps.setup-go.outputs.GOCACHE }}
Expand All @@ -23,12 +23,12 @@ jobs:
lint:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: WillAbides/setup-go-faster@v1
id: setup-go
with:
go-version: '1.20.x'
- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: |
${{ steps.setup-go.outputs.GOCACHE }}
Expand All @@ -40,12 +40,12 @@ jobs:
make-bindown:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: WillAbides/setup-go-faster@v1
id: setup-go
with:
go-version: '1.20.x'
- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: |
${{ steps.setup-go.outputs.GOCACHE }}
Expand All @@ -56,12 +56,12 @@ jobs:
generate:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: WillAbides/setup-go-faster@v1
id: setup-go
with:
go-version: '1.20.x'
- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: |
${{ steps.setup-go.outputs.GOCACHE }}
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ bin/bindown: gobuildcache bin/go
$(GOBUILD) -o $@ ./cmd/bindown
bins += bin/bindown

bin/build-bootstrapper: gobuildcache bin/go
$(GOBUILD) -o $@ ./internal/build-bootstrapper
bins += bin/build-bootstrapper

bin/golangci-lint: bin/bindown
bin/bindown install -q $(notdir $@)
bins += bin/golangci-lint
Expand Down Expand Up @@ -49,6 +53,14 @@ bin/shfmt: bin/bindown
bin/bindown install -q $(notdir $@)
bins += bin/shfmt

bin/gh: bin/bindown
bin/bindown install -q $(notdir $@)
bins += bin/gh

bin/jq: bin/bindown
bin/bindown install -q $(notdir $@)
bins += bin/jq

HANDCRAFTED_REV := 082e94edadf89c33db0afb48889c8419a2cb46a9
bin/handcrafted: Makefile
GOBIN=${CURDIR}/bin \
Expand Down
83 changes: 83 additions & 0 deletions bindown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ systems:
- linux/amd64
- windows/amd64
dependencies:
gh:
template: origin#gh
vars:
version: 2.25.1
go:
template: local-origin#go
vars:
Expand All @@ -23,6 +27,10 @@ dependencies:
template: local-origin#goreleaser
vars:
version: 1.16.1
jq:
template: origin#jq
vars:
version: "1.6"
mockgen:
template: local-origin#mockgen
vars:
Expand All @@ -44,6 +52,11 @@ dependencies:
vars:
version: 3.2.1
templates:
bindown-checksums:
url: https://github.com/WillAbides/bindown/releases/download/{{.tag}}/checksums.txt
bin: checksums.txt
required_vars:
- tag
local-origin#go:
url: https://dl.google.com/go/go{{.version}}.{{.os}}-{{.arch}}{{.urlsuffix}}
archive_path: go/bin/go{{.archivepathsuffix}}
Expand Down Expand Up @@ -272,6 +285,69 @@ templates:
- openbsd/amd64
- windows/386
- windows/amd64
origin#gh:
url: https://github.com/cli/cli/releases/download/v{{.version}}/gh_{{.version}}_{{.os}}_{{.arch}}.{{.archiveext}}
archive_path: gh_{{.version}}_{{.os}}_{{.arch}}/bin/gh
bin: gh
vars:
archiveext: tar.gz
required_vars:
- version
overrides:
- matcher:
os:
- windows
dependency:
archive_path: bin/gh.exe
vars:
archiveext: zip
substitutions:
os:
darwin: macOS
systems:
- linux/386
- linux/amd64
- linux/arm64
- darwin/amd64
- darwin/arm64
- windows/amd64
origin#jq:
url: https://github.com/stedolan/jq/releases/download/jq-{{.version}}/jq-{{.os}}{{.arch}}{{.extension}}
archive_path: jq-{{.os}}{{.arch}}{{.extension}}
bin: jq
vars:
extension: ""
required_vars:
- version
overrides:
- matcher:
arch:
- amd64
- arm64
os:
- darwin
dependency:
url: https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64
archive_path: jq-osx-amd64
- matcher:
os:
- windows
dependency:
vars:
extension: .exe
substitutions:
arch:
"386": "32"
amd64: "64"
os:
windows: win
systems:
- linux/386
- linux/amd64
- darwin/amd64
- darwin/arm64
- windows/386
- windows/amd64
origin#shellcheck:
url: https://github.com/koalaman/shellcheck/releases/download/v{{.version}}/shellcheck-v{{.version}}.{{.os}}.{{.arch}}.tar.xz
archive_path: shellcheck-v{{.version}}/shellcheck
Expand Down Expand Up @@ -336,6 +412,10 @@ url_checksums:
https://github.com/WillAbides/semver-next/releases/download/v0.11.3/semver-next_0.11.3_darwin_amd64.tar.gz: 4f7fbb6b16600bdbd049250a1389a01e08a1490f4ceb864b78a19be9f4f74e75
https://github.com/WillAbides/semver-next/releases/download/v0.11.3/semver-next_0.11.3_linux_amd64.tar.gz: 35b024a2ace9977672e78979f13e9bc48973c4a8f19ae7517f1796fbb91a6c11
https://github.com/WillAbides/semver-next/releases/download/v0.11.3/semver-next_0.11.3_windows_amd64.tar.gz: 0a5e3e743daf3e2eb1e8b41f4787472be1791ac9157c40ac2e9ff39c85eb8730
https://github.com/cli/cli/releases/download/v2.25.1/gh_2.25.1_linux_amd64.tar.gz: 869e780f9391804c1f9d355f6f7777931ef61447ca50c75433a5942f484e61d1
https://github.com/cli/cli/releases/download/v2.25.1/gh_2.25.1_macOS_amd64.tar.gz: 414f5766a50b95ec73fcff17af6dc5c354b44d7f041370fea9de8f91990cc419
https://github.com/cli/cli/releases/download/v2.25.1/gh_2.25.1_macOS_arm64.tar.gz: e18f423f9983f84f011bebf9dea3b3bcd67cc79ef419040316cba35595bdb4b4
https://github.com/cli/cli/releases/download/v2.25.1/gh_2.25.1_windows_amd64.zip: 33be468155dabc1990e984eb9cb9e134c50cfe1c0bbeab92643355a8ea04e162
https://github.com/golang/mock/releases/download/v1.6.0/mock_1.6.0_darwin_amd64.tar.gz: 938649db27055ec85bede32ccb95ff82f82af3c90a084ee52687a92066bcca92
https://github.com/golang/mock/releases/download/v1.6.0/mock_1.6.0_darwin_arm64.tar.gz: 67a4a597dd759e186e17400cbc273be99c8cffc590335381134b3e933b409c83
https://github.com/golang/mock/releases/download/v1.6.0/mock_1.6.0_linux_amd64.tar.gz: c73e117943739df5dd89d63fed6e80cb783852a0cc42abc3359d708c2e125b78
Expand All @@ -362,3 +442,6 @@ url_checksums:
https://github.com/mvdan/sh/releases/download/v3.6.0/shfmt_v3.6.0_darwin_arm64: 633f242246ee0a866c5f5df25cbf61b6af0d5e143555aca32950059cf13d91e0
https://github.com/mvdan/sh/releases/download/v3.6.0/shfmt_v3.6.0_linux_amd64: 5741a02a641de7e56b8da170e71a97e58050d66a3cf485fb268d6a5a8bb74afb
https://github.com/mvdan/sh/releases/download/v3.6.0/shfmt_v3.6.0_windows_amd64.exe: 18122d910ba434be366588f37c302c309cde4ca5403f93285254a3cf96839d01
https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64: af986793a515d500ab2d35f8d2aecd656e764504b789b66d7e1a0b727a124c44
https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64: 5c0a0a3ea600f302ee458b30317425dd9632d1ad8882259fcaf4e9b868b2b1ef
https://github.com/stedolan/jq/releases/download/jq-1.6/jq-win64.exe: a51d36968dcbdeabb3142c6f5cf9b401a65dc3a095f3144bd0c118d5bb192753
12 changes: 12 additions & 0 deletions internal/build-bootstrapper/assets/bootstrap-bindown.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

set -e

TAG="{{ .tag }}"

CHECKSUMS="
{{ .checksums }}
"

{{ .shlib }}
{{ .main }}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
##/ this is the static part of bootstrap-bindown.sh
##/ used in script/build-bootstrapper

FORMAT=tar.gz
GITHUB_DOWNLOAD=https://github.com/WillAbides/bindown/releases/download

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
##/ this is the part of bootstrap-bindown.sh that comes from https://github.com/client9/shlib
##/ used in script/build-bootstrapper

cat /dev/null <<EOF
------------------------------------------------------------------------
https://github.com/client9/shlib - portable posix shell functions
Expand Down
143 changes: 143 additions & 0 deletions internal/build-bootstrapper/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package main

import (
"bytes"
"embed"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)

//go:embed assets/*
var assets embed.FS

func execBindown(repoRoot string, arg ...string) error {
bindownPath := filepath.FromSlash("bin/bootstrapped/bindown")
//nolint:gosec // subprocess launch with variable
makeCmd := exec.Command("make", bindownPath)
makeCmd.Dir = repoRoot
err := makeCmd.Run()
if err != nil {
return err
}
//nolint:gosec // subprocess launch with variable
bindownCmd := exec.Command(bindownPath, arg...)
bindownCmd.Dir = repoRoot
return bindownCmd.Run()
}

func build(tag, repoRoot string) (_ string, errOut error) {
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return "", err
}
defer func() {
rmErr := os.RemoveAll(tmpDir)
if errOut == nil {
errOut = rmErr
}
}()
err = execBindown(repoRoot, "install", "shfmt")
if err != nil {
return "", err
}
err = execBindown(repoRoot, "install", "shellcheck")
if err != nil {
return "", err
}
err = execBindown(
repoRoot,
"dependency",
"add",
"bindown-checksums",
"bindown-checksums",
"--var",
fmt.Sprintf("tag=%s", tag),
"--skipchecksums",
)
if err != nil {
return "", err
}
defer func() {
removeErr := execBindown(repoRoot, "dependency", "remove", "bindown-checksums")
if errOut != nil {
errOut = removeErr
}
}()
checksumsDir := filepath.Join(tmpDir, "checksums")
err = execBindown(
repoRoot,
"extract",
"bindown-checksums",
"--allow-missing-checksum",
"--output",
checksumsDir,
)
if err != nil {
return "", err
}
checksums, err := os.ReadFile(filepath.Join(checksumsDir, "checksums.txt"))
if err != nil {
return "", err
}
shlibContent, err := assets.ReadFile("assets/shlib.sh")
if err != nil {
return "", err
}
mainContent, err := assets.ReadFile("assets/main.sh")
if err != nil {
return "", err
}
tmplContent, err := assets.ReadFile("assets/bootstrap-bindown.gotmpl")
if err != nil {
return "", err
}
tmpl, err := template.New("").Parse(string(tmplContent))
if err != nil {
return "", err
}
var tmplOut bytes.Buffer
err = tmpl.Execute(&tmplOut, map[string]string{
"tag": tag,
"checksums": strings.TrimSpace(string(checksums)),
"shlib": string(shlibContent),
"main": string(mainContent),
})
if err != nil {
return "", err
}
//nolint:gosec // subprocess launch with variable
shfmtCmd := exec.Command(filepath.Join(repoRoot, "bin", "shfmt"), "-i", "2", "-ci", "-sr", "-")
shfmtCmd.Stdin = &tmplOut
formatted, err := shfmtCmd.Output()
if err != nil {
return "", err
}
//nolint:gosec // subprocess launch with variable
shellcheckCmd := exec.Command(filepath.Join(repoRoot, "bin", "shellcheck"), "--shell", "sh", "-")
shellcheckCmd.Stdin = bytes.NewReader(formatted)
err = shellcheckCmd.Run()
if err != nil {
return "", err
}
return string(formatted), nil
}

func main() {
var tag, repoRoot string
flag.StringVar(&tag, "tag", "", "tag to build")
flag.StringVar(&repoRoot, "repo-root", ".", "path to bindown repo root")
flag.Parse()
if tag == "" {
panic("tag is required")
}
got, err := build(tag, repoRoot)
if err != nil {
panic(err)
}
fmt.Println(got)
}
18 changes: 18 additions & 0 deletions internal/build-bootstrapper/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)

func TestBuild(t *testing.T) {
got, err := build("v3.15.5", filepath.FromSlash("../../"))
require.NoError(t, err)
want, err := os.ReadFile(filepath.FromSlash("testdata/want.txt"))
require.NoError(t, err)
require.Empty(t, cmp.Diff(string(want), got))
}
Loading

0 comments on commit ce4cec7

Please sign in to comment.