microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/hostPlatform.ts
220lines · modeblame
8411fd8dDaniel Lebu10 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 | | |
a1005420dlebu10 years ago | 4 | import {ChildProcess} from "./node/childProcess"; |
aeaf46bcMeena Kunnathur Balakrishnan10 years ago | 5 | import {TargetPlatformId} from "./targetPlatformHelper"; |
8411fd8dDaniel Lebu10 years ago | 6 | import * as path from "path"; |
62d9cdd3dlebu10 years ago | 7 | import * as Q from "q"; |
8411fd8dDaniel Lebu10 years ago | 8 | |
| 9 | /** | |
| 10 | * Interface defining the host (desktop) platform specific operations. | |
| 11 | */ | |
a1005420dlebu10 years ago | 12 | interface IHostPlatform { |
8411fd8dDaniel Lebu10 years ago | 13 | getUserHomePath(): string; |
| 14 | getSettingsHome(): string; | |
a9955be6dlebu10 years ago | 15 | getNpmCliCommand(packageName: string): string; |
d3950d7edigeff10 years ago | 16 | getPipePath(pipeName: string): string; |
8411fd8dDaniel Lebu10 years ago | 17 | getPlatformId(): HostPlatformId; |
62d9cdd3dlebu10 years ago | 18 | setEnvironmentVariable(name: string, value: string): Q.Promise<void>; |
d3950d7edigeff10 years ago | 19 | getUserID(): string; |
a98974c3Daniel Lebu10 years ago | 20 | isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean; |
8411fd8dDaniel Lebu10 years ago | 21 | } |
| 22 | | |
| 23 | /** | |
| 24 | * Defines the identifiers of all the platforms we support. | |
| 25 | */ | |
| 26 | export enum HostPlatformId { | |
| 27 | WINDOWS, | |
bbdbbb3bdlebu10 years ago | 28 | OSX, |
27710197Vladimir Kotikov8 years ago | 29 | LINUX, |
8411fd8dDaniel Lebu10 years ago | 30 | } |
| 31 | | |
| 32 | /** | |
| 33 | * IHostPlatform implemenation for the Windows platform. | |
| 34 | */ | |
| 35 | class WindowsHostPlatform implements IHostPlatform { | |
| 36 | public getUserHomePath(): string { | |
3ff1d921annakocheshkova8 years ago | 37 | return process.env.USERPROFILE || ""; |
8411fd8dDaniel Lebu10 years ago | 38 | } |
| 39 | | |
62d9cdd3dlebu10 years ago | 40 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { |
a1005420dlebu10 years ago | 41 | return new ChildProcess().exec(`setx ${name} ${value}`).outcome; |
8411fd8dDaniel Lebu10 years ago | 42 | } |
| 43 | | |
| 44 | public getSettingsHome(): string { | |
3ff1d921annakocheshkova8 years ago | 45 | return path.join(process.env.APPDATA || "", "vscode-react-native"); |
8411fd8dDaniel Lebu10 years ago | 46 | } |
| 47 | | |
a9955be6dlebu10 years ago | 48 | public getNpmCliCommand(cliName: string): string { |
| 49 | return `${cliName}.cmd`; | |
8411fd8dDaniel Lebu10 years ago | 50 | } |
| 51 | | |
d3950d7edigeff10 years ago | 52 | public getPipePath(pipeName: string): string { |
7daed3fcArtem Egorov8 years ago | 53 | return `//?/pipe/${pipeName}`; |
8411fd8dDaniel Lebu10 years ago | 54 | } |
| 55 | | |
| 56 | public getPlatformId(): HostPlatformId { | |
| 57 | return HostPlatformId.WINDOWS; | |
| 58 | } | |
d3950d7edigeff10 years ago | 59 | |
| 60 | public getUserID(): string { | |
3ff1d921annakocheshkova8 years ago | 61 | return process.env.USERNAME || ""; |
d3950d7edigeff10 years ago | 62 | } |
fced706ddigeff10 years ago | 63 | |
a98974c3Daniel Lebu10 years ago | 64 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
299883f9Artem Egorov8 years ago | 65 | switch (targetPlatformId) { |
| 66 | case TargetPlatformId.ANDROID: | |
| 67 | case TargetPlatformId.EXPONENT: | |
| 68 | case TargetPlatformId.WINDOWS: | |
| 69 | return true; | |
| 70 | default: | |
| 71 | return false; | |
| 72 | } | |
42fac1f6Meena Kunnathur Balakrishnan10 years ago | 73 | } |
8411fd8dDaniel Lebu10 years ago | 74 | } |
| 75 | | |
bbdbbb3bdlebu10 years ago | 76 | abstract class UnixHostPlatform implements IHostPlatform { |
8411fd8dDaniel Lebu10 years ago | 77 | public getUserHomePath(): string { |
3ff1d921annakocheshkova8 years ago | 78 | return process.env.HOME || ""; |
8411fd8dDaniel Lebu10 years ago | 79 | } |
| 80 | | |
bbdbbb3bdlebu10 years ago | 81 | public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>; |
8411fd8dDaniel Lebu10 years ago | 82 | |
| 83 | public getSettingsHome(): string { | |
3ff1d921annakocheshkova8 years ago | 84 | return path.join(process.env.HOME || "", ".vscode-react-native"); |
8411fd8dDaniel Lebu10 years ago | 85 | } |
| 86 | | |
a9955be6dlebu10 years ago | 87 | public getNpmCliCommand(packageName: string): string { |
bbdbbb3bdlebu10 years ago | 88 | return packageName; |
8411fd8dDaniel Lebu10 years ago | 89 | } |
| 90 | | |
d3950d7edigeff10 years ago | 91 | public getPipePath(pipeName: string): string { |
| 92 | return `/tmp/${pipeName}.sock`; | |
8411fd8dDaniel Lebu10 years ago | 93 | } |
| 94 | | |
bbdbbb3bdlebu10 years ago | 95 | public abstract getPlatformId(): HostPlatformId; |
d3950d7edigeff10 years ago | 96 | |
| 97 | public abstract getUserID(): string; | |
fced706ddigeff10 years ago | 98 | |
a98974c3Daniel Lebu10 years ago | 99 | public abstract isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean; |
8411fd8dDaniel Lebu10 years ago | 100 | } |
| 101 | | |
bbdbbb3bdlebu10 years ago | 102 | /** |
| 103 | * IHostPlatform implemenation for the OSX platform. | |
| 104 | */ | |
| 105 | class OSXHostPlatform extends UnixHostPlatform { | |
| 106 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { | |
a1005420dlebu10 years ago | 107 | return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome; |
bbdbbb3bdlebu10 years ago | 108 | } |
| 109 | | |
| 110 | public getPlatformId(): HostPlatformId { | |
| 111 | return HostPlatformId.OSX; | |
| 112 | } | |
d3950d7edigeff10 years ago | 113 | |
| 114 | public getUserID(): string { | |
3ff1d921annakocheshkova8 years ago | 115 | return process.env.LOGNAME || ""; |
d3950d7edigeff10 years ago | 116 | } |
fced706ddigeff10 years ago | 117 | |
a98974c3Daniel Lebu10 years ago | 118 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
299883f9Artem Egorov8 years ago | 119 | switch (targetPlatformId) { |
| 120 | case TargetPlatformId.ANDROID: | |
| 121 | case TargetPlatformId.EXPONENT: | |
| 122 | case TargetPlatformId.IOS: | |
| 123 | return true; | |
| 124 | default: | |
| 125 | return false; | |
| 126 | } | |
e09724a1Meena Kunnathur Balakrishnan10 years ago | 127 | } |
bbdbbb3bdlebu10 years ago | 128 | } |
| 129 | | |
| 130 | /** | |
| 131 | * IHostPlatform implemenation for the Linux platform. | |
| 132 | */ | |
| 133 | class LinuxHostPlatform extends UnixHostPlatform { | |
| 134 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { | |
a1005420dlebu10 years ago | 135 | return new ChildProcess().exec(`export ${name}=${value}`).outcome; |
bbdbbb3bdlebu10 years ago | 136 | } |
| 137 | | |
| 138 | public getPlatformId(): HostPlatformId { | |
| 139 | return HostPlatformId.LINUX; | |
| 140 | } | |
d3950d7edigeff10 years ago | 141 | |
| 142 | public getUserID(): string { | |
3ff1d921annakocheshkova8 years ago | 143 | return process.env.USER || ""; |
d3950d7edigeff10 years ago | 144 | } |
fced706ddigeff10 years ago | 145 | |
a98974c3Daniel Lebu10 years ago | 146 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
299883f9Artem Egorov8 years ago | 147 | switch (targetPlatformId) { |
| 148 | case TargetPlatformId.ANDROID: | |
| 149 | case TargetPlatformId.EXPONENT: | |
| 150 | return true; | |
| 151 | default: | |
| 152 | return false; | |
| 153 | } | |
e09724a1Meena Kunnathur Balakrishnan10 years ago | 154 | } |
bbdbbb3bdlebu10 years ago | 155 | } |
| 156 | | |
8411fd8dDaniel Lebu10 years ago | 157 | /** |
a1005420dlebu10 years ago | 158 | * Allows platform specific operations based on the user's OS. |
8411fd8dDaniel Lebu10 years ago | 159 | */ |
a1005420dlebu10 years ago | 160 | export class HostPlatform { |
8411fd8dDaniel Lebu10 years ago | 161 | |
a1005420dlebu10 years ago | 162 | private static platformInstance: IHostPlatform; |
8411fd8dDaniel Lebu10 years ago | 163 | |
| 164 | /** | |
| 165 | * Resolves the dev machine, desktop platform. | |
| 166 | */ | |
a1005420dlebu10 years ago | 167 | private static get platform(): IHostPlatform { |
| 168 | if (!HostPlatform.platformInstance) { | |
| 169 | switch (process.platform) { | |
| 170 | case "win32": | |
| 171 | HostPlatform.platformInstance = new WindowsHostPlatform(); | |
9fe4ca93digeff10 years ago | 172 | break; |
a1005420dlebu10 years ago | 173 | case "darwin": |
| 174 | HostPlatform.platformInstance = new OSXHostPlatform(); | |
9fe4ca93digeff10 years ago | 175 | break; |
a1005420dlebu10 years ago | 176 | case "linux": |
c6d7ee27dlebu10 years ago | 177 | HostPlatform.platformInstance = new LinuxHostPlatform(); |
| 178 | break; | |
a1005420dlebu10 years ago | 179 | default: |
| 180 | HostPlatform.platformInstance = new LinuxHostPlatform(); | |
aab2095edigeff10 years ago | 181 | break; |
a1005420dlebu10 years ago | 182 | } |
8411fd8dDaniel Lebu10 years ago | 183 | } |
a1005420dlebu10 years ago | 184 | |
| 185 | return HostPlatform.platformInstance; | |
| 186 | } | |
| 187 | | |
| 188 | public static getUserHomePath(): string { | |
| 189 | return HostPlatform.platform.getUserHomePath(); | |
| 190 | } | |
| 191 | | |
| 192 | public static getSettingsHome(): string { | |
| 193 | return HostPlatform.platform.getSettingsHome(); | |
| 194 | } | |
| 195 | | |
| 196 | public static getNpmCliCommand(packageName: string): string { | |
a9955be6dlebu10 years ago | 197 | return HostPlatform.platform.getNpmCliCommand(packageName); |
a1005420dlebu10 years ago | 198 | } |
| 199 | | |
d3950d7edigeff10 years ago | 200 | public static getPipePath(pipeName: string): string { |
| 201 | return HostPlatform.platform.getPipePath(pipeName); | |
a1005420dlebu10 years ago | 202 | } |
| 203 | | |
| 204 | public static getPlatformId(): HostPlatformId { | |
| 205 | return HostPlatform.platform.getPlatformId(); | |
| 206 | } | |
| 207 | | |
| 208 | public static setEnvironmentVariable(name: string, value: string): Q.Promise<void> { | |
| 209 | return HostPlatform.platform.setEnvironmentVariable(name, value); | |
8411fd8dDaniel Lebu10 years ago | 210 | } |
d3950d7edigeff10 years ago | 211 | |
| 212 | /* Returns a value that is unique for each user of this computer */ | |
| 213 | public static getUserID(): string { | |
| 214 | return HostPlatform.platform.getUserID(); | |
| 215 | } | |
fced706ddigeff10 years ago | 216 | |
a98974c3Daniel Lebu10 years ago | 217 | public static isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
| 218 | return HostPlatform.platform.isCompatibleWithTarget(targetPlatformId); | |
42fac1f6Meena Kunnathur Balakrishnan10 years ago | 219 | } |
8411fd8dDaniel Lebu10 years ago | 220 | } |