Skip to content

Commit bf71c79

Browse files
authored
Merge branch 'master' into master
2 parents 6cf8d05 + 29c17a0 commit bf71c79

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/lib/react-native-utils.ts

+41-19
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,17 @@ export function getReactNativeProjectAppVersion(
8686
.concat(xcodeProjectConfig, '".\n')
8787
);
8888
const xcodeContents = fs.readFileSync(xcodeProjectConfig).toString();
89-
const xcodeVersion = [...xcodeContents.matchAll(/Release[\s\S]*MARKETING_VERSION = (\d+\.\d+\.\d+)/gm)][0][1];
89+
90+
const xcodeVersionRegex = /Release[\s\S]*MARKETING_VERSION = (\d+\.\d+\.\d+)/gm;
91+
let xcodeVersion;
92+
let match;
93+
94+
while ((match = xcodeVersionRegex.exec(xcodeContents)) !== null) {
95+
// 这个循环将重复执行,直到没有更多匹配项
96+
// match[1] 将包含第一个捕获组的值,即版本号
97+
xcodeVersion = match[1];
98+
break; // 因为我们只需要第一个匹配项,所以找到后就可以退出循环
99+
}
90100
out.text(
91101
'Using xcodeProjectConfig version, version "'.concat(
92102
xcodeVersion,
@@ -356,7 +366,7 @@ export function runHermesEmitBinaryCommand(
356366
): Promise<void> {
357367
const hermesArgs: string[] = [];
358368
const envNodeArgs: string = process.env.CODE_PUSH_NODE_ARGS;
359-
369+
out.text(chalk.cyan('Converting JS bundle to byte code via Hermes, running command:\n'));
360370
if (typeof envNodeArgs !== 'undefined') {
361371
Array.prototype.push.apply(hermesArgs, envNodeArgs.trim().split(/\s+/));
362372
}
@@ -480,29 +490,41 @@ export function runHermesEmitBinaryCommand(
480490
});
481491
}
482492

483-
export function getHermesEnabled(gradleFile?: string): Promise<boolean> {
484-
let buildGradlePath: string = path.join('android', 'app');
485-
if (gradleFile) {
486-
buildGradlePath = gradleFile;
487-
}
488-
if (fs.lstatSync(buildGradlePath).isDirectory()) {
493+
export async function getHermesEnabled(gradleFile?: string): Promise<boolean> {
494+
let buildGradlePath = gradleFile ?? path.join('android', 'app');
495+
496+
if (await fileDoesNotExistOrIsDirectory(buildGradlePath)) {
489497
buildGradlePath = path.join(buildGradlePath, 'build.gradle');
490498
}
491499

492-
if (fileDoesNotExistOrIsDirectory(buildGradlePath)) {
500+
if (await fileDoesNotExistOrIsDirectory(buildGradlePath)) {
493501
throw new Error(`Unable to find gradle file "${buildGradlePath}".`);
494502
}
495503

496-
return g2js
497-
.parseFile(buildGradlePath)
498-
.catch(() => {
499-
throw new Error(
500-
`Unable to parse the "${buildGradlePath}" file. Please ensure it is a well-formed Gradle file.`,
501-
);
502-
})
503-
.then((buildGradle: any) => {
504-
return Array.from(buildGradle['project.ext.react']).includes('enableHermes: true');
505-
});
504+
let gradlePropertyPath: string = path.join('android', 'gradle.properties');
505+
506+
if (await fileDoesNotExistOrIsDirectory(gradlePropertyPath)) {
507+
throw new Error(`Unable to find gradle file "${gradlePropertyPath}".`);
508+
}
509+
510+
try {
511+
const buildGradle = await g2js.parseFile(buildGradlePath);
512+
const buildProperty = await g2js.parseFile(gradlePropertyPath);
513+
514+
const hermesPropertyEnabled = buildProperty.hermesEnabled ?? false;
515+
let hermesBuildEnabled = false;
516+
517+
// 如果buildGradle["project.ext.react"]是一个数组,则继续处理
518+
if (Array.isArray(buildGradle["project.ext.react"])) {
519+
const reactSettings: string[] = buildGradle["project.ext.react"];
520+
hermesBuildEnabled = reactSettings.some(line => /^enableHermes\s*:\s*true/.test(line));
521+
}
522+
523+
return hermesPropertyEnabled || hermesBuildEnabled;
524+
} catch (error) {
525+
// error.message 可能需要根据实际在 try 块中发生的错误进行调整
526+
throw new Error(`An error occurred while processing the Gradle files: ${error.message}`);
527+
}
506528
}
507529

508530
export function getiOSHermesEnabled(podFile: string): Promise<boolean> {

0 commit comments

Comments
 (0)