-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
TfxInstaller.ts
106 lines (90 loc) · 3.32 KB
/
TfxInstaller.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
///<reference path="../Common/Common.ts"/>
import * as toolLib from 'azure-pipelines-tool-lib/tool';
import * as taskLib from 'azure-pipelines-task-lib/task';
import * as tr from 'azure-pipelines-task-lib/toolrunner';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
async function run()
{
try {
const version = taskLib.getInput("version", true);
const checkLatest = taskLib.getBoolInput("checkLatest", false) || false;
await getTfx(version, checkLatest);
await taskLib.tool("tfx").arg(["version", "--no-color"]).exec();
}
catch (error) {
taskLib.setResult(taskLib.TaskResult.Failed, error.message);
}
}
async function getTfx(versionSpec: string, checkLatest: boolean) {
if (toolLib.isExplicitVersion(versionSpec)) {
checkLatest = false; // check latest doesn't make sense when explicit version
}
let toolPath: string;
if (!checkLatest) {
toolPath = toolLib.findLocalTool('tfx', versionSpec);
}
if (!toolPath) {
let version: string;
if (toolLib.isExplicitVersion(versionSpec)) {
version = versionSpec;
}
else {
version = queryLatestMatch(versionSpec);
if (!version) {
throw new Error(`Unable to find Tfx version '${versionSpec}'`);
}
toolPath = toolLib.findLocalTool('tfx', version);
}
if (!toolPath) {
toolPath = await acquireTfx(version);
}
}
if (os.platform() !== "win32")
{
toolPath = path.join(toolPath, "/node_modules/.bin/");
}
taskLib.setVariable("__tfxpath", toolPath, false);
toolLib.prependPath(toolPath);
}
function queryLatestMatch(versionSpec: string): string {
const npmRunner = new tr.ToolRunner("npm");
npmRunner.arg(["show", "tfx-cli", "versions", "--json"]);
const result = npmRunner.execSync({ failOnStdErr: false, silent: true, ignoreReturnCode: false} as tr.IExecOptions);
if (result.code === 0)
{
let versions: string[] = JSON.parse(result.stdout.trim());
let version: string = toolLib.evaluateVersions(versions, versionSpec);
return version;
}
return "";
}
async function acquireTfx(version: string): Promise<string> {
try{
version = toolLib.cleanVersion(version);
let extPath: string;
taskLib.assertAgent('2.115.0');
extPath = taskLib.getVariable('Agent.TempDirectory');
if (!extPath) {
throw new Error('Expected Agent.TempDirectory to be set');
}
extPath = path.join(extPath, 'tfx'); // use as short a path as possible due to nested node_modules folders
taskLib.mkdirP(path.join(extPath));
const npmRunner = new tr.ToolRunner("npm");
npmRunner.arg(["install", "tfx-cli@" + version, "--prefix", extPath]);
const result = npmRunner.execSync({ failOnStdErr: false, silent: true, ignoreReturnCode: false} as tr.IExecOptions);
if (result.code === 0)
{
if (os.platform() === "win32")
{
fs.unlinkSync(path.join(extPath, "/tfx"));
}
return await toolLib.cacheDir(extPath, 'tfx', version);
}
}
catch {
return Promise.reject(new Error("Failed to install tfx"));
}
}
void run();