|
| 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 | +'use strict'; |
| 6 | + |
| 7 | +import * as vscode from 'vscode'; |
| 8 | +import * as child_process from 'child_process'; |
| 9 | +import * as fs from 'fs'; |
| 10 | +import * as path from 'path'; |
| 11 | +import TelemetryReporter from 'vscode-extension-telemetry'; |
| 12 | +import { CoreClrDebugUtil } from './util'; |
| 13 | +import * as debugInstall from './install'; |
| 14 | + |
| 15 | +let _reporter: TelemetryReporter = null; |
| 16 | +let _channel: vscode.OutputChannel = null; |
| 17 | +let _util: CoreClrDebugUtil = null; |
| 18 | + |
| 19 | +export function activate(context: vscode.ExtensionContext, reporter: TelemetryReporter) { |
| 20 | + _reporter = reporter; |
| 21 | + _channel = vscode.window.createOutputChannel('coreclr-debug'); |
| 22 | + _util = new CoreClrDebugUtil(context.extensionPath, _channel); |
| 23 | + |
| 24 | + if (CoreClrDebugUtil.existsSync(_util.installCompleteFilePath())) { |
| 25 | + console.log('.NET Core Debugger tools already installed'); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + if (!isOnPath('dotnet')) { |
| 30 | + const getDotNetMessage = "Get .NET CLI tools"; |
| 31 | + vscode.window.showErrorMessage("The .NET CLI tools cannot be located. .NET Core debugging will not be enabled. Make sure .NET CLI tools are installed and are on the path.", |
| 32 | + getDotNetMessage).then(value => { |
| 33 | + if (value === getDotNetMessage) { |
| 34 | + let open = require('open'); |
| 35 | + open("http://dotnet.github.io/getting-started/"); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + let installer = new debugInstall.DebugInstaller(_util); |
| 43 | + _util.createInstallLog(); |
| 44 | + |
| 45 | + let runtimeId = getPlatformRuntimeId(); |
| 46 | + |
| 47 | + let statusBarMessage = vscode.window.setStatusBarMessage("Downloading and configuring the .NET Core Debugger..."); |
| 48 | + |
| 49 | + let installStage = "installBegin"; |
| 50 | + let installError = ""; |
| 51 | + |
| 52 | + writeInstallBeginFile().then(() => { |
| 53 | + return installer.install(runtimeId); |
| 54 | + }).then(() => { |
| 55 | + installStage = "completeSuccess"; |
| 56 | + statusBarMessage.dispose(); |
| 57 | + vscode.window.setStatusBarMessage('Successfully installed .NET Core Debugger.'); |
| 58 | + }) |
| 59 | + .catch((error: debugInstall.InstallError) => { |
| 60 | + const viewLogMessage = "View Log"; |
| 61 | + vscode.window.showErrorMessage('Error while installing .NET Core Debugger.', viewLogMessage).then(value => { |
| 62 | + if (value === viewLogMessage) { |
| 63 | + _channel.show(vscode.ViewColumn.Three); |
| 64 | + } |
| 65 | + }); |
| 66 | + statusBarMessage.dispose(); |
| 67 | + |
| 68 | + installStage = error.installStage; |
| 69 | + installError = error.installError; |
| 70 | + }).then(() => { |
| 71 | + // log telemetry and delete install begin file |
| 72 | + logTelemetry('Acquisition', {installStage: installStage, installError: installError}); |
| 73 | + try { |
| 74 | + deleteInstallBeginFile(); |
| 75 | + } catch (err) { |
| 76 | + // if this throws there's really nothing we can do |
| 77 | + } |
| 78 | + _util.closeInstallLog(); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +function logTelemetry(eventName: string, properties?: {[prop: string]: string}): void { |
| 83 | + if (_reporter !== null) { |
| 84 | + _reporter.sendTelemetryEvent('coreclr-debug/' + eventName, properties); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function writeInstallBeginFile() : Promise<void> { |
| 89 | + return CoreClrDebugUtil.writeEmptyFile(_util.installBeginFilePath()); |
| 90 | +} |
| 91 | + |
| 92 | +function deleteInstallBeginFile() { |
| 93 | + if (CoreClrDebugUtil.existsSync(_util.installBeginFilePath())) { |
| 94 | + fs.unlinkSync(_util.installBeginFilePath()); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// Determines if the specified command is in one of the directories in the PATH environment variable. |
| 99 | +function isOnPath(command : string) : boolean { |
| 100 | + let pathValue = process.env['PATH']; |
| 101 | + if (!pathValue) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + let fileName = command; |
| 105 | + if (process.platform == 'win32') { |
| 106 | + // on Windows, add a '.exe', and the path is semi-colon seperatode |
| 107 | + fileName = fileName + ".exe"; |
| 108 | + } |
| 109 | + |
| 110 | + let pathSegments: string[] = pathValue.split(path.delimiter); |
| 111 | + for (let segment of pathSegments) { |
| 112 | + if (segment.length === 0 || !path.isAbsolute(segment)) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + const segmentPath = path.join(segment, fileName); |
| 117 | + |
| 118 | + try { |
| 119 | + if (CoreClrDebugUtil.existsSync(segmentPath)) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + } catch (err) { |
| 123 | + // any error from existsSync can be treated as the command not being on the path |
| 124 | + continue; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return false; |
| 129 | +} |
| 130 | + |
| 131 | +function getPlatformRuntimeId() : string { |
| 132 | + switch (process.platform) { |
| 133 | + case 'win32': |
| 134 | + return 'win7-x64'; |
| 135 | + case 'darwin': |
| 136 | + return getDotnetRuntimeId(); |
| 137 | + case 'linux': |
| 138 | + return getDotnetRuntimeId(); |
| 139 | + default: |
| 140 | + _util.log('Error: Unsupported platform ' + process.platform); |
| 141 | + throw Error('Unsupported platform ' + process.platform); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +function getDotnetRuntimeId(): string { |
| 146 | + _util.log("Starting 'dotnet --info'"); |
| 147 | + |
| 148 | + const cliVersionErrorMessage = "Ensure that .NET Core CLI Tools version >= 1.0.0-beta-002173 is installed. Run 'dotnet --version' to see what version is installed."; |
| 149 | + |
| 150 | + let child = child_process.spawnSync('dotnet', ['--info'], { cwd: _util.coreClrDebugDir() }); |
| 151 | + |
| 152 | + if (child.stderr.length > 0) { |
| 153 | + _util.log('Error: ' + child.stderr.toString()); |
| 154 | + } |
| 155 | + const out = child.stdout.toString(); |
| 156 | + if (out.length > 0) { |
| 157 | + _util.log(out); |
| 158 | + } |
| 159 | + |
| 160 | + if (child.status !== 0) { |
| 161 | + const message = `Error: 'dotnet --info' failed with error ${child.status}`; |
| 162 | + _util.log(message); |
| 163 | + _util.log(cliVersionErrorMessage); |
| 164 | + throw new Error(message); |
| 165 | + } |
| 166 | + |
| 167 | + if (out.length === 0) { |
| 168 | + const message = "Error: 'dotnet --info' provided no output"; |
| 169 | + _util.log(message); |
| 170 | + _util.log(cliVersionErrorMessage); |
| 171 | + throw new Error(message); |
| 172 | + } |
| 173 | + |
| 174 | + let lines = out.split('\n'); |
| 175 | + let ridLine = lines.filter(value => { |
| 176 | + return value.trim().startsWith('RID:'); |
| 177 | + }); |
| 178 | + |
| 179 | + if (ridLine.length < 1) { |
| 180 | + _util.log("Error: Cannot find 'RID' property"); |
| 181 | + _util.log(cliVersionErrorMessage); |
| 182 | + throw new Error('Cannot obtain Runtime ID from dotnet cli'); |
| 183 | + } |
| 184 | + |
| 185 | + let rid = ridLine[0].split(':')[1].trim(); |
| 186 | + |
| 187 | + if (!rid) { |
| 188 | + _util.log("Error: Unable to parse 'RID' property."); |
| 189 | + _util.log(cliVersionErrorMessage); |
| 190 | + throw new Error('Unable to determine Runtime ID'); |
| 191 | + } |
| 192 | + |
| 193 | + return rid; |
| 194 | +} |
0 commit comments