microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.5.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/hostPlatform.ts

200lines · modeblame

8411fd8dDaniel Lebu10 years ago1// 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 ago4import {ChildProcess} from "./node/childProcess";
aeaf46bcMeena Kunnathur Balakrishnan10 years ago5import {TargetPlatformId} from "./targetPlatformHelper";
8411fd8dDaniel Lebu10 years ago6import * as path from "path";
62d9cdd3dlebu10 years ago7import * as Q from "q";
8411fd8dDaniel Lebu10 years ago8
9/**
10* Interface defining the host (desktop) platform specific operations.
11*/
a1005420dlebu10 years ago12interface IHostPlatform {
8411fd8dDaniel Lebu10 years ago13getUserHomePath(): string;
14getSettingsHome(): string;
a9955be6dlebu10 years ago15getNpmCliCommand(packageName: string): string;
d3950d7edigeff10 years ago16getPipePath(pipeName: string): string;
8411fd8dDaniel Lebu10 years ago17getPlatformId(): HostPlatformId;
62d9cdd3dlebu10 years ago18setEnvironmentVariable(name: string, value: string): Q.Promise<void>;
d3950d7edigeff10 years ago19getUserID(): string;
a98974c3Daniel Lebu10 years ago20isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean;
8411fd8dDaniel Lebu10 years ago21}
22
23/**
24* Defines the identifiers of all the platforms we support.
25*/
26export enum HostPlatformId {
27WINDOWS,
bbdbbb3bdlebu10 years ago28OSX,
27710197Vladimir Kotikov8 years ago29LINUX,
8411fd8dDaniel Lebu10 years ago30}
31
32/**
33* IHostPlatform implemenation for the Windows platform.
34*/
35class WindowsHostPlatform implements IHostPlatform {
36public getUserHomePath(): string {
37return process.env.USERPROFILE;
38}
39
62d9cdd3dlebu10 years ago40public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
a1005420dlebu10 years ago41return new ChildProcess().exec(`setx ${name} ${value}`).outcome;
8411fd8dDaniel Lebu10 years ago42}
43
44public getSettingsHome(): string {
45return path.join(process.env.APPDATA, "vscode-react-native");
46}
47
a9955be6dlebu10 years ago48public getNpmCliCommand(cliName: string): string {
49return `${cliName}.cmd`;
8411fd8dDaniel Lebu10 years ago50}
51
d3950d7edigeff10 years ago52public getPipePath(pipeName: string): string {
7daed3fcArtem Egorov8 years ago53return `//?/pipe/${pipeName}`;
8411fd8dDaniel Lebu10 years ago54}
55
56public getPlatformId(): HostPlatformId {
57return HostPlatformId.WINDOWS;
58}
d3950d7edigeff10 years ago59
60public getUserID(): string {
61return process.env.USERNAME;
62}
fced706ddigeff10 years ago63
a98974c3Daniel Lebu10 years ago64public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
1c32fe84Patricio Beltran9 years ago65return targetPlatformId === TargetPlatformId.ANDROID || targetPlatformId === TargetPlatformId.EXPONENT;
42fac1f6Meena Kunnathur Balakrishnan10 years ago66}
8411fd8dDaniel Lebu10 years ago67}
68
bbdbbb3bdlebu10 years ago69abstract class UnixHostPlatform implements IHostPlatform {
8411fd8dDaniel Lebu10 years ago70public getUserHomePath(): string {
71return process.env.HOME;
72}
73
bbdbbb3bdlebu10 years ago74public abstract setEnvironmentVariable(name: string, value: string): Q.Promise<any>;
8411fd8dDaniel Lebu10 years ago75
76public getSettingsHome(): string {
77return path.join(process.env.HOME, ".vscode-react-native");
78}
79
a9955be6dlebu10 years ago80public getNpmCliCommand(packageName: string): string {
bbdbbb3bdlebu10 years ago81return packageName;
8411fd8dDaniel Lebu10 years ago82}
83
d3950d7edigeff10 years ago84public getPipePath(pipeName: string): string {
85return `/tmp/${pipeName}.sock`;
8411fd8dDaniel Lebu10 years ago86}
87
bbdbbb3bdlebu10 years ago88public abstract getPlatformId(): HostPlatformId;
d3950d7edigeff10 years ago89
90public abstract getUserID(): string;
fced706ddigeff10 years ago91
a98974c3Daniel Lebu10 years ago92public abstract isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean;
8411fd8dDaniel Lebu10 years ago93}
94
bbdbbb3bdlebu10 years ago95/**
96* IHostPlatform implemenation for the OSX platform.
97*/
98class OSXHostPlatform extends UnixHostPlatform {
99public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
a1005420dlebu10 years ago100return new ChildProcess().exec(`launchctl setenv ${name} ${value}`).outcome;
bbdbbb3bdlebu10 years ago101}
102
103public getPlatformId(): HostPlatformId {
104return HostPlatformId.OSX;
105}
d3950d7edigeff10 years ago106
107public getUserID(): string {
108return process.env.LOGNAME;
109}
fced706ddigeff10 years ago110
a98974c3Daniel Lebu10 years ago111public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
1c32fe84Patricio Beltran9 years ago112return targetPlatformId === TargetPlatformId.ANDROID || targetPlatformId === TargetPlatformId.IOS || targetPlatformId === TargetPlatformId.EXPONENT;
e09724a1Meena Kunnathur Balakrishnan10 years ago113}
bbdbbb3bdlebu10 years ago114}
115
116/**
117* IHostPlatform implemenation for the Linux platform.
118*/
119class LinuxHostPlatform extends UnixHostPlatform {
120public setEnvironmentVariable(name: string, value: string): Q.Promise<any> {
a1005420dlebu10 years ago121return new ChildProcess().exec(`export ${name}=${value}`).outcome;
bbdbbb3bdlebu10 years ago122}
123
124public getPlatformId(): HostPlatformId {
125return HostPlatformId.LINUX;
126}
d3950d7edigeff10 years ago127
128public getUserID(): string {
129return process.env.USER;
130}
fced706ddigeff10 years ago131
a98974c3Daniel Lebu10 years ago132public isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
1c32fe84Patricio Beltran9 years ago133return targetPlatformId === TargetPlatformId.ANDROID || targetPlatformId === TargetPlatformId.EXPONENT;
e09724a1Meena Kunnathur Balakrishnan10 years ago134}
bbdbbb3bdlebu10 years ago135}
136
8411fd8dDaniel Lebu10 years ago137/**
a1005420dlebu10 years ago138* Allows platform specific operations based on the user's OS.
8411fd8dDaniel Lebu10 years ago139*/
a1005420dlebu10 years ago140export class HostPlatform {
8411fd8dDaniel Lebu10 years ago141
a1005420dlebu10 years ago142private static platformInstance: IHostPlatform;
8411fd8dDaniel Lebu10 years ago143
144/**
145* Resolves the dev machine, desktop platform.
146*/
a1005420dlebu10 years ago147private static get platform(): IHostPlatform {
148if (!HostPlatform.platformInstance) {
149switch (process.platform) {
150case "win32":
151HostPlatform.platformInstance = new WindowsHostPlatform();
9fe4ca93digeff10 years ago152break;
a1005420dlebu10 years ago153case "darwin":
154HostPlatform.platformInstance = new OSXHostPlatform();
9fe4ca93digeff10 years ago155break;
a1005420dlebu10 years ago156case "linux":
c6d7ee27dlebu10 years ago157HostPlatform.platformInstance = new LinuxHostPlatform();
158break;
a1005420dlebu10 years ago159default:
160HostPlatform.platformInstance = new LinuxHostPlatform();
aab2095edigeff10 years ago161break;
a1005420dlebu10 years ago162}
8411fd8dDaniel Lebu10 years ago163}
a1005420dlebu10 years ago164
165return HostPlatform.platformInstance;
166}
167
168public static getUserHomePath(): string {
169return HostPlatform.platform.getUserHomePath();
170}
171
172public static getSettingsHome(): string {
173return HostPlatform.platform.getSettingsHome();
174}
175
176public static getNpmCliCommand(packageName: string): string {
a9955be6dlebu10 years ago177return HostPlatform.platform.getNpmCliCommand(packageName);
a1005420dlebu10 years ago178}
179
d3950d7edigeff10 years ago180public static getPipePath(pipeName: string): string {
181return HostPlatform.platform.getPipePath(pipeName);
a1005420dlebu10 years ago182}
183
184public static getPlatformId(): HostPlatformId {
185return HostPlatform.platform.getPlatformId();
186}
187
188public static setEnvironmentVariable(name: string, value: string): Q.Promise<void> {
189return HostPlatform.platform.setEnvironmentVariable(name, value);
8411fd8dDaniel Lebu10 years ago190}
d3950d7edigeff10 years ago191
192/* Returns a value that is unique for each user of this computer */
193public static getUserID(): string {
194return HostPlatform.platform.getUserID();
195}
fced706ddigeff10 years ago196
a98974c3Daniel Lebu10 years ago197public static isCompatibleWithTarget(targetPlatformId: TargetPlatformId): boolean {
198return HostPlatform.platform.isCompatibleWithTarget(targetPlatformId);
42fac1f6Meena Kunnathur Balakrishnan10 years ago199}
8411fd8dDaniel Lebu10 years ago200}