microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/hostPlatform.ts
220lines · 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 | |
| 4 | import {ChildProcess} from "./node/childProcess"; |
| 5 | import {TargetPlatformId} from "./targetPlatformHelper"; |
| 6 | import * as path from "path"; |
| 7 | import * as Q from "q"; |
| 8 | |
| 9 | /** |
| 10 | * Interface defining the host (desktop) platform specific operations. |
| 11 | */ |
| 12 | interface IHostPlatform { |
| 13 | getUserHomePath(): string; |
| 14 | getSettingsHome(): string; |
| 15 | getNpmCliCommand(packageName: string): string; |
| 16 | getPipePath(pipeName: string): string; |
| 17 | getPlatformId(): HostPlatformId; |
| 18 | setEnvironmentVariable(name: string, value: string): Q.Promise<void>; |
| 19 | getUserID(): string; |
| 20 | isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Defines the identifiers of all the platforms we support. |
| 25 | */ |
| 26 | export enum HostPlatformId { |
| 27 | WINDOWS, |
| 28 | OSX, |
| 29 | LINUX, |
| 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 | |
| 40 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { |
| 41 | return new ChildProcess().exec(`setx ${name} ${value}`).outcome; |
| 42 | } |
| 43 | |
| 44 | public getSettingsHome(): string { |
| 45 | return path.join(process.env.APPDATA || "", "vscode-react-native"); |
| 46 | } |
| 47 | |
| 48 | public getNpmCliCommand(cliName: string): string { |
| 49 | return `${cliName}.cmd`; |
| 50 | } |
| 51 | |
| 52 | public getPipePath(pipeName: string): string { |
| 53 | return `//?/pipe/${pipeName}`; |
| 54 | } |
| 55 | |
| 56 | public getPlatformId(): HostPlatformId { |
| 57 | return HostPlatformId.WINDOWS; |
| 58 | } |
| 59 | |
| 60 | public getUserID(): string { |
| 61 | return process.env.USERNAME || ""; |
| 62 | } |
| 63 | |
| 64 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
| 65 | switch (targetPlatformId) { |
| 66 | case TargetPlatformId.ANDROID: |
| 67 | case TargetPlatformId.EXPONENT: |
| 68 | case TargetPlatformId.WINDOWS: |
| 69 | return true; |
| 70 | default: |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | abstract class UnixHostPlatform implements IHostPlatform { |
| 77 | public getUserHomePath(): string { |
| 78 | return process.env.HOME || ""; |
| 79 | } |
| 80 | |
| 81 | public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>; |
| 82 | |
| 83 | public getSettingsHome(): string { |
| 84 | return path.join(process.env.HOME || "", ".vscode-react-native"); |
| 85 | } |
| 86 | |
| 87 | public getNpmCliCommand(packageName: string): string { |
| 88 | return packageName; |
| 89 | } |
| 90 | |
| 91 | public getPipePath(pipeName: string): string { |
| 92 | return `/tmp/${pipeName}.sock`; |
| 93 | } |
| 94 | |
| 95 | public abstract getPlatformId(): HostPlatformId; |
| 96 | |
| 97 | public abstract getUserID(): string; |
| 98 | |
| 99 | public abstract isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * IHostPlatform implemenation for the OSX platform. |
| 104 | */ |
| 105 | class OSXHostPlatform extends UnixHostPlatform { |
| 106 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { |
| 107 | return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome; |
| 108 | } |
| 109 | |
| 110 | public getPlatformId(): HostPlatformId { |
| 111 | return HostPlatformId.OSX; |
| 112 | } |
| 113 | |
| 114 | public getUserID(): string { |
| 115 | return process.env.LOGNAME || ""; |
| 116 | } |
| 117 | |
| 118 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
| 119 | switch (targetPlatformId) { |
| 120 | case TargetPlatformId.ANDROID: |
| 121 | case TargetPlatformId.EXPONENT: |
| 122 | case TargetPlatformId.IOS: |
| 123 | return true; |
| 124 | default: |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 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> { |
| 135 | return new ChildProcess().exec(`export ${name}=${value}`).outcome; |
| 136 | } |
| 137 | |
| 138 | public getPlatformId(): HostPlatformId { |
| 139 | return HostPlatformId.LINUX; |
| 140 | } |
| 141 | |
| 142 | public getUserID(): string { |
| 143 | return process.env.USER || ""; |
| 144 | } |
| 145 | |
| 146 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
| 147 | switch (targetPlatformId) { |
| 148 | case TargetPlatformId.ANDROID: |
| 149 | case TargetPlatformId.EXPONENT: |
| 150 | return true; |
| 151 | default: |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Allows platform specific operations based on the user's OS. |
| 159 | */ |
| 160 | export class HostPlatform { |
| 161 | |
| 162 | private static platformInstance: IHostPlatform; |
| 163 | |
| 164 | /** |
| 165 | * Resolves the dev machine, desktop platform. |
| 166 | */ |
| 167 | private static get platform(): IHostPlatform { |
| 168 | if (!HostPlatform.platformInstance) { |
| 169 | switch (process.platform) { |
| 170 | case "win32": |
| 171 | HostPlatform.platformInstance = new WindowsHostPlatform(); |
| 172 | break; |
| 173 | case "darwin": |
| 174 | HostPlatform.platformInstance = new OSXHostPlatform(); |
| 175 | break; |
| 176 | case "linux": |
| 177 | HostPlatform.platformInstance = new LinuxHostPlatform(); |
| 178 | break; |
| 179 | default: |
| 180 | HostPlatform.platformInstance = new LinuxHostPlatform(); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 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 { |
| 197 | return HostPlatform.platform.getNpmCliCommand(packageName); |
| 198 | } |
| 199 | |
| 200 | public static getPipePath(pipeName: string): string { |
| 201 | return HostPlatform.platform.getPipePath(pipeName); |
| 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); |
| 210 | } |
| 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 | } |
| 216 | |
| 217 | public static isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
| 218 | return HostPlatform.platform.isCompatibleWithTarget(targetPlatformId); |
| 219 | } |
| 220 | } |
| 221 | |