[tauri] TauriPlatforms: The shell around the built game, the Android privacy patch, the shell build of the game, and the device check

This commit is contained in:
2026-11-17 18:45:37 +01:00
parent 3f95a1b37d
commit a93ef09956
33 changed files with 5902 additions and 86 deletions
+115
View File
@@ -0,0 +1,115 @@
/**
* The Android app's privacy on the generated project, and the proof on the built APK
* (src/android-privacy.ts; tauri-platforms.md D6).
*
* tsx scripts/android.ts prepare `tauri android init` if needed, icons, then apply
* tsx scripts/android.ts apply patch the manifest, copy the rule files, then check
* tsx scripts/android.ts check what the generated project lacks; exit 1 if anything
* tsx scripts/android.ts verify [apk] the same, read from the BUILT APK with aapt2
*
* `apply` runs from tauri.android.conf.json's beforeBuildCommand, so `tauri android build`
* cannot make an APK without it. A Gradle build started outside the Tauri CLI skips that hook;
* `verify` reads the artefact itself, and `pnpm tauri:android` runs it last.
*/
import { spawnSync } from 'node:child_process';
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { apkProblems, manifestProblems, patchManifest, RULE_FILES, RULE_SECTIONS, ruleProblems } from '../src/android-privacy.js';
import { PACKAGE, SRC_TAURI, tauri } from './tauri.js';
const MAIN = join(SRC_TAURI, 'gen', 'android', 'app', 'src', 'main');
const MANIFEST = join(MAIN, 'AndroidManifest.xml');
const RES_FROM = join(PACKAGE, 'android', 'res', 'xml');
const RES_TO = join(MAIN, 'res', 'xml');
const OUTPUTS = join(SRC_TAURI, 'gen', 'android', 'app', 'build', 'outputs', 'apk');
function fail(problems: readonly string[]): never {
for (const p of problems) console.error(`android: ${p}`);
process.exit(1);
}
function problems(): string[] {
if (!existsSync(MANIFEST)) return [`no generated project (${MANIFEST}); run 'pnpm tauri:android'`];
const found = manifestProblems(readFileSync(MANIFEST, 'utf8'));
for (const f of RULE_FILES) {
const at = join(RES_TO, f);
if (!existsSync(at)) found.push(`res/xml/${f} is missing`);
else if (readFileSync(at, 'utf8') !== readFileSync(join(RES_FROM, f), 'utf8')) found.push(`res/xml/${f} differs from android/res/xml/${f}`);
}
return found;
}
function apply(): void {
for (const f of RULE_FILES) {
const left = ruleProblems(readFileSync(join(RES_FROM, f), 'utf8'), RULE_SECTIONS[f]);
if (left.length > 0) fail(left.map((p) => `android/res/xml/${f}: ${p}`));
}
if (!existsSync(MANIFEST)) fail([`no generated project (${MANIFEST}); run 'pnpm tauri:android'`]);
const before = readFileSync(MANIFEST, 'utf8');
const after = patchManifest(before);
mkdirSync(RES_TO, { recursive: true });
for (const f of RULE_FILES) copyFileSync(join(RES_FROM, f), join(RES_TO, f));
if (after !== before) writeFileSync(MANIFEST, after);
const left = problems();
if (left.length > 0) fail(left);
console.log(
`android: privacy ${after === before ? 'already in place' : 'applied'} ` +
'(no INTERNET, no backup or transfer, WebView metrics and Safe Browsing off)'
);
}
function newestApk(dir: string): string | undefined {
if (!existsSync(dir)) return undefined;
let best: { path: string; at: number } | undefined;
const walk = (d: string): void => {
for (const name of readdirSync(d)) {
const path = join(d, name);
const stat = statSync(path);
if (stat.isDirectory()) walk(path);
else if (name.endsWith('.apk') && (!best || stat.mtimeMs > best.at)) best = { path, at: stat.mtimeMs };
}
};
walk(dir);
return best?.path;
}
function aapt2(): string {
const sdk = process.env['ANDROID_HOME'] ?? process.env['ANDROID_SDK_ROOT'];
if (!sdk) fail(['ANDROID_HOME is not set']);
const tools = join(sdk, 'build-tools');
const versions = readdirSync(tools).sort((a, b) => b.localeCompare(a, undefined, { numeric: true }));
const exe = process.platform === 'win32' ? 'aapt2.exe' : 'aapt2';
for (const v of versions) if (existsSync(join(tools, v, exe))) return join(tools, v, exe);
return fail([`no aapt2 under ${tools}`]);
}
function verify(apk: string | undefined): void {
const file = apk ?? newestApk(OUTPUTS);
if (file === undefined || !existsSync(file)) fail([`no APK to verify under ${OUTPUTS}`]);
const tool = aapt2();
const run = (...args: string[]): string => {
const r = spawnSync(tool, args, { encoding: 'utf8' });
if (r.status !== 0) fail([`aapt2 ${args.join(' ')} failed: ${r.stderr}`]);
return r.stdout;
};
const left = apkProblems(run('dump', 'permissions', file), run('dump', 'xmltree', '--file', 'AndroidManifest.xml', file));
if (left.length > 0) fail(left);
console.log(`android: ${file} verified (no permission asked, no backup, WebView metrics and Safe Browsing off)`);
}
const [command, arg] = process.argv.slice(2);
try {
if (command === 'prepare') {
if (!existsSync(MANIFEST)) tauri('android', 'init', '--ci', '--skip-targets-install');
await import('./icons.js');
apply();
} else if (command === 'apply') apply();
else if (command === 'check') {
const left = problems();
if (left.length > 0) fail(left);
console.log('android: privacy in place');
} else if (command === 'verify') verify(arg);
else fail(['usage: android.ts prepare|apply|check|verify [apk]']);
} catch (error) {
fail([(error as Error).message]);
}
+27
View File
@@ -0,0 +1,27 @@
/**
* The app's icons, made from the PWA's icon (`apps/rosea/public/icon.svg`) before every build
* (tauri-platforms.md D8). They are build output, never committed: the only images in git are
* the source SVG and optimised art under art/final/ (CLAUDE.md §5.2). The icon is still a
* placeholder; the real one comes through the art pipeline (GAP_ANALYSIS G0.21).
*
* tsx scripts/icons.ts makes them when missing or older than the SVG
*/
import { existsSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { PACKAGE, SRC_TAURI, tauri } from './tauri.js';
const SOURCE = join(PACKAGE, '..', 'rosea', 'public', 'icon.svg');
const MARK = join(SRC_TAURI, 'icons', 'icon.ico');
const ANDROID = join(SRC_TAURI, 'gen', 'android');
const ANDROID_MARK = join(ANDROID, 'app', 'src', 'main', 'res', 'mipmap-mdpi', 'ic_launcher.png');
const stale = (file: string): boolean => !existsSync(file) || statSync(file).mtimeMs < statSync(SOURCE).mtimeMs;
// `tauri icon` writes the Android launcher icons into gen/android only when that exists, so it
// runs again after `tauri android init` (which puts Tauri's own logo there).
if (stale(MARK) || (existsSync(ANDROID) && stale(ANDROID_MARK))) {
tauri('icon', SOURCE);
console.log('icons: made from apps/rosea/public/icon.svg');
} else {
console.log('icons: up to date');
}
+22
View File
@@ -0,0 +1,22 @@
/**
* The iOS app. Tauri can build it only on macOS with Xcode, and putting it on a phone needs an
* Apple developer account (free for one's own device; paid for TestFlight and the App Store).
* Elsewhere this refuses with that reason instead of failing somewhere deep in the CLI
* (CLAUDE.md §8, *iOS cannot be built on Windows*; tauri-platforms.md G-T1).
*
* tsx scripts/ios.ts on macOS: `tauri ios init` once, icons, then `tauri ios build --debug`
*/
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { SRC_TAURI, tauri } from './tauri.js';
if (process.platform !== 'darwin') {
console.error(
'ios: the iOS app can only be built on macOS with Xcode (and an Apple developer account to run it on a phone).\n' +
`ios: this machine is ${process.platform}. Nothing was built. See .agents/features/tauri-platforms.md, G-T1.`
);
process.exit(1);
}
if (!existsSync(join(SRC_TAURI, 'gen', 'apple'))) tauri('ios', 'init', '--ci');
await import('./icons.js');
tauri('ios', 'build', '--debug', ...process.argv.slice(2));
+19
View File
@@ -0,0 +1,19 @@
/** Runs the Tauri CLI of this package, in this package's folder, and stops the script if it fails. */
import { spawnSync } from 'node:child_process';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
export const PACKAGE = join(dirname(fileURLToPath(import.meta.url)), '..');
export const SRC_TAURI = join(PACKAGE, 'src-tauri');
export function tauri(...args: string[]): void {
const run = spawnSync('pnpm', ['exec', 'tauri', ...args], {
cwd: PACKAGE,
stdio: 'inherit',
shell: process.platform === 'win32'
});
if (run.status !== 0) {
console.error(`tauri ${args.join(' ')} failed (exit ${String(run.status ?? run.signal)})`);
process.exit(1);
}
}