microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/hostPlatform.ts
200lines · 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 { | |
| 37 | return process.env.USERPROFILE; | |
| 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 { | |
| 45 | return path.join(process.env.APPDATA, "vscode-react-native"); | |
| 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 { | |
| 61 | return process.env.USERNAME; | |
| 62 | } | |
fced706ddigeff10 years ago | 63 | |
a98974c3Daniel Lebu10 years ago | 64 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
1c32fe84Patricio Beltran9 years ago | 65 | return targetPlatformId === TargetPlatformId.ANDROID || targetPlatformId === TargetPlatformId.EXPONENT; |
42fac1f6Meena Kunnathur Balakrishnan10 years ago | 66 | } |
8411fd8dDaniel Lebu10 years ago | 67 | } |
| 68 | | |
bbdbbb3bdlebu10 years ago | 69 | abstract class UnixHostPlatform implements IHostPlatform { |
8411fd8dDaniel Lebu10 years ago | 70 | public getUserHomePath(): string { |
| 71 | return process.env.HOME; | |
| 72 | } | |
| 73 | | |
bbdbbb3bdlebu10 years ago | 74 | public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>; |
8411fd8dDaniel Lebu10 years ago | 75 | |
| 76 | public getSettingsHome(): string { | |
| 77 | return path.join(process.env.HOME, ".vscode-react-native"); | |
| 78 | } | |
| 79 | | |
a9955be6dlebu10 years ago | 80 | public getNpmCliCommand(packageName: string): string { |
bbdbbb3bdlebu10 years ago | 81 | return packageName; |
8411fd8dDaniel Lebu10 years ago | 82 | } |
| 83 | | |
d3950d7edigeff10 years ago | 84 | public getPipePath(pipeName: string): string { |
| 85 | return `/tmp/${pipeName}.sock`; | |
8411fd8dDaniel Lebu10 years ago | 86 | } |
| 87 | | |
bbdbbb3bdlebu10 years ago | 88 | public abstract getPlatformId(): HostPlatformId; |
d3950d7edigeff10 years ago | 89 | |
| 90 | public abstract getUserID(): string; | |
fced706ddigeff10 years ago | 91 | |
a98974c3Daniel Lebu10 years ago | 92 | public abstract isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean; |
8411fd8dDaniel Lebu10 years ago | 93 | } |
| 94 | | |
bbdbbb3bdlebu10 years ago | 95 | /** |
| 96 | * IHostPlatform implemenation for the OSX platform. | |
| 97 | */ | |
| 98 | class OSXHostPlatform extends UnixHostPlatform { | |
| 99 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { | |
a1005420dlebu10 years ago | 100 | return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome; |
bbdbbb3bdlebu10 years ago | 101 | } |
| 102 | | |
| 103 | public getPlatformId(): HostPlatformId { | |
| 104 | return HostPlatformId.OSX; | |
| 105 | } | |
d3950d7edigeff10 years ago | 106 | |
| 107 | public getUserID(): string { | |
| 108 | return process.env.LOGNAME; | |
| 109 | } | |
fced706ddigeff10 years ago | 110 | |
a98974c3Daniel Lebu10 years ago | 111 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
1c32fe84Patricio Beltran9 years ago | 112 | return targetPlatformId === TargetPlatformId.ANDROID || targetPlatformId === TargetPlatformId.IOS || targetPlatformId === TargetPlatformId.EXPONENT; |
e09724a1Meena Kunnathur Balakrishnan10 years ago | 113 | } |
bbdbbb3bdlebu10 years ago | 114 | } |
| 115 | | |
| 116 | /** | |
| 117 | * IHostPlatform implemenation for the Linux platform. | |
| 118 | */ | |
| 119 | class LinuxHostPlatform extends UnixHostPlatform { | |
| 120 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { | |
a1005420dlebu10 years ago | 121 | return new ChildProcess().exec(`export ${name}=${value}`).outcome; |
bbdbbb3bdlebu10 years ago | 122 | } |
| 123 | | |
| 124 | public getPlatformId(): HostPlatformId { | |
| 125 | return HostPlatformId.LINUX; | |
| 126 | } | |
d3950d7edigeff10 years ago | 127 | |
| 128 | public getUserID(): string { | |
| 129 | return process.env.USER; | |
| 130 | } | |
fced706ddigeff10 years ago | 131 | |
a98974c3Daniel Lebu10 years ago | 132 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
1c32fe84Patricio Beltran9 years ago | 133 | return targetPlatformId === TargetPlatformId.ANDROID || targetPlatformId === TargetPlatformId.EXPONENT; |
e09724a1Meena Kunnathur Balakrishnan10 years ago | 134 | } |
bbdbbb3bdlebu10 years ago | 135 | } |
| 136 | | |
8411fd8dDaniel Lebu10 years ago | 137 | /** |
a1005420dlebu10 years ago | 138 | * Allows platform specific operations based on the user's OS. |
8411fd8dDaniel Lebu10 years ago | 139 | */ |
a1005420dlebu10 years ago | 140 | export class HostPlatform { |
8411fd8dDaniel Lebu10 years ago | 141 | |
a1005420dlebu10 years ago | 142 | private static platformInstance: IHostPlatform; |
8411fd8dDaniel Lebu10 years ago | 143 | |
| 144 | /** | |
| 145 | * Resolves the dev machine, desktop platform. | |
| 146 | */ | |
a1005420dlebu10 years ago | 147 | private static get platform(): IHostPlatform { |
| 148 | if (!HostPlatform.platformInstance) { | |
| 149 | switch (process.platform) { | |
| 150 | case "win32": | |
| 151 | HostPlatform.platformInstance = new WindowsHostPlatform(); | |
9fe4ca93digeff10 years ago | 152 | break; |
a1005420dlebu10 years ago | 153 | case "darwin": |
| 154 | HostPlatform.platformInstance = new OSXHostPlatform(); | |
9fe4ca93digeff10 years ago | 155 | break; |
a1005420dlebu10 years ago | 156 | case "linux": |
c6d7ee27dlebu10 years ago | 157 | HostPlatform.platformInstance = new LinuxHostPlatform(); |
| 158 | break; | |
a1005420dlebu10 years ago | 159 | default: |
| 160 | HostPlatform.platformInstance = new LinuxHostPlatform(); | |
aab2095edigeff10 years ago | 161 | break; |
a1005420dlebu10 years ago | 162 | } |
8411fd8dDaniel Lebu10 years ago | 163 | } |
a1005420dlebu10 years ago | 164 | |
| 165 | return HostPlatform.platformInstance; | |
| 166 | } | |
| 167 | | |
| 168 | public static getUserHomePath(): string { | |
| 169 | return HostPlatform.platform.getUserHomePath(); | |
| 170 | } | |
| 171 | | |
| 172 | public static getSettingsHome(): string { | |
| 173 | return HostPlatform.platform.getSettingsHome(); | |
| 174 | } | |
| 175 | | |
| 176 | public static getNpmCliCommand(packageName: string): string { | |
a9955be6dlebu10 years ago | 177 | return HostPlatform.platform.getNpmCliCommand(packageName); |
a1005420dlebu10 years ago | 178 | } |
| 179 | | |
d3950d7edigeff10 years ago | 180 | public static getPipePath(pipeName: string): string { |
| 181 | return HostPlatform.platform.getPipePath(pipeName); | |
a1005420dlebu10 years ago | 182 | } |
| 183 | | |
| 184 | public static getPlatformId(): HostPlatformId { | |
| 185 | return HostPlatform.platform.getPlatformId(); | |
| 186 | } | |
| 187 | | |
| 188 | public static setEnvironmentVariable(name: string, value: string): Q.Promise<void> { | |
| 189 | return HostPlatform.platform.setEnvironmentVariable(name, value); | |
8411fd8dDaniel Lebu10 years ago | 190 | } |
d3950d7edigeff10 years ago | 191 | |
| 192 | /* Returns a value that is unique for each user of this computer */ | |
| 193 | public static getUserID(): string { | |
| 194 | return HostPlatform.platform.getUserID(); | |
| 195 | } | |
fced706ddigeff10 years ago | 196 | |
a98974c3Daniel Lebu10 years ago | 197 | public static isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
| 198 | return HostPlatform.platform.isCompatibleWithTarget(targetPlatformId); | |
42fac1f6Meena Kunnathur Balakrishnan10 years ago | 199 | } |
8411fd8dDaniel Lebu10 years ago | 200 | } |