Skip to content

Commit 4dfd15e

Browse files
committed
refactor scripts for single responsibility
1 parent 56f041f commit 4dfd15e

File tree

6 files changed

+427
-291
lines changed

6 files changed

+427
-291
lines changed

scripts/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Android USB Support Scripts
2+
3+
This directory contains scripts to configure Android USB serial support for the Betaflight Configurator Tauri app.
4+
5+
## Scripts Overview
6+
7+
### `tauri-patch-android.sh` (Main Orchestrator)
8+
The main script that coordinates all Android USB configuration. Run this after `cargo tauri android init`.
9+
10+
**What it does:**
11+
- Calls all individual scripts in the correct order
12+
- Provides overall progress reporting
13+
- Ensures Android project exists before proceeding
14+
15+
### `patch-android-manifest.sh`
16+
Configures the Android manifest with USB permissions and intent filters.
17+
18+
**What it does:**
19+
- Adds `android.permission.USB_PERMISSION` permission
20+
- Adds `android.hardware.usb.host` feature
21+
- Adds USB device attach intent filter
22+
- Adds metadata referencing the device filter XML
23+
24+
### `create-device-filter.sh`
25+
Creates the USB device filter XML file that defines supported USB devices.
26+
27+
**What it does:**
28+
- Creates `res/xml/device_filter.xml`
29+
- Defines USB device filters for Betaflight-compatible devices:
30+
- FT232R USB UART
31+
- STM32 devices (various modes)
32+
- CP210x devices
33+
- GD32 devices
34+
- AT32 devices
35+
- APM32 devices
36+
- Raspberry Pi Pico devices
37+
38+
### `patch-gradle-settings.sh`
39+
Configures project-level Gradle repositories in `settings.gradle.kts`.
40+
41+
**What it does:**
42+
- Adds jitpack.io repository to `dependencyResolutionManagement`
43+
- Adds jitpack.io repository to `pluginManagement`
44+
- Ensures Google Maven and Maven Central are available
45+
46+
### `patch-app-gradle.sh`
47+
Configures app-level Gradle dependencies and repositories.
48+
49+
**What it does:**
50+
- Adds jitpack.io repository to app module
51+
- Adds `usb-serial-for-android:3.8.0` dependency
52+
- Ensures dependency resolution works at module level
53+
54+
## Usage
55+
56+
After running `cargo tauri android init`, execute:
57+
58+
```bash
59+
bash scripts/tauri-patch-android.sh
60+
```
61+
62+
This will run all configuration steps automatically.
63+
64+
## Individual Script Usage
65+
66+
You can also run individual scripts if needed:
67+
68+
```bash
69+
# Only update manifest permissions
70+
bash scripts/patch-android-manifest.sh
71+
72+
# Only update device filters
73+
bash scripts/create-device-filter.sh
74+
75+
# Only update Gradle settings
76+
bash scripts/patch-gradle-settings.sh
77+
78+
# Only update app dependencies
79+
bash scripts/patch-app-gradle.sh
80+
```
81+
82+
## Maintenance Benefits
83+
84+
- **Separation of Concerns**: Each script handles one specific aspect
85+
- **Independent Testing**: Scripts can be tested and debugged individually
86+
- **Selective Updates**: Only run the scripts that need changes
87+
- **Clear Documentation**: Each script has a focused purpose
88+
- **Easier Maintenance**: Changes to one aspect don't affect others

