Skip to content

Commit 5a6ad28

Browse files
authored
ci(sdk): Create script/action for SDK auto roll (#2615)
* ci(sdk): Create script/action for SDK auto roll * CI Test 1 * CI Test 2 * CI Test 3
1 parent aaf19e4 commit 5a6ad28

File tree

3 files changed

+420
-0
lines changed

3 files changed

+420
-0
lines changed

.github/actions/flutter-deps/action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ runs:
99
# NB! Keep up-to-date with the flutter version used for development
1010
flutter-version: "3.29.2"
1111
channel: "stable"
12+
cache: true
1213

1314
- name: Prepare build directory
1415
shell: bash
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
#!/usr/bin/env bash
2+
3+
# Script to roll SDK packages in all Flutter/Dart projects
4+
# Designed to handle git-based dependencies and work with the GitHub CI workflow
5+
6+
set -e
7+
8+
# Configuration
9+
# Set to "true" to upgrade all packages, "false" to only upgrade SDK packages
10+
UPGRADE_ALL_PACKAGES=${UPGRADE_ALL_PACKAGES:-false}
11+
# Branch to target for PR creation
12+
TARGET_BRANCH=${TARGET_BRANCH:-"dev"}
13+
14+
# Get the current date for branch naming and commit messages
15+
CURRENT_DATE=$(date '+%Y-%m-%d')
16+
REPO_ROOT=$(pwd)
17+
CHANGES_FILE="$REPO_ROOT/SDK_CHANGELOG.md"
18+
19+
# SDK packages to check
20+
SDK_PACKAGES=(
21+
"komodo_cex_market_data"
22+
"komodo_coin_updates"
23+
"komodo_coins"
24+
"komodo_defi_framework"
25+
"komodo_defi_local_auth"
26+
"komodo_defi_remote"
27+
"komodo_defi_rpc_methods"
28+
"komodo_defi_sdk"
29+
"komodo_defi_types"
30+
"komodo_defi_workers"
31+
"komodo_symbol_converter"
32+
"komodo_ui"
33+
"komodo_wallet_build_transformer"
34+
"komodo_wallet_cli"
35+
)
36+
37+
# Extract version information from the pubspec.lock file
38+
get_package_info_from_lock() {
39+
local package_name=$1
40+
local lock_file=$2
41+
42+
# If pubspec.lock doesn't exist, return empty
43+
if [ ! -f "$lock_file" ]; then
44+
echo ""
45+
return
46+
fi
47+
48+
# Extract the entire package section
49+
local start_line=$(grep -n "^ $package_name:" "$lock_file" | cut -d: -f1)
50+
if [ -z "$start_line" ]; then
51+
echo ""
52+
return
53+
fi
54+
55+
# Find the end line (next non-indented line or EOF)
56+
local end_line=$(tail -n +$((start_line+1)) "$lock_file" | grep -n "^[^ ]" | head -1 | cut -d: -f1)
57+
if [ -z "$end_line" ]; then
58+
# If no end found, use end of file
59+
end_line=$(wc -l < "$lock_file")
60+
else
61+
# Adjust for the offset from tail command
62+
end_line=$((start_line + end_line))
63+
fi
64+
65+
# Extract the entire section
66+
local package_section=$(sed -n "${start_line},${end_line}p" "$lock_file" | sed '$d')
67+
68+
if [ -n "$package_section" ]; then
69+
# Get package info
70+
local version=$(echo "$package_section" | grep " version:" | head -1 | sed 's/.*version: *"\([^"]*\)".*/\1/')
71+
local source=$(echo "$package_section" | grep " source:" | head -1 | sed 's/.*source: *\([^ ]*\).*/\1/')
72+
73+
# Extract git specific info if available
74+
local git_url=""
75+
local git_ref=""
76+
77+
if echo "$package_section" | grep -q " url:"; then
78+
git_url=$(echo "$package_section" | grep " url:" | head -1 | sed 's/.*url: *"\([^"]*\)".*/\1/')
79+
fi
80+
81+
if echo "$package_section" | grep -q " resolved-ref:"; then
82+
git_ref=$(echo "$package_section" | grep " resolved-ref:" | head -1 | sed 's/.*resolved-ref: *"\([^"]*\)".*/\1/')
83+
elif echo "$package_section" | grep -q " ref:"; then
84+
git_ref=$(echo "$package_section" | grep " ref:" | head -1 | sed 's/.*ref: *\([^ ]*\).*/\1/')
85+
fi
86+
87+
# Format the output based on what we found
88+
if [ -n "$git_url" ] && [ -n "$git_ref" ]; then
89+
echo "version: \"$version\", source: $source, git: $git_url, ref: $git_ref"
90+
else
91+
echo "version: \"$version\", source: $source"
92+
fi
93+
else
94+
echo ""
95+
fi
96+
}
97+
98+
# Initialize changes file
99+
echo "# SDK Package Rolls" > "$CHANGES_FILE"
100+
echo "" >> "$CHANGES_FILE"
101+
echo "**Date:** $CURRENT_DATE" >> "$CHANGES_FILE"
102+
echo "**Target Branch:** $TARGET_BRANCH" >> "$CHANGES_FILE"
103+
echo "**Upgrade Mode:** $([ "$UPGRADE_ALL_PACKAGES" = "true" ] && echo "All Packages" || echo "SDK Packages Only")" >> "$CHANGES_FILE"
104+
echo "" >> "$CHANGES_FILE"
105+
echo "The following SDK packages were rolled to newer versions:" >> "$CHANGES_FILE"
106+
echo "" >> "$CHANGES_FILE"
107+
108+
# Find all pubspec.yaml files
109+
echo "Finding all pubspec.yaml files..."
110+
PUBSPEC_FILES=$(find "$REPO_ROOT" -name "pubspec.yaml" -not -path "*/build/*" -not -path "*/\.*/*" -not -path "*/ios/*" -not -path "*/android/*")
111+
112+
echo "Found $(echo "$PUBSPEC_FILES" | wc -l) pubspec.yaml files"
113+
114+
ROLLS_MADE=false
115+
116+
for PUBSPEC in $PUBSPEC_FILES; do
117+
PROJECT_DIR=$(dirname "$PUBSPEC")
118+
PROJECT_NAME=$(basename "$PROJECT_DIR")
119+
120+
echo "Processing $PROJECT_NAME ($PROJECT_DIR)"
121+
122+
cd "$PROJECT_DIR"
123+
124+
# Check if any SDK package is listed as a dependency
125+
CONTAINS_SDK_PACKAGE=false
126+
SDK_PACKAGES_FOUND=()
127+
128+
for PACKAGE in "${SDK_PACKAGES[@]}"; do
129+
if grep -q -E "^\s+$PACKAGE:" "$PUBSPEC"; then
130+
CONTAINS_SDK_PACKAGE=true
131+
SDK_PACKAGES_FOUND+=("$PACKAGE")
132+
fi
133+
done
134+
135+
if [ "$CONTAINS_SDK_PACKAGE" = true ]; then
136+
echo "SDK packages found in $PROJECT_NAME: ${SDK_PACKAGES_FOUND[*]}"
137+
138+
# Save hash of current pubspec.lock
139+
if [ -f "pubspec.lock" ]; then
140+
PRE_UPDATE_HASH=$(sha256sum pubspec.lock | awk '{print $1}')
141+
else
142+
PRE_UPDATE_HASH=""
143+
fi
144+
145+
# Backup current pubspec.lock
146+
if [ -f "pubspec.lock" ]; then
147+
cp pubspec.lock pubspec.lock.bak
148+
fi
149+
150+
# Get the current git refs/versions for SDK packages before update
151+
SDK_PACKAGE_REFS_BEFORE=()
152+
for PACKAGE in "${SDK_PACKAGES_FOUND[@]}"; do
153+
if grep -q "$PACKAGE:" "$PUBSPEC"; then
154+
# Get the git reference line or version line
155+
if grep -q -A 10 "$PACKAGE:" "$PUBSPEC" | grep -q "git:"; then
156+
REF_LINE=$(grep -A 10 "$PACKAGE:" "$PUBSPEC" | grep -m 1 "ref:")
157+
GIT_URL=$(grep -A 10 "$PACKAGE:" "$PUBSPEC" | grep -m 1 "git:")
158+
if [ -n "$REF_LINE" ] && [ -n "$GIT_URL" ]; then
159+
REF_VALUE=$(echo "$REF_LINE" | sed 's/.*ref: *\([^ ]*\).*/\1/')
160+
GIT_VALUE=$(echo "$GIT_URL" | sed 's/.*git: *\([^ ]*\).*/\1/')
161+
SDK_PACKAGE_REFS_BEFORE+=("$PACKAGE: git: $GIT_VALUE ref: $REF_VALUE")
162+
fi
163+
else
164+
# If not git-based, get version
165+
VERSION_LINE=$(grep -A 1 "$PACKAGE:" "$PUBSPEC" | tail -1)
166+
if [ -n "$VERSION_LINE" ]; then
167+
VERSION=$(echo "$VERSION_LINE" | sed 's/.*: *\([^ ]*\).*/\1/')
168+
SDK_PACKAGE_REFS_BEFORE+=("$PACKAGE: version: $VERSION")
169+
fi
170+
fi
171+
fi
172+
done
173+
174+
# Perform the update - based on configuration
175+
if [ "$UPGRADE_ALL_PACKAGES" = "true" ]; then
176+
echo "Running flutter pub upgrade --major-versions in $PROJECT_NAME (all packages)"
177+
flutter pub upgrade --major-versions
178+
else
179+
echo "Running flutter pub upgrade for SDK packages only in $PROJECT_NAME"
180+
# Upgrade only the SDK packages
181+
for PACKAGE in "${SDK_PACKAGES_FOUND[@]}"; do
182+
echo "Upgrading $PACKAGE"
183+
flutter pub upgrade "$PACKAGE"
184+
done
185+
fi
186+
187+
# Check if the pubspec.lock was modified
188+
if [ -f "pubspec.lock" ]; then
189+
POST_UPDATE_HASH=$(sha256sum pubspec.lock | awk '{print $1}')
190+
191+
if [ "$PRE_UPDATE_HASH" != "$POST_UPDATE_HASH" ]; then
192+
echo "Changes detected in $PROJECT_NAME pubspec.lock"
193+
ROLLS_MADE=true
194+
195+
# Get information about packages from lock file before and after
196+
if [ -f "pubspec.lock.bak" ]; then
197+
LOCK_BEFORE="pubspec.lock.bak"
198+
else
199+
LOCK_BEFORE=""
200+
fi
201+
LOCK_AFTER="pubspec.lock"
202+
203+
# Add the project to the changes list
204+
echo "## $PROJECT_NAME" >> "$CHANGES_FILE"
205+
echo "" >> "$CHANGES_FILE"
206+
207+
# List the SDK packages that were rolled with detailed info
208+
for PACKAGE in "${SDK_PACKAGES_FOUND[@]}"; do
209+
echo "- Rolled \`$PACKAGE\`" >> "$CHANGES_FILE"
210+
211+
# Get before and after info
212+
if [ -n "$LOCK_BEFORE" ]; then
213+
BEFORE_INFO=$(get_package_info_from_lock "$PACKAGE" "$LOCK_BEFORE")
214+
else
215+
BEFORE_INFO=""
216+
fi
217+
AFTER_INFO=$(get_package_info_from_lock "$PACKAGE" "$LOCK_AFTER")
218+
219+
# Add detailed information if available
220+
if [ -n "$BEFORE_INFO" ] && [ -n "$AFTER_INFO" ] && [ "$BEFORE_INFO" != "$AFTER_INFO" ]; then
221+
echo " - From: \`$BEFORE_INFO\`" >> "$CHANGES_FILE"
222+
echo " - To: \`$AFTER_INFO\`" >> "$CHANGES_FILE"
223+
elif [ -n "$AFTER_INFO" ]; then
224+
echo " - Current: \`$AFTER_INFO\`" >> "$CHANGES_FILE"
225+
fi
226+
done
227+
228+
echo "" >> "$CHANGES_FILE"
229+
else
230+
echo "No changes in $PROJECT_NAME pubspec.lock"
231+
fi
232+
else
233+
echo "No pubspec.lock file generated for $PROJECT_NAME"
234+
fi
235+
else
236+
echo "No SDK packages found in $PROJECT_NAME, skipping..."
237+
fi
238+
239+
cd "$REPO_ROOT"
240+
done
241+
242+
# Add the SDK rolls image at the bottom of the changes file
243+
if [ "$ROLLS_MADE" = true ]; then
244+
echo "![SDK Package Rolls](https://raw.githubusercontent.com/KomodoPlatform/komodo-wallet/aaf19e4605c62854ba176bf1ea75d75b3cb48df9/docs/assets/sdk-rolls.png)" >> "$CHANGES_FILE"
245+
echo "" >> "$CHANGES_FILE"
246+
247+
# Clean up all .bak files to avoid committing them
248+
echo "Cleaning up backup files..."
249+
find "$REPO_ROOT" -name "*.bak" -type f -delete
250+
fi
251+
252+
# Set output for GitHub Actions
253+
if [ -n "${GITHUB_OUTPUT}" ]; then
254+
if [ "$ROLLS_MADE" = true ]; then
255+
echo "updates_found=true" >> $GITHUB_OUTPUT
256+
echo "Rolls found and applied!"
257+
exit 0
258+
else
259+
echo "updates_found=false" >> $GITHUB_OUTPUT
260+
echo "No rolls needed."
261+
exit 1
262+
fi
263+
else
264+
# When running outside of GitHub Actions
265+
if [ "$ROLLS_MADE" = true ]; then
266+
echo "Rolls found and applied! See $CHANGES_FILE for details."
267+
exit 0
268+
else
269+
echo "No rolls needed."
270+
exit 1
271+
fi
272+
fi

0 commit comments

Comments
 (0)