forked from microsoft/vscode-java-dependency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectController.ts
More file actions
205 lines (184 loc) · 7.22 KB
/
Copy pathprojectController.ts
File metadata and controls
205 lines (184 loc) · 7.22 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as fse from "fs-extra";
import * as _ from "lodash";
import * as path from "path";
import { commands, Disposable, Extension, ExtensionContext, extensions, QuickPickItem, Uri, window, workspace } from "vscode";
import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper";
import { Commands } from "../commands";
import { Utility } from "../utility";
export class ProjectController implements Disposable {
private disposable: Disposable;
public constructor(public readonly context: ExtensionContext) {
this.disposable = Disposable.from(
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_CREATE, () => this.createJavaProject()),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_OPEN, () => this.openJavaProject()),
);
}
public dispose() {
this.disposable.dispose();
}
public async openJavaProject() {
const availableCommands: string[] = await commands.getCommands();
if (availableCommands.includes(Commands.WORKBENCH_ACTION_FILES_OPENFOLDER)) {
return commands.executeCommand(Commands.WORKBENCH_ACTION_FILES_OPENFOLDER);
}
return commands.executeCommand(Commands.WORKBENCH_ACTION_FILES_OPENFILEFOLDER);
}
public async createJavaProject() {
const items: IProjectTypeQuickPick[] = projectTypes.map((type: IProjectType) => {
return {
label: type.displayName,
description: type.description,
detail: type.metadata.extensionName ? `Provided by $(extensions) ${type.metadata.extensionName}` : type.detail,
metadata: type.metadata,
};
});
const choice = await window.showQuickPick(items, {
ignoreFocusOut: true,
placeHolder: "Select the project type",
});
if (!choice || !await ensureExtension(choice.label, choice.metadata)) {
return;
}
if (choice.metadata.type === ProjectType.NoBuildTool) {
await scaffoldSimpleProject(this.context);
} else if (choice.metadata.createCommandId) {
await commands.executeCommand(choice.metadata.createCommandId);
}
}
}
interface IProjectType {
displayName: string;
description?: string;
detail?: string;
metadata: IProjectTypeMetadata;
}
interface IProjectTypeMetadata {
type: ProjectType;
extensionId: string;
extensionName: string;
createCommandId: string;
}
interface IProjectTypeQuickPick extends QuickPickItem {
metadata: IProjectTypeMetadata;
}
enum ProjectType {
NoBuildTool = "NoBuildTool",
Maven = "Maven",
SpringBoot = "SpringBoot",
Quarkus = "Quarkus",
MicroProfile = "MicroProfile",
}
async function ensureExtension(typeName: string, metaData: IProjectTypeMetadata): Promise<boolean> {
if (!metaData.extensionId) {
return true;
}
const extension: Extension<any> | undefined = extensions.getExtension(metaData.extensionId);
if (extension === undefined) {
await promptInstallExtension(typeName, metaData);
return false;
}
await extension.activate();
return true;
}
async function promptInstallExtension(projectType: string, metaData: IProjectTypeMetadata): Promise<void> {
const choice: string | undefined = await window.showInformationMessage(`${metaData.extensionName} is required to create ${projectType} projects. Please re-run the command 'Java: Create Java Project...' after the extension is installed.`, "Install");
if (choice === "Install") {
commands.executeCommand("workbench.extensions.installExtension", metaData.extensionId);
// So far there is no API to query the extension's state, so we open the extension's homepage
// here, where users can check the state: installing, disabled, installed, etc...
// See: https://github.com/microsoft/vscode/issues/14444
commands.executeCommand("extension.open", metaData.extensionId);
}
}
async function scaffoldSimpleProject(context: ExtensionContext): Promise<void> {
const workspaceFolder = Utility.getDefaultWorkspaceFolder();
const location: Uri[] | undefined = await window.showOpenDialog({
defaultUri: workspaceFolder && workspaceFolder.uri,
canSelectFiles: false,
canSelectFolders: true,
openLabel: "Select the project location",
});
if (!location || !location.length) {
return;
}
const basePath: string = location[0].fsPath;
const projectName: string | undefined = await window.showInputBox({
prompt: "Input a Java project name",
ignoreFocusOut: true,
validateInput: async (name: string): Promise<string> => {
if (name && !name.match(/^[^*~/\\]+$/)) {
return "Please input a valid project name";
}
if (name && await fse.pathExists(path.join(basePath, name))) {
return "A project with this name already exists";
}
return "";
},
});
if (!projectName) {
return;
}
const projectRoot: string = path.join(basePath, projectName);
const templateRoot: string = path.join(context.extensionPath, "templates", "invisible-project");
try {
await fse.ensureDir(projectRoot);
await fse.copy(templateRoot, projectRoot);
await fse.ensureDir(path.join(projectRoot, "lib"));
} catch (error) {
window.showErrorMessage(error.message);
return;
}
const openInNewWindow = workspace && !_.isEmpty(workspace.workspaceFolders);
await commands.executeCommand(Commands.VSCODE_OPEN_FOLDER, Uri.file(path.join(basePath, projectName)), openInNewWindow);
}
const projectTypes: IProjectType[] = [
{
displayName: "No build tools",
detail: "Work with source code directly without any build tools",
metadata: {
type: ProjectType.NoBuildTool,
extensionId: "",
extensionName: "",
createCommandId: "",
},
},
{
displayName: "Maven",
description: "create from archetype",
metadata: {
type: ProjectType.Maven,
extensionId: "vscjava.vscode-maven",
extensionName: "Maven for Java",
createCommandId: "maven.archetype.generate",
},
},
{
displayName: "Spring Boot",
metadata: {
type: ProjectType.SpringBoot,
extensionId: "vscjava.vscode-spring-initializr",
extensionName: "Spring Initializr Java Support",
createCommandId: "spring.initializr.createProject",
},
},
{
displayName: "Quarkus",
metadata: {
type: ProjectType.Quarkus,
extensionId: "redhat.vscode-quarkus",
extensionName: "Quarkus",
createCommandId: "quarkusTools.createProject",
},
},
{
displayName: "MicroProfile",
metadata: {
type: ProjectType.MicroProfile,
extensionId: "microprofile-community.mp-starter-vscode-ext",
extensionName: "MicroProfile Starter",
createCommandId: "extension.microProfileStarter",
},
},
];