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