|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Check version argument |
| 6 | +if [[ $# -ne 3 ]]; then |
| 7 | + echo "Usage: $0 <version> <base_folder> <json_path>" |
| 8 | + echo "Example: $0 5.0.dev1 /tmp/esptool /tmp/esptool-5.0.dev1.json" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +VERSION=$1 |
| 13 | +BASE_FOLDER=$2 |
| 14 | +JSON_PATH=$3 |
| 15 | + |
| 16 | +export COPYFILE_DISABLE=1 |
| 17 | + |
| 18 | +shopt -s nullglob # So for loop doesn't run if no matches |
| 19 | + |
| 20 | +# Function to update JSON for a given host |
| 21 | +function update_json_for_host { |
| 22 | + local host=$1 |
| 23 | + local archive=$2 |
| 24 | + |
| 25 | + # Extract the old url from the JSON for this host, then replace only the filename |
| 26 | + old_url=$(jq -r --arg host "$host" ' |
| 27 | + .packages[].tools[] | select(.name == "esptool_py") | .systems[] | select(.host == $host) | .url // empty |
| 28 | + ' "$tmp_json") |
| 29 | + if [[ -n "$old_url" ]]; then |
| 30 | + base_url="${old_url%/*}" |
| 31 | + url="$base_url/$archive" |
| 32 | + else |
| 33 | + echo "No old url found for $host" |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + |
| 37 | + archiveFileName="$archive" |
| 38 | + checksum="SHA-256:$(shasum -a 256 "$archive" | awk '{print $1}')" |
| 39 | + size=$(stat -f%z "$archive") |
| 40 | + |
| 41 | + # Use jq to update the JSON |
| 42 | + jq --arg host "$host" \ |
| 43 | + --arg url "$url" \ |
| 44 | + --arg archiveFileName "$archiveFileName" \ |
| 45 | + --arg checksum "$checksum" \ |
| 46 | + --arg size "$size" \ |
| 47 | + ' |
| 48 | + .packages[].tools[] |
| 49 | + |= if .name == "esptool_py" then |
| 50 | + .systems = ( |
| 51 | + ((.systems // []) | map(select(.host != $host))) + [{ |
| 52 | + host: $host, |
| 53 | + url: $url, |
| 54 | + archiveFileName: $archiveFileName, |
| 55 | + checksum: $checksum, |
| 56 | + size: $size |
| 57 | + }] |
| 58 | + ) |
| 59 | + else |
| 60 | + . |
| 61 | + end |
| 62 | + ' "$tmp_json" > "$tmp_json.new" && mv "$tmp_json.new" "$tmp_json" |
| 63 | +} |
| 64 | + |
| 65 | +cd "$BASE_FOLDER" |
| 66 | + |
| 67 | +# Delete all archives before starting |
| 68 | +rm -f esptool-*.tar.gz esptool-*.zip |
| 69 | + |
| 70 | +for dir in esptool-*; do |
| 71 | + # Check if directory exists and is a directory |
| 72 | + if [[ ! -d "$dir" ]]; then |
| 73 | + continue |
| 74 | + fi |
| 75 | + |
| 76 | + base="${dir#esptool-}" |
| 77 | + |
| 78 | + # Add 'linux-' prefix if base doesn't contain linux/macos/win64 |
| 79 | + if [[ "$base" != *linux* && "$base" != *macos* && "$base" != *win64* ]]; then |
| 80 | + base="linux-${base}" |
| 81 | + fi |
| 82 | + |
| 83 | + if [[ "$dir" == esptool-win* ]]; then |
| 84 | + # Windows zip archive |
| 85 | + zipfile="esptool-v${VERSION}-${base}.zip" |
| 86 | + echo "Creating $zipfile from $dir ..." |
| 87 | + zip -r "$zipfile" "$dir" |
| 88 | + else |
| 89 | + # Non-Windows: set permissions and tar.gz archive |
| 90 | + tarfile="esptool-v${VERSION}-${base}.tar.gz" |
| 91 | + echo "Setting permissions and creating $tarfile from $dir ..." |
| 92 | + chmod -R u=rwx,g=rx,o=rx "$dir" |
| 93 | + tar -cvzf "$tarfile" "$dir" |
| 94 | + fi |
| 95 | +done |
| 96 | + |
| 97 | +# After the for loop, update the JSON for each archive |
| 98 | +# Create a temporary JSON file to accumulate changes |
| 99 | +tmp_json="${JSON_PATH}.tmp" |
| 100 | +cp "$JSON_PATH" "$tmp_json" |
| 101 | + |
| 102 | +for archive in esptool-v"${VERSION}"-*.tar.gz esptool-v"${VERSION}"-*.zip; do |
| 103 | + [ -f "$archive" ] || continue |
| 104 | + |
| 105 | + echo "Updating JSON for $archive" |
| 106 | + |
| 107 | + # Determine host from archive name |
| 108 | + case "$archive" in |
| 109 | + *linux-amd64*) host="x86_64-pc-linux-gnu" ;; |
| 110 | + *linux-armv7*) host="arm-linux-gnueabihf" ;; |
| 111 | + *linux-aarch64*) host="aarch64-linux-gnu" ;; |
| 112 | + *macos-amd64*) host="x86_64-apple-darwin" ;; |
| 113 | + *macos-arm64*) host="arm64-apple-darwin" ;; |
| 114 | + *win64*) hosts=("x86_64-mingw32" "i686-mingw32") ;; |
| 115 | + *) echo "Unknown host for $archive"; continue ;; |
| 116 | + esac |
| 117 | + |
| 118 | + # For win64, loop over both hosts; otherwise, use a single host |
| 119 | + if [[ "$archive" == *win64* ]]; then |
| 120 | + for host in "${hosts[@]}"; do |
| 121 | + update_json_for_host "$host" "$archive" |
| 122 | + done |
| 123 | + else |
| 124 | + update_json_for_host "$host" "$archive" |
| 125 | + fi |
| 126 | +done |
| 127 | + |
| 128 | +# After all archives are processed, move the temporary JSON to the final file |
| 129 | +mv "$tmp_json" "$JSON_PATH" |
0 commit comments