Skip to content

Commit 34128c1

Browse files
Rationalize (and improve) code to detect current platform
1 parent 7e3f874 commit 34128c1

File tree

6 files changed

+223
-119
lines changed

6 files changed

+223
-119
lines changed

src/common.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as cp from 'child_process';
7+
8+
let extensionPath: string;
9+
10+
export function setExtensionPath(path: string) {
11+
extensionPath = path;
12+
}
13+
14+
export function getExtensionPath() {
15+
if (!extensionPath) {
16+
throw new Error('Failed to set extension path');
17+
}
18+
19+
return extensionPath;
20+
}
21+
22+
export function execChildProcess(command: string, workingDirectory: string = getExtensionPath()): Promise<string> {
23+
return new Promise<string>((resolve, reject) => {
24+
cp.exec(command, { cwd: workingDirectory, maxBuffer: 500 * 1024 }, (error, stdout, stderr) => {
25+
if (error) {
26+
reject(error);
27+
}
28+
else if (stderr && stderr.length > 0) {
29+
reject(new Error(stderr));
30+
}
31+
else {
32+
resolve(stdout);
33+
}
34+
});
35+
});
36+
}

src/coreclr-debug/activate.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as fs from 'fs';
99
import TelemetryReporter from 'vscode-extension-telemetry';
1010
import { CoreClrDebugUtil } from './util';
1111
import * as debugInstall from './install';
12-
import { Platform, getCurrentPlatform } from './../platform';
12+
import { PlatformInformation, OperatingSystem, CoreClrFlavor } from './../platform';
1313
import * as semver from 'semver';
1414

1515
const MINIMUM_SUPPORTED_DOTNET_CLI: string = '1.0.0-preview2-003121';
@@ -166,35 +166,34 @@ function deleteInstallBeginFile() {
166166
}
167167

