Skip to content

Commit 29c17a0

Browse files
committed
Fix:fix android bundle upload error
1 parent bab6a9c commit 29c17a0

File tree

1 file changed

+72
-18
lines changed

1 file changed

+72
-18
lines changed

src/lib/react-native-utils.ts

+72-18
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,37 @@ export function getReactNativeProjectAppVersion(
7676
}
7777

7878
if (parsedPlist && parsedPlist.CFBundleShortVersionString) {
79+
if (/MARKETING_VERSION/i.test(parsedPlist.CFBundleShortVersionString)) {
80+
try {
81+
const xcodeProjectConfig =
82+
path.resolve(resolvedPlistFile, "../") +
83+
".xcodeproj/project.pbxproj";
84+
out.text(
85+
'Using xcodeProjectConfig version, file path "'
86+
.concat(xcodeProjectConfig, '".\n')
87+
);
88+
const xcodeContents = fs.readFileSync(xcodeProjectConfig).toString();
89+
const xcodeVersionRegex = /Release[\s\S]*MARKETING_VERSION = (\d+\.\d+\.\d+)/gm;
90+
let xcodeVersion;
91+
let match;
92+
93+
while ((match = xcodeVersionRegex.exec(xcodeContents)) !== null) {
94+
// 这个循环将重复执行,直到没有更多匹配项
95+
// match[1] 将包含第一个捕获组的值,即版本号
96+
xcodeVersion = match[1];
97+
break; // 因为我们只需要第一个匹配项,所以找到后就可以退出循环
98+
}
99+
out.text(
100+
'Using xcodeProjectConfig version, version "'.concat(
101+
xcodeVersion,
102+
'".\n'
103+
)
104+
);
105+
parsedPlist.CFBundleShortVersionString = xcodeVersion;
106+
} catch (error) {
107+
108+
}
109+
}
79110
if (isValidVersion(parsedPlist.CFBundleShortVersionString)) {
80111
out.text(
81112
`Using the target binary version value "${parsedPlist.CFBundleShortVersionString}" from "${resolvedPlistFile}".\n`,
@@ -334,7 +365,7 @@ export function runHermesEmitBinaryCommand(
334365
): Promise<void> {
335366
const hermesArgs: string[] = [];
336367
const envNodeArgs: string = process.env.CODE_PUSH_NODE_ARGS;
337-
368+
out.text(chalk.cyan('Converting JS bundle to byte code via Hermes, running command:\n'));
338369
if (typeof envNodeArgs !== 'undefined') {
339370
Array.prototype.push.apply(hermesArgs, envNodeArgs.trim().split(/\s+/));
340371
}
@@ -458,29 +489,41 @@ export function runHermesEmitBinaryCommand(
458489
});
459490
}
460491

461-
export function getHermesEnabled(gradleFile?: string): Promise<boolean> {
462-
let buildGradlePath: string = path.join('android', 'app');
463-
if (gradleFile) {
464-
buildGradlePath = gradleFile;
465-
}
466-
if (fs.lstatSync(buildGradlePath).isDirectory()) {
492+
export async function getHermesEnabled(gradleFile?: string): Promise<boolean> {
493+
let buildGradlePath = gradleFile ?? path.join('android', 'app');
494+
495+
if (await fileDoesNotExistOrIsDirectory(buildGradlePath)) {
467496
buildGradlePath = path.join(buildGradlePath, 'build.gradle');
468497
}
469498

470-
if (fileDoesNotExistOrIsDirectory(buildGradlePath)) {
499+
if (await fileDoesNotExistOrIsDirectory(buildGradlePath)) {
471500
throw new Error(`Unable to find gradle file "${buildGradlePath}".`);
472501
}
473502

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

486529
export function getiOSHermesEnabled(podFile: string): Promise<boolean> {
@@ -538,6 +581,17 @@ function getHermesCommand(): string {
538581
}
539582
};
540583
// assume if hermes-engine exists it should be used instead of hermesvm
584+
const hermesEngineNew = path.join(
585+
'node_modules',
586+
'react-native',
587+
'sdks',
588+
'hermesc',
589+
getHermesOSBin(),
590+
getHermesOSExe()
591+
);
592+
if (fileExists(hermesEngineNew)) {
593+
return hermesEngineNew;
594+
}
541595
const hermesEngine = path.join(
542596
'node_modules',
543597
'hermes-engine',

0 commit comments

Comments
 (0)