@hot-updater/android-helper 1.2.0-rc.46 → 1.2.0-rc.47

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/dist/index.cjs CHANGED
@@ -100,6 +100,54 @@ const formatCommand = (words, width = 74) => {
100
100
  return lines.join("\n");
101
101
  };
102
102
  //#endregion
103
+ //#region src/utils/windowsGradle.ts
104
+ /**
105
+ * The init script every Windows build runs with. ninja 1.10.2, which the
106
+ * Android SDK's CMake 3.22.1 ships, can't reach a path over MAX_PATH (260
107
+ * characters), and React Native's codegen object paths, which repeat the
108
+ * project's path, pass it. At 250, CMake's own default on Windows, CMake
109
+ * hashes the long part of an object's name. Only the names of objects
110
+ * under `.cxx` change, never what they compile to. A module whose CMake
111
+ * arguments set CMAKE_OBJECT_PATH_MAX, in its default config, a build type
112
+ * or a flavor, keeps its own.
113
+ */
114
+ const WINDOWS_INIT_SCRIPT = `// Written by hot-updater for one Windows build: object paths within MAX_PATH.
115
+ allprojects { project ->
116
+ ['com.android.application', 'com.android.library'].each { id ->
117
+ project.plugins.withId(id) {
118
+ project.extensions.getByName('androidComponents').finalizeDsl { android ->
119
+ def configs = [android.defaultConfig] + android.buildTypes.toList() + android.productFlavors.toList()
120
+ def own = configs.any { config ->
121
+ config.externalNativeBuild.cmake.arguments.any { it.startsWith('-DCMAKE_OBJECT_PATH_MAX') }
122
+ }
123
+ if (!own) {
124
+ android.defaultConfig.externalNativeBuild.cmake.arguments.add('-DCMAKE_OBJECT_PATH_MAX=250')
125
+ }
126
+ }
127
+ }
128
+ }
129
+ }
130
+ `;
131
+ /**
132
+ * Runs `build` with the Gradle arguments a Windows build needs: none
133
+ * elsewhere. The init script is written outside the project, and removed
134
+ * after.
135
+ */
136
+ const withWindowsGradleArgs = async (build) => {
137
+ if (!process.platform.startsWith("win")) return build([]);
138
+ const directory = await fs.default.promises.mkdtemp(path.default.join(os.default.tmpdir(), "hot-updater-windows-"));
139
+ try {
140
+ const script = path.default.join(directory, "windows.init.gradle");
141
+ await fs.default.promises.writeFile(script, WINDOWS_INIT_SCRIPT);
142
+ return await build(["--init-script", script]);
143
+ } finally {
144
+ await fs.default.promises.rm(directory, {
145
+ recursive: true,
146
+ force: true
147
+ });
148
+ }
149
+ };
150
+ //#endregion
103
151
  //#region src/builder/runGradle.ts
104
152
  /** Why Gradle failed, without its command line. */
105
153
  const describeFailure = (error) => {
@@ -116,9 +164,10 @@ function getTaskNames(moduleName, tasks) {
116
164
  return tasks.map((task) => `${moduleName}:${task}`);
117
165
  }
118
166
  /**
119
- * Get gradle wrapper command based on platform
167
+ * The project's Gradle wrapper. On Windows by its path: cmd.exe finds no
168
+ * program in the current directory with NoDefaultCurrentDirectoryInExePath.
120
169
  */
121
- const getGradleWrapper = () => process.platform.startsWith("win") ? "gradlew.bat" : "./gradlew";
170
+ const getGradleWrapper = (androidProjectPath) => process.platform.startsWith("win") ? path.default.join(androidProjectPath, "gradlew.bat") : "./gradlew";
122
171
  /** A variable a POSIX shell can't name, as the signing environment's are. */
123
172
  const isShellName = (name) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name);
124
173
  /**
@@ -127,8 +176,8 @@ const isShellName = (name) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name);
127
176
  * a shell name, such as `ORG_GRADLE_PROJECT_android.injected.signing.store.file`:
128
177
  * the build then signs with the project's own config. bash keeps them.
129
178
  */
