feat: desktop-only build isolation for Flatpak packaging (#5360)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Austin <vidplace7@gmail.com>
This commit is contained in:
James Rich
2026-05-06 11:43:35 -05:00
committed by GitHub
parent 94e3901bd4
commit 086c9afbaf
17 changed files with 241 additions and 46 deletions

67
scripts/desktop-only-prep.sh Executable file
View File

@@ -0,0 +1,67 @@
#!/usr/bin/env bash
# desktop-only-prep.sh — Prepare source tree for a desktop-only (JVM) build.
#
# Usage:
# ./scripts/desktop-only-prep.sh
# DESKTOP_ONLY=true ./gradlew :desktop:packageUberJarForCurrentOS
#
# This script comments out Android-specific blocks in module build scripts so
# Gradle can configure without the Android SDK. It is designed for Flatpak and
# other sandboxed Linux packaging environments.
#
# Prerequisites:
# npm install -g @ast-grep/cli
# OR
# pipx install ast-grep-cli
#
# The companion in-code guards in build-logic convention plugins handle:
# - Skipping Android/iOS plugin application (KmpLibraryConventionPlugin)
# - Skipping iOS targets (configureKotlinMultiplatform)
# - Creating a placeholder androidMain source set
# - Excluding Android-only modules from settings.gradle.kts
#
# This script handles what can't be done in-code:
# - `kotlin { android { ... } }` blocks in module build.gradle.kts files
# - `androidMain.dependencies { ... }` blocks with project dependencies to
# excluded modules (e.g., projects.core.barcode, projects.core.api)
#
# To reverse: `git checkout -- .` or rebuild from clean source.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
SCRIPT_DIR="$REPO_ROOT/scripts"
cd "$REPO_ROOT"
if ! command -v sg &>/dev/null; then
echo "ERROR: ast-grep (sg) is required but not found in PATH." >&2
echo " Install: https://ast-grep.github.io/guide/quick-start.html" >&2
exit 1
fi
echo "==> Preparing desktop-only build..."
# Collect target build.gradle.kts files (excluding modules that stay as-is)
mapfile -t FILES < <(find . -name "build.gradle.kts" \
-not -path "./build-logic/*" \
-not -path "./.agent_refs/*" \
-not -path "./coil/*" \
-not -path "./kable/*" \
-not -path "./app/*" \
-not -path "./core/api/*" \
-not -path "./core/barcode/*" \
-not -path "./feature/widget/*" \
-not -path "./desktop/*" \
-not -path "./build/*")
if [[ ${#FILES[@]} -eq 0 ]]; then
echo "WARNING: No build.gradle.kts files found to process." >&2
exit 0
fi
# Apply all ast-grep rules from the rules directory.
# Each rule YAML defines a pattern to match and a fix (comment replacement).
sg scan -c "$SCRIPT_DIR/sgconfig.yml" --update-all "${FILES[@]}"
echo "==> Desktop-only prep complete."
echo " Run: DESKTOP_ONLY=true ./gradlew :desktop:packageUberJarForCurrentOS"

View File

@@ -0,0 +1,10 @@
id: remove-android-block
language: kotlin
severity: warning
message: "Remove android { } block for desktop-only build"
rule:
pattern: |-
android {
$$$BODY
}
fix: "// [desktop-only] android { ... } block removed"

View File

@@ -0,0 +1,10 @@
id: remove-android-device-test
language: kotlin
severity: warning
message: "Remove val androidDeviceTest block for desktop-only build"
rule:
pattern: |-
val androidDeviceTest by getting {
$$$BODY
}
fix: "// [desktop-only] val androidDeviceTest { ... } block removed"

View File

@@ -0,0 +1,10 @@
id: remove-android-host-test
language: kotlin
severity: warning
message: "Remove val androidHostTest block for desktop-only build"
rule:
pattern: |-
val androidHostTest by getting {
$$$BODY
}
fix: "// [desktop-only] val androidHostTest { ... } block removed"

View File

@@ -0,0 +1,7 @@
id: remove-android-imports
language: kotlin
severity: warning
message: "Remove com.android import for desktop-only build"
rule:
pattern: "import com.android.$$$REST"
fix: "// [desktop-only] android import removed"

View File

@@ -0,0 +1,10 @@
id: remove-android-instrumented-test
language: kotlin
severity: warning
message: "Remove val androidInstrumentedTest block for desktop-only build"
rule:
pattern: |-
val androidInstrumentedTest by getting {
$$$BODY
}
fix: "// [desktop-only] val androidInstrumentedTest { ... } block removed"

View File

@@ -0,0 +1,10 @@
id: remove-android-main-deps
language: kotlin
severity: warning
message: "Remove androidMain.dependencies { } block for desktop-only build"
rule:
pattern: |-
androidMain.dependencies {
$$$BODY
}
fix: "// [desktop-only] androidMain.dependencies { ... } block removed"

View File

@@ -0,0 +1,8 @@
id: remove-ksp-android
language: kotlin
severity: warning
message: "Remove kspAndroid configuration for desktop-only build"
rule:
kind: call_expression
regex: "^\"kspAndroid"
fix: "// [desktop-only] kspAndroid config removed"

View File

@@ -0,0 +1,7 @@
id: remove-parcelize-plugin
language: kotlin
severity: warning
message: "Remove parcelize plugin for desktop-only build"
rule:
pattern: "alias(libs.plugins.kotlin.parcelize)"
fix: "// [desktop-only] alias(libs.plugins.kotlin.parcelize)"

2
scripts/sgconfig.yml Normal file
View File

@@ -0,0 +1,2 @@
ruleDirs:
- desktop-only-rules