forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.ts
More file actions
133 lines (120 loc) · 6.64 KB
/
tools.ts
File metadata and controls
133 lines (120 loc) · 6.64 KB
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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import { Platform } from "./util/platform";
import { Archive } from "./util/archive";
import { which } from "shelljs";
import { DownloadUtil } from "./util/download";
import hasha = require("hasha");
import * as vscode from 'vscode';
import * as path from 'path';
import * as fsex from 'fs-extra';
import * as fs from 'fs';
import { Cli } from './cli';
import semver = require('semver');
import configData = require('./tools.json');
export class ToolsConfig {
public static tools: object = ToolsConfig.loadMetadata(configData, Platform.OS);
public static loadMetadata(requirements, platform): object {
const reqs = JSON.parse(JSON.stringify(requirements));
for (const object in requirements) {
if (reqs[object].platform) {
if (reqs[object].platform[platform]) {
Object.assign(reqs[object], reqs[object].platform[platform]);
delete reqs[object].platform;
} else {
delete reqs[object];
}
}
}
return reqs;
}
public static resetConfiguration(): void {
ToolsConfig.tools = ToolsConfig.loadMetadata(configData, Platform.OS);
}
public static async detectOrDownload(cmd: string): Promise<string> {
let toolLocation: string = ToolsConfig.tools[cmd].location;
if (toolLocation === undefined) {
const toolCacheLocation = path.resolve(Platform.getUserHomePath(), '.vs-openshift', ToolsConfig.tools[cmd].cmdFileName);
const whichLocation = which(cmd);
const toolLocations: string[] = [whichLocation ? whichLocation.stdout : null, toolCacheLocation];
toolLocation = await ToolsConfig.selectTool(toolLocations, ToolsConfig.tools[cmd].versionRange);
if (toolLocation === undefined) {
// otherwise request permission to download
const toolDlLocation = path.resolve(Platform.getUserHomePath(), '.vs-openshift', ToolsConfig.tools[cmd].dlFileName);
const installRequest = `Download and install v${ToolsConfig.tools[cmd].version}`;
const response = await vscode.window.showInformationMessage(
`Cannot find ${ToolsConfig.tools[cmd].description} ${ToolsConfig.tools[cmd].versionRangeLabel}.`, installRequest, 'Help', 'Cancel');
fsex.ensureDirSync(path.resolve(Platform.getUserHomePath(), '.vs-openshift'));
if (response === installRequest) {
let action: string;
do {
action = undefined;
await vscode.window.withProgress({
cancellable: true,
location: vscode.ProgressLocation.Notification,
title: `Downloading ${ToolsConfig.tools[cmd].description}`
},
(progress: vscode.Progress<{increment: number, message: string}>, token: vscode.CancellationToken) => {
return DownloadUtil.downloadFile(
ToolsConfig.tools[cmd].url,
toolDlLocation,
(dlProgress, increment) => progress.report({ increment, message: `${dlProgress}%`})
);
});
const sha256sum: string = await hasha.fromFile(toolDlLocation, {algorithm: 'sha256'});
if (sha256sum !== ToolsConfig.tools[cmd].sha256sum) {
fsex.removeSync(toolDlLocation);
action = await vscode.window.showInformationMessage(`Checksum for downloaded ${ToolsConfig.tools[cmd].description} v${ToolsConfig.tools[cmd].version} is not correct.`, 'Download again', 'Cancel');
}
} while (action === 'Download again');
if (action !== 'Cancel') {
if (toolDlLocation.endsWith('.zip') || toolDlLocation.endsWith('.tar.gz')) {
await Archive.unzip(toolDlLocation, path.resolve(Platform.getUserHomePath(), '.vs-openshift'), ToolsConfig.tools[cmd].filePrefix);
} else if (toolDlLocation.endsWith('.gz')) {
await Archive.unzip(toolDlLocation, toolCacheLocation, ToolsConfig.tools[cmd].filePrefix);
}
fsex.removeSync(toolDlLocation);
if (Platform.OS !== 'win32') {
fs.chmodSync(toolCacheLocation, 0o765);
}
toolLocation = toolCacheLocation;
}
} else if (response === `Help`) {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(`https://github.com/redhat-developer/vscode-openshift-tools#dependencies`));
}
}
if (toolLocation) {
ToolsConfig.tools[cmd].location = toolLocation;
}
}
return toolLocation;
}
public static async getVersion(location: string, cmd: string = path.parse(location).name): Promise<string> {
let detectedVersion: string;
if (fs.existsSync(location)) {
const version = new RegExp(`${cmd.toLocaleLowerCase()} v((([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:-([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?)(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?).*`);
const result = await Cli.getInstance().execute(`"${location}" version`);
if (result.stdout) {
const toolVersion: string[] = result.stdout.trim().split('\n').filter((value) => {
return value.match(version);
}).map((value)=>version.exec(value)[1]);
if (toolVersion.length) {
detectedVersion = toolVersion[0];
}
}
}
return detectedVersion;
}
public static async selectTool(locations: string[], versionRange: string): Promise<string> {
let result;
for (const location of locations) {
if (location && semver.satisfies(await ToolsConfig.getVersion(location), versionRange)) {
result = location;
break;
}
}
return result;
}
}