scripts/create-device-filter.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
# Script to create USB device filter XML for Betaflight-compatible devices
3+
# This should be run after 'cargo tauri android init' or 'cargo tauri android build'
4+
5+
set -e
6+
7+
DEVICE_FILTER_PATH="src-tauri/gen/android/app/src/main/res/xml/device_filter.xml"
8+
9+
echo "Creating USB device filter..."
10+
11+
mkdir -p "$(dirname "$DEVICE_FILTER_PATH")"
12+
cat > "$DEVICE_FILTER_PATH" << 'EOF'
13+
<?xml version="1.0" encoding="utf-8"?>
14+
<resources>
15+
<!-- USB device filters for Betaflight-compatible devices -->
16+
17+
<!-- FT232R USB UART -->
18+
<usb-device vendor-id="1027" product-id="24577" />
19+
20+
<!-- STM32 devices -->
21+
<usb-device vendor-id="1155" product-id="12886" /> <!-- STM32 in HID mode -->
22+
<usb-device vendor-id="1155" product-id="14158" /> <!-- STLink Virtual COM Port (NUCLEO boards) -->
23+
<usb-device vendor-id="1155" product-id="22336" /> <!-- STM Electronics Virtual COM Port -->
24+
<usb-device vendor-id="1155" product-id="57105" /> <!-- STM Device in DFU Mode -->
25+
26+
<!-- CP210x devices -->
27+
<usb-device vendor-id="4292" product-id="60000" />
28+
<usb-device vendor-id="4292" product-id="60001" />
29+
<usb-device vendor-id="4292" product-id="60002" />
30+
31+
<!-- GD32 devices -->
32+
<usb-device vendor-id="10473" product-id="394" /> <!-- GD32 VCP -->
33+
<usb-device vendor-id="10473" product-id="393" /> <!-- GD32 DFU Bootloader -->
34+
35+
<!-- AT32 devices -->
36+
<usb-device vendor-id="11836" product-id="22336" /> <!-- AT32 VCP -->
37+
<usb-device vendor-id="11836" product-id="57105" /> <!-- AT32F435 DFU Bootloader -->
38+
39+
<!-- APM32 devices -->
40+
<usb-device vendor-id="12619" product-id="22336" /> <!-- APM32 VCP -->
41+
<usb-device vendor-id="12619" product-id="262" /> <!-- APM32 DFU Bootloader -->
42+
43+
<!-- Raspberry Pi Pico devices -->
44+
<usb-device vendor-id="11914" product-id="9" /> <!-- Raspberry Pi Pico VCP -->
45+
<usb-device vendor-id="11914" product-id="15" /> <!-- Raspberry Pi Pico in Bootloader mode -->
46+
</resources>
47+
EOF
48+
49+
echo "✓ USB device filter created successfully!"

scripts/patch-android-manifest.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/bash
2+
# Script to patch the Android manifest with USB permissions and intent filters
3+
# This should be run after 'cargo tauri android init' or 'cargo tauri android build'
4+
5+
set -e
6+
7+
MANIFEST_PATH="src-tauri/gen/android/app/src/main/AndroidManifest.xml"
8+
9+
if [ ! -f "$MANIFEST_PATH" ]; then
10+
echo "Error: Android manifest not found at $MANIFEST_PATH"
11+
echo "Please run 'cargo tauri android init' first"
12+
exit 1
13+
fi
14+
15+
echo "Patching Android manifest for USB serial support..."
16+
17+
# Backup original manifest
18+
cp "$MANIFEST_PATH" "$MANIFEST_PATH.bak"
19+
20+
# Check if USB permissions already added
21+
if grep -q "android.permission.USB_PERMISSION" "$MANIFEST_PATH"; then
22+
echo "USB permissions already present in manifest"
23+
else
24+
# Add USB permissions before </manifest>
25+
# Using awk for portability across macOS and Linux
26+
awk '
27+
/<\/manifest>/ {
28+
print " <!-- USB permissions for serial communication -->"
29+
print " <uses-permission android:name=\"android.permission.USB_PERMISSION\" />"
30+
print " <uses-feature android:name=\"android.hardware.usb.host\" android:required=\"false\" />"
31+
}
32+
{ print }
33+
' "$MANIFEST_PATH" > "$MANIFEST_PATH.tmp" && mv "$MANIFEST_PATH.tmp" "$MANIFEST_PATH"
34+
echo "Added USB permissions to manifest"
35+
fi
36+
37+
# Check if USB intent filter already added
38+
if grep -q "USB_DEVICE_ATTACHED" "$MANIFEST_PATH"; then
39+
echo "USB intent filter already present in manifest"
40+
else
41+
# Add USB device intent filter and metadata before </activity>
42+
# Using awk for portability across macOS and Linux
43+
awk '
44+
/<\/activity>/ && !found {
45+
print " <!-- Intent filter for USB device attach -->"
46+
print " <intent-filter>"
47+
print " <action android:name=\"android.hardware.usb.action.USB_DEVICE_ATTACHED\" />"
48+
print " </intent-filter>"
49+
print ""
50+
print " <!-- USB device filter metadata -->"
51+
print " <meta-data"
52+
print " android:name=\"android.hardware.usb.action.USB_DEVICE_ATTACHED\""
53+
print " android:resource=\"@xml/device_filter\" />"
54+
found=1
55+
}
56+
{ print }
57+
' "$MANIFEST_PATH" > "$MANIFEST_PATH.tmp" && mv "$MANIFEST_PATH.tmp" "$MANIFEST_PATH"
58+
echo "Added USB intent filter to manifest"
59+
fi
60+
61+
echo "✓ Android manifest patched successfully!"

