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