microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/hostPlatform.ts
200lines · 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 | return targetPlatformId === TargetPlatformId.ANDROID; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | abstract class UnixHostPlatform implements IHostPlatform { |
| 70 | public getUserHomePath(): string { |
| 71 | return process.env.HOME; |
| 72 | } |
| 73 | |
| 74 | public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>; |
| 75 | |
| 76 | public getSettingsHome(): string { |
| 77 | return path.join(process.env.HOME, ".vscode-react-native"); |
| 78 | } |
| 79 | |
| 80 | public getNpmCliCommand(packageName: string): string { |
| 81 | return packageName; |
| 82 | } |
| 83 | |
| 84 | public getPipePath(pipeName: string): string { |
| 85 | return `/tmp/${pipeName}.sock`; |
| 86 | } |
| 87 | |
| 88 | public abstract getPlatformId(): HostPlatformId; |
| 89 | |
| 90 | public abstract getUserID(): string; |
| 91 | |
| 92 | public abstract isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * IHostPlatform implemenation for the OSX platform. |
| 97 | */ |
| 98 | class OSXHostPlatform extends UnixHostPlatform { |
| 99 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { |
| 100 | return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome; |
| 101 | } |
| 102 | |
| 103 | public getPlatformId(): HostPlatformId { |
| 104 | return HostPlatformId.OSX; |
| 105 | } |
| 106 | |
| 107 | public getUserID(): string { |
| 108 | return process.env.LOGNAME; |
| 109 | } |
| 110 | |
| 111 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
| 112 | return targetPlatformId === TargetPlatformId.ANDROID || targetPlatformId === TargetPlatformId.IOS; |
| 113 | } |
| 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> { |
| 121 | return new ChildProcess().exec(`export ${name}=${value}`).outcome; |
| 122 | } |
| 123 | |
| 124 | public getPlatformId(): HostPlatformId { |
| 125 | return HostPlatformId.LINUX; |
| 126 | } |
| 127 | |
| 128 | public getUserID(): string { |
| 129 | return process.env.USER; |
| 130 | } |
| 131 | |
| 132 | public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
| 133 | return targetPlatformId === TargetPlatformId.ANDROID; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Allows platform specific operations based on the user's OS. |
| 139 | */ |
| 140 | export class HostPlatform { |
| 141 | |
| 142 | private static platformInstance: IHostPlatform; |
| 143 | |
| 144 | /** |
| 145 | * Resolves the dev machine, desktop platform. |
| 146 | */ |
| 147 | private static get platform(): IHostPlatform { |
| 148 | if (!HostPlatform.platformInstance) { |
| 149 | switch (process.platform) { |
| 150 | case "win32": |
| 151 | HostPlatform.platformInstance = new WindowsHostPlatform(); |
| 152 | break; |
| 153 | case "darwin": |
| 154 | HostPlatform.platformInstance = new OSXHostPlatform(); |
| 155 | break; |
| 156 | case "linux": |
| 157 | HostPlatform.platformInstance = new LinuxHostPlatform(); |
| 158 | break; |
| 159 | default: |
| 160 | HostPlatform.platformInstance = new LinuxHostPlatform(); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 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 { |
| 177 | return HostPlatform.platform.getNpmCliCommand(packageName); |
| 178 | } |
| 179 | |
| 180 | public static getPipePath(pipeName: string): string { |
| 181 | return HostPlatform.platform.getPipePath(pipeName); |
| 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); |
| 190 | } |
| 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 | } |
| 196 | |
| 197 | public static isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean { |
| 198 | return HostPlatform.platform.isCompatibleWithTarget(targetPlatformId); |
| 199 | } |
| 200 | } |
| 201 | |