diff --git a/README.md b/README.md index 2034675..22a3708 100644 --- a/README.md +++ b/README.md @@ -252,9 +252,9 @@ The library is organized in layers: The flasher includes pre-compiled bootloader stubs from [esp-flasher-stub](https://github.com/espressif/esp-flasher-stub) releases. To update stubs: -1. Edit `STUB_VERSION` in `tools/update-stubs.sh` to the desired release version +1. Edit `stubVersion` in `tools/update-stubs.go` to the desired release version 2. Run `go generate ./pkg/espflasher/...` to download and embed the latest stubs -3. The `go:generate` directive in `pkg/espflasher/stub.go` will invoke `tools/update-stubs.sh` +3. The `go:generate` directive in `pkg/espflasher/stub.go` will invoke `tools/update-stubs.go` ## Protocol Reference diff --git a/pkg/espflasher/stub.go b/pkg/espflasher/stub.go index d3df9e2..d8f0901 100644 --- a/pkg/espflasher/stub.go +++ b/pkg/espflasher/stub.go @@ -7,7 +7,7 @@ import ( "fmt" ) -//go:generate bash ../../tools/update-stubs.sh +//go:generate go run ../../tools/update-stubs.go //go:embed stubs var stubFS embed.FS diff --git a/tools/update-stubs.go b/tools/update-stubs.go new file mode 100644 index 0000000..ccde755 --- /dev/null +++ b/tools/update-stubs.go @@ -0,0 +1,71 @@ +// Command update-stubs downloads pre-compiled bootloader stubs from the +// esp-flasher-stub GitHub releases and writes them to pkg/espflasher/stubs/. +package main + +import ( + "fmt" + "io" + "net/http" + "os" + "path/filepath" +) + +const ( + stubVersion = "v0.5.1" + baseURL = "https://github.com/espressif/esp-flasher-stub/releases/download/" + stubVersion +) + +var chips = []string{ + "esp8266", + "esp32", + "esp32s2", + "esp32s3", + "esp32c2", + "esp32c3", + "esp32c5", + "esp32c6", + "esp32h2", +} + +func main() { + // Resolve the stubs directory relative to this tool's source file. + stubDir := filepath.Join("..", "pkg", "espflasher", "stubs") + if dir := os.Getenv("STUB_DIR"); dir != "" { + stubDir = dir + } + + fmt.Printf("Downloading stubs from %s to %s\n", baseURL, stubDir) + + for _, chip := range chips { + url := baseURL + "/" + chip + ".json" + dest := filepath.Join(stubDir, chip+".json") + + if err := download(url, dest); err != nil { + fmt.Fprintf(os.Stderr, "error downloading %s: %v\n", chip, err) + os.Exit(1) + } + } + + fmt.Printf("Successfully updated all %d stubs to %s\n", len(chips), stubVersion) +} + +func download(url, dest string) error { + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("HTTP %s for %s", resp.Status, url) + } + + f, err := os.Create(dest) + if err != nil { + return err + } + defer f.Close() + + _, err = io.Copy(f, resp.Body) + return err +} diff --git a/tools/update-stubs.sh b/tools/update-stubs.sh deleted file mode 100755 index edfa41d..0000000 --- a/tools/update-stubs.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -STUB_VERSION=v0.5.1 -CHIPS=(esp8266 esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c5 esp32c6 esp32h2) -BASE_URL=https://github.com/espressif/esp-flasher-stub/releases/download/${STUB_VERSION} -STUB_DIR=$(cd "$(dirname "$0")/.." && pwd)/pkg/espflasher/stubs - -echo "Downloading stubs from ${BASE_URL} to ${STUB_DIR}" - -for chip in "${CHIPS[@]}"; do - curl -sL -o "${STUB_DIR}/${chip}.json" "${BASE_URL}/${chip}.json" -done - -echo "Successfully updated all ${#CHIPS[@]} stubs to ${STUB_VERSION}"