Skip to content

Commit 63d654e

Browse files
committed
scripts: Add merge script for Arduino package index
This script merges the ESP32 and nRF52 package index files into a single Fobe package index file, ensuring the latest versions are included and properly formatted. Signed-off-by: Chiho Sin <[email protected]>
1 parent a7a93e6 commit 63d654e

File tree

3 files changed

+287
-0
lines changed

3 files changed

+287
-0
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/bin/bash
2+
3+
# Merge package_esp32_index.json and package_nrf52_index.json into package_fobe_index.json
4+
5+
set -e # Exit on error
6+
7+
# Get the directory where this script is located and navigate to project root
8+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
10+
cd "${PROJECT_ROOT}"
11+
12+
# Function to clean up build directory
13+
cleanup() {
14+
if [ -d "build" ]; then
15+
echo "Cleaning up build directory..."
16+
rm -rf build
17+
fi
18+
}
19+
20+
# Set trap to clean up on exit or error
21+
trap cleanup EXIT
22+
23+
# Check if jq is installed
24+
if ! command -v jq &> /dev/null; then
25+
echo "Error: jq tool is required to process JSON files"
26+
echo "On macOS, you can install it with: brew install jq"
27+
exit 1
28+
fi
29+
30+
# Function to find the latest version directory
31+
find_latest_version() {
32+
local platform_dir="$1"
33+
if [ ! -d "$platform_dir" ]; then
34+
echo ""
35+
return
36+
fi
37+
38+
# Find all version directories and sort them
39+
find "$platform_dir" -maxdepth 1 -type d -name "*.*.*" | \
40+
xargs -I {} basename {} | \
41+
sort -V | \
42+
tail -1
43+
}
44+
45+
echo "Loading package index files from local directories..."
46+
47+
# Create build directory
48+
mkdir -p build
49+
50+
# Find latest ESP32 version and copy index file
51+
echo "Looking for ESP32 package index..."
52+
ESP32_LATEST_VERSION=$(find_latest_version "arduino/esp32")
53+
if [ -z "$ESP32_LATEST_VERSION" ]; then
54+
echo "Error: No ESP32 version directories found in arduino/esp32/"
55+
exit 1
56+
fi
57+
58+
ESP32_INDEX_FILE="arduino/esp32/$ESP32_LATEST_VERSION/package_fobe_esp32_index.json"
59+
if [ ! -f "$ESP32_INDEX_FILE" ]; then
60+
echo "Error: Could not find $ESP32_INDEX_FILE"
61+
exit 1
62+
fi
63+
64+
cp "$ESP32_INDEX_FILE" build/package_esp32_index.json
65+
echo "✓ Copied ESP32 package index from version $ESP32_LATEST_VERSION"
66+
67+
# Find latest nRF52 version and copy index file
68+
echo "Looking for nRF52 package index..."
69+
NRF52_LATEST_VERSION=$(find_latest_version "arduino/nrf52")
70+
if [ -z "$NRF52_LATEST_VERSION" ]; then
71+
echo "Error: No nRF52 version directories found in arduino/nrf52/"
72+
exit 1
73+
fi
74+
75+
NRF52_INDEX_FILE="arduino/nrf52/$NRF52_LATEST_VERSION/package_fobe_nrf52_index.json"
76+
if [ ! -f "$NRF52_INDEX_FILE" ]; then
77+
echo "Error: Could not find $NRF52_INDEX_FILE"
78+
exit 1
79+
fi
80+
81+
cp "$NRF52_INDEX_FILE" build/package_nrf52_index.json
82+
echo "✓ Copied nRF52 package index from version $NRF52_LATEST_VERSION"
83+
84+
# Check if required files exist after download
85+
if [ ! -f "build/package_esp32_index.json" ]; then
86+
echo "Error: Failed to download package_esp32_index.json"
87+
exit 1
88+
fi
89+
90+
if [ ! -f "build/package_nrf52_index.json" ]; then
91+
echo "Error: Failed to download package_nrf52_index.json"
92+
exit 1
93+
fi
94+
95+
if [ ! -f "arduino/package_fobe_index.json" ]; then
96+
echo "Error: arduino/package_fobe_index.json does not exist, please create a base version first"
97+
exit 1
98+
fi
99+
100+
echo "Starting smart merge of JSON files..."
101+
102+
# Create temporary file
103+
TEMP_FILE=$(mktemp)
104+
105+
# Complex merge logic
106+
jq -s '
107+
# Get original file, ESP32 file, and nRF52 file
108+
.[2] as $original |
109+
.[0] as $esp32 |
110+
.[1] as $nrf52 |
111+
112+
# Merge platforms: keep original versions, add new versions, overwrite with new one for the same version number
113+
($original.packages[0].platforms // []) as $original_platforms |
114+
($esp32.packages[0].platforms // []) as $esp32_platforms |
115+
($nrf52.packages[0].platforms // []) as $nrf52_platforms |
116+
117+
# Create an array of all new platforms (with deduplication)
118+
($esp32_platforms + $nrf52_platforms |
119+
group_by(.architecture + "_" + .version) |
120+
map(.[0])
121+
) as $new_platforms |
122+
123+
# Merge platforms logic
124+
(
125+
# First add all new platforms (already deduplicated)
126+
$new_platforms +
127+
# Then add original platforms, but only those whose architecture and version do not exist in new platforms
128+
($original_platforms | map(
129+
. as $orig |
130+
if ($new_platforms | any(.architecture == $orig.architecture and .version == $orig.version))
131+
then empty
132+
else $orig
133+
end
134+
))
135+
) as $merged_platforms |
136+
137+
# Merge tools: completely based on new files, keep tools with the same name but different versions
138+
($esp32.packages[0].tools // []) as $esp32_tools |
139+
($nrf52.packages[0].tools // []) as $nrf52_tools |
140+
141+
# Merge all tools, deduplication logic: keep only one with the same name and version
142+
($esp32_tools + $nrf52_tools |
143+
group_by(.name + "_" + .version) |
144+
map(.[0])
145+
) as $merged_tools |
146+
147+
{
148+
"packages": [
149+
{
150+
"name": ($original.packages[0].name),
151+
"maintainer": ($original.packages[0].maintainer),
152+
"websiteURL": ($original.packages[0].websiteURL),
153+
"help": ($original.packages[0].help),
154+
"platforms": $merged_platforms,
155+
"tools": $merged_tools
156+
}
157+
]
158+
}
159+
' build/package_esp32_index.json build/package_nrf52_index.json arduino/package_fobe_index.json > "$TEMP_FILE"
160+
161+
# Check if the generated file is valid JSON
162+
if jq empty "$TEMP_FILE" 2>/dev/null; then
163+
# Move temporary file to target location
164+
mv "$TEMP_FILE" arduino/package_fobe_index.json
165+
echo "✅ Smart merge completed!"
166+
167+
# Display merge statistics
168+
echo ""
169+
echo "📊 Merge Statistics:"
170+
171+
# Count platform numbers
172+
ESP32_PLATFORMS=$(jq '.packages[0].platforms | length' build/package_esp32_index.json)
173+
NRF52_PLATFORMS=$(jq '.packages[0].platforms | length' build/package_nrf52_index.json)
174+
FINAL_PLATFORMS=$(jq '.packages[0].platforms | length' arduino/package_fobe_index.json)
175+
176+
echo " ESP32 new platforms: $ESP32_PLATFORMS"
177+
echo " nRF52 new platforms: $NRF52_PLATFORMS"
178+
echo " Total platforms after merge: $FINAL_PLATFORMS"
179+
180+
# Count tools
181+
ESP32_TOOLS=$(jq '.packages[0].tools | length' build/package_esp32_index.json 2>/dev/null || echo "0")
182+
NRF52_TOOLS=$(jq '.packages[0].tools | length' build/package_nrf52_index.json 2>/dev/null || echo "0")
183+
FINAL_TOOLS=$(jq '.packages[0].tools | length' arduino/package_fobe_index.json)
184+
185+
echo " ESP32 tools: $ESP32_TOOLS"
186+
echo " nRF52 tools: $NRF52_TOOLS"
187+
echo " Total tools after merge: $FINAL_TOOLS"
188+
189+
# Verify platform architectures
190+
echo ""
191+
echo "📋 Included platform architectures and versions:"
192+
jq -r '.packages[0].platforms[] | " - \(.architecture): \(.name) v\(.version)"' arduino/package_fobe_index.json | sort
193+
194+
# Verify tools
195+
echo ""
196+
echo "🔧 Included tools:"
197+
jq -r '.packages[0].tools[] | " - \(.name) v\(.version)"' arduino/package_fobe_index.json | sort
198+
199+
# Show platform update information
200+
echo ""
201+
echo "🔄 Platform update check:"
202+
203+
# Compare versions for each architecture
204+
for arch in $(jq -r '.packages[0].platforms[].architecture' arduino/package_fobe_index.json | sort | uniq); do
205+
NEW_VERSION=$(jq -r --arg arch "$arch" '.packages[0].platforms[] | select(.architecture == $arch) | .version' arduino/package_fobe_index.json | head -1)
206+
echo " - $arch: v$NEW_VERSION (merged)"
207+
done
208+
209+
else
210+
echo "❌ Error: Generated JSON file is invalid"
211+
rm -f "$TEMP_FILE"
212+
exit 1
213+
fi
214+
215+
echo ""
216+
echo "🎉 Smart merge completed!"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Update Arduino Package Index
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
commit_message:
7+
description: "Commit message for the update"
8+
required: false
9+
default: "Update Arduino package index from latest commit"
10+
push:
11+
branches: [main]
12+
paths:
13+
- "arduino/**"
14+
- "!arduino/package_fobe_index.json"
15+
pull_request:
16+
branches: [main]
17+
paths:
18+
- "arduino/**"
19+
- "!arduino/package_fobe_index.json"
20+
21+
jobs:
22+
update-index:
23+
runs-on: ubuntu-latest
24+
25+
permissions:
26+
contents: write
27+
pages: write
28+
id-token: write
29+
30+
steps:
31+
- name: Checkout repository
32+
uses: actions/checkout@v4
33+
34+
- name: Install dependencies
35+
run: |
36+
sudo apt-get update
37+
sudo apt-get install -y jq curl
38+
39+
- name: Make update script executable
40+
run: chmod +x .github/scripts/merge_arduino_index.sh
41+
42+
- name: Run update script
43+
run: .github/scripts/merge_arduino_index.sh
44+
45+
- name: Check for changes
46+
id: git-check
47+
run: |
48+
git diff --exit-code arduino/package_fobe_index.json || echo "changes=true" >> $GITHUB_OUTPUT
49+
50+
- name: Commit and push changes
51+
if: steps.git-check.outputs.changes == 'true'
52+
run: |
53+
git config --local user.email "[email protected]"
54+
git config --local user.name "GitHub Action"
55+
git add arduino/package_fobe_index.json
56+
git commit -m "${{ github.event.inputs.commit_message || 'Update arduino package index from latest commit' }}"
57+
git push

arduino/package_fobe_index.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "fobe",
5+
"maintainer": "FoBE Studio",
6+
"websiteURL": "https://docs.fobestudio.com",
7+
"help": {
8+
"online": "https://docs.fobestudio.com"
9+
},
10+
"platforms": [],
11+
"tools": []
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)