/
CliAcquisition.ts
195 lines (169 loc) · 8.23 KB
/
CliAcquisition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
// https://code.visualstudio.com/api/extension-capabilities/common-capabilities#output-channel
import * as nls from 'vscode-nls';
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
import * as path from 'path';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as os from 'os';
import { Extract } from 'unzip-stream'
import { ITelemetry } from '../telemetry/ITelemetry';
import find from 'find-process';
import { spawnSync } from 'child_process';
// allow for DI without direct reference to vscode's d.ts file: that definintions file is being generated at VS Code runtime
export interface ICliAcquisitionContext {
readonly extensionPath: string;
readonly globalStorageLocalPath: string;
readonly telemetry: ITelemetry;
showInformationMessage(message: string, ...items: string[]): void;
showErrorMessage(message: string, ...items: string[]): void;
}
export interface IDisposable {
dispose(): void;
}
export class CliAcquisition implements IDisposable {
private readonly _context: ICliAcquisitionContext;
private readonly _cliPath: string;
private readonly _cliVersion: string;
private readonly _nupkgsFolder: string;
private readonly _installedTrackerFile: string;
public get cliVersion(): string {
return this._cliVersion;
}
public get cliExePath(): string {
const execName = (os.platform() === 'win32') ? 'pac.exe' : 'pac';
return path.join(this._cliPath, 'tools', execName);
}
public constructor(context: ICliAcquisitionContext, cliVersion?: string) {
this._context = context;
this._nupkgsFolder = path.join(this._context.extensionPath, 'dist', 'pac');
this._cliVersion = cliVersion || this.getLatestNupkgVersion();
// https://code.visualstudio.com/api/extension-capabilities/common-capabilities#data-storage
this._cliPath = path.resolve(context.globalStorageLocalPath, 'pac');
this._installedTrackerFile = path.resolve(context.globalStorageLocalPath, 'installTracker.json');
}
public dispose(): void {
this._context.showInformationMessage('Bye');
}
public async ensureInstalled(): Promise<string> {
const basename = this.getNupkgBasename();
return this.installCli(path.join(this._context.extensionPath, 'dist', 'pac', `${basename}.${this.cliVersion}.nupkg`));
}
async installCli(pathToNupkg: string): Promise<string> {
const pacToolsPath = path.join(this._cliPath, 'tools');
if (this.isCliExpectedVersion()) {
return Promise.resolve(pacToolsPath);
}
// nupkg has not been extracted yet:
this._context.showInformationMessage(
localize({
key: "cliAquisition.preparingMessage",
comment: ["{0} represents the version number"]},
"Preparing pac CLI (v{0})...", this.cliVersion));
await this.killProcessesInUse(pacToolsPath);
fs.emptyDirSync(this._cliPath);
return new Promise((resolve, reject) => {
fs.createReadStream(pathToNupkg)
.pipe(Extract({ path: this._cliPath }))
.on('close', () => {
this._context.telemetry.sendTelemetryEvent('PacCliInstalled', { cliVersion: this.cliVersion });
this._context.showInformationMessage(localize("cliAquisition.successMessage", 'The pac CLI is ready for use in your VS Code terminal!'));
if (os.platform() !== 'win32') {
fs.chmodSync(this.cliExePath, 0o755);
}
this.setInstalledVersion(this._cliVersion);
resolve(pacToolsPath);
}).on('error', (err: unknown) => {
this._context.showErrorMessage(localize({
key: "cliAquisition.installationErrorMessage",
comment: ["{0} represents the error message returned from the exception"]},
"Cannot install pac CLI: {0}", String(err)));
reject(err);
})
});
}
isCliExpectedVersion(): boolean {
const installedVersion = this.getInstalledVersion();
if (!installedVersion) {
return false;
}
return installedVersion === this._cliVersion;
}
async killProcessesInUse(pacInstallPath: string): Promise<void> {
const list = await this.findPacProcesses(pacInstallPath);
list.forEach(info => process.kill(info.pid));
}
async findPacProcesses(pacInstallPath: string): Promise<{pid: number, cmd: string}[]> {
try {
// In most cases, find-process will handle the OS specifics to find the running
// Pac and PacTelemetryUpload processes
const processes = (await find('name', 'pac', true))
.concat(await find('name', 'pacTelemetryUpload', false)); // strict = false, as this may either be 'pacTelemetryProcess' or 'dotnet pacTelemetryProcess.dll'
// VS Code Install path and find-process disagree on the casing of "C:\" on Windows
return processes.filter(info => (os.platform() === 'win32')
? info.cmd.toLowerCase().includes(pacInstallPath.toLowerCase())
: info.cmd.includes(pacInstallPath));
} catch (err) {
// find-process fails with the WSL remoting pseudo terminal, so we'll need to call 'ps' ourselves
if (typeof err === "string" && err.includes("screen size is bogus") && os.platform() === "linux") {
const psResult = spawnSync("ps", ["ax","-ww","-o","pid,args"], {encoding: "utf-8"});
// Output is a single '\n' delimated string. First row is a header, the rest are in
// the format [optional left padding spaces][PID][single space][full command line arguments of the running process]
const processes = psResult.stdout.split(os.EOL)
.filter(line => line && line.includes(pacInstallPath))
.map(line => line.trimStart())
.map(line => line.split(' ', 2))
.map(split => ({pid: parseInt(split[0]), cmd: split[1]}));
return processes;
}
throw err;
}
}
getLatestNupkgVersion(): string {
const basename = this.getNupkgBasename();
const nuPkgExtension = '.nupkg';
const versions = glob.sync(`${basename}*${nuPkgExtension}`, { cwd: this._nupkgsFolder })
.map(file => file.substring(basename.length + 1).slice(0, -nuPkgExtension.length)) // isolate version part of file name
.filter(version => !isNaN(Number.parseInt(version.charAt(0)))) // expect version to start with number; dotnetCore and .NET pkg names share common base name
.sort();
if (versions.length < 1) {
throw new Error(`Corrupt .vsix? Did not find any *.nupkg files under: ${this._nupkgsFolder}`);
}
return versions[0];
}
getNupkgBasename(): string {
const platformName = os.platform();
switch (platformName) {
case 'win32':
return 'microsoft.powerapps.cli';
case 'darwin':
return 'microsoft.powerapps.cli.core.osx-x64';
case 'linux':
return 'microsoft.powerapps.cli.core.linux-x64';
default:
throw new Error(`Unsupported OS platform for pac CLI: ${platformName}`);
}
}
setInstalledVersion(version: string): void {
const trackerInfo = {
pac: version
};
fs.writeFileSync(this._installedTrackerFile, JSON.stringify(trackerInfo), 'utf-8');
}
getInstalledVersion(): string | undefined {
if (!fs.existsSync(this._installedTrackerFile)) {
return undefined;
}
try {
const trackerInfo = JSON.parse(fs.readFileSync(this._installedTrackerFile, 'utf-8'));
return trackerInfo.pac;
}
catch {
return undefined;
}
}
}