1+ #! /usr/bin/env bash
2+ set -euo pipefail
3+
4+ # Get the directory of this script
5+ script_dir=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
6+ package_file=" $script_dir /package.nix"
7+
8+ # Function to fetch latest version from GitHub releases
9+ fetch_latest_version () {
10+ # Use GitHub API to get the latest release tag
11+ curl -s \
12+ -H " Accept: application/vnd.github.v3+json" \
13+ https://api.github.com/repos/editor-code-assistant/eca/releases/latest | \
14+ jq -r ' .tag_name'
15+ }
16+
17+ # Fetch latest version
18+ echo " Fetching latest version from GitHub releases..."
19+ latest_version=$( fetch_latest_version)
20+
21+ # Remove 'v' prefix if present
22+ latest_version=" ${latest_version# v} "
23+ echo " Latest version: $latest_version "
24+
25+ # Extract current version from package.nix
26+ current_version=$( nix eval .# eca.version --raw)
27+ echo " Current version: $current_version "
28+
29+ # Check if update is needed
30+ if [ " $latest_version " = " $current_version " ]; then
31+ echo " Package is already up to date!"
32+ exit 0
33+ fi
34+
35+ echo " Update available: $current_version -> $latest_version "
36+
37+ # Calculate hashes for all platforms
38+ echo " Calculating hashes for all platforms..."
39+
40+ # Create temporary file for updated content
41+ tmp_file=$( mktemp)
42+ cp " $package_file " " $tmp_file "
43+
44+ # Update version
45+ sed -i " s/version = \" ${current_version} \" ;/version = \" ${latest_version} \" ;/" " $tmp_file "
46+
47+ # Platform-specific URLs and hash calculation
48+ declare -A platforms=(
49+ [" x86_64-linux" ]=" https://github.com/editor-code-assistant/eca/releases/download/${latest_version} /eca-native-linux-amd64.zip"
50+ [" aarch64-linux" ]=" https://github.com/editor-code-assistant/eca/releases/download/${latest_version} /eca-native-linux-aarch64.zip"
51+ [" x86_64-darwin" ]=" https://github.com/editor-code-assistant/eca/releases/download/${latest_version} /eca-native-macos-amd64.zip"
52+ [" aarch64-darwin" ]=" https://github.com/editor-code-assistant/eca/releases/download/${latest_version} /eca-native-macos-aarch64.zip"
53+ )
54+
55+ for platform in " ${! platforms[@]} " ; do
56+ url=" ${platforms[$platform]} "
57+ echo " Calculating hash for $platform ..."
58+
59+ # Use nix-build to automatically get the correct hash
60+ export NIX_PATH=nixpkgs=flake:nixpkgs
61+
62+ # All platforms now use native binary zip files
63+ hash_output=$( nix-build -E " with import <nixpkgs> {}; fetchzip { url = \" ${url} \" ; sha256 = \"\" ; }" 2>&1 || true)
64+
65+ new_hash=$( echo " $hash_output " | grep " got:" | awk ' {print $2}' )
66+
67+ if [ -z " $new_hash " ]; then
68+ echo " ERROR: Failed to calculate hash for $platform "
69+ echo " Output: $hash_output "
70+ continue
71+ fi
72+
73+ # Update the specific hash for this platform
74+ # Find the line number for this platform's hash
75+ line_num=$( grep -n " system = \" $platform \" " " $tmp_file " | cut -d: -f1)
76+ if [ -n " $line_num " ]; then
77+ # Find the next hash = line after the system declaration
78+ # Look for the pattern in the rest of the file from the system declaration
79+ hash_line=$( tail -n " +$line_num " " $tmp_file " | grep -n " hash = " | head -1 | cut -d: -f1)
80+ if [ -n " $hash_line " ]; then
81+ actual_line=$(( line_num + hash_line - 1 ))
82+ sed -i " ${actual_line} s|hash = \" [^\" ]*\" ;|hash = \" ${new_hash} \" ;|" " $tmp_file "
83+ echo " $platform : $new_hash "
84+ else
85+ echo " WARNING: Could not find hash line for $platform "
86+ fi
87+ else
88+ # If not found in platform-specific section, try to find JAR version hash
89+ jar_line=$( grep -n " eca.jar" " $tmp_file " | head -1 | cut -d: -f1)
90+ if [ -n " $jar_line " ]; then
91+ # Find the hash line near the JAR file URL
92+ hash_line=$( tail -n " +$jar_line " " $tmp_file " | grep -n " hash = " | head -1 | cut -d: -f1)
93+ if [ -n " $hash_line " ]; then
94+ actual_line=$(( jar_line + hash_line - 1 ))
95+ sed -i " ${actual_line} s|hash = \" [^\" ]*\" ;|hash = \" ${new_hash} \" ;|" " $tmp_file "
96+ echo " JAR version: $new_hash "
97+ else
98+ echo " WARNING: Could not find hash line for JAR version"
99+ fi
100+ else
101+ echo " WARNING: Could not find platform or JAR section for $platform "
102+ fi
103+ fi
104+ done
105+
106+ # Check if any changes were made
107+ if ! diff -q " $tmp_file " " $package_file " > /dev/null 2>&1 ; then
108+ # Move updated file back
109+ mv " $tmp_file " " $package_file "
110+
111+ echo " Building package to verify..."
112+ nix build .# eca
113+
114+ echo " Update completed successfully!"
115+ echo " eca has been updated from $current_version to $latest_version "
116+ else
117+ echo " No changes needed - all hashes are already correct"
118+ rm -f " $tmp_file "
119+ fi
0 commit comments