From b6e94ce2128968d83b0714904b251de434b58ab2 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Fri, 4 Apr 2025 13:42:58 +0200 Subject: refactor: move shell logic into build.go --- .gitignore | 1 - build.go | 48 +++++++++++++++++++++++++++++++++++++++++++++--- clone.sh | 2 -- fetch_and_build.sh | 5 ----- find-lib-feats.sh | 5 +++++ find.sh | 2 -- template.html.tmpl | 2 +- 7 files changed, 51 insertions(+), 14 deletions(-) delete mode 100755 clone.sh delete mode 100755 fetch_and_build.sh create mode 100755 find-lib-feats.sh delete mode 100755 find.sh diff --git a/.gitignore b/.gitignore index 55b34c1..c082863 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ caniuse.rs/ rust/ out/ -lib_feats.txt diff --git a/build.go b/build.go index 9e91800..b1a525e 100644 --- a/build.go +++ b/build.go @@ -1,12 +1,15 @@ package main import ( + "bytes" _ "embed" "encoding/json" + "flag" "fmt" "html/template" "maps" "os" + "os/exec" "regexp" "slices" "strconv" @@ -16,6 +19,9 @@ import ( log "github.com/sirupsen/logrus" ) +const caniuseRepoURL = "https://github.com/jplatte/caniuse.rs" +const rustRepoURL = "https://github.com/rust-lang/rust" + var codeRegex = regexp.MustCompile("`(.+?)`") //go:embed template.html.tmpl @@ -27,13 +33,39 @@ var script []byte //go:embed style.css var style []byte +//go:embed find-lib-feats.sh +var findLibFeatsShellCommand string + func main() { caniuseRepo := "caniuse.rs" + rustRepo := "rust" outDir := "out" - libFeaturesText, err := os.ReadFile("lib_feats.txt") + skipDownload := flag.Bool("skip-download", false, + fmt.Sprintf("skip cloning/updating the %s and %s repos", caniuseRepo, rustRepo)) + flag.Parse() + + if !*skipDownload { + if _, err := os.Stat(caniuseRepo); os.IsNotExist(err) { + runCommand("git", "clone", caniuseRepoURL, caniuseRepo) + } else { + runCommand("git", "-C", caniuseRepo, "pull") + } + + if _, err := os.Stat(rustRepo); os.IsNotExist(err) { + runCommand("git", "clone", rustRepoURL, rustRepo, "--depth", "1") + } else { + runCommand("git", "-C", rustRepo, "fetch", "--depth", "1") + runCommand("git", "-C", rustRepo, "reset", "--hard", "origin/master") + } + } + + cmd := exec.Command("sh", "-c", findLibFeatsShellCommand, "find-lib-feats.sh", rustRepo) + var stderr bytes.Buffer + cmd.Stderr = &stderr + libFeaturesText, err := cmd.Output() if err != nil { - log.Fatalf("error reading lib_feats.txt: %s", err) + log.WithField("stderr", stderr.String()).Fatalf("failed to run shell command to find library features: %s", err) } libFeatureFlags := strings.Split(string(libFeaturesText), "\n") @@ -110,7 +142,8 @@ func main() { err = tmpl.Execute(outputFile, map[string]any{ - "Versions": versions, + "Versions": versions, + "CaniuseRepoURL": caniuseRepoURL, }, ) if err != nil { @@ -235,6 +268,15 @@ func compareVersion(a, b string) int { return aMinor - bMinor } +func runCommand(name string, args ...string) { + cmd := exec.Command(name, args...) + var stderr bytes.Buffer + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + log.WithField("stderr", stderr.String()).Fatalf("command '%s %v' failed: %v", name, args, err) + } +} + // workaround for https://github.com/golang/go/issues/37711 func nilAsEmptyArray[T any](slice []T) []T { return append([]T{}, slice...) diff --git a/clone.sh b/clone.sh deleted file mode 100755 index 0214f70..0000000 --- a/clone.sh +++ /dev/null @@ -1,2 +0,0 @@ -git clone https://github.com/jplatte/caniuse.rs -git clone --depth 1 https://github.com/rust-lang/rust diff --git a/fetch_and_build.sh b/fetch_and_build.sh deleted file mode 100755 index ba1c91c..0000000 --- a/fetch_and_build.sh +++ /dev/null @@ -1,5 +0,0 @@ -(cd caniuse.rs; git checkout main; git pull --ff-only) -(cd rust; git checkout master; git pull --ff-only) -./find.sh | grep -oP '(?<=feature = ")([^"]+)' > lib_feats.txt -rm -r build/ -go run build.go diff --git a/find-lib-feats.sh b/find-lib-feats.sh new file mode 100755 index 0000000..5db0973 --- /dev/null +++ b/find-lib-feats.sh @@ -0,0 +1,5 @@ +set -o pipefail +git -C "$1" grep -hEo '(un)?stable\(feature =.+\)' library/ \ + | grep -v 'issue = "none"' \ + | grep -oP '(?<=feature = ")([^"]+)' \ + | sort -u diff --git a/find.sh b/find.sh deleted file mode 100755 index 105c8cc..0000000 --- a/find.sh +++ /dev/null @@ -1,2 +0,0 @@ -cd rust -git grep -hEo '(un)?stable\(feature =.+\)' library/ | grep -v 'issue = "none"' | sort | uniq diff --git a/template.html.tmpl b/template.html.tmpl index d993725..e7e3921 100644 --- a/template.html.tmpl +++ b/template.html.tmpl @@ -23,7 +23,7 @@ With JavaScript you get a search box here.
page generated by push-f - from the caniuse.rs dataset + from the caniuse.rs dataset {{range .Versions}}
-- cgit v1.2.3