168168
function getPlatformRuntimeId(): Promise<string> {
169-
switch (process.platform) {
170-
case 'win32':
171-
return Promise.resolve('win7-x64');
172-
case 'darwin':
173-
return Promise.resolve('osx.10.11-x64');
174-
case 'linux':
175-
return getCurrentPlatform().then(platform => {
176-
switch (platform)
177-
{
178-
case Platform.CentOS:
179-
return 'centos.7-x64';
180-
case Platform.Fedora:
181-
return 'fedora.23-x64';
182-
case Platform.OpenSUSE:
183-
return 'opensuse.13.2-x64';
184-
case Platform.RHEL:
185-
return 'rhel.7-x64';
186-
case Platform.Debian:
187-
return 'debian.8-x64';
188-
case Platform.Ubuntu14:
189-
return 'ubuntu.14.04-x64';
190-
case Platform.Ubuntu16:
191-
return 'ubuntu.16.04-x64';
192-
default:
193-
throw Error('Error: Unsupported linux platform');
169+
return PlatformInformation.GetCurrent().then(info => {
170+
switch (info.getCoreClrFlavor()) {
171+
case CoreClrFlavor.Windows:
172+
return 'win7-x64';
173+
case CoreClrFlavor.OSX:
174+
return 'osx.10.11-x64';
175+
case CoreClrFlavor.CentOS:
176+
return 'centos.7-x64';
177+
case CoreClrFlavor.Fedora:
178+
return 'fedora.23-x64';
179+
case CoreClrFlavor.OpenSUSE:
180+
return 'opensuse.13.2-x64';
181+
case CoreClrFlavor.RHEL:
182+
return 'rhel.7-x64';
183+
case CoreClrFlavor.Debian:
184+
return 'debian.8-x64';
185+
case CoreClrFlavor.Ubuntu14:
186+
return 'ubuntu.14.04-x64';
187+
case CoreClrFlavor.Ubuntu16:
188+
return 'ubuntu.16.04-x64';
189+
190+
default:
191+
if (info.operatingSystem == OperatingSystem.Linux) {
192+
throw new Error('Error: Unsupported linux platform');
194193
}
195-
});
196-
default:
197-
_util.log('Error: Unsupported platform ' + process.platform);
198-
throw Error('Unsupported platform ' + process.platform);
199-
}
194+
195+
_util.log('Error: Unsupported platform ' + process.platform);
196+
throw Error('Unsupported platform ' + process.platform);
197+
}
198+
});
200199
}

src/omnisharp/download.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
* to be usable outside of VS Code.
99
*/
1010

11-
'use strict';
12-
1311
import * as fs from 'fs-extra-promise';
1412
import * as path from 'path';
1513
import * as https from 'https';
1614
import * as stream from 'stream';
1715
import * as tmp from 'tmp';
1816
import {parse} from 'url';
1917
import {Flavor, getInstallDirectory} from './omnisharp';
20-
import {Platform} from '../platform';
18+
import {PlatformInformation, CoreClrFlavor} from '../platform';
2119
import {getProxyAgent} from '../proxy';
2220
import {Logger} from './logger';
2321

@@ -28,46 +26,44 @@ const OmniSharpVersion = '1.9-beta18';
2826

2927
tmp.setGracefulCleanup();
3028

31-
function getDownloadFileName(flavor: Flavor, platform: Platform): string {
29+
function getDownloadFileName(flavor: Flavor, coreClrFlavor: CoreClrFlavor): string {
3230
let fileName = `omnisharp-${OmniSharpVersion}-`;
3331

3432
if (flavor === Flavor.CoreCLR) {
35-
switch (platform) {
36-
case Platform.Windows:
33+
switch (coreClrFlavor) {
34+
case CoreClrFlavor.Windows:
3735
fileName += 'win-x64-netcoreapp1.0.zip';
3836
break;
39-
case Platform.OSX:
37+
case CoreClrFlavor.OSX:
4038
fileName += 'osx-x64-netcoreapp1.0.tar.gz';
4139
break;
42-
case Platform.CentOS:
40+
case CoreClrFlavor.CentOS:
4341
fileName += 'centos-x64-netcoreapp1.0.tar.gz';
4442
break;
45-
case Platform.Debian:
43+
case CoreClrFlavor.Debian:
4644
fileName += 'debian-x64-netcoreapp1.0.tar.gz';
4745
break;
48-
case Platform.Fedora:
46+
case CoreClrFlavor.Fedora:
4947
fileName += 'fedora-x64-netcoreapp1.0.tar.gz';
5048
break;
51-
case Platform.OpenSUSE:
49+
case CoreClrFlavor.OpenSUSE:
5250
fileName += 'opensuse-x64-netcoreapp1.0.tar.gz';
5351
break;
54-
case Platform.RHEL:
52+
case CoreClrFlavor.RHEL:
5553
fileName += 'rhel-x64-netcoreapp1.0.tar.gz';
5654
break;
57-
case Platform.Ubuntu14:
55+
case CoreClrFlavor.Ubuntu14:
5856
fileName += 'ubuntu14-x64-netcoreapp1.0.tar.gz';
5957
break;
60-
case Platform.Ubuntu16:
58+
case CoreClrFlavor.Ubuntu16:
6159
fileName += 'ubuntu16-x64-netcoreapp1.0.tar.gz';
6260
break;
63-
6461
default:
6562
if (process.platform === 'linux') {
66-
throw new Error(`Unsupported linux distribution`);
67-
}
68-
else {
69-
throw new Error(`Unsupported platform: ${process.platform}`);
63+
throw new Error('Unsupported linux distribution');
7064
}
65+
66+
throw new Error(`Unsupported platform: ${process.platform}`);
7167
}
7268
}
7369
else if (flavor === Flavor.Desktop) {
@@ -109,9 +105,9 @@ function download(urlString: string, proxy?: string, strictSSL?: boolean): Promi
109105
});
110106
}
111107

112-
export function go(flavor: Flavor, platform: Platform, logger: Logger, proxy?: string, strictSSL?: boolean) {
108+
export function go(flavor: Flavor, coreClrFlavor: CoreClrFlavor, logger: Logger, proxy?: string, strictSSL?: boolean) {
113109
return new Promise<boolean>((resolve, reject) => {
114-
const fileName = getDownloadFileName(flavor, platform);
110+
const fileName = getDownloadFileName(flavor, coreClrFlavor);
115111
const installDirectory = getInstallDirectory(flavor);
116112

117113
logger.appendLine(`Installing OmniSharp to ${installDirectory}`);

src/omnisharp/launcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import {spawn, ChildProcess} from 'child_process';
99
import {satisfies} from 'semver';
10-
import {Platform, getCurrentPlatform} from '../platform';
10+
import {PlatformInformation, OperatingSystem} from '../platform';
1111
import * as omnisharp from './omnisharp';
1212
import * as path from 'path';
1313
import * as vscode from 'vscode';
@@ -172,8 +172,8 @@ export function launchOmniSharp(details: LaunchDetails): Promise<LaunchResult> {
172172
}
173173

174174
function launch(details: LaunchDetails): Promise<LaunchResult> {
175-
return getCurrentPlatform().then(platform => {
176-
if (platform === Platform.Windows) {
175+
return PlatformInformation.GetCurrent().then(platform => {
176+
if (platform.operatingSystem === OperatingSystem.Windows) {
177177
return launchWindows(details);
178178
}
179179
else {

src/omnisharp/server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {readOptions} from './options';
1717
import {Logger} from './logger';
1818
import {DelayTracker} from './delayTracker';
1919
import {LaunchTarget, findLaunchTargets, getDefaultFlavor} from './launcher';
20-
import {Platform, getCurrentPlatform} from '../platform';
20+
import {PlatformInformation, OperatingSystem} from '../platform';
2121
import TelemetryReporter from 'vscode-extension-telemetry';
2222
import * as vscode from 'vscode';
2323

@@ -423,8 +423,8 @@ export abstract class OmnisharpServer {
423423
}).catch(err => {
424424
return omnisharp.findServerPath(installDirectory);
425425
}).catch(err => {
426-
return getCurrentPlatform().then(platform => {
427-
if (platform === Platform.Unknown && process.platform === 'linux') {
426+
return PlatformInformation.GetCurrent().then(platform => {
427+
if (platform.operatingSystem === OperatingSystem.Linux && !platform.hasCoreClrFlavor()) {
428428
this._channel.appendLine("[ERROR] Could not locate an OmniSharp server that supports your Linux distribution.");
429429
this._channel.appendLine("");
430430
this._channel.appendLine("OmniSharp provides a richer C# editing experience, with features like IntelliSense and Find All References.");
@@ -447,7 +447,7 @@ export abstract class OmnisharpServer {
447447

448448
this._fireEvent(Events.BeforeServerInstall, this);
449449

450-
return download.go(flavor, platform, this._logger, proxy, strictSSL).then(_ => {
450+
return download.go(flavor, platform.getCoreClrFlavor(), this._logger, proxy, strictSSL).then(_ => {
451451
return omnisharp.findServerPath(installDirectory);
452452
});
453453
});

0 commit comments

Comments
 (0)