scripts/patch-app-gradle.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
# Script to patch app/build.gradle.kts with USB serial dependencies and repositories
3+
# This should be run after 'cargo tauri android init' or 'cargo tauri android build'
4+
5+
set -e
6+
7+
APP_BUILD_GRADLE="src-tauri/gen/android/app/build.gradle.kts"
8+
echo "Ensuring usb-serial-for-android dependency and repositories in app/build.gradle.kts..."
9+
10+
if [ -f "$APP_BUILD_GRADLE" ]; then
11+
# Add repositories block to app/build.gradle.kts if missing
12+
if ! grep -q "^repositories {" "$APP_BUILD_GRADLE"; then
13+
echo "Adding repositories block to app/build.gradle.kts..."
14+
# Insert repositories block after plugins block
15+
awk '
16+
/^plugins \{/ {
17+
print $0
18+
in_plugins=1
19+
next
20+
}
21+
in_plugins && /^\}/ {
22+
print $0
23+
print ""
24+
print "repositories {"
25+
print " maven { url = uri(\"https://jitpack.io\") }"
26+
print "}"
27+
in_plugins=0
28+
next
29+
}
30+
{ print }
31+
' "$APP_BUILD_GRADLE" > "$APP_BUILD_GRADLE.tmp" && mv "$APP_BUILD_GRADLE.tmp" "$APP_BUILD_GRADLE"
32+
else
33+
# Check if jitpack.io is already in repositories
34+
if ! grep -A 10 "^repositories {" "$APP_BUILD_GRADLE" | grep -q "jitpack.io"; then
35+
echo "Adding jitpack.io repository to existing repositories block..."
36+
# Insert jitpack.io repository into existing repositories block
37+
awk '
38+
/^repositories \{/ {
39+
print $0
40+
print " maven { url = uri(\"https://jitpack.io\") }"
41+
found=1
42+
next
43+
}
44+
{ print }
45+
' "$APP_BUILD_GRADLE" > "$APP_BUILD_GRADLE.tmp" && mv "$APP_BUILD_GRADLE.tmp" "$APP_BUILD_GRADLE"
46+
fi
47+
fi
48+
49+
if ! grep -q "usb-serial-for-android" "$APP_BUILD_GRADLE"; then
50+
echo "Adding usb-serial-for-android dependency to app module..."
51+
# Find the dependencies block and add the dependency
52+
if grep -q "^dependencies {" "$APP_BUILD_GRADLE"; then
53+
# Insert after the opening dependencies { line
54+
sed -i '/^dependencies {/a\
55+
implementation("com.github.mik3y:usb-serial-for-android:3.8.0")
56+
' "$APP_BUILD_GRADLE"
57+
else
58+
# Create dependencies block if it doesn't exist
59+
echo "Creating dependencies block with usb-serial-for-android..."
60+
cat >> "$APP_BUILD_GRADLE" << 'EOF'
61+
62+
dependencies {
63+
implementation("com.github.mik3y:usb-serial-for-android:3.8.0")
64+
}
65+
EOF
66+
fi
67+
else
68+
echo "usb-serial-for-android dependency already present in app module"
69+
fi
70+
else
71+
echo "Warning: app/build.gradle.kts not found, cannot add usb-serial-for-android dependency"
72+
fi
73+
74+
echo "✓ App Gradle dependencies and repositories configured successfully!"

0 commit comments

Comments
 (0)