-
Notifications
You must be signed in to change notification settings - Fork 9
/
extension.ts
178 lines (163 loc) · 9.06 KB
/
extension.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
'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import { Catalog } from './Catalog';
import * as yauzl from 'yauzl';
let fs = require('fs');
let p = require('path');
import { startTelemetry, sendTelemetry, sanitize } from './telemetry';
let catalogBuilder:Catalog = new Catalog(vscode.workspace.getConfiguration("project.initializer").get<string>("endpointUrl", "https://forge.api.openshift.io/api/"));
export const CAMEL_FUSE_RUNTIME_IDS = ['camel', 'fuse'];
export const VERTX_RUNTIME_IDS = ['vert.x'];
export const GOLANG_RUNTIME_IDS = ['golang'];
export const NODEJS_RUNTIME_IDS = ['nodejs'];
export const THORNTAIL_RUNTIME_IDS = ['thorntail'];
export const SPRINGBOOT_RUNTIME_IDS = ['spring-boot'];
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let genericGenerationCommand = vscode.commands.registerCommand('project.initializer.generate', () => generateFromAllChoices('project.initializer.generate'));
let fuseGenerationCommand = registerCommandForRuntimes('camelfuse', CAMEL_FUSE_RUNTIME_IDS);
let vertxGenerationCommand = registerCommandForRuntimes('vertx', VERTX_RUNTIME_IDS);
let goLangGenerationCommand = registerCommandForRuntimes('golang', GOLANG_RUNTIME_IDS);
let nodeJSGenerationCommand = registerCommandForRuntimes('nodejs', NODEJS_RUNTIME_IDS);
let thorntailGenerationCommand = registerCommandForRuntimes('thorntail', THORNTAIL_RUNTIME_IDS);
let springBootGenerationCommand = registerCommandForRuntimes('springboot', SPRINGBOOT_RUNTIME_IDS);
let disposableListener = vscode.workspace.onDidChangeConfiguration(event => {updateCatalog(event);});
context.subscriptions.push(genericGenerationCommand);
context.subscriptions.push(fuseGenerationCommand);
context.subscriptions.push(vertxGenerationCommand);
context.subscriptions.push(goLangGenerationCommand);
context.subscriptions.push(nodeJSGenerationCommand);
context.subscriptions.push(thorntailGenerationCommand);
context.subscriptions.push(springBootGenerationCommand);
context.subscriptions.push(disposableListener);
startTelemetry(context);
}
function updateCatalog(event: vscode.ConfigurationChangeEvent) {
if (event.affectsConfiguration("project.initializer.endpointUrl")) {
let url = vscode.workspace.getConfiguration("project.initializer").get<string>("endpointUrl", "https://forge.api.openshift.io/api/");
if (catalogBuilder.endpoint !== url) {
catalogBuilder = new Catalog(url);
}
}
}
function registerCommandForRuntimes(commandIdSuffix: string, runtimeIds:string[]) {
const commandId = 'project.initializer.generate.'+commandIdSuffix;
return vscode.commands.registerCommand(commandId, () => generateForRuntimes(commandId, runtimeIds));
}
async function generateForRuntimes(commandId:string, runtimeIds:string[]) {
try {
let catalog = await catalogBuilder.getCatalog();
await generate(commandId, filterCatalogForRuntimes(catalog, runtimeIds));
}
catch (error) {
vscode.window.showErrorMessage("Error while processing Project Initializer" + error);
}
}
export function filterCatalogForRuntimes(catalog: any, runtimeIds:string[]) {
let filteredCatalog = JSON.parse(JSON.stringify(catalog));
filteredCatalog.runtimes = catalog.runtimes.filter((runtime: any) => { return runtimeIds.indexOf(runtime.id) > -1;});
let filteredBoosters = catalog.boosters.filter((booster: any) => { return runtimeIds.indexOf(booster.runtime) > -1;});
filteredCatalog.boosters = filteredBoosters;
let filteredMissionIds = filteredBoosters.map((booster:any) => {return booster.mission;});
filteredCatalog.missions = catalog.missions.filter((mission:any) => {return filteredMissionIds.includes(mission.id);});
return filteredCatalog;
}
async function generateFromAllChoices(commandId: string) {
try {
let catalog = await catalogBuilder.getCatalog();
await generate(commandId, catalog);
}
catch (error) {
vscode.window.showErrorMessage("Error while processing Project Initializer" + error);
}
}
async function generate(commandId: string, catalog: any) {
let telemetryProps: any = {
identifier: commandId,
};
const startTime = Date.now();
try {
let missions = catalog.missions.map((mission:any) => ({label: mission.id, description:mission.description}));
let missionId: any = await vscode.window.showQuickPick(missions, { placeHolder: 'Choose mission' });
if (missionId) {
let boosters = catalog.boosters.filter((item: any) => item.mission == missionId.label);
if (boosters) {
let runtimeId: any = await vscode.window.showQuickPick(boosters.map((item: any) => ({ label: `${item.runtime} ${item.version}`})), { placeHolder: 'Choose runtime' });
if (runtimeId) {
let groupId = await vscode.window.showInputBox({ prompt: 'Group Id', placeHolder: 'Enter the group id', value: vscode.workspace.getConfiguration("project.initializer").get<string>("defaultGroupId") });
if (groupId) {
let artifactId = await vscode.window.showInputBox({ prompt: 'Artifact Id', placeHolder: 'Enter the artifact id', value: vscode.workspace.getConfiguration("project.initializer").get<string>("defaultArtifactId") });
if (artifactId) {
let version = await vscode.window.showInputBox({ prompt: 'Version', placeHolder: 'Enter the version', value: vscode.workspace.getConfiguration("project.initializer").get<string>("defaultVersion") });
if (version) {
vscode.window.setStatusBarMessage("Downloading zip project file", 1000);
let runtimeLabel = runtimeId.label.substring(0, runtimeId.label.indexOf(' '));
let runtimeDescription = runtimeId.label.substring(runtimeId.label.indexOf(' ') + 1, runtimeId.label.length);
console.log(`runtimelabel: ${runtimeLabel} + ${runtimeDescription}`);
let zipProject = await catalogBuilder.zip(artifactId, missionId.label, runtimeLabel, runtimeDescription, groupId, artifactId, version);
if (zipProject) {
let folder = await vscode.window.showWorkspaceFolderPick({ placeHolder: 'Select the target workspace folder' });
if (folder) {
vscode.window.setStatusBarMessage("Unzipping project file", 1000);
extract(zipProject as Buffer, folder.uri.fsPath);
vscode.window.showInformationMessage("Project saved to " + folder!.uri.fsPath);
telemetryProps.mission = missionId.label;
telemetryProps.runtime = runtimeId.label;
telemetryProps.runtimeVersion = runtimeId.description;
}
}
}
}
}
}
}
}
} catch (error) {
telemetryProps.error = sanitize(error.toString());
} finally {
telemetryProps.duration = Date.now() - startTime;
sendTelemetry('command', telemetryProps);
}
}
function extract(content:Buffer, path:string) {
yauzl.fromBuffer(content, {lazyEntries:true}, (err, zipfile) => {
if (err) {
throw err;
}
zipfile!.readEntry();
zipfile!.on("entry", (entry:yauzl.Entry) => {
console.log("Processing " + entry.fileName);
let entryPath = removeFirstLevel(entry.fileName);
if (entryPath !== "/") {
let mappedPath = p.resolve(path, p.normalize(entryPath));
if (entryPath.endsWith("/")) {
fs.mkdirSync(mappedPath);
} else {
zipfile!.openReadStream(entry, (err,stream) => {
if (err) {
throw err;
}
stream!.pipe(fs.createWriteStream(mappedPath, {mode: entry.externalFileAttributes >>> 16}));
});
}
}
zipfile!.readEntry();
});
});
}
function removeFirstLevel(path:string) {
let index = path.indexOf("/");
if (index !== (-1)) {
path = path.substring(index + 1);
}
return path;
}
// this method is called when your extension is deactivated
export function deactivate() {
}