-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathbuildFilesSelector.ts
210 lines (190 loc) · 6.63 KB
/
buildFilesSelector.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { ExtensionContext, MessageItem, QuickPickItem, QuickPickItemKind, Uri, WorkspaceFolder, extensions, window, workspace } from "vscode";
import { getExclusionGlob as getExclusionGlobPattern } from "./utils";
import * as path from "path";
export const PICKED_BUILD_FILES = "java.pickedBuildFiles";
export class BuildFileSelector {
private buildTypes: IBuildTool[] = [];
private context: ExtensionContext;
private exclusionGlobPattern: string;
constructor(context: ExtensionContext) {
this.context = context;
// TODO: should we introduce the exclusion globs into the contribution point?
this.exclusionGlobPattern = getExclusionGlobPattern(["**/target/**", "**/bin/**", "**/build/**"]);
for (const extension of extensions.all) {
const javaBuildTools: IBuildTool[] = extension.packageJSON.contributes?.javaBuildTools;
if (!Array.isArray(javaBuildTools)) {
continue;
}
for (const buildType of javaBuildTools) {
if (!this.isValidBuildTypeConfiguration(buildType)) {
continue;
}
this.buildTypes.push(buildType);
}
}
}
/**
* @returns `true` if there are build files in the workspace, `false` otherwise.
*/
public async hasBuildFiles(): Promise<boolean> {
for (const buildType of this.buildTypes) {
const uris: Uri[] = await workspace.findFiles(buildType.fileSearchPattern, this.exclusionGlobPattern, 1);
if (uris.length > 0) {
return true;
}
}
return false;
}
/**
* Get the uri strings for the build files that the user selected.
* @returns An array of uri string for the build files that the user selected.
* An empty array means user canceled the selection.
*/
public async getBuildFiles(): Promise<string[] | undefined> {
const cache = this.context.workspaceState.get<string[]>(PICKED_BUILD_FILES);
if (cache !== undefined) {
return cache;
}
const choice = await this.chooseBuildFilePickers();
const pickedUris = await this.eliminateBuildToolConflict(choice);
if (pickedUris.length > 0) {
this.context.workspaceState.update(PICKED_BUILD_FILES, pickedUris);
}
return pickedUris;
}
private isValidBuildTypeConfiguration(buildType: IBuildTool): boolean {
return !!buildType.displayName && !!buildType.fileSearchPattern;
}
private async chooseBuildFilePickers(): Promise<IBuildFilePicker[]> {
return window.showQuickPick(this.getBuildFilePickers(), {
placeHolder: "Note: Currently only Maven projects can be partially imported.",
title: "Select build files to import",
ignoreFocusOut: true,
canPickMany: true,
matchOnDescription: true,
matchOnDetail: true,
});
}
/**
* Get pickers for all build files in the workspace.
*/
private async getBuildFilePickers(): Promise<IBuildFilePicker[]> {
const addedFolders: Map<string, IBuildFilePicker> = new Map<string, IBuildFilePicker>();
for (const buildType of this.buildTypes) {
const uris: Uri[] = await workspace.findFiles(buildType.fileSearchPattern, this.exclusionGlobPattern);
for (const uri of uris) {
const containingFolder = path.dirname(uri.fsPath);
if (addedFolders.has(containingFolder)) {
const picker = addedFolders.get(containingFolder);
if (!picker.buildTypeAndUri.has(buildType)) {
picker.detail += `, ./${workspace.asRelativePath(uri)}`;
picker.description += `, ${buildType.displayName}`;
picker.buildTypeAndUri.set(buildType, uri);
}
} else {
addedFolders.set(containingFolder, {
label: path.basename(containingFolder),
detail: `./${workspace.asRelativePath(uri)}`,
description: buildType.displayName,
buildTypeAndUri: new Map<IBuildTool, Uri>([[buildType, uri]]),
picked: true,
});
}
}
}
const pickers: IBuildFilePicker[] = Array.from(addedFolders.values());
return this.addSeparator(pickers);
}
/**
* Add a separator pickers between pickers that belong to different workspace folders.
*/
private addSeparator(pickers: IBuildFilePicker[]): IBuildFilePicker[] {
// group pickers by their containing workspace folder
const workspaceFolders = new Map<WorkspaceFolder, IBuildFilePicker[]>();
for (const picker of pickers) {
const folder = workspace.getWorkspaceFolder(picker.buildTypeAndUri.values().next().value);
if (!folder) {
continue;
}
if (!workspaceFolders.has(folder)) {
workspaceFolders.set(folder, []);
}
workspaceFolders.get(folder)?.push(picker);
}
const newPickers: IBuildFilePicker[] = [];
const folderArray = Array.from(workspaceFolders.keys());
folderArray.sort((a, b) => a.name.localeCompare(b.name));
for (const folder of folderArray) {
const pickersInFolder = workspaceFolders.get(folder);
newPickers.push({
label: folder.name,
kind: QuickPickItemKind.Separator,
buildTypeAndUri: null
});
newPickers.push(...this.sortPickers(pickersInFolder));
}
return newPickers;
}
private sortPickers(pickers: IBuildFilePicker[]): IBuildFilePicker[] {
return pickers.sort((a, b) => {
const pathA = path.dirname(a.buildTypeAndUri.values().next().value.fsPath);
const pathB = path.dirname(b.buildTypeAndUri.values().next().value.fsPath);
return pathA.localeCompare(pathB);
});
}
/**
* Ask user to choose a build tool when there are multiple build tools in the same folder.
*/
private async eliminateBuildToolConflict(choice?: IBuildFilePicker[]): Promise<string[]> {
if (!choice) {
return [];
}
const conflictPickers = new Set<IBuildFilePicker>();
const result: string[] = [];
for (const picker of choice) {
if (picker.buildTypeAndUri.size > 1) {
conflictPickers.add(picker);
} else {
result.push(picker.buildTypeAndUri.values().next().value.toString());
}
}
if (conflictPickers.size > 0) {
for (const picker of conflictPickers) {
const conflictItems: IConflictItem[] = [{
title: "Skip",
isCloseAffordance: true,
}];
for (const buildType of picker.buildTypeAndUri.keys()) {
conflictItems.push({
title: buildType.displayName,
uri: picker.buildTypeAndUri.get(buildType),
});
}
const choice = await window.showInformationMessage<IConflictItem>(
`Which build tool would you like to use for folder: ${picker.label}?`,
{
modal: true,
},
...conflictItems
);
if (choice?.title !== "Skip" && choice?.uri) {
result.push(choice.uri.toString());
}
}
}
return result;
}
}
interface IBuildTool {
displayName: string;
fileSearchPattern: string;
}
interface IConflictItem extends MessageItem {
uri?: Uri;
}
interface IBuildFilePicker extends QuickPickItem {
buildTypeAndUri: Map<IBuildTool, Uri>;
}
export function cleanupProjectPickerCache(context: ExtensionContext) {
context.workspaceState.update(PICKED_BUILD_FILES, undefined);
}