microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/plistBuddy.ts
355lines · modeblame
0a68f8dbArtem Egorov8 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
bad42581EzioLi3 years ago | 3 | |
0a68f8dbArtem Egorov8 years ago | 4 | import * as path from "path"; |
a949e43fRuslan Bikkinin7 years ago | 5 | import * as fs from "fs"; |
09f6024fHeniker4 years ago | 6 | import * as glob from "glob"; |
a949e43fRuslan Bikkinin7 years ago | 7 | import * as semver from "semver"; |
b3753d4eRedMickey6 years ago | 8 | import { Node } from "../../common/node/node"; |
| 9 | import { ChildProcess } from "../../common/node/childProcess"; | |
d7d405aeYuri Skorokhodov7 years ago | 10 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 11 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; | |
e3706a1cRedMickey6 years ago | 12 | import { ProjectVersionHelper } from "../../common/projectVersionHelper"; |
de838bbfJiglioNero6 years ago | 13 | import { getFileNameWithoutExtension } from "../../common/utils"; |
2d8af448Yuri Skorokhodov6 years ago | 14 | import customRequire from "../../common/customRequire"; |
1c2424f4RedMickey5 years ago | 15 | import { PlatformType } from "../launchArgs"; |
4dfb1c4cetatanova5 years ago | 16 | import { AppLauncher } from "../appLauncher"; |
7a4506dclexie0111 years ago | 17 | import { SettingsHelper } from "../settingsHelper"; |
de838bbfJiglioNero6 years ago | 18 | |
| 19 | export interface ConfigurationData { | |
| 20 | fullProductName: string; | |
| 21 | configurationFolder: string; | |
| 22 | } | |
1c2424f4RedMickey5 years ago | 23 | export interface IOSBuildLocationData { |
| 24 | executable: string; | |
| 25 | configurationFolder: string; | |
| 26 | } | |
0a68f8dbArtem Egorov8 years ago | 27 | export class PlistBuddy { |
47927908RedMickey4 years ago | 28 | private static readonly plistBuddyExecutable = "/usr/libexec/PlistBuddy"; |
| 29 | private static readonly SCHEME_IN_PRODUCTS_FOLDER_PATH_VERSION = "0.59.0"; | |
| 30 | private static readonly NEW_RN_IOS_CLI_LOCATION_VERSION = "0.60.0"; | |
8f9463c2Samriel3 years ago | 31 | private static readonly RN69_FUND_XCODE_PROJECT_LOCATION_VERSION = "0.69.0"; |
4c1a7831lexie0112 years ago | 32 | private static readonly RN_VERSION_CLI_PLATFORM_APPLE = "0.74.0"; |
1bac57dblexie0111 years ago | 33 | private static readonly RN_VERSION_CLI_CONFIG_APPLE = "0.76.2"; |
de838bbfJiglioNero6 years ago | 34 | private readonly TARGET_BUILD_DIR_SEARCH_KEY = "TARGET_BUILD_DIR"; |
| 35 | private readonly FULL_PRODUCT_NAME_SEARCH_KEY = "FULL_PRODUCT_NAME"; | |
0a68f8dbArtem Egorov8 years ago | 36 | private nodeChildProcess: ChildProcess; |
34472878RedMickey5 years ago | 37 | constructor({ nodeChildProcess = new Node.ChildProcess() } = {}) { |
0a68f8dbArtem Egorov8 years ago | 38 | this.nodeChildProcess = nodeChildProcess; |
| 39 | } | |
0d77292aJiglioNero4 years ago | 40 | public async getBundleId( |
1c2424f4RedMickey5 years ago | 41 | platformProjectRoot: string, |
34472878RedMickey5 years ago | 42 | projectRoot: string, |
1c2424f4RedMickey5 years ago | 43 | platform: PlatformType.iOS | PlatformType.macOS, |
34472878RedMickey5 years ago | 44 | simulator: boolean = true, |
| 45 | configuration: string = "Debug", | |
| 46 | productName?: string, | |
| 47 | scheme?: string, | |
| 48 | ): Promise<string> { | |
0d77292aJiglioNero4 years ago | 49 | const iOSBuildLocationData = await this.getExecutableAndConfigurationFolder( |
1c2424f4RedMickey5 years ago | 50 | platformProjectRoot, |
| 51 | projectRoot, | |
| 52 | platform, | |
| 53 | simulator, | |
| 54 | configuration, | |
| 55 | productName, | |
| 56 | scheme, | |
0d77292aJiglioNero4 years ago | 57 | ); |
| 58 | const infoPlistPath = path.join( | |
| 59 | iOSBuildLocationData.configurationFolder, | |
| 60 | iOSBuildLocationData.executable, | |
| 61 | platform === PlatformType.iOS ? "Info.plist" : path.join("Contents", "Info.plist"), | |
| 62 | ); | |
| 63 | return await this.invokePlistBuddy("Print:CFBundleIdentifier", infoPlistPath); | |
1c2424f4RedMickey5 years ago | 64 | } |
0d77292aJiglioNero4 years ago | 65 | public async getExecutableAndConfigurationFolder( |
1c2424f4RedMickey5 years ago | 66 | platformProjectRoot: string, |
| 67 | projectRoot: string, | |
| 68 | platform: PlatformType.iOS | PlatformType.macOS, | |
| 69 | simulator: boolean = true, | |
| 70 | configuration: string = "Debug", | |
| 71 | productName?: string, | |
| 72 | scheme?: string, | |
| 73 | ): Promise<IOSBuildLocationData> { | |
0d77292aJiglioNero4 years ago | 74 | const rnVersions = await ProjectVersionHelper.getReactNativeVersions(projectRoot); |
| 75 | let productsFolder; | |
47927908RedMickey4 years ago | 76 | if ( |
| 77 | semver.gte( | |
| 78 | rnVersions.reactNativeVersion, | |
| 79 | PlistBuddy.SCHEME_IN_PRODUCTS_FOLDER_PATH_VERSION, | |
| 80 | ) || | |
| 81 | ProjectVersionHelper.isCanaryVersion(rnVersions.reactNativeVersion) | |
| 82 | ) { | |
0d77292aJiglioNero4 years ago | 83 | if (!scheme) { |
| 84 | // If no scheme were provided via runOptions.scheme or via runArguments then try to get scheme using the way RN CLI does. | |
| 85 | scheme = this.getInferredScheme( | |
| 86 | platformProjectRoot, | |
| 87 | projectRoot, | |
| 88 | rnVersions.reactNativeVersion, | |
| 89 | ); | |
| 90 | if (platform === PlatformType.macOS) { | |
09f6024fHeniker4 years ago | 91 | scheme = `${scheme}-macOS`; |
a949e43fRuslan Bikkinin7 years ago | 92 | } |
0d77292aJiglioNero4 years ago | 93 | } |
| 94 | productsFolder = path.join(platformProjectRoot, "build", scheme, "Build", "Products"); | |
| 95 | } else { | |
| 96 | productsFolder = path.join(platformProjectRoot, "build", "Build", "Products"); | |
| 97 | } | |
09f6024fHeniker4 years ago | 98 | const sdkType = |
0d77292aJiglioNero4 years ago | 99 | platform === PlatformType.iOS ? this.getSdkType(simulator, scheme) : undefined; |
| 100 | let configurationFolder = path.join( | |
| 101 | productsFolder, | |
| 102 | `${configuration}${sdkType ? `-${sdkType}` : ""}`, | |
| 103 | ); | |
| 104 | let executable = ""; | |
| 105 | if (productName) { | |
| 106 | executable = `${productName}.app`; | |
| 107 | if (!fs.existsSync(path.join(configurationFolder, executable))) { | |
| 108 | const configurationData = this.getConfigurationData( | |
| 109 | projectRoot, | |
| 110 | rnVersions.reactNativeVersion, | |
1c2424f4RedMickey5 years ago | 111 | platformProjectRoot, |
0d77292aJiglioNero4 years ago | 112 | configuration, |
1c2424f4RedMickey5 years ago | 113 | scheme, |
0d77292aJiglioNero4 years ago | 114 | configurationFolder, |
| 115 | sdkType, | |
1c2424f4RedMickey5 years ago | 116 | ); |
0d77292aJiglioNero4 years ago | 117 | configurationFolder = configurationData.configurationFolder; |
db6fd42aRuslan Bikkinin7 years ago | 118 | } |
0d77292aJiglioNero4 years ago | 119 | } else { |
| 120 | const executableList = this.findExecutable(configurationFolder); | |
| 121 | if (!executableList.length) { | |
| 122 | const configurationData_1 = this.getConfigurationData( | |
| 123 | projectRoot, | |
| 124 | rnVersions.reactNativeVersion, | |
| 125 | platformProjectRoot, | |
| 126 | configuration, | |
| 127 | scheme, | |
| 128 | configurationFolder, | |
| 129 | sdkType, | |
| 130 | ); | |
| 131 | configurationFolder = configurationData_1.configurationFolder; | |
| 132 | executableList.push(configurationData_1.fullProductName); | |
| 133 | } else if (executableList.length > 1) { | |
| 134 | throw ErrorHelper.getInternalError( | |
| 135 | InternalErrorCode.IOSFoundMoreThanOneExecutablesCleanupBuildFolder, | |
| 136 | configurationFolder, | |
| 137 | ); | |
| 138 | } | |
| 139 | executable = `${executableList[0]}`; | |
| 140 | } | |
| 141 | return { | |
| 142 | executable, | |
| 143 | configurationFolder, | |
| 144 | }; | |
0a68f8dbArtem Egorov8 years ago | 145 | } |
0d77292aJiglioNero4 years ago | 146 | public async setPlistProperty( |
| 147 | plistFile: string, | |
| 148 | property: string, | |
| 149 | value: string, | |
| 150 | ): Promise<void> { | |
0a68f8dbArtem Egorov8 years ago | 151 | // Attempt to set the value, and if it fails due to the key not existing attempt to create the key |
0d77292aJiglioNero4 years ago | 152 | try { |
| 153 | await this.invokePlistBuddy(`Set ${property} ${value}`, plistFile); | |
| 154 | } catch (e) { | |
| 155 | await this.invokePlistBuddy(`Add ${property} string ${value}`, plistFile); | |
| 156 | } | |
0a68f8dbArtem Egorov8 years ago | 157 | } |
0d77292aJiglioNero4 years ago | 158 | public async setPlistBooleanProperty( |
34472878RedMickey5 years ago | 159 | plistFile: string, |
| 160 | property: string, | |
| 161 | value: boolean, | |
| 162 | ): Promise<void> { | |
0a68f8dbArtem Egorov8 years ago | 163 | // Attempt to set the value, and if it fails due to the key not existing attempt to create the key |
0d77292aJiglioNero4 years ago | 164 | try { |
09f6024fHeniker4 years ago | 165 | await this.invokePlistBuddy(`Set ${property} ${String(value)}`, plistFile); |
0d77292aJiglioNero4 years ago | 166 | } catch (e) { |
09f6024fHeniker4 years ago | 167 | await this.invokePlistBuddy(`Add ${property} bool ${String(value)}`, plistFile); |
0d77292aJiglioNero4 years ago | 168 | } |
0a68f8dbArtem Egorov8 years ago | 169 | } |
0d77292aJiglioNero4 years ago | 170 | public async deletePlistProperty(plistFile: string, property: string): Promise<void> { |
| 171 | try { | |
| 172 | await this.invokePlistBuddy(`Delete ${property}`, plistFile); | |
| 173 | } catch (err) { | |
| 174 | if (!err.toString().toLowerCase().includes("does not exist")) { | |
| 175 | throw err; | |
| 176 | } | |
| 177 | } | |
0a68f8dbArtem Egorov8 years ago | 178 | } |
ce5e88eeYuri Skorokhodov5 years ago | 179 | public readPlistProperty(plistFile: string, property: string): Promise<string> { |
0a68f8dbArtem Egorov8 years ago | 180 | return this.invokePlistBuddy(`Print ${property}`, plistFile); |
| 181 | } | |
de838bbfJiglioNero6 years ago | 182 | public getBuildPathAndProductName( |
1c2424f4RedMickey5 years ago | 183 | platformProjectRoot: string, |
b3753d4eRedMickey6 years ago | 184 | projectWorkspaceConfigName: string, |
| 185 | configuration: string, | |
| 186 | scheme: string, | |
1c2424f4RedMickey5 years ago | 187 | sdkType?: string, |
de838bbfJiglioNero6 years ago | 188 | ): ConfigurationData { |
1c2424f4RedMickey5 years ago | 189 | const xcodebuildParams = ["-workspace", projectWorkspaceConfigName, "-scheme", scheme]; |
| 190 | if (sdkType) { | |
| 191 | xcodebuildParams.push("-sdk", sdkType); | |
| 192 | } | |
| 193 | xcodebuildParams.push("-configuration", configuration, "-showBuildSettings"); | |
| 194 | const buildSettings = this.nodeChildProcess.execFileSync("xcodebuild", xcodebuildParams, { | |
| 195 | encoding: "utf8", | |
| 196 | cwd: platformProjectRoot, | |
| 197 | }); | |
34472878RedMickey5 years ago | 198 | const targetBuildDir = this.fetchParameterFromBuildSettings( |
| 199 | <string>buildSettings, | |
| 200 | this.TARGET_BUILD_DIR_SEARCH_KEY, | |
| 201 | ); | |
| 202 | const fullProductName = this.fetchParameterFromBuildSettings( | |
| 203 | <string>buildSettings, | |
| 204 | this.FULL_PRODUCT_NAME_SEARCH_KEY, | |
| 205 | ); | |
b3753d4eRedMickey6 years ago | 206 | if (!targetBuildDir) { |
| 207 | throw new Error("Failed to get the target build directory."); | |
| 208 | } | |
de838bbfJiglioNero6 years ago | 209 | if (!fullProductName) { |
| 210 | throw new Error("Failed to get full product name."); | |
| 211 | } | |
| 212 | return { | |
| 213 | fullProductName, | |
| 214 | configurationFolder: targetBuildDir, | |
| 215 | }; | |
b3753d4eRedMickey6 years ago | 216 | } |
34472878RedMickey5 years ago | 217 | public getInferredScheme( |
1c2424f4RedMickey5 years ago | 218 | platformProjectRoot: string, |
34472878RedMickey5 years ago | 219 | projectRoot: string, |
| 220 | rnVersion: string, | |
| 221 | ): string { | |
| 222 | const projectWorkspaceConfigName = this.getProjectWorkspaceConfigName( | |
1c2424f4RedMickey5 years ago | 223 | platformProjectRoot, |
34472878RedMickey5 years ago | 224 | projectRoot, |
| 225 | rnVersion, | |
| 226 | ); | |
de838bbfJiglioNero6 years ago | 227 | return getFileNameWithoutExtension(projectWorkspaceConfigName); |
| 228 | } | |
cc826bdaRedMickey5 years ago | 229 | public getSdkType(simulator: boolean, scheme?: string): string { |
9cde42d9Michele Bonazza5 years ago | 230 | const sdkSuffix = simulator ? "simulator" : "os"; |
| 231 | const deviceType = | |
| 232 | (scheme?.toLowerCase().indexOf("tvos") ?? -1) > -1 ? "appletv" : "iphone"; | |
| 233 | return `${deviceType}${sdkSuffix}`; | |
| 234 | } | |
34472878RedMickey5 years ago | 235 | public getProjectWorkspaceConfigName( |
1c2424f4RedMickey5 years ago | 236 | platformProjectRoot: string, |
34472878RedMickey5 years ago | 237 | projectRoot: string, |
| 238 | rnVersion: string, | |
| 239 | ): string { | |
3021756bYuri Skorokhodov6 years ago | 240 | // Portion of code was taken from https://github.com/react-native-community/cli/blob/master/packages/platform-ios/src/commands/runIOS/index.js |
a949e43fRuslan Bikkinin7 years ago | 241 | // and modified a little bit |
| 242 | /** | |
| 243 | * Copyright (c) Facebook, Inc. and its affiliates. | |
| 244 | * | |
| 245 | * This source code is licensed under the MIT license found in the | |
| 246 | * LICENSE file in the root directory of this source tree. | |
| 247 | * | |
| 248 | * @flow | |
| 249 | * @format | |
| 250 | */ | |
4c1a7831lexie0112 years ago | 251 | |
01f9e3d8lexie0111 years ago | 252 | const nodeModulesRoot: string = AppLauncher.getNodeModulesRootByProjectPath(projectRoot); |
| 253 | | |
4c1a7831lexie0112 years ago | 254 | const iOSCliPlatform = semver.gte(rnVersion, PlistBuddy.RN_VERSION_CLI_PLATFORM_APPLE) |
1bac57dblexie0111 years ago | 255 | ? semver.gte(rnVersion, PlistBuddy.RN_VERSION_CLI_CONFIG_APPLE) |
| 256 | ? "cli-config-apple" | |
| 257 | : "cli-platform-apple" | |
4c1a7831lexie0112 years ago | 258 | : "cli-platform-ios"; |
47927908RedMickey4 years ago | 259 | const iOSCliFolderName = |
| 260 | semver.gte(rnVersion, PlistBuddy.NEW_RN_IOS_CLI_LOCATION_VERSION) || | |
| 261 | ProjectVersionHelper.isCanaryVersion(rnVersion) | |
4c1a7831lexie0112 years ago | 262 | ? iOSCliPlatform |
47927908RedMickey4 years ago | 263 | : "cli"; |
01f9e3d8lexie0111 years ago | 264 | |
| 265 | let findXcodeBase = "node_modules/@react-native-community"; | |
| 266 | | |
| 267 | const pnpmProjectPath = path.resolve(nodeModulesRoot, "node_modules", ".pnpm"); | |
| 268 | | |
| 269 | const isPnpmProject = | |
| 270 | fs.existsSync(pnpmProjectPath) && SettingsHelper.getPackageManager() === "pnpm"; | |
| 271 | if (isPnpmProject) { | |
| 272 | const modules = fs.readdirSync(pnpmProjectPath); | |
| 273 | const regex = new RegExp(`\@react-native-community\\+${iOSCliFolderName}@`); | |
| 274 | const communityModule = modules.find(module => regex.test(module)); | |
| 275 | if (communityModule) { | |
| 276 | findXcodeBase = path.join( | |
| 277 | "node_modules", | |
| 278 | ".pnpm", | |
| 279 | communityModule, | |
| 280 | "node_modules", | |
| 281 | "@react-native-community", | |
| 282 | ); | |
| 283 | } | |
| 284 | } | |
| 285 | | |
| 286 | const findXcodeProjectLocation = `${findXcodeBase}/${iOSCliFolderName}/build/${ | |
8f9463c2Samriel3 years ago | 287 | semver.gte(rnVersion, PlistBuddy.RN69_FUND_XCODE_PROJECT_LOCATION_VERSION) |
| 288 | ? "config/findXcodeProject" | |
| 289 | : "commands/runIOS/findXcodeProject" | |
| 290 | }`; | |
01f9e3d8lexie0111 years ago | 291 | |
34472878RedMickey5 years ago | 292 | const findXcodeProject = customRequire( |
01f9e3d8lexie0111 years ago | 293 | path.join(nodeModulesRoot, findXcodeProjectLocation), |
34472878RedMickey5 years ago | 294 | ).default; |
1c2424f4RedMickey5 years ago | 295 | const xcodeProject = findXcodeProject(fs.readdirSync(platformProjectRoot)); |
a949e43fRuslan Bikkinin7 years ago | 296 | if (!xcodeProject) { |
1c2424f4RedMickey5 years ago | 297 | throw new Error( |
| 298 | `Could not find Xcode project files in "${platformProjectRoot}" folder`, | |
| 299 | ); | |
a949e43fRuslan Bikkinin7 years ago | 300 | } |
de838bbfJiglioNero6 years ago | 301 | return xcodeProject.name; |
| 302 | } | |
| 303 | public getConfigurationData( | |
| 304 | projectRoot: string, | |
| 305 | reactNativeVersion: string, | |
1c2424f4RedMickey5 years ago | 306 | platformProjectRoot: string, |
de838bbfJiglioNero6 years ago | 307 | configuration: string, |
| 308 | scheme: string | undefined, | |
34472878RedMickey5 years ago | 309 | oldConfigurationFolder: string, |
1c2424f4RedMickey5 years ago | 310 | sdkType?: string, |
de838bbfJiglioNero6 years ago | 311 | ): ConfigurationData { |
| 312 | if (!scheme) { | |
34472878RedMickey5 years ago | 313 | throw ErrorHelper.getInternalError( |
| 314 | InternalErrorCode.IOSCouldNotFoundExecutableInFolder, | |
| 315 | oldConfigurationFolder, | |
| 316 | ); | |
de838bbfJiglioNero6 years ago | 317 | } |
34472878RedMickey5 years ago | 318 | const projectWorkspaceConfigName = this.getProjectWorkspaceConfigName( |
1c2424f4RedMickey5 years ago | 319 | platformProjectRoot, |
34472878RedMickey5 years ago | 320 | projectRoot, |
| 321 | reactNativeVersion, | |
| 322 | ); | |
de838bbfJiglioNero6 years ago | 323 | return this.getBuildPathAndProductName( |
1c2424f4RedMickey5 years ago | 324 | platformProjectRoot, |
de838bbfJiglioNero6 years ago | 325 | projectWorkspaceConfigName, |
| 326 | configuration, | |
| 327 | scheme, | |
34472878RedMickey5 years ago | 328 | sdkType, |
a949e43fRuslan Bikkinin7 years ago | 329 | ); |
| 330 | } | |
b3753d4eRedMickey6 years ago | 331 | /** |
| 332 | * @param {string} buildSettings | |
de838bbfJiglioNero6 years ago | 333 | * @param {string} parameterName |
b3753d4eRedMickey6 years ago | 334 | * @returns {string | null} |
| 335 | */ | |
34472878RedMickey5 years ago | 336 | public fetchParameterFromBuildSettings( |
| 337 | buildSettings: string, | |
| 338 | parameterName: string, | |
| 339 | ): string | null { | |
de838bbfJiglioNero6 years ago | 340 | const targetBuildMatch = new RegExp(`${parameterName} = (.+)$`, "m").exec(buildSettings); |
34472878RedMickey5 years ago | 341 | return targetBuildMatch && targetBuildMatch[1] ? targetBuildMatch[1].trim() : null; |
b3753d4eRedMickey6 years ago | 342 | } |
db6fd42aRuslan Bikkinin7 years ago | 343 | private findExecutable(folder: string): string[] { |
| 344 | return glob.sync("*.app", { | |
| 345 | cwd: folder, | |
| 346 | }); | |
| 347 | } | |
0d77292aJiglioNero4 years ago | 348 | private async invokePlistBuddy(command: string, plistFile: string): Promise<string> { |
| 349 | const res = await this.nodeChildProcess.exec( | |
| 350 | `${PlistBuddy.plistBuddyExecutable} -c '${command}' '${plistFile}'`, | |
| 351 | ); | |
| 352 | const outcome = await res.outcome; | |
| 353 | return outcome.toString().trim(); | |
0a68f8dbArtem Egorov8 years ago | 354 | } |
| 355 | } |