microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/hostPlatform.ts
160lines · 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"; |
8411fd8dDaniel Lebu10 years ago | 5 | import * as path from "path"; |
62d9cdd3dlebu10 years ago | 6 | import * as Q from "q"; |
8411fd8dDaniel Lebu10 years ago | 7 | |
| 8 | /** | |
| 9 | * Interface defining the host (desktop) platform specific operations. | |
| 10 | */ | |
a1005420dlebu10 years ago | 11 | interface IHostPlatform { |
8411fd8dDaniel Lebu10 years ago | 12 | getUserHomePath(): string; |
| 13 | getSettingsHome(): string; | |
a9955be6dlebu10 years ago | 14 | getNpmCliCommand(packageName: string): string; |
8411fd8dDaniel Lebu10 years ago | 15 | getExtensionPipePath(): string; |
| 16 | getPlatformId(): HostPlatformId; | |
62d9cdd3dlebu10 years ago | 17 | setEnvironmentVariable(name: string, value: string): Q.Promise<void>; |
8411fd8dDaniel Lebu10 years ago | 18 | } |
| 19 | | |
| 20 | /** | |
| 21 | * Defines the identifiers of all the platforms we support. | |
| 22 | */ | |
| 23 | export enum HostPlatformId { | |
| 24 | WINDOWS, | |
bbdbbb3bdlebu10 years ago | 25 | OSX, |
| 26 | LINUX | |
8411fd8dDaniel Lebu10 years ago | 27 | } |
| 28 | | |
| 29 | /** | |
| 30 | * IHostPlatform implemenation for the Windows platform. | |
| 31 | */ | |
| 32 | class WindowsHostPlatform implements IHostPlatform { | |
| 33 | public getUserHomePath(): string { | |
| 34 | return process.env.USERPROFILE; | |
| 35 | } | |
| 36 | | |
62d9cdd3dlebu10 years ago | 37 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { |
a1005420dlebu10 years ago | 38 | return new ChildProcess().exec(`setx ${name} ${value}`).outcome; |
8411fd8dDaniel Lebu10 years ago | 39 | } |
| 40 | | |
| 41 | public getSettingsHome(): string { | |
| 42 | return path.join(process.env.APPDATA, "vscode-react-native"); | |
| 43 | } | |
| 44 | | |
a9955be6dlebu10 years ago | 45 | public getNpmCliCommand(cliName: string): string { |
| 46 | return `${cliName}.cmd`; | |
8411fd8dDaniel Lebu10 years ago | 47 | } |
| 48 | | |
| 49 | public getExtensionPipePath(): string { | |
| 50 | return "\\\\?\\pipe\\vscodereactnative"; | |
| 51 | } | |
| 52 | | |
| 53 | public getPlatformId(): HostPlatformId { | |
| 54 | return HostPlatformId.WINDOWS; | |
| 55 | } | |
| 56 | } | |
| 57 | | |
bbdbbb3bdlebu10 years ago | 58 | abstract class UnixHostPlatform implements IHostPlatform { |
8411fd8dDaniel Lebu10 years ago | 59 | public getUserHomePath(): string { |
| 60 | return process.env.HOME; | |
| 61 | } | |
| 62 | | |
bbdbbb3bdlebu10 years ago | 63 | public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>; |
8411fd8dDaniel Lebu10 years ago | 64 | |
| 65 | public getSettingsHome(): string { | |
| 66 | return path.join(process.env.HOME, ".vscode-react-native"); | |
| 67 | } | |
| 68 | | |
a9955be6dlebu10 years ago | 69 | public getNpmCliCommand(packageName: string): string { |
bbdbbb3bdlebu10 years ago | 70 | return packageName; |
8411fd8dDaniel Lebu10 years ago | 71 | } |
| 72 | | |
| 73 | public getExtensionPipePath(): string { | |
| 74 | return "/tmp/vscodereactnative.sock"; | |
| 75 | } | |
| 76 | | |
bbdbbb3bdlebu10 years ago | 77 | public abstract getPlatformId(): HostPlatformId; |
8411fd8dDaniel Lebu10 years ago | 78 | } |
| 79 | | |
bbdbbb3bdlebu10 years ago | 80 | /** |
| 81 | * IHostPlatform implemenation for the OSX platform. | |
| 82 | */ | |
| 83 | class OSXHostPlatform extends UnixHostPlatform { | |
| 84 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { | |
a1005420dlebu10 years ago | 85 | return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome; |
bbdbbb3bdlebu10 years ago | 86 | } |
| 87 | | |
| 88 | public getPlatformId(): HostPlatformId { | |
| 89 | return HostPlatformId.OSX; | |
| 90 | } | |
| 91 | } | |
| 92 | | |
| 93 | /** | |
| 94 | * IHostPlatform implemenation for the Linux platform. | |
| 95 | */ | |
| 96 | class LinuxHostPlatform extends UnixHostPlatform { | |
| 97 | public setEnvironmentVariable(name: string, value: string): Q.Promise<any> { | |
a1005420dlebu10 years ago | 98 | return new ChildProcess().exec(`export ${name}=${value}`).outcome; |
bbdbbb3bdlebu10 years ago | 99 | } |
| 100 | | |
| 101 | public getPlatformId(): HostPlatformId { | |
| 102 | return HostPlatformId.LINUX; | |
| 103 | } | |
| 104 | } | |
| 105 | | |
8411fd8dDaniel Lebu10 years ago | 106 | /** |
a1005420dlebu10 years ago | 107 | * Allows platform specific operations based on the user's OS. |
8411fd8dDaniel Lebu10 years ago | 108 | */ |
a1005420dlebu10 years ago | 109 | export class HostPlatform { |
8411fd8dDaniel Lebu10 years ago | 110 | |
a1005420dlebu10 years ago | 111 | private static platformInstance: IHostPlatform; |
8411fd8dDaniel Lebu10 years ago | 112 | |
| 113 | /** | |
| 114 | * Resolves the dev machine, desktop platform. | |
| 115 | */ | |
a1005420dlebu10 years ago | 116 | private static get platform(): IHostPlatform { |
| 117 | if (!HostPlatform.platformInstance) { | |
| 118 | switch (process.platform) { | |
| 119 | case "win32": | |
| 120 | HostPlatform.platformInstance = new WindowsHostPlatform(); | |
9fe4ca93digeff10 years ago | 121 | break; |
a1005420dlebu10 years ago | 122 | case "darwin": |
| 123 | HostPlatform.platformInstance = new OSXHostPlatform(); | |
9fe4ca93digeff10 years ago | 124 | break; |
a1005420dlebu10 years ago | 125 | case "linux": |
c6d7ee27dlebu10 years ago | 126 | HostPlatform.platformInstance = new LinuxHostPlatform(); |
| 127 | break; | |
a1005420dlebu10 years ago | 128 | default: |
| 129 | HostPlatform.platformInstance = new LinuxHostPlatform(); | |
aab2095edigeff10 years ago | 130 | break; |
a1005420dlebu10 years ago | 131 | } |
8411fd8dDaniel Lebu10 years ago | 132 | } |
a1005420dlebu10 years ago | 133 | |
| 134 | return HostPlatform.platformInstance; | |
| 135 | } | |
| 136 | | |
| 137 | public static getUserHomePath(): string { | |
| 138 | return HostPlatform.platform.getUserHomePath(); | |
| 139 | } | |
| 140 | | |
| 141 | public static getSettingsHome(): string { | |
| 142 | return HostPlatform.platform.getSettingsHome(); | |
| 143 | } | |
| 144 | | |
| 145 | public static getNpmCliCommand(packageName: string): string { | |
a9955be6dlebu10 years ago | 146 | return HostPlatform.platform.getNpmCliCommand(packageName); |
a1005420dlebu10 years ago | 147 | } |
| 148 | | |
| 149 | public static getExtensionPipePath(): string { | |
| 150 | return HostPlatform.platform.getExtensionPipePath(); | |
| 151 | } | |
| 152 | | |
| 153 | public static getPlatformId(): HostPlatformId { | |
| 154 | return HostPlatform.platform.getPlatformId(); | |
| 155 | } | |
| 156 | | |
| 157 | public static setEnvironmentVariable(name: string, value: string): Q.Promise<void> { | |
| 158 | return HostPlatform.platform.setEnvironmentVariable(name, value); | |
8411fd8dDaniel Lebu10 years ago | 159 | } |
| 160 | } |