microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/generalMobilePlatform.ts
228lines · 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. | |
| 3 | | |
e26a1f43Artem Egorov8 years ago | 4 | import * as fs from "fs"; |
0a68f8dbArtem Egorov8 years ago | 5 | import {IRunOptions} from "./launchArgs"; |
4787ec09Artem Egorov8 years ago | 6 | import {Packager} from "../common/packager"; |
| 7 | import {PackagerStatusIndicator, PackagerStatus} from "./packagerStatusIndicator"; | |
0a68f8dbArtem Egorov8 years ago | 8 | import {SettingsHelper} from "./settingsHelper"; |
| 9 | import {OutputChannelLogger} from "./log/OutputChannelLogger"; | |
d7d405aeYuri Skorokhodov7 years ago | 10 | import * as nls from "vscode-nls"; |
68a5b8d5JiglioNero5 years ago | 11 | import { isBoolean } from "util"; |
| 12 | import { IVirtualDevice } from "./VirtualDeviceManager"; | |
2d8af448Yuri Skorokhodov6 years ago | 13 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
d7d405aeYuri Skorokhodov7 years ago | 14 | const localize = nls.loadMessageBundle(); |
0a68f8dbArtem Egorov8 years ago | 15 | |
| 16 | export interface MobilePlatformDeps { | |
| 17 | packager?: Packager; | |
| 18 | } | |
| 19 | | |
| 20 | export type TargetType = "device" | "simulator"; | |
| 21 | | |
| 22 | export class GeneralMobilePlatform { | |
| 23 | protected projectPath: string; | |
| 24 | protected platformName: string; | |
| 25 | protected packager: Packager; | |
| 26 | protected logger: OutputChannelLogger; | |
| 27 | | |
| 28 | protected static deviceString: TargetType = "device"; | |
| 29 | protected static simulatorString: TargetType = "simulator"; | |
df8c800dArtem Egorov8 years ago | 30 | protected static NO_PACKAGER_VERSION = "0.42.0"; |
0a68f8dbArtem Egorov8 years ago | 31 | |
db6fd42aRuslan Bikkinin7 years ago | 32 | public runArguments: string[]; |
| 33 | | |
0a68f8dbArtem Egorov8 years ago | 34 | constructor(protected runOptions: IRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 35 | this.platformName = this.runOptions.platform; | |
| 36 | this.projectPath = this.runOptions.projectRoot; | |
869b2b5cJiglioNero5 years ago | 37 | this.packager = platformDeps.packager || new Packager(this.runOptions.workspaceRoot, this.projectPath, SettingsHelper.getPackagerPort(this.runOptions.workspaceRoot), new PackagerStatusIndicator(this.projectPath)); |
de838bbfJiglioNero6 years ago | 38 | this.packager.setRunOptions(runOptions); |
aca27f7fYuri Skorokhodov7 years ago | 39 | this.logger = OutputChannelLogger.getChannel(localize("ReactNativeRunPlatform", "React Native: Run {0}", this.platformName), true); |
0a68f8dbArtem Egorov8 years ago | 40 | this.logger.clear(); |
db6fd42aRuslan Bikkinin7 years ago | 41 | this.runArguments = this.getRunArguments(); |
0a68f8dbArtem Egorov8 years ago | 42 | } |
| 43 | | |
68a5b8d5JiglioNero5 years ago | 44 | public resolveVirtualDevice(target: string): Promise<IVirtualDevice | null> { |
| 45 | return Promise.resolve(null); | |
| 46 | } | |
| 47 | | |
ce5e88eeYuri Skorokhodov5 years ago | 48 | public runApp(): Promise<void> { |
d7d405aeYuri Skorokhodov7 years ago | 49 | this.logger.info(localize("ConnectedToPackager", "Connected to packager. You can now open your app in the simulator.")); |
ce5e88eeYuri Skorokhodov5 years ago | 50 | return Promise.resolve(); |
0a68f8dbArtem Egorov8 years ago | 51 | } |
| 52 | | |
ce5e88eeYuri Skorokhodov5 years ago | 53 | public enableJSDebuggingMode(): Promise<void> { |
d7d405aeYuri Skorokhodov7 years ago | 54 | this.logger.info(localize("DebuggerReadyEnableRemoteDebuggingInApp", "Debugger ready. Enable remote debugging in app.")); |
ce5e88eeYuri Skorokhodov5 years ago | 55 | return Promise.resolve(); |
0a68f8dbArtem Egorov8 years ago | 56 | } |
| 57 | | |
ce5e88eeYuri Skorokhodov5 years ago | 58 | public disableJSDebuggingMode(): Promise<void> { |
d7d405aeYuri Skorokhodov7 years ago | 59 | this.logger.info(localize("DebuggerReadyDisableRemoteDebuggingInApp", "Debugger ready. Disable remote debugging in app.")); |
ce5e88eeYuri Skorokhodov5 years ago | 60 | return Promise.resolve(); |
0a68f8dbArtem Egorov8 years ago | 61 | } |
| 62 | | |
ce5e88eeYuri Skorokhodov5 years ago | 63 | public beforeStartPackager(): Promise<void> { |
| 64 | return Promise.resolve(); | |
4787ec09Artem Egorov8 years ago | 65 | } |
| 66 | | |
ce5e88eeYuri Skorokhodov5 years ago | 67 | public startPackager(): Promise<void> { |
d7d405aeYuri Skorokhodov7 years ago | 68 | this.logger.info(localize("StartingReactNativePackager", "Starting React Native Packager.")); |
4787ec09Artem Egorov8 years ago | 69 | return this.packager.isRunning() |
| 70 | .then((running) => { | |
0a68f8dbArtem Egorov8 years ago | 71 | if (running) { |
4787ec09Artem Egorov8 years ago | 72 | if (this.packager.getPackagerStatus() !== PackagerStatus.PACKAGER_STARTED) { |
| 73 | return this.packager.stop(); | |
0a68f8dbArtem Egorov8 years ago | 74 | } |
| 75 | | |
d7d405aeYuri Skorokhodov7 years ago | 76 | this.logger.info(localize("AttachingToRunningReactNativePackager", "Attaching to running React Native packager")); |
0a68f8dbArtem Egorov8 years ago | 77 | } |
| 78 | return void 0; | |
| 79 | }) | |
4787ec09Artem Egorov8 years ago | 80 | .then(() => { |
| 81 | return this.packager.start(); | |
| 82 | }); | |
0a68f8dbArtem Egorov8 years ago | 83 | } |
| 84 | | |
ce5e88eeYuri Skorokhodov5 years ago | 85 | public prewarmBundleCache(): Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 86 | // generalMobilePlatform should do nothing here. Method should be overriden by children for specific behavior. |
ce5e88eeYuri Skorokhodov5 years ago | 87 | return Promise.resolve(); |
0a68f8dbArtem Egorov8 years ago | 88 | } |
| 89 | | |
68a5b8d5JiglioNero5 years ago | 90 | public static removeRunArgument(runArguments: any[], optName: string, binary: boolean) { |
| 91 | const optIdx = runArguments.indexOf(optName); | |
| 92 | if (optIdx > -1) { | |
| 93 | if (binary) { | |
| 94 | runArguments.splice(optIdx, 1); | |
| 95 | } | |
| 96 | else { | |
| 97 | runArguments.splice(optIdx, 2); | |
| 98 | } | |
| 99 | } | |
| 100 | } | |
| 101 | | |
| 102 | public static setRunArgument(runArguments: any[], optName: string, value: string | boolean) { | |
| 103 | const isBinary = isBoolean(value); | |
| 104 | const optIdx = runArguments.indexOf(optName); | |
| 105 | if (optIdx > -1) { | |
| 106 | if (isBinary && !value) { | |
| 107 | GeneralMobilePlatform.removeRunArgument(runArguments, optName, true); | |
| 108 | } | |
| 109 | if (!isBinary) { | |
| 110 | runArguments[optIdx + 1] = value; | |
| 111 | } | |
| 112 | } | |
| 113 | else { | |
| 114 | if (isBinary && value) { | |
| 115 | runArguments.push(optName); | |
| 116 | } | |
| 117 | if (!isBinary) { | |
| 118 | runArguments.push(optName); | |
| 119 | runArguments.push(value); | |
| 120 | } | |
| 121 | } | |
| 122 | } | |
| 123 | | |
116c3cb0Ruslan Bikkinin7 years ago | 124 | public static getOptFromRunArgs(runArguments: any[], optName: string, binary: boolean = false): any { |
| 125 | if (runArguments.length > 0) { | |
| 126 | const optIdx = runArguments.indexOf(optName); | |
| 127 | let result: any = undefined; | |
db6fd42aRuslan Bikkinin7 years ago | 128 | |
| 129 | if (optIdx > -1) { | |
116c3cb0Ruslan Bikkinin7 years ago | 130 | result = binary ? true : runArguments[optIdx + 1]; |
db6fd42aRuslan Bikkinin7 years ago | 131 | } else { |
116c3cb0Ruslan Bikkinin7 years ago | 132 | for (let i = 0; i < runArguments.length; i++) { |
| 133 | const arg = runArguments[i]; | |
db6fd42aRuslan Bikkinin7 years ago | 134 | if (arg.indexOf(optName) > -1) { |
116c3cb0Ruslan Bikkinin7 years ago | 135 | if (binary) { |
| 136 | result = true; | |
| 137 | } else { | |
| 138 | const tokens = arg.split("="); | |
| 139 | if (tokens.length > 1) { | |
| 140 | result = tokens[1].trim(); | |
| 141 | } else { | |
| 142 | result = undefined; | |
| 143 | } | |
| 144 | } | |
db6fd42aRuslan Bikkinin7 years ago | 145 | } |
| 146 | } | |
| 147 | } | |
| 148 | | |
116c3cb0Ruslan Bikkinin7 years ago | 149 | // Binary parameters can either exists (e.g. be true) or be absent. You can not pass false binary parameter. |
db6fd42aRuslan Bikkinin7 years ago | 150 | if (binary) { |
116c3cb0Ruslan Bikkinin7 years ago | 151 | if (result === undefined) { |
| 152 | return undefined; | |
| 153 | } else { | |
| 154 | return true; | |
| 155 | } | |
db6fd42aRuslan Bikkinin7 years ago | 156 | } |
| 157 | | |
| 158 | if (result) { | |
| 159 | try { | |
| 160 | return JSON.parse(result); | |
| 161 | } catch (err) { | |
116c3cb0Ruslan Bikkinin7 years ago | 162 | // simple string value, return as is |
db6fd42aRuslan Bikkinin7 years ago | 163 | return result; |
| 164 | } | |
| 165 | } | |
| 166 | } | |
| 167 | | |
| 168 | return undefined; | |
| 169 | } | |
| 170 | | |
cbc7ac5bArtem Egorov7 years ago | 171 | public getRunArguments(): string[] { |
db6fd42aRuslan Bikkinin7 years ago | 172 | throw new Error("Not yet implemented: GeneralMobilePlatform.getRunArguments"); |
| 173 | } | |
| 174 | | |
de838bbfJiglioNero6 years ago | 175 | public static getEnvArgument(processEnv: any, env?: any, envFile?: string): any { |
| 176 | let modifyEnv = Object.assign({}, processEnv); | |
e26a1f43Artem Egorov8 years ago | 177 | |
de838bbfJiglioNero6 years ago | 178 | if (envFile) { |
| 179 | // .env variables never overwrite existing variables | |
| 180 | const argsFromEnvFile = this.readEnvFile(envFile); | |
| 181 | if (argsFromEnvFile != null) { | |
| 182 | for (let key in argsFromEnvFile) { | |
| 183 | if (!modifyEnv[key] && argsFromEnvFile.hasOwnProperty(key)) { | |
| 184 | modifyEnv[key] = argsFromEnvFile[key]; | |
| 185 | } | |
| 186 | } | |
| 187 | } | |
| 188 | } | |
| 189 | | |
| 190 | if (env) { | |
| 191 | // launch config env vars overwrite .env vars | |
| 192 | for (let key in env) { | |
| 193 | if (env.hasOwnProperty(key)) { | |
| 194 | modifyEnv[key] = env[key]; | |
| 195 | } | |
| 196 | } | |
| 197 | } | |
| 198 | return modifyEnv; | |
| 199 | } | |
| 200 | | |
| 201 | private static readEnvFile(filePath: string): any { | |
| 202 | if (fs.existsSync(filePath)) { | |
| 203 | let buffer = fs.readFileSync(filePath, "utf8"); | |
| 204 | let result = {}; | |
e26a1f43Artem Egorov8 years ago | 205 | |
| 206 | // Strip BOM | |
| 207 | if (buffer && buffer[0] === "\uFEFF") { | |
| 208 | buffer = buffer.substr(1); | |
| 209 | } | |
| 210 | | |
| 211 | buffer.split("\n").forEach((line: string) => { | |
| 212 | const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/); | |
| 213 | if (r !== null) { | |
| 214 | const key = r[1]; | |
de838bbfJiglioNero6 years ago | 215 | let value = r[2] || ""; |
| 216 | if (value.length > 0 && value.charAt(0) === "\"" && value.charAt(value.length - 1) === "\"") { | |
| 217 | value = value.replace(/\\n/gm, "\n"); | |
e26a1f43Artem Egorov8 years ago | 218 | } |
de838bbfJiglioNero6 years ago | 219 | result[key] = value.replace(/(^['"]|['"]$)/g, ""); |
e26a1f43Artem Egorov8 years ago | 220 | } |
| 221 | }); | |
| 222 | | |
de838bbfJiglioNero6 years ago | 223 | return result; |
| 224 | } else { | |
| 225 | return null; | |
e26a1f43Artem Egorov8 years ago | 226 | } |
| 227 | } | |
0a68f8dbArtem Egorov8 years ago | 228 | } |