@moku-labs/native 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.cjs +592 -128
- package/dist/index.d.cts +13 -8
- package/dist/index.d.mts +13 -8
- package/dist/index.mjs +592 -128
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1528,7 +1528,9 @@ function placeholderIconPng() {
|
|
|
1528
1528
|
* @file project plugin — mobile gen/-tree required-file completeness gate.
|
|
1529
1529
|
*/
|
|
1530
1530
|
/**
|
|
1531
|
-
* Required top-level entries per mobile `gen/<platform>` tree
|
|
1531
|
+
* Required top-level entries per mobile `gen/<platform>` tree. The android set is verified
|
|
1532
|
+
* against the tree `tauri android init` writes in tauri-cli 2.12.1: the settings file is
|
|
1533
|
+
* Groovy `settings.gradle`, not `settings.gradle.kts`.
|
|
1532
1534
|
* A partial init (tauri#13902) can leave some of these absent — `completeness` checks
|
|
1533
1535
|
* the required-file SET, not mere directory existence.
|
|
1534
1536
|
*/
|
|
@@ -1541,7 +1543,7 @@ const REQUIRED_FILES = {
|
|
|
1541
1543
|
],
|
|
1542
1544
|
android: [
|
|
1543
1545
|
"build.gradle.kts",
|
|
1544
|
-
"settings.gradle
|
|
1546
|
+
"settings.gradle",
|
|
1545
1547
|
"gradle.properties",
|
|
1546
1548
|
"app/build.gradle.kts",
|
|
1547
1549
|
"app/src/main/AndroidManifest.xml"
|
|
@@ -1555,7 +1557,7 @@ const REQUIRED_FILES = {
|
|
|
1555
1557
|
* @returns The frozen list of required paths, relative to `src-tauri/gen/<platform>`.
|
|
1556
1558
|
* @example
|
|
1557
1559
|
* ```ts
|
|
1558
|
-
* requiredFiles("android"); // ["build.gradle.kts", "settings.gradle
|
|
1560
|
+
* requiredFiles("android"); // ["build.gradle.kts", "settings.gradle", ...]
|
|
1559
1561
|
* ```
|
|
1560
1562
|
*/
|
|
1561
1563
|
function requiredFiles(platform) {
|
|
@@ -1911,7 +1913,7 @@ async function patchAndroidManifest(projectDirectory, genDirectory, entries) {
|
|
|
1911
1913
|
* touch". Both iOS patches (the runner-command rewrite and the Xcode build-settings patch)
|
|
1912
1914
|
* read the same list from here, so neither can drift into a file the other does not know
|
|
1913
1915
|
* about — and neither ever descends into `gen/apple/build` or `gen/android/build`, which
|
|
1914
|
-
* belong to the toolchain.
|
|
1916
|
+
* belong to the toolchain. The Android status bar patch finds its MainActivity.kt here too.
|
|
1915
1917
|
*/
|
|
1916
1918
|
/** Source extensions that can carry the Android runner command. */
|
|
1917
1919
|
const ANDROID_SOURCE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
@@ -1919,6 +1921,15 @@ const ANDROID_SOURCE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
|
1919
1921
|
".kts",
|
|
1920
1922
|
".gradle"
|
|
1921
1923
|
]);
|
|
1924
|
+
/** The Kotlin file `tauri android init` writes the app's activity into. */
|
|
1925
|
+
const MAIN_ACTIVITY_FILE = "MainActivity.kt";
|
|
1926
|
+
/** Where `tauri android init` writes the app's Kotlin sources, below `gen/android`. */
|
|
1927
|
+
const ANDROID_JAVA_SEGMENTS = [
|
|
1928
|
+
"app",
|
|
1929
|
+
"src",
|
|
1930
|
+
"main",
|
|
1931
|
+
"java"
|
|
1932
|
+
];
|
|
1922
1933
|
/** Derived directories a patch scan never descends into — the toolchain owns them. */
|
|
1923
1934
|
const SKIPPED_DIRECTORIES = /* @__PURE__ */ new Set([
|
|
1924
1935
|
"build",
|
|
@@ -1989,6 +2000,186 @@ async function androidPatchFiles(genDirectory) {
|
|
|
1989
2000
|
const gradleFiles = [node_path.default.join(genDirectory, "build.gradle.kts"), node_path.default.join(genDirectory, "app", "build.gradle.kts")].filter((file) => (0, node_fs.existsSync)(file));
|
|
1990
2001
|
return [...buildSource, ...gradleFiles];
|
|
1991
2002
|
}
|
|
2003
|
+
/**
|
|
2004
|
+
* Returns the directory `tauri android init` writes the app's Kotlin sources into. The
|
|
2005
|
+
* MainActivity.kt sits below it, in a folder per segment of the app identifier.
|
|
2006
|
+
*
|
|
2007
|
+
* @param genDirectory - The `src-tauri/gen/android` directory.
|
|
2008
|
+
* @returns The `app/src/main/java` directory of that tree.
|
|
2009
|
+
* @example
|
|
2010
|
+
* ```ts
|
|
2011
|
+
* androidJavaDirectory("/repo/.moku/tauri/src-tauri/gen/android");
|
|
2012
|
+
* // "/repo/.moku/tauri/src-tauri/gen/android/app/src/main/java"
|
|
2013
|
+
* ```
|
|
2014
|
+
*/
|
|
2015
|
+
function androidJavaDirectory(genDirectory) {
|
|
2016
|
+
return node_path.default.join(genDirectory, ...ANDROID_JAVA_SEGMENTS);
|
|
2017
|
+
}
|
|
2018
|
+
/**
|
|
2019
|
+
* Lists every `MainActivity.kt` below `app/src/main/java`, sorted by path. Build output is
|
|
2020
|
+
* never listed. A generated tree holds exactly one; the caller decides what more or fewer
|
|
2021
|
+
* means.
|
|
2022
|
+
*
|
|
2023
|
+
* @param genDirectory - The `src-tauri/gen/android` directory.
|
|
2024
|
+
* @returns The MainActivity.kt paths found, possibly none.
|
|
2025
|
+
* @example
|
|
2026
|
+
* ```ts
|
|
2027
|
+
* await androidMainActivityFiles("/repo/.moku/tauri/src-tauri/gen/android");
|
|
2028
|
+
* // ["/repo/.moku/tauri/src-tauri/gen/android/app/src/main/java/com/acme/demo/MainActivity.kt"]
|
|
2029
|
+
* ```
|
|
2030
|
+
*/
|
|
2031
|
+
async function androidMainActivityFiles(genDirectory) {
|
|
2032
|
+
return (await collectFiles(androidJavaDirectory(genDirectory), (name) => name === MAIN_ACTIVITY_FILE)).toSorted();
|
|
2033
|
+
}
|
|
2034
|
+
//#endregion
|
|
2035
|
+
//#region src/plugins/project/mobile/signing.ts
|
|
2036
|
+
/**
|
|
2037
|
+
* @file project plugin — the Android release-signing block the mobile patch pass keeps in
|
|
2038
|
+
* `gen/android/app/build.gradle.kts`. Only env-var NAMES are ever written to disk: Gradle
|
|
2039
|
+
* reads the passwords itself, at build time.
|
|
2040
|
+
*/
|
|
2041
|
+
const SIGNING_START = "// MOKU-SIGNING-START";
|
|
2042
|
+
const SIGNING_END = "// MOKU-SIGNING-END";
|
|
2043
|
+
/**
|
|
2044
|
+
* Characters that change the meaning of a Kotlin string literal: a backslash starts an
|
|
2045
|
+
* escape, a double quote closes the literal, a dollar sign starts a template expression,
|
|
2046
|
+
* and a raw newline ends the line outright.
|
|
2047
|
+
*/
|
|
2048
|
+
const KOTLIN_ESCAPES = [
|
|
2049
|
+
[/\\/g, String.raw`\\`],
|
|
2050
|
+
[/"/g, String.raw`\"`],
|
|
2051
|
+
[/\$/g, String.raw`\$`],
|
|
2052
|
+
[/\r/g, String.raw`\r`],
|
|
2053
|
+
[/\n/g, String.raw`\n`]
|
|
2054
|
+
];
|
|
2055
|
+
/**
|
|
2056
|
+
* Escapes a value for use inside a double-quoted Kotlin string literal. Config values land
|
|
2057
|
+
* verbatim in `build.gradle.kts` — an unescaped `"` would close the literal and an
|
|
2058
|
+
* unescaped `$` would interpolate a Gradle expression, so every one of them is escaped
|
|
2059
|
+
* here rather than trusted.
|
|
2060
|
+
*
|
|
2061
|
+
* @param value - The raw config value (keystore path, key alias, env-var name).
|
|
2062
|
+
* @returns The value, safe to place between double quotes in Kotlin source.
|
|
2063
|
+
* @example
|
|
2064
|
+
* ```ts
|
|
2065
|
+
* kotlinString('re"lease$HOME'); // 're\\"lease\\$HOME'
|
|
2066
|
+
* ```
|
|
2067
|
+
*/
|
|
2068
|
+
function kotlinString(value) {
|
|
2069
|
+
let escaped = value;
|
|
2070
|
+
for (const [pattern, replacement] of KOTLIN_ESCAPES) escaped = escaped.replace(pattern, replacement);
|
|
2071
|
+
return escaped;
|
|
2072
|
+
}
|
|
2073
|
+
/**
|
|
2074
|
+
* Renders the moku-managed Gradle signing block. Passwords are read by Gradle itself from
|
|
2075
|
+
* the environment at build time — only env-var NAMES are ever written to disk, per the
|
|
2076
|
+
* `SigningConfig` invariant. `keyPasswordEnv` falls back to `keystorePasswordEnv`, which
|
|
2077
|
+
* is the common single-password keystore. Every interpolated value goes through
|
|
2078
|
+
* {@link kotlinString}, so a path or alias can never close the literal it sits in.
|
|
2079
|
+
*
|
|
2080
|
+
* @param signing - The Android signing config slice, with a keystore path already present.
|
|
2081
|
+
* @param keystorePath - The configured keystore path.
|
|
2082
|
+
* @returns The sentinel-delimited Gradle block.
|
|
2083
|
+
* @example
|
|
2084
|
+
* ```ts
|
|
2085
|
+
* renderSigningBlock({ keyAlias: "release" }, "release.jks");
|
|
2086
|
+
* ```
|
|
2087
|
+
*/
|
|
2088
|
+
function renderSigningBlock(signing, keystorePath) {
|
|
2089
|
+
const storePasswordEnvironment = signing.keystorePasswordEnv ?? "";
|
|
2090
|
+
const keyPasswordEnvironment = signing.keyPasswordEnv ?? storePasswordEnvironment;
|
|
2091
|
+
return [
|
|
2092
|
+
SIGNING_START,
|
|
2093
|
+
"android {",
|
|
2094
|
+
" signingConfigs {",
|
|
2095
|
+
" maybeCreate(\"release\").apply {",
|
|
2096
|
+
` storeFile = file("${kotlinString(keystorePath)}")`,
|
|
2097
|
+
` keyAlias = "${kotlinString(signing.keyAlias ?? "")}"`,
|
|
2098
|
+
` storePassword = System.getenv("${kotlinString(storePasswordEnvironment)}")`,
|
|
2099
|
+
` keyPassword = System.getenv("${kotlinString(keyPasswordEnvironment)}")`,
|
|
2100
|
+
" }",
|
|
2101
|
+
" }",
|
|
2102
|
+
" buildTypes {",
|
|
2103
|
+
" getByName(\"release\") {",
|
|
2104
|
+
" signingConfig = signingConfigs.getByName(\"release\")",
|
|
2105
|
+
" }",
|
|
2106
|
+
" }",
|
|
2107
|
+
"}",
|
|
2108
|
+
SIGNING_END
|
|
2109
|
+
].join("\n");
|
|
2110
|
+
}
|
|
2111
|
+
/**
|
|
2112
|
+
* Strips the moku-managed signing block (and the blank line it was inserted with) from
|
|
2113
|
+
* Gradle content, leaving content that never carried one untouched.
|
|
2114
|
+
*
|
|
2115
|
+
* @param existing - The current `build.gradle.kts` content.
|
|
2116
|
+
* @returns The content without a moku signing block.
|
|
2117
|
+
* @example
|
|
2118
|
+
* ```ts
|
|
2119
|
+
* removeSigningBlock(patchedGradleKts);
|
|
2120
|
+
* ```
|
|
2121
|
+
*/
|
|
2122
|
+
function removeSigningBlock(existing) {
|
|
2123
|
+
const startIndex = existing.indexOf(SIGNING_START);
|
|
2124
|
+
const endIndex = existing.indexOf(SIGNING_END);
|
|
2125
|
+
if (startIndex === -1 || endIndex === -1) return existing;
|
|
2126
|
+
let before = existing.slice(0, startIndex);
|
|
2127
|
+
while (before.endsWith("\n")) before = before.slice(0, -1);
|
|
2128
|
+
let after = existing.slice(endIndex + 19);
|
|
2129
|
+
while (after.startsWith("\n")) after = after.slice(1);
|
|
2130
|
+
return before.length > 0 ? `${before}\n${after}` : after;
|
|
2131
|
+
}
|
|
2132
|
+
/**
|
|
2133
|
+
* Idempotently inserts, replaces or removes the moku-managed signing block inside an
|
|
2134
|
+
* existing `app/build.gradle.kts`. Re-applying to already-patched content is a text-level
|
|
2135
|
+
* no-op; an unconfigured keystore REMOVES a block a previous build left behind, so
|
|
2136
|
+
* dropping `signing.android.keystorePath` really does return to unsigned builds.
|
|
2137
|
+
*
|
|
2138
|
+
* @param existing - The current `build.gradle.kts` content.
|
|
2139
|
+
* @param signing - The Android signing config slice (empty object when unconfigured).
|
|
2140
|
+
* @returns The patched content with exactly one signing block, or none when unconfigured.
|
|
2141
|
+
* @example
|
|
2142
|
+
* ```ts
|
|
2143
|
+
* applySigningBlock(gradleKts, { keystorePath: "release.jks", keyAlias: "release" });
|
|
2144
|
+
* ```
|
|
2145
|
+
*/
|
|
2146
|
+
function applySigningBlock(existing, signing) {
|
|
2147
|
+
if (!signing.keystorePath) return removeSigningBlock(existing);
|
|
2148
|
+
const block = renderSigningBlock(signing, signing.keystorePath);
|
|
2149
|
+
const startIndex = existing.indexOf(SIGNING_START);
|
|
2150
|
+
const endIndex = existing.indexOf(SIGNING_END);
|
|
2151
|
+
if (startIndex === -1 || endIndex === -1) return `${existing}${existing.endsWith("\n") ? "" : "\n"}\n${block}\n`;
|
|
2152
|
+
return `${existing.slice(0, startIndex)}${block}${existing.slice(endIndex + 19)}`;
|
|
2153
|
+
}
|
|
2154
|
+
/**
|
|
2155
|
+
* Patches Android release signing into `app/build.gradle.kts`, and clears the legacy
|
|
2156
|
+
* `keystore.properties` a previous version of this plugin wrote. Signing now lives in
|
|
2157
|
+
* Gradle only, so nothing ever re-creates that file.
|
|
2158
|
+
*
|
|
2159
|
+
* @param projectDirectory - The Tauri project root.
|
|
2160
|
+
* @param genDirectory - The `src-tauri/gen/android` directory.
|
|
2161
|
+
* @param signing - The Android signing config slice (empty object when unconfigured).
|
|
2162
|
+
* @returns The paths patched vs. left unchanged.
|
|
2163
|
+
* @example
|
|
2164
|
+
* ```ts
|
|
2165
|
+
* await patchAndroidSigning(projectDir, genDir, { keystorePath: "release.jks" });
|
|
2166
|
+
* ```
|
|
2167
|
+
*/
|
|
2168
|
+
async function patchAndroidSigning(projectDirectory, genDirectory, signing) {
|
|
2169
|
+
const patched = [];
|
|
2170
|
+
const unchanged = [];
|
|
2171
|
+
const buildGradlePath = node_path.default.join(genDirectory, "app", "build.gradle.kts");
|
|
2172
|
+
(await writeIfChanged(buildGradlePath, applySigningBlock(await (0, node_fs_promises.readFile)(buildGradlePath, "utf8"), signing), projectDirectory) === "written" ? patched : unchanged).push(buildGradlePath);
|
|
2173
|
+
const keystorePropertiesPath = node_path.default.join(genDirectory, "keystore.properties");
|
|
2174
|
+
if (!signing.keystorePath && (0, node_fs.existsSync)(keystorePropertiesPath)) {
|
|
2175
|
+
await (0, node_fs_promises.rm)(keystorePropertiesPath, { force: true });
|
|
2176
|
+
patched.push(keystorePropertiesPath);
|
|
2177
|
+
}
|
|
2178
|
+
return {
|
|
2179
|
+
patched,
|
|
2180
|
+
unchanged
|
|
2181
|
+
};
|
|
2182
|
+
}
|
|
1992
2183
|
//#endregion
|
|
1993
2184
|
//#region src/plugins/project/mobile/runner.ts
|
|
1994
2185
|
/**
|
|
@@ -1997,6 +2188,11 @@ async function androidPatchFiles(genDirectory) {
|
|
|
1997
2188
|
* generated Xcode/Android-Studio build phase, and none of those resolve inside those IDEs —
|
|
1998
2189
|
* so an unpatched tree fails on its first build phase. Every occurrence becomes the
|
|
1999
2190
|
* absolute `<node> <tauri.js>` pair the tauri plugin itself spawns with.
|
|
2191
|
+
*
|
|
2192
|
+
* Two shapes carry the runner. Xcode and older Android sources hold ONE command string
|
|
2193
|
+
* (`node tauri android android-studio-script`). The Android `BuildTask.kt` holds it as Kotlin
|
|
2194
|
+
* code instead: `val executable = """node""";` plus
|
|
2195
|
+
* `listOf("tauri", "android", "android-studio-script")`.
|
|
2000
2196
|
*/
|
|
2001
2197
|
/** `tauri ios init` bakes `<detected-runner> ios xcode-script …` into the Xcode project. */
|
|
2002
2198
|
const IOS_RUNNER_VERB = "ios xcode-script";
|
|
@@ -2038,6 +2234,24 @@ const PACKAGE_RUNNER = [
|
|
|
2038
2234
|
const SHELL_METACHARACTER_PATTERN = /[\\"$`]/g;
|
|
2039
2235
|
/** A line break inside a runner path would split the generated build phase into two commands. */
|
|
2040
2236
|
const LINE_BREAK_PATTERN = /[\n\r]/;
|
|
2237
|
+
/**
|
|
2238
|
+
* Characters a raw Kotlin string (`"""…"""`) cannot hold as they are. A raw string has no
|
|
2239
|
+
* backslash escapes, so `$` (a template) and `"` (part of the closing `"""`) are written as
|
|
2240
|
+
* `${'$'}` / `${'"'}` templates. A backslash is literal there and stays as it is.
|
|
2241
|
+
*/
|
|
2242
|
+
const KOTLIN_RAW_STRING_SPECIAL_PATTERN = /["$]/g;
|
|
2243
|
+
/** The Android verb as `BuildTask.kt` passes it: two separate `listOf` arguments. */
|
|
2244
|
+
const KOTLIN_VERB_ARGUMENTS = String.raw`"android"\s*,\s*"android-studio-script"`;
|
|
2245
|
+
/** One plain Kotlin string literal on one line, backslash escapes included. */
|
|
2246
|
+
const KOTLIN_STRING_LITERAL = String.raw`"(?:[^"\\\n]|\\.)*"`;
|
|
2247
|
+
/**
|
|
2248
|
+
* The runner arguments of the `BuildTask.kt` `listOf(…)`: every string literal before the
|
|
2249
|
+
* verb pair (`"tauri"`, npm's `"run", "--", "tauri"`, an already-patched `"<tauri.js>"`).
|
|
2250
|
+
* Group 1 is the `listOf(` opener, kept as it is.
|
|
2251
|
+
*/
|
|
2252
|
+
const KOTLIN_RUNNER_ARGUMENTS = String.raw`(listOf\(\s*)(?:${KOTLIN_STRING_LITERAL}\s*,\s*)*?(?=${KOTLIN_VERB_ARGUMENTS})`;
|
|
2253
|
+
/** The raw-string body of `val executable = """…"""`. Group 1 is everything up to the body. */
|
|
2254
|
+
const KOTLIN_EXECUTABLE = String.raw`(\bval\s+executable\s*=\s*""")[^\n]*?(?=""")`;
|
|
2041
2255
|
/** Regex metacharacters, escaped before a caller-supplied verb is embedded in a pattern. */
|
|
2042
2256
|
const REGEX_METACHARACTER_PATTERN = /[.*+?^${}()|[\]\\]/g;
|
|
2043
2257
|
/**
|
|
@@ -2129,10 +2343,96 @@ function unknownRunnerError(verb, filePath) {
|
|
|
2129
2343
|
* ```
|
|
2130
2344
|
*/
|
|
2131
2345
|
function escapeRunnerPath(value) {
|
|
2132
|
-
|
|
2346
|
+
assertSingleLine(value);
|
|
2133
2347
|
return value.replaceAll(SHELL_METACHARACTER_PATTERN, (match) => `\\${match}`);
|
|
2134
2348
|
}
|
|
2135
2349
|
/**
|
|
2350
|
+
* Refuses a runner path with a line break. In a shell build phase it would split the command
|
|
2351
|
+
* in two; in a Kotlin literal it would end the line mid-string.
|
|
2352
|
+
*
|
|
2353
|
+
* @param value - The absolute path to check.
|
|
2354
|
+
* @throws {Error} `[native]` when the path contains a line break.
|
|
2355
|
+
* @example
|
|
2356
|
+
* ```ts
|
|
2357
|
+
* assertSingleLine("/opt/homebrew/bin/node"); // passes
|
|
2358
|
+
* ```
|
|
2359
|
+
*/
|
|
2360
|
+
function assertSingleLine(value) {
|
|
2361
|
+
if (!LINE_BREAK_PATTERN.test(value)) return;
|
|
2362
|
+
throw new Error(`[native] Refusing to write a runner path containing a line break: ${JSON.stringify(value)}.\n Install Node and the Tauri CLI under a path without line breaks, or set pluginConfigs.tauri.nodePath.`);
|
|
2363
|
+
}
|
|
2364
|
+
/**
|
|
2365
|
+
* Escapes one runner path for a raw Kotlin string (`"""…"""`), which has no backslash
|
|
2366
|
+
* escapes: `$` and `"` become `${'$'}` / `${'"'}` templates, everything else stays as it is.
|
|
2367
|
+
*
|
|
2368
|
+
* @param value - The absolute path to escape.
|
|
2369
|
+
* @returns The raw string's body.
|
|
2370
|
+
* @throws {Error} `[native]` when the path contains a line break.
|
|
2371
|
+
* @example
|
|
2372
|
+
* ```ts
|
|
2373
|
+
* kotlinRawStringBody("/opt/node $HOME/bin/node"); // "/opt/node ${'$'}HOME/bin/node"
|
|
2374
|
+
* ```
|
|
2375
|
+
*/
|
|
2376
|
+
function kotlinRawStringBody(value) {
|
|
2377
|
+
assertSingleLine(value);
|
|
2378
|
+
return value.replaceAll(KOTLIN_RAW_STRING_SPECIAL_PATTERN, (match) => `\${'${match}'}`);
|
|
2379
|
+
}
|
|
2380
|
+
/**
|
|
2381
|
+
* Tests whether a file passes the Android verb as Kotlin `listOf` arguments at all.
|
|
2382
|
+
*
|
|
2383
|
+
* @param content - The file content to search.
|
|
2384
|
+
* @returns Whether `"android", "android-studio-script"` appears.
|
|
2385
|
+
* @example
|
|
2386
|
+
* ```ts
|
|
2387
|
+
* mentionsKotlinVerb('listOf("tauri", "android", "android-studio-script")'); // true
|
|
2388
|
+
* ```
|
|
2389
|
+
*/
|
|
2390
|
+
function mentionsKotlinVerb(content) {
|
|
2391
|
+
return new RegExp(KOTLIN_VERB_ARGUMENTS).test(content);
|
|
2392
|
+
}
|
|
2393
|
+
/**
|
|
2394
|
+
* Tests whether a file carries the full `BuildTask.kt` runner shape: the `listOf` arguments
|
|
2395
|
+
* AND the raw-string `executable`. Rewriting only one of them still leaves a broken runner.
|
|
2396
|
+
*
|
|
2397
|
+
* @param content - The file content to search.
|
|
2398
|
+
* @returns Whether both parts of the Kotlin runner are present.
|
|
2399
|
+
* @example
|
|
2400
|
+
* ```ts
|
|
2401
|
+
* hasKotlinRunner(buildTaskSource); // true for the tauri 2.12 template
|
|
2402
|
+
* ```
|
|
2403
|
+
*/
|
|
2404
|
+
function hasKotlinRunner(content) {
|
|
2405
|
+
return new RegExp(KOTLIN_RUNNER_ARGUMENTS).test(content) && new RegExp(KOTLIN_EXECUTABLE).test(content);
|
|
2406
|
+
}
|
|
2407
|
+
/**
|
|
2408
|
+
* Rewrites the runner `tauri android init` bakes into `BuildTask.kt` as Kotlin code: the
|
|
2409
|
+
* raw-string `executable` becomes the absolute Node path, and the `listOf` arguments before
|
|
2410
|
+
* `"android", "android-studio-script"` become the one absolute `tauri.js` path. Gradle runs
|
|
2411
|
+
* the executable directly, with no shell, so each path is escaped for its Kotlin literal
|
|
2412
|
+
* only. Everything else is kept byte for byte. A file without that `listOf` is returned as
|
|
2413
|
+
* it is, executable included.
|
|
2414
|
+
*
|
|
2415
|
+
* @param existing - The file content to rewrite.
|
|
2416
|
+
* @param runner - The absolute Node and `tauri.js` paths.
|
|
2417
|
+
* @returns The rewritten content (a no-op once already rewritten onto this runner).
|
|
2418
|
+
* @throws {Error} `[native]` when a runner path contains a line break.
|
|
2419
|
+
* @example
|
|
2420
|
+
* ```ts
|
|
2421
|
+
* applyKotlinRunner('val executable = """node""";\nval args = listOf("tauri", "android", "android-studio-script");', {
|
|
2422
|
+
* nodePath: "/opt/homebrew/bin/node",
|
|
2423
|
+
* tauriJsPath: "/repo/node_modules/@tauri-apps/cli/tauri.js"
|
|
2424
|
+
* });
|
|
2425
|
+
* // 'val executable = """/opt/homebrew/bin/node""";\nval args = listOf("/repo/node_modules/@tauri-apps/cli/tauri.js", "android", "android-studio-script");'
|
|
2426
|
+
* ```
|
|
2427
|
+
*/
|
|
2428
|
+
function applyKotlinRunner(existing, runner) {
|
|
2429
|
+
if (!new RegExp(KOTLIN_RUNNER_ARGUMENTS).test(existing)) return existing;
|
|
2430
|
+
const executable = kotlinRawStringBody(runner.nodePath);
|
|
2431
|
+
assertSingleLine(runner.tauriJsPath);
|
|
2432
|
+
const tauriJsArgument = `"${kotlinString(runner.tauriJsPath)}", `;
|
|
2433
|
+
return existing.replaceAll(new RegExp(KOTLIN_EXECUTABLE, "g"), (_match, open) => open + executable).replaceAll(new RegExp(KOTLIN_RUNNER_ARGUMENTS, "g"), (_match, open) => open + tauriJsArgument);
|
|
2434
|
+
}
|
|
2435
|
+
/**
|
|
2136
2436
|
* Rewrites the runner Tauri baked into a generated build phase into an absolute
|
|
2137
2437
|
* `<node> <tauri.js> <verb>` invocation. Only the runner-shaped token run directly before
|
|
2138
2438
|
* the verb is replaced — anything else on the line (indentation, a `script:` key, a
|
|
@@ -2173,12 +2473,54 @@ function quoteFor(filePath) {
|
|
|
2173
2473
|
return filePath.endsWith(".yml") ? "\"" : ESCAPED_QUOTE;
|
|
2174
2474
|
}
|
|
2175
2475
|
/**
|
|
2476
|
+
* Tests whether every runner a file carries is one this rewrite recognizes. A file that
|
|
2477
|
+
* never mentions the verb passes: it has no runner to rewrite.
|
|
2478
|
+
*
|
|
2479
|
+
* @param content - The file content to check.
|
|
2480
|
+
* @param verb - The Tauri verb the build phase invokes.
|
|
2481
|
+
* @returns Whether the rewrite can patch the file completely.
|
|
2482
|
+
* @example
|
|
2483
|
+
* ```ts
|
|
2484
|
+
* isRecognizedRunner('val command = "deno task tauri-cli android android-studio-script"', "android android-studio-script"); // false
|
|
2485
|
+
* ```
|
|
2486
|
+
*/
|
|
2487
|
+
function isRecognizedRunner(content, verb) {
|
|
2488
|
+
if (mentionsRunnerVerb(content, verb) && !hasRunnerCommand(content, verb)) return false;
|
|
2489
|
+
return !(verb === ANDROID_RUNNER_VERB && mentionsKotlinVerb(content)) || hasKotlinRunner(content);
|
|
2490
|
+
}
|
|
2491
|
+
/**
|
|
2492
|
+
* Rewrites every runner shape a platform's file may carry: the one-string command, and on
|
|
2493
|
+
* Android also the Kotlin `BuildTask.kt` shape.
|
|
2494
|
+
*
|
|
2495
|
+
* @param existing - The file content to rewrite.
|
|
2496
|
+
* @param filePath - The file being patched, which picks the quote form.
|
|
2497
|
+
* @param options - How to rewrite the command.
|
|
2498
|
+
* @param options.runner - The absolute Node and `tauri.js` paths.
|
|
2499
|
+
* @param options.verb - The Tauri verb the build phase invokes.
|
|
2500
|
+
* @returns The rewritten content.
|
|
2501
|
+
* @example
|
|
2502
|
+
* ```ts
|
|
2503
|
+
* rewriteRunner(buildTaskSource, buildTaskPath, { runner, verb: "android android-studio-script" });
|
|
2504
|
+
* ```
|
|
2505
|
+
*/
|
|
2506
|
+
function rewriteRunner(existing, filePath, options) {
|
|
2507
|
+
const { runner, verb } = options;
|
|
2508
|
+
const rewritten = applyRunnerCommand(existing, {
|
|
2509
|
+
runner,
|
|
2510
|
+
verb,
|
|
2511
|
+
quote: quoteFor(filePath)
|
|
2512
|
+
});
|
|
2513
|
+
if (verb !== ANDROID_RUNNER_VERB) return rewritten;
|
|
2514
|
+
return applyKotlinRunner(rewritten, runner);
|
|
2515
|
+
}
|
|
2516
|
+
/**
|
|
2176
2517
|
* Applies the runner rewrite to every candidate file for a platform, reporting which
|
|
2177
2518
|
* files actually changed. A second pass rewrites the absolute pair onto itself and
|
|
2178
2519
|
* reports everything unchanged.
|
|
2179
2520
|
*
|
|
2180
2521
|
* A file that invokes the verb but carries a runner shape this rewrite does not recognize
|
|
2181
|
-
*
|
|
2522
|
+
* (on Android also: a `listOf(…, "android", "android-studio-script")` without the
|
|
2523
|
+
* `val executable = """…"""` beside it) is an ERROR, not an "unchanged" file: Tauri baked in a command that will not resolve
|
|
2182
2524
|
* inside Xcode/Android Studio, and reporting success there means the failure surfaces much
|
|
2183
2525
|
* later, as an opaque build-phase exit. A file that never mentions the verb is simply
|
|
2184
2526
|
* unchanged.
|
|
@@ -2202,11 +2544,10 @@ async function patchRunner(projectDirectory, genDirectory, opts) {
|
|
|
2202
2544
|
const unchanged = [];
|
|
2203
2545
|
for (const file of files) {
|
|
2204
2546
|
const existing = await (0, node_fs_promises.readFile)(file, "utf8");
|
|
2205
|
-
if (
|
|
2206
|
-
(await writeIfChanged(file,
|
|
2547
|
+
if (!isRecognizedRunner(existing, verb)) throw unknownRunnerError(verb, file);
|
|
2548
|
+
(await writeIfChanged(file, rewriteRunner(existing, file, {
|
|
2207
2549
|
runner: opts.runner,
|
|
2208
|
-
verb
|
|
2209
|
-
quote: quoteFor(file)
|
|
2550
|
+
verb
|
|
2210
2551
|
}), projectDirectory) === "written" ? patched : unchanged).push(file);
|
|
2211
2552
|
}
|
|
2212
2553
|
return {
|
|
@@ -2215,152 +2556,268 @@ async function patchRunner(projectDirectory, genDirectory, opts) {
|
|
|
2215
2556
|
};
|
|
2216
2557
|
}
|
|
2217
2558
|
//#endregion
|
|
2218
|
-
//#region src/plugins/project/mobile/
|
|
2559
|
+
//#region src/plugins/project/mobile/system-bars.ts
|
|
2560
|
+
/**
|
|
2561
|
+
* @file project plugin — the Android system bar patch. The generated MainActivity.kt
|
|
2562
|
+
* calls `enableEdgeToEdge()`, whose default `SystemBarStyle.auto` picks the status bar
|
|
2563
|
+
* icon colour from the SYSTEM night mode, not from the app. A dark app on a light-mode
|
|
2564
|
+
* phone then gets dark icons on a dark page. When `app.backgroundColor` is set, this patch
|
|
2565
|
+
* rewrites that one call so the icons follow the colour's luminance instead.
|
|
2566
|
+
*
|
|
2567
|
+
* The transform is a pure text function: only the call and the two imports it needs
|
|
2568
|
+
* change, every other byte (line endings included) survives, a second pass is a no-op, and
|
|
2569
|
+
* unsetting the colour restores the generated call.
|
|
2570
|
+
*/
|
|
2571
|
+
/**
|
|
2572
|
+
* A background whose relative luminance is below this is dark, so it gets light icons.
|
|
2573
|
+
* Where white and black icons have equal WCAG contrast: sqrt(1.05 * 0.05) - 0.05.
|
|
2574
|
+
*/
|
|
2575
|
+
const DARK_LUMINANCE_THRESHOLD = .179;
|
|
2576
|
+
/** `#rrggbb` or `#rrggbbaa`, the three colour channels captured; alpha is ignored. */
|
|
2577
|
+
const HEX_CHANNELS_PATTERN = /^#([\da-f]{2})([\da-f]{2})([\da-f]{2})(?:[\da-f]{2})?$/i;
|
|
2578
|
+
/** The Rec. 709 weights of red, green and blue in relative luminance. */
|
|
2579
|
+
const LUMINANCE_WEIGHTS = [
|
|
2580
|
+
.2126,
|
|
2581
|
+
.7152,
|
|
2582
|
+
.0722
|
|
2583
|
+
];
|
|
2584
|
+
/** What marks the generated activity: it is the file that turns edge-to-edge on. */
|
|
2585
|
+
const EDGE_TO_EDGE_CALL = "enableEdgeToEdge(";
|
|
2586
|
+
/** The call exactly as `tauri android init` (cli 2.12) writes it. */
|
|
2587
|
+
const BARE_CALL = "enableEdgeToEdge()";
|
|
2219
2588
|
/**
|
|
2220
|
-
*
|
|
2221
|
-
*
|
|
2222
|
-
*
|
|
2589
|
+
* Builds the styled call: both bars get the same style, with a transparent scrim so the
|
|
2590
|
+
* page still draws behind them.
|
|
2591
|
+
*
|
|
2592
|
+
* @param style - The Kotlin `SystemBarStyle` expression for both bars.
|
|
2593
|
+
* @returns The `enableEdgeToEdge(...)` call.
|
|
2594
|
+
* @example
|
|
2595
|
+
* ```ts
|
|
2596
|
+
* styledCall("SystemBarStyle.dark(Color.TRANSPARENT)");
|
|
2597
|
+
* // "enableEdgeToEdge(statusBarStyle = SystemBarStyle.dark(Color.TRANSPARENT), navigationBarStyle = SystemBarStyle.dark(Color.TRANSPARENT))"
|
|
2598
|
+
* ```
|
|
2223
2599
|
*/
|
|
2224
|
-
|
|
2225
|
-
|
|
2600
|
+
function styledCall(style) {
|
|
2601
|
+
return `enableEdgeToEdge(statusBarStyle = ${style}, navigationBarStyle = ${style})`;
|
|
2602
|
+
}
|
|
2603
|
+
/** The call this patch writes per tone: `dark()` draws light icons, `light()` dark ones. */
|
|
2604
|
+
const STYLED_CALLS = {
|
|
2605
|
+
dark: styledCall("SystemBarStyle.dark(Color.TRANSPARENT)"),
|
|
2606
|
+
light: styledCall("SystemBarStyle.light(Color.TRANSPARENT, Color.TRANSPARENT)")
|
|
2607
|
+
};
|
|
2608
|
+
/** Every call this patch may replace: the generated one and its own two rewrites. */
|
|
2609
|
+
const KNOWN_CALLS = [
|
|
2610
|
+
BARE_CALL,
|
|
2611
|
+
STYLED_CALLS.dark,
|
|
2612
|
+
STYLED_CALLS.light
|
|
2613
|
+
];
|
|
2614
|
+
/** The imports the styled call needs, in the order they are added after the last import. */
|
|
2615
|
+
const STYLE_IMPORTS = [{
|
|
2616
|
+
line: "import androidx.activity.SystemBarStyle",
|
|
2617
|
+
usage: /\bSystemBarStyle\b/
|
|
2618
|
+
}, {
|
|
2619
|
+
line: "import android.graphics.Color",
|
|
2620
|
+
usage: /\bColor\b/
|
|
2621
|
+
}];
|
|
2622
|
+
/** One Kotlin import line, without its line ending. */
|
|
2623
|
+
const IMPORT_LINE_PATTERN = /^import [^\r\n]*/gm;
|
|
2624
|
+
/** The carriage return a CRLF line keeps after a split on `\n`. */
|
|
2625
|
+
const TRAILING_CARRIAGE_RETURN = /\r$/;
|
|
2226
2626
|
/**
|
|
2227
|
-
*
|
|
2228
|
-
*
|
|
2229
|
-
*
|
|
2627
|
+
* Builds the `[native]` error for a MainActivity.kt whose edge-to-edge call this patch
|
|
2628
|
+
* does not recognise.
|
|
2629
|
+
*
|
|
2630
|
+
* @param filePath - The MainActivity.kt that was read.
|
|
2631
|
+
* @returns A formatted `[native] ...` error.
|
|
2632
|
+
* @example
|
|
2633
|
+
* ```ts
|
|
2634
|
+
* throw unknownCallError("/repo/.moku/tauri/src-tauri/gen/android/app/src/main/java/com/acme/demo/MainActivity.kt");
|
|
2635
|
+
* ```
|
|
2230
2636
|
*/
|
|
2231
|
-
|
|
2232
|
-
[
|
|
2233
|
-
|
|
2234
|
-
[/\$/g, String.raw`\$`],
|
|
2235
|
-
[/\r/g, String.raw`\r`],
|
|
2236
|
-
[/\n/g, String.raw`\n`]
|
|
2237
|
-
];
|
|
2637
|
+
function unknownCallError(filePath) {
|
|
2638
|
+
return /* @__PURE__ */ new Error(`[native] The enableEdgeToEdge() call in ${filePath} is not the one tauri android init generates.\n Re-run the mobile init pass (tauri android init) to restore it, or unset app.backgroundColor.`);
|
|
2639
|
+
}
|
|
2238
2640
|
/**
|
|
2239
|
-
*
|
|
2240
|
-
*
|
|
2241
|
-
* unescaped `$` would interpolate a Gradle expression, so every one of them is escaped
|
|
2242
|
-
* here rather than trusted.
|
|
2641
|
+
* Builds the `[native]` error for a MainActivity.kt with no import line, so the system bar
|
|
2642
|
+
* imports have no place to go.
|
|
2243
2643
|
*
|
|
2244
|
-
* @param
|
|
2245
|
-
* @returns
|
|
2644
|
+
* @param filePath - The MainActivity.kt that was read.
|
|
2645
|
+
* @returns A formatted `[native] ...` error.
|
|
2246
2646
|
* @example
|
|
2247
2647
|
* ```ts
|
|
2248
|
-
*
|
|
2648
|
+
* throw noImportLineError("/repo/.moku/tauri/src-tauri/gen/android/app/src/main/java/com/acme/demo/MainActivity.kt");
|
|
2249
2649
|
* ```
|
|
2250
2650
|
*/
|
|
2251
|
-
function
|
|
2252
|
-
|
|
2253
|
-
for (const [pattern, replacement] of KOTLIN_ESCAPES) escaped = escaped.replace(pattern, replacement);
|
|
2254
|
-
return escaped;
|
|
2651
|
+
function noImportLineError(filePath) {
|
|
2652
|
+
return /* @__PURE__ */ new Error(`[native] ${filePath} has no import line to add the system bar imports after.\n Re-run the mobile init pass (tauri android init), or unset app.backgroundColor.`);
|
|
2255
2653
|
}
|
|
2256
2654
|
/**
|
|
2257
|
-
*
|
|
2258
|
-
*
|
|
2259
|
-
* `SigningConfig` invariant. `keyPasswordEnv` falls back to `keystorePasswordEnv`, which
|
|
2260
|
-
* is the common single-password keystore. Every interpolated value goes through
|
|
2261
|
-
* {@link kotlinString}, so a path or alias can never close the literal it sits in.
|
|
2655
|
+
* Builds the `[native]` error for a gen tree without exactly one MainActivity.kt that calls
|
|
2656
|
+
* `enableEdgeToEdge()`.
|
|
2262
2657
|
*
|
|
2263
|
-
* @param
|
|
2264
|
-
* @param
|
|
2265
|
-
* @returns
|
|
2658
|
+
* @param javaDirectory - The `app/src/main/java` directory that was searched.
|
|
2659
|
+
* @param found - The candidates found, sorted; empty when there were none.
|
|
2660
|
+
* @returns A formatted `[native] ...` error.
|
|
2266
2661
|
* @example
|
|
2267
2662
|
* ```ts
|
|
2268
|
-
*
|
|
2663
|
+
* throw notOneActivityError("/repo/.moku/tauri/src-tauri/gen/android/app/src/main/java", []);
|
|
2269
2664
|
* ```
|
|
2270
2665
|
*/
|
|
2271
|
-
function
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
return [
|
|
2275
|
-
SIGNING_START,
|
|
2276
|
-
"android {",
|
|
2277
|
-
" signingConfigs {",
|
|
2278
|
-
" maybeCreate(\"release\").apply {",
|
|
2279
|
-
` storeFile = file("${kotlinString(keystorePath)}")`,
|
|
2280
|
-
` keyAlias = "${kotlinString(signing.keyAlias ?? "")}"`,
|
|
2281
|
-
` storePassword = System.getenv("${kotlinString(storePasswordEnvironment)}")`,
|
|
2282
|
-
` keyPassword = System.getenv("${kotlinString(keyPasswordEnvironment)}")`,
|
|
2283
|
-
" }",
|
|
2284
|
-
" }",
|
|
2285
|
-
" buildTypes {",
|
|
2286
|
-
" getByName(\"release\") {",
|
|
2287
|
-
" signingConfig = signingConfigs.getByName(\"release\")",
|
|
2288
|
-
" }",
|
|
2289
|
-
" }",
|
|
2290
|
-
"}",
|
|
2291
|
-
SIGNING_END
|
|
2292
|
-
].join("\n");
|
|
2666
|
+
function notOneActivityError(javaDirectory, found) {
|
|
2667
|
+
if (found.length === 0) return /* @__PURE__ */ new Error(`[native] Could not find the MainActivity.kt that calls enableEdgeToEdge() under ${javaDirectory}.\n Re-run the mobile init pass (tauri android init), or unset app.backgroundColor.`);
|
|
2668
|
+
return /* @__PURE__ */ new Error(`[native] Found ${found.length} MainActivity.kt files that call enableEdgeToEdge() under ${javaDirectory}: ${found.join(", ")}.\n Keep the one tauri android init generated, or re-run the mobile init pass (tauri android init).`);
|
|
2293
2669
|
}
|
|
2294
2670
|
/**
|
|
2295
|
-
*
|
|
2296
|
-
* Gradle content, leaving content that never carried one untouched.
|
|
2671
|
+
* Linearises one sRGB channel, the transfer curve relative luminance is defined on.
|
|
2297
2672
|
*
|
|
2298
|
-
* @param
|
|
2299
|
-
* @returns The
|
|
2673
|
+
* @param hexPair - The channel as two hex digits.
|
|
2674
|
+
* @returns The linear channel value, 0 to 1.
|
|
2300
2675
|
* @example
|
|
2301
2676
|
* ```ts
|
|
2302
|
-
*
|
|
2677
|
+
* linearChannel("ff"); // 1
|
|
2678
|
+
* linearChannel("00"); // 0
|
|
2303
2679
|
* ```
|
|
2304
2680
|
*/
|
|
2305
|
-
function
|
|
2306
|
-
const
|
|
2307
|
-
|
|
2308
|
-
if (startIndex === -1 || endIndex === -1) return existing;
|
|
2309
|
-
let before = existing.slice(0, startIndex);
|
|
2310
|
-
while (before.endsWith("\n")) before = before.slice(0, -1);
|
|
2311
|
-
let after = existing.slice(endIndex + 19);
|
|
2312
|
-
while (after.startsWith("\n")) after = after.slice(1);
|
|
2313
|
-
return before.length > 0 ? `${before}\n${after}` : after;
|
|
2681
|
+
function linearChannel(hexPair) {
|
|
2682
|
+
const encoded = Number.parseInt(hexPair, 16) / 255;
|
|
2683
|
+
return encoded <= .04045 ? encoded / 12.92 : ((encoded + .055) / 1.055) ** 2.4;
|
|
2314
2684
|
}
|
|
2315
2685
|
/**
|
|
2316
|
-
*
|
|
2317
|
-
*
|
|
2318
|
-
* no-op; an unconfigured keystore REMOVES a block a previous build left behind, so
|
|
2319
|
-
* dropping `signing.android.keystorePath` really does return to unsigned builds.
|
|
2686
|
+
* Tells a dark background from a light one by its relative luminance (WCAG), alpha
|
|
2687
|
+
* ignored. Below {@link DARK_LUMINANCE_THRESHOLD} the background is dark.
|
|
2320
2688
|
*
|
|
2321
|
-
* @param
|
|
2322
|
-
* @
|
|
2323
|
-
* @
|
|
2689
|
+
* @param colour - The `app.backgroundColor` value, `#rrggbb` or `#rrggbbaa`.
|
|
2690
|
+
* @returns `"dark"` when the background wants light icons, `"light"` when it wants dark ones.
|
|
2691
|
+
* @throws {Error} `[native]` when the value is not a hex colour.
|
|
2324
2692
|
* @example
|
|
2325
2693
|
* ```ts
|
|
2326
|
-
*
|
|
2694
|
+
* backgroundTone("#10161d"); // "dark"
|
|
2695
|
+
* backgroundTone("#ffffffff"); // "light"
|
|
2327
2696
|
* ```
|
|
2328
2697
|
*/
|
|
2329
|
-
function
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
const endIndex = existing.indexOf(SIGNING_END);
|
|
2334
|
-
if (startIndex === -1 || endIndex === -1) return `${existing}${existing.endsWith("\n") ? "" : "\n"}\n${block}\n`;
|
|
2335
|
-
return `${existing.slice(0, startIndex)}${block}${existing.slice(endIndex + 19)}`;
|
|
2698
|
+
function backgroundTone(colour) {
|
|
2699
|
+
const channels = HEX_CHANNELS_PATTERN.exec(colour)?.slice(1);
|
|
2700
|
+
if (!channels) throw new Error(`[native] app.backgroundColor "${colour}" is not a valid colour.\n Use a hex colour such as "#10161d".`);
|
|
2701
|
+
return channels.reduce((sum, channel, index) => sum + (LUMINANCE_WEIGHTS[index] ?? 0) * linearChannel(channel), 0) < DARK_LUMINANCE_THRESHOLD ? "dark" : "light";
|
|
2336
2702
|
}
|
|
2337
2703
|
/**
|
|
2338
|
-
*
|
|
2339
|
-
*
|
|
2340
|
-
*
|
|
2704
|
+
* Adds the imports the styled call needs, each on its own line after the last import, in
|
|
2705
|
+
* the file's own line ending. An import already present is not added again.
|
|
2706
|
+
*
|
|
2707
|
+
* @param source - The MainActivity.kt content, call already styled.
|
|
2708
|
+
* @param filePath - The MainActivity.kt path, named in the error.
|
|
2709
|
+
* @returns The content with both imports present.
|
|
2710
|
+
* @throws {Error} `[native]` when the file has no import line to add them after.
|
|
2711
|
+
* @example
|
|
2712
|
+
* ```ts
|
|
2713
|
+
* addStyleImports("import android.os.Bundle\n\nclass A", "MainActivity.kt");
|
|
2714
|
+
* // "import android.os.Bundle\nimport androidx.activity.SystemBarStyle\nimport android.graphics.Color\n\nclass A"
|
|
2715
|
+
* ```
|
|
2716
|
+
*/
|
|
2717
|
+
function addStyleImports(source, filePath) {
|
|
2718
|
+
const lineEnding = source.includes("\r\n") ? "\r\n" : "\n";
|
|
2719
|
+
let result = source;
|
|
2720
|
+
for (const styleImport of STYLE_IMPORTS) {
|
|
2721
|
+
if (result.split(/\r?\n/).includes(styleImport.line)) continue;
|
|
2722
|
+
const lastImport = [...result.matchAll(IMPORT_LINE_PATTERN)].at(-1);
|
|
2723
|
+
if (!lastImport) throw noImportLineError(filePath);
|
|
2724
|
+
const end = lastImport.index + lastImport[0].length;
|
|
2725
|
+
result = `${result.slice(0, end)}${lineEnding}${styleImport.line}${result.slice(end)}`;
|
|
2726
|
+
}
|
|
2727
|
+
return result;
|
|
2728
|
+
}
|
|
2729
|
+
/**
|
|
2730
|
+
* Removes the style imports nothing uses any more, each with its own line. An import that
|
|
2731
|
+
* other code still uses stays, so a file that imported `Color` itself keeps it.
|
|
2732
|
+
*
|
|
2733
|
+
* @param source - The MainActivity.kt content, call already restored to bare.
|
|
2734
|
+
* @returns The content without the unused style imports.
|
|
2735
|
+
* @example
|
|
2736
|
+
* ```ts
|
|
2737
|
+
* removeUnusedStyleImports("import android.os.Bundle\nimport android.graphics.Color\n\nclass A");
|
|
2738
|
+
* // "import android.os.Bundle\n\nclass A"
|
|
2739
|
+
* ```
|
|
2740
|
+
*/
|
|
2741
|
+
function removeUnusedStyleImports(source) {
|
|
2742
|
+
let lines = source.split("\n");
|
|
2743
|
+
for (const styleImport of STYLE_IMPORTS) {
|
|
2744
|
+
if (lines.filter((line) => !line.startsWith("import ")).some((line) => styleImport.usage.test(line))) continue;
|
|
2745
|
+
lines = lines.filter((line) => line.replace(TRAILING_CARRIAGE_RETURN, "") !== styleImport.line);
|
|
2746
|
+
}
|
|
2747
|
+
return lines.join("\n");
|
|
2748
|
+
}
|
|
2749
|
+
/**
|
|
2750
|
+
* Sets the system bar style of a MainActivity.kt. A tone writes the styled call and adds
|
|
2751
|
+
* its imports; no tone restores the bare generated call and drops the imports nothing else
|
|
2752
|
+
* uses. Only a call this patch knows is touched: the generated one, or one of its own
|
|
2753
|
+
* rewrites.
|
|
2754
|
+
*
|
|
2755
|
+
* @param source - The MainActivity.kt content.
|
|
2756
|
+
* @param tone - The background's tone, or undefined when `app.backgroundColor` is unset.
|
|
2757
|
+
* @param filePath - The MainActivity.kt path, named in the error.
|
|
2758
|
+
* @returns The content with the style applied.
|
|
2759
|
+
* @throws {Error} `[native]` when a tone is set and the call is not one this patch knows.
|
|
2760
|
+
* @example
|
|
2761
|
+
* ```ts
|
|
2762
|
+
* applySystemBarStyle("import androidx.activity.enableEdgeToEdge\n\nenableEdgeToEdge()\n", "dark", "MainActivity.kt");
|
|
2763
|
+
* // "import androidx.activity.enableEdgeToEdge\nimport androidx.activity.SystemBarStyle\nimport android.graphics.Color\n\n"
|
|
2764
|
+
* // + "enableEdgeToEdge(statusBarStyle = SystemBarStyle.dark(Color.TRANSPARENT), navigationBarStyle = SystemBarStyle.dark(Color.TRANSPARENT))\n"
|
|
2765
|
+
* ```
|
|
2766
|
+
*/
|
|
2767
|
+
function applySystemBarStyle(source, tone, filePath) {
|
|
2768
|
+
if (!KNOWN_CALLS.some((call) => source.includes(call))) {
|
|
2769
|
+
if (tone === void 0) return source;
|
|
2770
|
+
throw unknownCallError(filePath);
|
|
2771
|
+
}
|
|
2772
|
+
const wanted = tone === void 0 ? BARE_CALL : STYLED_CALLS[tone];
|
|
2773
|
+
let result = source;
|
|
2774
|
+
for (const call of KNOWN_CALLS) result = result.replaceAll(call, () => wanted);
|
|
2775
|
+
return tone === void 0 ? removeUnusedStyleImports(result) : addStyleImports(result, filePath);
|
|
2776
|
+
}
|
|
2777
|
+
/**
|
|
2778
|
+
* Styles the Android system bars from `app.backgroundColor`, through the write-if-changed
|
|
2779
|
+
* writer. The target is the one MainActivity.kt below `gen/android/app/src/main/java` that
|
|
2780
|
+
* calls `enableEdgeToEdge()`. Without a colour a missing or ambiguous activity is left
|
|
2781
|
+
* alone, so an app that never set one keeps building; with one it is an error.
|
|
2341
2782
|
*
|
|
2342
2783
|
* @param projectDirectory - The Tauri project root.
|
|
2343
2784
|
* @param genDirectory - The `src-tauri/gen/android` directory.
|
|
2344
|
-
* @param
|
|
2345
|
-
* @returns The
|
|
2785
|
+
* @param backgroundColor - `app.backgroundColor`, or undefined when unset.
|
|
2786
|
+
* @returns The MainActivity.kt path, patched or unchanged; empty when there was none to touch.
|
|
2787
|
+
* @throws {Error} `[native]` when a colour is set and there is not exactly one MainActivity.kt
|
|
2788
|
+
* calling `enableEdgeToEdge()`, or its call is not one this patch knows.
|
|
2346
2789
|
* @example
|
|
2347
2790
|
* ```ts
|
|
2348
|
-
*
|
|
2791
|
+
* // First pass on the generated activity; a second pass lists it under `unchanged`.
|
|
2792
|
+
* await patchAndroidSystemBars(".moku/tauri", ".moku/tauri/src-tauri/gen/android", "#10161d");
|
|
2793
|
+
* // { patched: [".moku/tauri/src-tauri/gen/android/app/src/main/java/com/acme/demo/MainActivity.kt"], unchanged: [] }
|
|
2349
2794
|
* ```
|
|
2350
2795
|
*/
|
|
2351
|
-
async function
|
|
2352
|
-
const
|
|
2353
|
-
const
|
|
2354
|
-
const
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2796
|
+
async function patchAndroidSystemBars(projectDirectory, genDirectory, backgroundColor) {
|
|
2797
|
+
const tone = backgroundColor === void 0 ? void 0 : backgroundTone(backgroundColor);
|
|
2798
|
+
const activities = [];
|
|
2799
|
+
for (const file of await androidMainActivityFiles(genDirectory)) {
|
|
2800
|
+
const source = await (0, node_fs_promises.readFile)(file, "utf8");
|
|
2801
|
+
if (source.includes(EDGE_TO_EDGE_CALL)) activities.push({
|
|
2802
|
+
path: file,
|
|
2803
|
+
source
|
|
2804
|
+
});
|
|
2360
2805
|
}
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2806
|
+
const [activity] = activities;
|
|
2807
|
+
if (activities.length !== 1 || !activity) {
|
|
2808
|
+
if (tone === void 0) return {
|
|
2809
|
+
patched: [],
|
|
2810
|
+
unchanged: []
|
|
2811
|
+
};
|
|
2812
|
+
throw notOneActivityError(androidJavaDirectory(genDirectory), activities.map((candidate) => candidate.path));
|
|
2813
|
+
}
|
|
2814
|
+
const patchedSource = applySystemBarStyle(activity.source, tone, activity.path);
|
|
2815
|
+
return await writeIfChanged(activity.path, patchedSource, projectDirectory) === "written" ? {
|
|
2816
|
+
patched: [activity.path],
|
|
2817
|
+
unchanged: []
|
|
2818
|
+
} : {
|
|
2819
|
+
patched: [],
|
|
2820
|
+
unchanged: [activity.path]
|
|
2364
2821
|
};
|
|
2365
2822
|
}
|
|
2366
2823
|
//#endregion
|
|
@@ -2683,7 +3140,8 @@ async function patchXcodeSettings(projectDirectory, genDirectory) {
|
|
|
2683
3140
|
* @file project plugin — the post-init mobile patch pass: it decides WHICH patches a
|
|
2684
3141
|
* platform needs and in which order. The patches themselves live next door, in
|
|
2685
3142
|
* `signing.ts` (the Android release-signing block), `manifest.ts` (the Android main
|
|
2686
|
-
* activity's attributes, such as the orientation lock), `
|
|
3143
|
+
* activity's attributes, such as the orientation lock), `system-bars.ts` (the Android
|
|
3144
|
+
* status bar icon style from `app.backgroundColor`), `runner.ts` (the Xcode /
|
|
2687
3145
|
* Android-Studio runner command) and `xcode-settings.ts` (the iOS
|
|
2688
3146
|
* entitlements-modification setting that makes a REBUILD work).
|
|
2689
3147
|
*/
|
|
@@ -2709,25 +3167,29 @@ function mergePatchResults(...results) {
|
|
|
2709
3167
|
/**
|
|
2710
3168
|
* Deterministic, idempotent post-init patch pass over an existing complete gen/ tree.
|
|
2711
3169
|
* Android gets, in this order, its release signing block, the main activity's manifest
|
|
2712
|
-
* attributes, then the runner command. iOS gets
|
|
2713
|
-
* setting (without it the SECOND build of the same
|
|
2714
|
-
* The runner rewrite runs only when the caller
|
|
2715
|
-
*
|
|
2716
|
-
*
|
|
3170
|
+
* attributes, the system bar style in MainActivity.kt, then the runner command. iOS gets
|
|
3171
|
+
* the Xcode entitlements-modification setting (without it the SECOND build of the same
|
|
3172
|
+
* tree fails), then the runner command. The runner rewrite runs only when the caller
|
|
3173
|
+
* supplies a `runner`. iOS takes no manifest entries and no background colour: its
|
|
3174
|
+
* orientation lock and signing travel through the generated `Info.ios.plist` sidecar and
|
|
3175
|
+
* tauri.conf.json, and its status bar adapts to the page on its own.
|
|
2717
3176
|
*
|
|
2718
3177
|
* @param projectDirectory - The Tauri project root.
|
|
2719
3178
|
* @param opts - The mobile platform, and optionally the absolute Node/tauri.js runner pair.
|
|
2720
3179
|
* @param signing - The full signing config (only `.android` is consulted).
|
|
2721
3180
|
* @param manifest - Android manifest entries for the main activity (ignored on iOS).
|
|
3181
|
+
* @param backgroundColor - `app.backgroundColor`; the Android status bar icons follow it
|
|
3182
|
+
* (ignored on iOS).
|
|
2722
3183
|
* @returns The paths patched vs. left unchanged, one merged report over every patch.
|
|
2723
3184
|
* @throws {Error} When the Android `gen/android` tree (or its `app/build.gradle.kts`) is
|
|
2724
|
-
* missing,
|
|
3185
|
+
* missing, manifest entries meet a missing manifest or an unclear main activity, or a
|
|
3186
|
+
* background colour meets a tree without exactly one MainActivity.kt to style.
|
|
2725
3187
|
* @example
|
|
2726
3188
|
* ```ts
|
|
2727
|
-
* await patchMobile("/repo/.moku/tauri", { target: "android", runner }, {}, orientationManifest("portrait"));
|
|
3189
|
+
* await patchMobile("/repo/.moku/tauri", { target: "android", runner }, {}, orientationManifest("portrait"), "#10161d");
|
|
2728
3190
|
* ```
|
|
2729
3191
|
*/
|
|
2730
|
-
async function patchMobile(projectDirectory, opts, signing, manifest = []) {
|
|
3192
|
+
async function patchMobile(projectDirectory, opts, signing, manifest = [], backgroundColor) {
|
|
2731
3193
|
const genDirectory = genDirectoryPath(projectDirectory, opts.target);
|
|
2732
3194
|
if (opts.target === "ios") {
|
|
2733
3195
|
if (!(0, node_fs.existsSync)(genDirectory)) return {
|
|
@@ -2745,8 +3207,9 @@ async function patchMobile(projectDirectory, opts, signing, manifest = []) {
|
|
|
2745
3207
|
if (!(0, node_fs.existsSync)(genDirectory) || !(0, node_fs.existsSync)(buildGradlePath)) throw new Error(`[native] Android gen/ tree not found or incomplete at ${genDirectory}.\n Run the mobile init pass (tauri android init) before patching signing state.`);
|
|
2746
3208
|
const signingResult = await patchAndroidSigning(projectDirectory, genDirectory, signing.android ?? {});
|
|
2747
3209
|
const manifestResult = await patchAndroidManifest(projectDirectory, genDirectory, manifest);
|
|
2748
|
-
|
|
2749
|
-
return mergePatchResults(signingResult, manifestResult,
|
|
3210
|
+
const systemBarsResult = await patchAndroidSystemBars(projectDirectory, genDirectory, backgroundColor);
|
|
3211
|
+
if (!opts.runner) return mergePatchResults(signingResult, manifestResult, systemBarsResult);
|
|
3212
|
+
return mergePatchResults(signingResult, manifestResult, systemBarsResult, await patchRunner(projectDirectory, genDirectory, {
|
|
2750
3213
|
target: opts.target,
|
|
2751
3214
|
runner: opts.runner
|
|
2752
3215
|
}));
|
|
@@ -3158,7 +3621,8 @@ function createProjectApi(ctx) {
|
|
|
3158
3621
|
*/
|
|
3159
3622
|
const checkCompleteness = (opts) => completeness(ctx.global.projectDir, opts.target);
|
|
3160
3623
|
/**
|
|
3161
|
-
* Implements {@link Api.patchMobile}: hands Android its manifest entries
|
|
3624
|
+
* Implements {@link Api.patchMobile}: hands Android its manifest entries and
|
|
3625
|
+
* `app.backgroundColor` (the status bar icons follow it), then logs.
|
|
3162
3626
|
*
|
|
3163
3627
|
* @param opts - See {@link Api.patchMobile}.
|
|
3164
3628
|
* @returns See {@link Api.patchMobile}.
|
|
@@ -3169,7 +3633,7 @@ function createProjectApi(ctx) {
|
|
|
3169
3633
|
*/
|
|
3170
3634
|
const runPatchMobile = async (opts) => {
|
|
3171
3635
|
const manifest = opts.target === "android" ? androidManifestEntries(ctx.global) : [];
|
|
3172
|
-
const result = await patchMobile(ctx.global.projectDir, opts, ctx.global.signing, manifest);
|
|
3636
|
+
const result = await patchMobile(ctx.global.projectDir, opts, ctx.global.signing, manifest, ctx.global.app.backgroundColor);
|
|
3173
3637
|
ctx.log.info("project:patchMobile", {
|
|
3174
3638
|
target: opts.target,
|
|
3175
3639
|
patched: result.patched.length
|