microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.14

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/ios/plistBuddy.ts

78lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as path from "path";
5import * as Q from "q";
6import * as glob from "glob";
7
8import {Node} from "../../common/node/node";
9import {ChildProcess} from "../../common/node/childProcess";
10
11export class PlistBuddy {
12 private static plistBuddyExecutable = "/usr/libexec/PlistBuddy";
13
14 private nodeChildProcess: ChildProcess;
15
16 constructor({
17 nodeChildProcess = new Node.ChildProcess(),
18 } = {}) {
19 this.nodeChildProcess = nodeChildProcess;
20 }
21
22 public getBundleId(projectRoot: string, simulator: boolean = true, configuration: string = "Debug", productName?: string): Q.Promise<string> {
23 const productsFolder = path.join(projectRoot, "build", "Build", "Products");
24 const configurationFolder = path.join(productsFolder, `${configuration}${simulator ? "-iphonesimulator" : "-iphoneos"}`);
25 let executable = "";
26 if (productName) {
27 executable = `${productName}.app`;
28 } else {
29 const executableList = this.findExecutable(configurationFolder);
30 if (!executableList.length) {
31 throw new Error(`Could not found executable in ${configurationFolder}`);
32 } else if (executableList.length > 1) {
33 throw new Error(`Found more than one executables in ${configurationFolder}. Please cleanup build folder or setup 'productName' launch option.`);
34 }
35 executable = `${executableList[0]}`;
36 }
37
38 const infoPlistPath = path.join(configurationFolder, executable, "Info.plist");
39 return this.invokePlistBuddy("Print:CFBundleIdentifier", infoPlistPath);
40
41 }
42
43 public setPlistProperty(plistFile: string, property: string, value: string): Q.Promise<void> {
44 // Attempt to set the value, and if it fails due to the key not existing attempt to create the key
45 return this.invokePlistBuddy(`Set ${property} ${value}`, plistFile).fail(() =>
46 this.invokePlistBuddy(`Add ${property} string ${value}`, plistFile)
47 ).then(() => { });
48 }
49
50 public setPlistBooleanProperty(plistFile: string, property: string, value: boolean): Q.Promise<void> {
51 // Attempt to set the value, and if it fails due to the key not existing attempt to create the key
52 return this.invokePlistBuddy(`Set ${property} ${value}`, plistFile)
53 .fail(() =>
54 this.invokePlistBuddy(`Add ${property} bool ${value}`, plistFile)
55 )
56 .then(() => { });
57 }
58
59 public deletePlistProperty(plistFile: string, property: string): Q.Promise<void> {
60 return this.invokePlistBuddy(`Delete ${property}`, plistFile).then(() => { });
61 }
62
63 public readPlistProperty(plistFile: string, property: string): Q.Promise<string> {
64 return this.invokePlistBuddy(`Print ${property}`, plistFile);
65 }
66
67 private findExecutable(folder: string): string[] {
68 return glob.sync("*.app", {
69 cwd: folder,
70 });
71 }
72
73 private invokePlistBuddy(command: string, plistFile: string): Q.Promise<string> {
74 return this.nodeChildProcess.exec(`${PlistBuddy.plistBuddyExecutable} -c '${command}' '${plistFile}'`).outcome.then((result: string) => {
75 return result.toString().trim();
76 });
77 }
78}
79