130
- const gradleCommand = (env) => {
131
- const wrapper = getGradleWrapper();
179
+ const gradleCommand = (androidProjectPath, env) => {
180
+ const wrapper = getGradleWrapper(androidProjectPath);
132
181
  return wrapper === "./gradlew" && Object.keys(env ?? {}).some((name) => !isShellName(name)) ? ["bash", [wrapper]] : [wrapper, []];
133
182
  };
134
183
  /**
@@ -159,21 +208,27 @@ async function runGradleTasks({ tasks, logPrefix, args, env, androidProjectPath,
159
208
  `Project ${path.default.relative((0, _hot_updater_cli_tools.getCwd)(), androidProjectPath) || "."}`,
160
209
  `App module ${appModuleName}`,
161
210
  `Tasks ${tasks.join(", ")}`,
162
- `$ ${formatCommand([path.default.basename(getGradleWrapper()), ...gradleArgs])}`,
211
+ `$ ${formatCommand([path.default.basename(getGradleWrapper(androidProjectPath)), ...gradleArgs])}`,
163
212
  ""
164
213
  ]
165
214
  });
166
215
  let logFilePath = null;
167
216
  try {
168
- const [command, prefix] = gradleCommand(env);
169
- const gradle = (0, execa.execa)(command, [...prefix, ...gradleArgs], {
170
- cwd: androidProjectPath,
171
- env,
172
- all: true,
173
- buffer: false
217
+ const [command, prefix] = gradleCommand(androidProjectPath, env);
218
+ const [gradleResult, loggingResult] = await withWindowsGradleArgs((platformArgs) => {
219
+ const gradle = (0, execa.execa)(command, [
220
+ ...prefix,
221
+ ...gradleArgs,
222
+ ...platformArgs
223
+ ], {
224
+ cwd: androidProjectPath,
225
+ env,
226
+ all: true,
227
+ buffer: false
228
+ });
229
+ const outputStream = gradle.all ?? gradle.stdout;
230
+ return Promise.allSettled([gradle, outputStream ? logger.processStream(outputStream) : void 0]);
174
231
  });
175
- const outputStream = gradle.all ?? gradle.stdout;
176
- const [gradleResult, loggingResult] = await Promise.allSettled([gradle, outputStream ? logger.processStream(outputStream) : void 0]);
177
232
  if (gradleResult.status === "rejected") throw gradleResult.reason;
178
233
  if (loggingResult.status === "rejected") throw loggingResult.reason;
179
234
  logger.stop(`Built ${tasks.join(", ")}`);
@@ -360,6 +415,8 @@ const findAapt2 = (androidProjectPath) => {
360
415
  return fs.default.readdirSync(buildTools).sort(compareVersions).reverse().map((version) => path.default.join(buildTools, version, executable)).find((aapt2) => fs.default.existsSync(aapt2));
361
416
  };
362
417
  const RAW_MARKER = "\" (Raw: \"";
418
+ /** aapt2's output lines: aapt2.exe ends each with CRLF. */
419
+ const outputLines = (output) => output.split(/\r?\n/);
363
420
  /**
364
421
  * An attribute aapt2 prints as `="<value>" (Raw: "<value>")`, given the text
365
422
  * after `="`. aapt2 does not escape the value, so it is the prefix the rest
@@ -387,21 +444,23 @@ const readAttribute = (line, marker) => {
387
444
  * A `<meta-data>` value from `aapt2 dump xmltree` of the manifest: the
388
445
  * literal, or a resource reference such as `@string/channel`. null when the
389
446
  * manifest has none, and undefined when aapt2 printed one it cannot read
390
- * exactly.
447
+ * exactly, or a meta-data whose name it cannot read.
391
448
  */
392
449
  const parseManifestMetaData = (xmltree, name) => {
393
- const lines = xmltree.split("\n");
450
+ const lines = outputLines(xmltree);
451
+ let unreadable = false;
394
452
  for (let i = 0; i < lines.length; i++) {
395
453
  if (!/^\s*E: meta-data\b/.test(lines[i])) continue;
396
454
  let metaDataName = null;
397
455
  let value = null;
398
456
  for (let j = i + 1; j < lines.length && /^\s*A: /.test(lines[j]); j++) {
399
- metaDataName ||= readAttribute(lines[j], ":name(0x01010003)=");
457
+ if (metaDataName === null) metaDataName = readAttribute(lines[j], ":name(0x01010003)=");
400
458
  if (value === null) value = readAttribute(lines[j], ":value(0x01010024)=");
401
459
  }
402
460
  if (metaDataName === name) return value ?? void 0;
461
+ if (metaDataName === void 0) unreadable = true;
403
462
  }
404
- return null;
463
+ return unreadable ? void 0 : null;
405
464
  };
406
465
  /**
407
466
  * The `android:versionName` of the `<manifest>` element in `aapt2 dump
@@ -409,7 +468,7 @@ const parseManifestMetaData = (xmltree, name) => {
409
468
  * without one, and undefined when it cannot be read exactly.
410
469
  */
411
470
  const parseManifestVersionName = (xmltree) => {
412
- for (const line of xmltree.split("\n")) {
471
+ for (const line of outputLines(xmltree)) {
413
472
  const value = readAttribute(line, ":versionName(0x0101021c)=");
414
473
  if (value !== null) return value;
415
474
  }
@@ -429,7 +488,7 @@ const parseManifestVersionCode = (xmltree) => {
429
488
  * table has none aapt2 printed on one line.
430
489
  */
431
490
  const parseResourceString = (resources, reference) => {
432
- const lines = resources.split("\n");
491
+ const lines = outputLines(resources);
433
492
  const start = lines.findIndex((line) => {
434
493
  const [, id, name] = line.match(/^\s*resource (0x[0-9a-f]+) (\S+)/) ?? [];
435
494
  return id !== void 0 && (reference === `@${id}` || reference === `@${name}`);
package/dist/index.mjs CHANGED
@@ -72,6 +72,54 @@ const formatCommand = (words, width = 74) => {
72
72
  return lines.join("\n");
73
73
  };
74
74
  //#endregion
75
+ //#region src/utils/windowsGradle.ts
76
+ /**
77
+ * The init script every Windows build runs with. ninja 1.10.2, which the
78
+ * Android SDK's CMake 3.22.1 ships, can't reach a path over MAX_PATH (260
79
+ * characters), and React Native's codegen object paths, which repeat the
80
+ * project's path, pass it. At 250, CMake's own default on Windows, CMake
81
+ * hashes the long part of an object's name. Only the names of objects
82
+ * under `.cxx` change, never what they compile to. A module whose CMake
83
+ * arguments set CMAKE_OBJECT_PATH_MAX, in its default config, a build type
84
+ * or a flavor, keeps its own.
85
+ */
86
+ const WINDOWS_INIT_SCRIPT = `// Written by hot-updater for one Windows build: object paths within MAX_PATH.
87
+ allprojects { project ->
88
+ ['com.android.application', 'com.android.library'].each { id ->
89
+ project.plugins.withId(id) {
90
+ project.extensions.getByName('androidComponents').finalizeDsl { android ->
91
+ def configs = [android.defaultConfig] + android.buildTypes.toList() + android.productFlavors.toList()
92
+ def own = configs.any { config ->
93
+ config.externalNativeBuild.cmake.arguments.any { it.startsWith('-DCMAKE_OBJECT_PATH_MAX') }
94
+ }
95
+ if (!own) {
96
+ android.defaultConfig.externalNativeBuild.cmake.arguments.add('-DCMAKE_OBJECT_PATH_MAX=250')
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
102
+ `;
103
+ /**
104
+ * Runs `build` with the Gradle arguments a Windows build needs: none
105
+ * elsewhere. The init script is written outside the project, and removed
106
+ * after.
107
+ */
108
+ const withWindowsGradleArgs = async (build) => {
109
+ if (!process.platform.startsWith("win")) return build([]);
110
+ const directory = await fs.promises.mkdtemp(path.join(os.tmpdir(), "hot-updater-windows-"));
111
+ try {
112
+ const script = path.join(directory, "windows.init.gradle");
113
+ await fs.promises.writeFile(script, WINDOWS_INIT_SCRIPT);
114
+ return await build(["--init-script", script]);
115
+ } finally {
116
+ await fs.promises.rm(directory, {
117
+ recursive: true,
118
+ force: true
119
+ });
120
+ }
121
+ };
122
+ //#endregion
75
123
  //#region src/builder/runGradle.ts
76
124
  /** Why Gradle failed, without its command line. */
77
125
  const describeFailure = (error) => {
@@ -88,9 +136,10 @@ function getTaskNames(moduleName, tasks) {
88
136
  return tasks.map((task) => `${moduleName}:${task}`);
89
137
  }
90
138
  /**
91
- * Get gradle wrapper command based on platform
139
+ * The project's Gradle wrapper. On Windows by its path: cmd.exe finds no
140
+ * program in the current directory with NoDefaultCurrentDirectoryInExePath.
92
141
  */
93
- const getGradleWrapper = () => process.platform.startsWith("win") ? "gradlew.bat" : "./gradlew";
142
+ const getGradleWrapper = (androidProjectPath) => process.platform.startsWith("win") ? path.join(androidProjectPath, "gradlew.bat") : "./gradlew";
94
143
  /** A variable a POSIX shell can't name, as the signing environment's are. */
95
144
  const isShellName = (name) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name);
96
145
  /**
@@ -99,8 +148,8 @@ const isShellName = (name) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(name);
99
148
  * a shell name, such as `ORG_GRADLE_PROJECT_android.injected.signing.store.file`:
100
149
  * the build then signs with the project's own config. bash keeps them.
101
150
  */
102
- const gradleCommand = (env) => {
103
- const wrapper = getGradleWrapper();
151
+ const gradleCommand = (androidProjectPath, env) => {
152
+ const wrapper = getGradleWrapper(androidProjectPath);
104
153
  return wrapper === "./gradlew" && Object.keys(env ?? {}).some((name) => !isShellName(name)) ? ["bash", [wrapper]] : [wrapper, []];
105
154
  };
106
155
  /**
@@ -131,21 +180,27 @@ async function runGradleTasks({ tasks, logPrefix, args, env, androidProjectPath,
131
180
  `Project ${path.relative(getCwd(), androidProjectPath) || "."}`,
132
181
  `App module ${appModuleName}`,
133
182
  `Tasks ${tasks.join(", ")}`,
134
- `$ ${formatCommand([path.basename(getGradleWrapper()), ...gradleArgs])}`,
183
+ `$ ${formatCommand([path.basename(getGradleWrapper(androidProjectPath)), ...gradleArgs])}`,
135
184
  ""
136
185
  ]
137
186
  });
138
187
  let logFilePath = null;
139
188
  try {
140
- const [command, prefix] = gradleCommand(env);
141
- const gradle = execa(command, [...prefix, ...gradleArgs], {
142
- cwd: androidProjectPath,
143
- env,
144
- all: true,
145
- buffer: false
189
+ const [command, prefix] = gradleCommand(androidProjectPath, env);
190
+ const [gradleResult, loggingResult] = await withWindowsGradleArgs((platformArgs) => {
191
+ const gradle = execa(command, [
192
+ ...prefix,
193
+ ...gradleArgs,
194
+ ...platformArgs
195
+ ], {
196
+ cwd: androidProjectPath,
197
+ env,
198
+ all: true,
199
+ buffer: false
200
+ });
201
+ const outputStream = gradle.all ?? gradle.stdout;
202
+ return Promise.allSettled([gradle, outputStream ? logger.processStream(outputStream) : void 0]);
146
203
  });
147
- const outputStream = gradle.all ?? gradle.stdout;
148
- const [gradleResult, loggingResult] = await Promise.allSettled([gradle, outputStream ? logger.processStream(outputStream) : void 0]);
149
204
  if (gradleResult.status === "rejected") throw gradleResult.reason;
150
205
  if (loggingResult.status === "rejected") throw loggingResult.reason;
151
206
  logger.stop(`Built ${tasks.join(", ")}`);
@@ -332,6 +387,8 @@ const findAapt2 = (androidProjectPath) => {
332
387
  return fs.readdirSync(buildTools).sort(compareVersions).reverse().map((version) => path.join(buildTools, version, executable)).find((aapt2) => fs.existsSync(aapt2));
333
388
  };
334
389
  const RAW_MARKER = "\" (Raw: \"";
390
+ /** aapt2's output lines: aapt2.exe ends each with CRLF. */
391
+ const outputLines = (output) => output.split(/\r?\n/);
335
392
  /**
336
393
  * An attribute aapt2 prints as `="<value>" (Raw: "<value>")`, given the text
337
394
  * after `="`. aapt2 does not escape the value, so it is the prefix the rest
@@ -359,21 +416,23 @@ const readAttribute = (line, marker) => {
359
416
  * A `<meta-data>` value from `aapt2 dump xmltree` of the manifest: the
360
417
  * literal, or a resource reference such as `@string/channel`. null when the
361
418
  * manifest has none, and undefined when aapt2 printed one it cannot read
362
- * exactly.
419
+ * exactly, or a meta-data whose name it cannot read.
363
420
  */
364
421
  const parseManifestMetaData = (xmltree, name) => {
365
- const lines = xmltree.split("\n");
422
+ const lines = outputLines(xmltree);
423
+ let unreadable = false;
366
424
  for (let i = 0; i < lines.length; i++) {
367
425
  if (!/^\s*E: meta-data\b/.test(lines[i])) continue;
368
426
  let metaDataName = null;
369
427
  let value = null;
370
428
  for (let j = i + 1; j < lines.length && /^\s*A: /.test(lines[j]); j++) {
371
- metaDataName ||= readAttribute(lines[j], ":name(0x01010003)=");
429
+ if (metaDataName === null) metaDataName = readAttribute(lines[j], ":name(0x01010003)=");
372
430
  if (value === null) value = readAttribute(lines[j], ":value(0x01010024)=");
373
431
  }
374
432
  if (metaDataName === name) return value ?? void 0;
433
+ if (metaDataName === void 0) unreadable = true;
375
434
  }
376
- return null;
435
+ return unreadable ? void 0 : null;
377
436
  };
378
437
  /**
379
438
  * The `android:versionName` of the `<manifest>` element in `aapt2 dump
@@ -381,7 +440,7 @@ const parseManifestMetaData = (xmltree, name) => {
381
440
  * without one, and undefined when it cannot be read exactly.
382
441
  */
383
442
  const parseManifestVersionName = (xmltree) => {
384
- for (const line of xmltree.split("\n")) {
443
+ for (const line of outputLines(xmltree)) {
385
444
  const value = readAttribute(line, ":versionName(0x0101021c)=");
386
445
  if (value !== null) return value;
387
446
  }
@@ -401,7 +460,7 @@ const parseManifestVersionCode = (xmltree) => {
401
460
  * table has none aapt2 printed on one line.
402
461
  */
403
462
  const parseResourceString = (resources, reference) => {
404
- const lines = resources.split("\n");
463
+ const lines = outputLines(resources);
405
464
  const start = lines.findIndex((line) => {
406
465
  const [, id, name] = line.match(/^\s*resource (0x[0-9a-f]+) (\S+)/) ?? [];
407
466
  return id !== void 0 && (reference === `@${id}` || reference === `@${name}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hot-updater/android-helper",
3
- "version": "1.2.0-rc.46",
3
+ "version": "1.2.0-rc.47",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -30,9 +30,9 @@
30
30
  "execa": "9.5.2",
31
31
  "fast-xml-parser": "5.7.2",
32
32
  "jszip": "^3.10.1",
33
- "@hot-updater/cli-tools": "1.2.0-rc.46",
34
- "@hot-updater/plugin-core": "1.2.0-rc.46",
35
- "@hot-updater/protocol": "1.2.0-rc.46"
33
+ "@hot-updater/cli-tools": "1.2.0-rc.47",
34
+ "@hot-updater/protocol": "1.2.0-rc.47",
35
+ "@hot-updater/plugin-core": "1.2.0-rc.47"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/node": "^20"