diff --git a/package.json b/package.json index 5b73c7b59..81b2c1bc2 100644 --- a/package.json +++ b/package.json @@ -483,6 +483,12 @@ "title": "Run EAS Build", "category": "React Native", "enablement": "!config.security.workspace.trust.enabled || isWorkspaceTrusted" + }, + { + "command": "reactNative.buildPrecompiledIOS", + "title": "Build iOS Precompiled (React Native)", + "category": "React Native", + "enablement": "!config.security.workspace.trust.enabled || isWorkspaceTrusted" } ], "menus": { diff --git a/src/common/error/errorStrings.ts b/src/common/error/errorStrings.ts index 201f1e0b1..57a0362a6 100644 --- a/src/common/error/errorStrings.ts +++ b/src/common/error/errorStrings.ts @@ -426,4 +426,5 @@ export const ERROR_STRINGS = { [InternalErrorCode.FaiedToSetNewArch]: "Failed to set New Architecture", [InternalErrorCode.FailedToToggleNetworkView]: "Failed to config network view", [InternalErrorCode.FailedToRunEasBuild]: "Failed to run eas build", + [InternalErrorCode.FailedToBuildPrecompiledIOS]: "Failed to build Precompiled IOS", }; diff --git a/src/common/error/internalErrorCode.ts b/src/common/error/internalErrorCode.ts index 2b7e18aa0..18d2dda9f 100644 --- a/src/common/error/internalErrorCode.ts +++ b/src/common/error/internalErrorCode.ts @@ -39,6 +39,7 @@ export enum InternalErrorCode { FaiedToSetNewArch = 134, FailedToToggleNetworkView = 135, FailedToRunEasBuild = 136, + FailedToBuildPrecompiledIOS = 137, // Device Deployer errors IOSDeployNotFound = 201, diff --git a/src/extension/commands/buildPrecompiledIos.ts b/src/extension/commands/buildPrecompiledIos.ts new file mode 100644 index 000000000..8265f548d --- /dev/null +++ b/src/extension/commands/buildPrecompiledIos.ts @@ -0,0 +1,72 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +import { spawn } from "child_process"; +import * as os from "os"; +import * as vscode from "vscode"; +import { ErrorHelper } from "../../common/error/errorHelper"; +import { InternalErrorCode } from "../../common/error/internalErrorCode"; +import { ReactNativeCommand } from "./util/reactNativeCommand"; +// Removed unused import + +export class buildPrecompiledIOS extends ReactNativeCommand { + codeName = "buildPrecompiledIOS"; + label = "Build Precompiled iOS Bundle"; + error = ErrorHelper.getInternalError( + InternalErrorCode.CommandFailed, + "ReactNativeBuildPrecompiledIOS", + "React Native: Build Precompiled iOS Bundle", + ); + + async baseFn(p0: { + iosFolder: string; + useBundler: boolean; // 如果项目用 Bundler 管理 Pods,改为 true + useRnDep: boolean; + usePrebuiltRnCore: boolean; + timeoutMs: number; + }): Promise { + if (os.platform() !== "darwin") { + void vscode.window.showErrorMessage("iOS pod install 仅支持在 macOS 上运行。"); + throw new Error("PlatformNotSupported: iOS pod install requires macOS."); + } + + await this.baseFn({ + iosFolder: "ios", + useBundler: false, // 如果项目用 Bundler 管理 Pods,改为 true + useRnDep: true, + usePrebuiltRnCore: true, + timeoutMs: 15 * 60 * 1000, // CI 中可适当加大或减小 + }); + + // 创建输出通道 + const output = vscode.window.createOutputChannel("React Native iOS Precompiled Build"); + output.show(true); + + // 设置环境变量 + const env = { ...process.env, RCT_USE_PREBUILT_RNCORE: "1" }; + + output.appendLine("[Info] 已设置环境变量:RCT_USE_PREBUILT_RNCORE=1"); + output.appendLine("[Info] 开始执行 pod install..."); + + // 执行 pod install + await new Promise((resolve, reject) => { + const child = spawn("pod", ["install"], { + cwd: `${vscode.workspace.workspaceFolders?.[0].uri.fsPath}/ios`, + env, + shell: true, + }); + + child.stdout.on("data", data => output.append(data.toString())); + child.stderr.on("data", data => output.append(data.toString())); + + child.on("close", code => { + if (code === 0) { + output.appendLine("[Success] pod install 完成 ✅"); + resolve(); + } else { + reject(new Error(`pod install 失败,退出码:${code}`)); + } + }); + }); + } +} diff --git a/src/extension/commands/index.ts b/src/extension/commands/index.ts index beeb93d39..ee55e6d02 100644 --- a/src/extension/commands/index.ts +++ b/src/extension/commands/index.ts @@ -38,3 +38,4 @@ export * from "./killPort"; export * from "./setNewArch"; export * from "./networkView"; export * from "./runEasBuild"; +export * from "./buildPrecompiledIos";