diff --git a/.gitignore b/.gitignore index 397d243..e503c99 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules devAssets .DS_Store +.cinny-stash-pending diff --git a/package.json b/package.json index 7bf9a19..194c708 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,12 @@ }, "scripts": { "android:prepare": "node scripts/apply-overlay.mjs && cd cinny && npm install && npm run build && npx cap sync android", + "android:restore": "node scripts/restore-cinny.mjs", "android:open": "cd cinny && npx cap open android", "android:run": "cd cinny && npx cap run android", - "android:apk": "npm run android:prepare && cd cinny/android && gradlew.bat assembleDebug", - "android:apk:release": "npm run android:prepare && cd cinny/android && gradlew.bat assembleRelease", - "android:aab": "npm run android:prepare && cd cinny/android && gradlew.bat bundleRelease" + "android:apk": "npm run android:prepare && cd cinny/android && gradlew.bat assembleDebug && cd ../.. && node scripts/restore-cinny.mjs", + "android:apk:release": "npm run android:prepare && cd cinny/android && gradlew.bat assembleRelease && cd ../.. && node scripts/restore-cinny.mjs", + "android:aab": "npm run android:prepare && cd cinny/android && gradlew.bat bundleRelease && cd ../.. && node scripts/restore-cinny.mjs" }, "keywords": [], "author": { diff --git a/scripts/apply-overlay.mjs b/scripts/apply-overlay.mjs index 8701f5a..98b5230 100644 --- a/scripts/apply-overlay.mjs +++ b/scripts/apply-overlay.mjs @@ -14,7 +14,7 @@ * the Capacitor / Android platform changes on top of it. */ -import { readFileSync, writeFileSync, mkdirSync, copyFileSync, readdirSync, statSync, existsSync } from 'fs'; +import { readFileSync, writeFileSync, mkdirSync, copyFileSync, readdirSync, statSync, existsSync, unlinkSync } from 'fs'; import { join, dirname, relative, resolve } from 'path'; import { fileURLToPath } from 'url'; import { execSync } from 'child_process'; @@ -23,6 +23,20 @@ const __dirname = dirname(fileURLToPath(import.meta.url)); const ROOT = join(__dirname, '..'); const CINNY = join(ROOT, 'cinny'); const OVERLAY = join(ROOT, 'overlay'); +const STASH_FLAG = join(ROOT, '.cinny-stash-pending'); + +// Stash any pre-existing cinny changes so the overlay applies cleanly. +// The restore-cinny script will pop them back afterwards. +const cinnyStatus = execSync('git status --porcelain', { cwd: CINNY, encoding: 'utf8' }); +if (cinnyStatus.trim()) { + console.log('[apply-overlay] Stashing pre-existing cinny changes...'); + execSync('git stash push -u -m "pre-overlay stash"', { cwd: CINNY, stdio: 'inherit' }); + writeFileSync(STASH_FLAG, '1', 'utf8'); + console.log('[apply-overlay] Stash saved.'); +} else { + // Ensure no stale flag from a previous interrupted run + if (existsSync(STASH_FLAG)) unlinkSync(STASH_FLAG); +} /** * Recursively copy all files from src directory to dest directory. diff --git a/scripts/restore-cinny.mjs b/scripts/restore-cinny.mjs new file mode 100644 index 0000000..a8baebe --- /dev/null +++ b/scripts/restore-cinny.mjs @@ -0,0 +1,55 @@ +/** + * Restores the cinny submodule to its pre-build state after an Android build. + * + * This script: + * 1. Reverts all tracked file changes made by apply-overlay (git checkout) + * 2. Removes untracked overlay files (capacitor.config.json) + * 3. Removes the cinny/android junction + * 4. Pops the pre-overlay git stash if apply-overlay saved one + */ + +import { existsSync, unlinkSync } from 'fs'; +import { join, dirname } from 'path'; +import { fileURLToPath } from 'url'; +import { execSync } from 'child_process'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const ROOT = join(__dirname, '..'); +const CINNY = join(ROOT, 'cinny'); +const STASH_FLAG = join(ROOT, '.cinny-stash-pending'); + +// 1. Revert all tracked overlay modifications +console.log('[restore-cinny] Reverting tracked changes...'); +execSync('git checkout -- .', { cwd: CINNY, stdio: 'inherit' }); + +// 2. Remove untracked capacitor.config.json written by the overlay +const capConfig = join(CINNY, 'capacitor.config.json'); +if (existsSync(capConfig)) { + unlinkSync(capConfig); + console.log('[restore-cinny] Removed cinny/capacitor.config.json'); +} + +// 3. Remove the android junction (rmdir only removes the junction, not the real directory) +const androidJunction = join(CINNY, 'android'); +if (existsSync(androidJunction)) { + try { + execSync(`cmd /c rmdir "${androidJunction}"`, { stdio: 'pipe' }); + console.log('[restore-cinny] Removed cinny/android junction'); + } catch (err) { + console.warn('[restore-cinny] Could not remove android junction:', err.message); + } +} + +// 4. Pop stash if apply-overlay saved one +if (existsSync(STASH_FLAG)) { + console.log('[restore-cinny] Popping pre-overlay stash...'); + try { + execSync('git stash pop', { cwd: CINNY, stdio: 'inherit' }); + console.log('[restore-cinny] Stash restored.'); + } catch (err) { + console.warn('[restore-cinny] Stash pop failed (may have conflicts):', err.message); + } + unlinkSync(STASH_FLAG); +} + +console.log('[restore-cinny] Done. cinny submodule is clean.');