Skip to content

Commit 376ebad

Browse files
committed
Use portable awk-based approach instead of GNU
1 parent 4dfd15e commit 376ebad

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

scripts/patch-app-gradle.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ if [ -f "$APP_BUILD_GRADLE" ]; then
5151
# Find the dependencies block and add the dependency
5252
if grep -q "^dependencies {" "$APP_BUILD_GRADLE"; then
5353
# Insert after the opening dependencies { line
54-
sed -i '/^dependencies {/a\
5554
implementation("com.github.mik3y:usb-serial-for-android:3.8.0")
5655
' "$APP_BUILD_GRADLE"
56+
awk '
57+
/^dependencies \{/ {
58+
print $0
59+
print " implementation(\"com.github.mik3y:usb-serial-for-android:3.8.0\")"
60+
found=1
61+
next
62+
}
63+
{ print }
64+
' "$APP_BUILD_GRADLE" > "$APP_BUILD_GRADLE.tmp" && mv "$APP_BUILD_GRADLE.tmp" "$APP_BUILD_GRADLE"
5765
else
5866
# Create dependencies block if it doesn't exist
5967
echo "Creating dependencies block with usb-serial-for-android..."

scripts/patch-gradle-settings.sh

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,48 @@ dependencyResolutionManagement {
4444
}
4545
EOF
4646
else
47-
# Check and add missing repositories in dependencyResolutionManagement
48-
if ! grep -A 10 "dependencyResolutionManagement" "$SETTINGS_GRADLE" | grep -q "google()"; then
49-
sed -i '/dependencyResolutionManagement {/,/}/ { /repositories {/a\
5047
google()
51-
}' "$SETTINGS_GRADLE"
48+
mavenCentral()
49+
maven { url = uri("https://jitpack.io") }
50+
# Portable awk-based insertion for missing repositories in dependencyResolutionManagement
51+
if ! grep -A 10 "dependencyResolutionManagement" "$SETTINGS_GRADLE" | grep -q "google()"; then
52+
awk '
53+
/dependencyResolutionManagement \{/,/\}/ {
54+
if ($0 ~ /repositories \{/ && !found_google) {
55+
print $0
56+
print " google()"
57+
found_google=1
58+
next
59+
}
60+
}
61+
{ print }
62+
' "$SETTINGS_GRADLE" > "$SETTINGS_GRADLE.tmp" && mv "$SETTINGS_GRADLE.tmp" "$SETTINGS_GRADLE"
5263
fi
5364
if ! grep -A 10 "dependencyResolutionManagement" "$SETTINGS_GRADLE" | grep -q "mavenCentral()"; then
54-
sed -i '/dependencyResolutionManagement {/,/}/ { /repositories {/a\
55-
mavenCentral()
56-
}' "$SETTINGS_GRADLE"
65+
awk '
66+
/dependencyResolutionManagement \{/,/\}/ {
67+
if ($0 ~ /repositories \{/ && !found_mavenCentral) {
68+
print $0
69+
print " mavenCentral()"
70+
found_mavenCentral=1
71+
next
72+
}
73+
}
74+
{ print }
75+
' "$SETTINGS_GRADLE" > "$SETTINGS_GRADLE.tmp" && mv "$SETTINGS_GRADLE.tmp" "$SETTINGS_GRADLE"
5776
fi
5877
if ! grep -A 10 "dependencyResolutionManagement" "$SETTINGS_GRADLE" | grep -q "jitpack.io"; then
59-
sed -i '/dependencyResolutionManagement {/,/}/ { /repositories {/a\
60-
maven { url = uri("https://jitpack.io") }
61-
}' "$SETTINGS_GRADLE"
78+
awk '
79+
/dependencyResolutionManagement \{/,/\}/ {
80+
if ($0 ~ /repositories \{/ && !found_jitpack) {
81+
print $0
82+
print " maven { url = uri(\"https://jitpack.io\") }"
83+
found_jitpack=1
84+
next
85+
}
86+
}
87+
{ print }
88+
' "$SETTINGS_GRADLE" > "$SETTINGS_GRADLE.tmp" && mv "$SETTINGS_GRADLE.tmp" "$SETTINGS_GRADLE"
6289
fi
6390
fi
6491

0 commit comments

Comments
 (0)