-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathGenerateJarExecutor.ts
More file actions
255 lines (244 loc) · 11.9 KB
/
Copy pathGenerateJarExecutor.ts
File metadata and controls
255 lines (244 loc) · 11.9 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { ensureDir, pathExists } from "fs-extra";
import { globby } from "globby";
import * as _ from "lodash";
import { basename, dirname, extname, isAbsolute, join, normalize, relative } from "path";
import { Disposable, ProgressLocation, QuickInputButtons, QuickPickItem, Uri, window, WorkspaceFolder } from "vscode";
import { sendInfo } from "vscode-extension-telemetry-wrapper";
import { Jdtls } from "../../java/jdtls";
import { INodeData } from "../../java/nodeData";
import { IExportJarStepExecutor } from "./IExportJarStepExecutor";
import { IClasspath, IStepMetadata } from "./IStepMetadata";
import { createPickBox, ExportJarMessages, ExportJarStep, ExportJarTargets, getExtensionApi, toPosixPath } from "./utility";
export class GenerateJarExecutor implements IExportJarStepExecutor {
private readonly currentStep: ExportJarStep = ExportJarStep.GenerateJar;
public async execute(stepMetadata: IStepMetadata): Promise<boolean> {
return this.generateJar(stepMetadata);
}
private async generateJar(stepMetadata: IStepMetadata): Promise<boolean> {
if (_.isEmpty(stepMetadata.elements)) {
// If the user uses wizard or custom task with a empty list of elements,
// the classpaths should be specified manually.
if (!(await this.generateClasspaths(stepMetadata))) {
return false;
}
}
const folder: WorkspaceFolder | undefined = stepMetadata.workspaceFolder;
if (!folder) {
throw new Error(ExportJarMessages.fieldUndefinedMessage(ExportJarMessages.Field.WORKSPACEFOLDER, this.currentStep));
}
let destPath = "";
if (stepMetadata.outputPath === ExportJarTargets.SETTING_ASKUSER || stepMetadata.outputPath === "") {
if (stepMetadata.outputPath === ExportJarTargets.SETTING_ASKUSER) {
sendInfo("", { exportJarPath: stepMetadata.outputPath });
}
const outputUri: Uri | undefined = await window.showSaveDialog({
defaultUri: Uri.file(join(folder.uri.fsPath, `${folder.name}.jar`)),
filters: {
"Java Archive": ["jar"],
},
});
if (!outputUri) {
return Promise.reject();
}
destPath = outputUri.fsPath;
} else {
const outputPath: string | undefined = stepMetadata.outputPath;
if (!outputPath) {
throw new Error(ExportJarMessages.fieldUndefinedMessage(ExportJarMessages.Field.OUTPUTPATH, this.currentStep));
}
// Both the absolute path and the relative path (to workspace folder) are supported.
destPath = (isAbsolute(outputPath)) ? outputPath : join(folder.uri.fsPath, outputPath);
// Since both the specific target folder and the specific target file are supported,
// we regard a path as a file if it ends with ".jar". Otherwise, it was regarded as a folder.
if (extname(outputPath) !== ".jar") {
destPath = join(destPath, folder.name + ".jar");
}
await ensureDir(dirname(destPath));
}
destPath = normalize(destPath);
return window.withProgress({
location: ProgressLocation.Window,
title: "Exporting Jar : Generating jar...",
cancellable: true,
}, (_progress, token) => {
return new Promise<boolean>(async (resolve, reject) => {
token.onCancellationRequested(() => {
return reject();
});
const mainClass: string | undefined = stepMetadata.mainClass;
// For "no main class" option, we get an empty string in stepMetadata.mainClass,
// so this condition would be (mainClass === undefined)
if (mainClass === undefined) {
return reject(new Error(ExportJarMessages.fieldUndefinedMessage(ExportJarMessages.Field.MAINCLASS, this.currentStep)));
}
const classpaths: IClasspath[] = stepMetadata.classpaths;
if (_.isEmpty(classpaths)) {
return reject(new Error(ExportJarMessages.CLASSPATHS_EMPTY));
}
if (!stepMetadata.terminalId) {
return reject(new Error("Can't find related terminal."));
}
const exportResult: boolean | undefined = await Jdtls.exportJar(basename(mainClass),
classpaths, destPath, stepMetadata.terminalId, token);
if (exportResult === true) {
stepMetadata.outputPath = destPath;
return resolve(true);
} else {
return reject(new Error("Export jar failed."));
}
});
});
}
private async generateClasspaths(stepMetadata: IStepMetadata): Promise<boolean> {
const extensionApi: any = await getExtensionApi();
const dependencyItems: IJarQuickPickItem[] = await window.withProgress({
location: ProgressLocation.Window,
title: "Exporting Jar : Resolving classpaths...",
cancellable: true,
}, (_progress, token) => {
return new Promise<IJarQuickPickItem[]>(async (resolve, reject) => {
token.onCancellationRequested(() => {
return reject();
});
const pickItems: IJarQuickPickItem[] = [];
const uriSet: Set<string> = new Set<string>();
const projectList: INodeData[] = stepMetadata.projectList;
if (_.isEmpty(projectList)) {
return reject(new Error(ExportJarMessages.WORKSPACE_EMPTY));
}
const workspaceFolder: WorkspaceFolder | undefined = stepMetadata.workspaceFolder;
if (!workspaceFolder) {
return reject(new Error(ExportJarMessages.fieldUndefinedMessage(ExportJarMessages.Field.WORKSPACEFOLDER, this.currentStep)));
}
for (const project of projectList) {
const projectUri: string = project.metaData?.UnmanagedFolderInnerPath || project.uri;
let classpaths: IClasspathResult;
let testClasspaths: IClasspathResult;
try {
classpaths = await extensionApi.getClasspaths(projectUri, { scope: "runtime" });
testClasspaths = await extensionApi.getClasspaths(projectUri, { scope: "test" });
} catch (e) {
return reject(new Error(e));
}
pickItems.push(
...await this.parseDependencyItems(classpaths.classpaths, uriSet, workspaceFolder.uri.fsPath, "runtime"),
...await this.parseDependencyItems(classpaths.modulepaths, uriSet, workspaceFolder.uri.fsPath, "runtime"));
pickItems.push(
...await this.parseDependencyItems(testClasspaths.classpaths, uriSet, workspaceFolder.uri.fsPath, "test"),
...await this.parseDependencyItems(testClasspaths.modulepaths, uriSet, workspaceFolder.uri.fsPath, "test"));
}
return resolve(pickItems);
});
});
if (_.isEmpty(dependencyItems)) {
throw new Error(ExportJarMessages.PROJECT_EMPTY);
} else if (dependencyItems.length === 1) {
await this.setStepMetadataFromOutputFolder(dependencyItems[0].path, stepMetadata.classpaths);
return true;
}
dependencyItems.sort((node1, node2) => {
if (node1.description !== node2.description) {
return (node1.description || "").localeCompare(node2.description || "");
}
if (node1.type !== node2.type) {
return node2.type.localeCompare(node1.type);
}
return node1.label.localeCompare(node2.label);
});
const pickedDependencyItems: IJarQuickPickItem[] = [];
for (const item of dependencyItems) {
if (item.picked) {
pickedDependencyItems.push(item);
}
}
const disposables: Disposable[] = [];
let result: boolean = false;
try {
result = await new Promise<boolean>(async (resolve, reject) => {
const pickBox = createPickBox<IJarQuickPickItem>("Export Jar : Determine elements", "Select the elements",
dependencyItems, stepMetadata.steps.length > 0, true);
pickBox.selectedItems = pickedDependencyItems;
disposables.push(
pickBox.onDidTriggerButton((item) => {
if (item === QuickInputButtons.Back) {
return resolve(false);
}
}),
pickBox.onDidAccept(async () => {
if (_.isEmpty(pickBox.selectedItems)) {
return;
}
for (const item of pickBox.selectedItems) {
if (item.type === "artifact") {
const classpath: IClasspath = {
source: item.path,
destination: undefined,
isArtifact: true,
};
stepMetadata.classpaths.push(classpath);
} else {
await this.setStepMetadataFromOutputFolder(item.path, stepMetadata.classpaths);
}
}
return resolve(true);
}),
pickBox.onDidHide(() => {
return reject();
}),
);
disposables.push(pickBox);
pickBox.show();
});
} finally {
for (const d of disposables) {
d.dispose();
}
}
return result;
}
private async setStepMetadataFromOutputFolder(folderPath: string, classpaths: IClasspath[]): Promise<void> {
const posixPath: string = toPosixPath(folderPath);
for (const path of await globby(posixPath)) {
const classpath: IClasspath = {
source: path,
destination: relative(posixPath, path),
isArtifact: false,
};
classpaths.push(classpath);
}
}
private async parseDependencyItems(paths: string[], uriSet: Set<string>, projectPath: string, scope: string): Promise<IJarQuickPickItem[]> {
const dependencyItems: IJarQuickPickItem[] = [];
for (const classpath of paths) {
if (await pathExists(classpath) === false) {
continue;
}
const extName = extname(classpath);
const baseName = Uri.parse(classpath).fsPath.startsWith(Uri.parse(projectPath).fsPath) ?
relative(projectPath, classpath) : basename(classpath);
const typeValue = (extName === ".jar") ? "artifact" : "outputFolder";
if (!uriSet.has(classpath)) {
uriSet.add(classpath);
dependencyItems.push({
label: baseName,
description: scope,
path: classpath,
type: typeValue,
picked: scope === "runtime",
});
}
}
return dependencyItems;
}
}
export interface IClasspathResult {
projectRoot: string;
classpaths: string[];
modulepaths: string[];
}
interface IJarQuickPickItem extends QuickPickItem {
path: string;
type: string;
}