-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathcreateNewProject.ts
185 lines (171 loc) · 6.97 KB
/
createNewProject.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as vscode from "vscode";
import * as fs from "fs/promises";
import configuration from "../configuration";
import { SwiftToolchain, SwiftProjectTemplate } from "../toolchain/toolchain";
import { showToolchainError } from "../ui/ToolchainSelection";
import { withDelayedProgress } from "../ui/withDelayedProgress";
import { execSwift } from "../utilities/utilities";
import { Version } from "../utilities/version";
/**
* Prompts the user to input project details and then executes `swift package init`
* to create the project.
*/
export async function createNewProject(toolchain: SwiftToolchain | undefined): Promise<void> {
// It is possible for this command to be run without a valid toolchain because it can be
// run before the Swift extension is activated. Show the toolchain error notification in
// this case.
if (!toolchain) {
showToolchainError();
return;
}
// The context key `swift.createNewProjectAvailable` only works if the extension has been
// activated. As such, we also have to allow this command to run when no workspace is
// active. Show an error to the user if the command is unavailable.
if (!toolchain.swiftVersion.isGreaterThanOrEqual(new Version(5, 8, 0))) {
vscode.window.showErrorMessage(
"Creating a new swift project is only available starting in swift version 5.8.0."
);
return;
}
// Prompt the user for the type of project they would like to create
const availableProjectTemplates = await toolchain.getProjectTemplates();
const selectedProjectTemplate = await vscode.window.showQuickPick<
vscode.QuickPickItem & { type: SwiftProjectTemplate }
>(
availableProjectTemplates.map(type => ({
label: type.name,
description: type.id,
detail: type.description,
type,
})),
{
placeHolder: "Select a swift project template",
}
);
if (!selectedProjectTemplate) {
return undefined;
}
const projectType = selectedProjectTemplate.type.id;
// Prompt the user for a location in which to create the new project
const selectedFolder = await vscode.window.showOpenDialog({
title: "Select a folder to create a new swift project in",
openLabel: "Select folder",
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
});
if (!selectedFolder || selectedFolder.length === 0) {
return undefined;
}
// Prompt the user for the project name
const existingNames = await fs.readdir(selectedFolder[0].fsPath, { encoding: "utf-8" });
let initialValue = `swift-${projectType}`;
for (let i = 1; ; i++) {
if (!existingNames.includes(initialValue)) {
break;
}
initialValue = `swift-${projectType}-${i}`;
}
const projectName = await vscode.window.showInputBox({
value: initialValue,
prompt: "Enter a name for your new swift project",
validateInput(value) {
// Swift Package Manager doesn't seem to do any validation on the name.
// So, we'll just check for obvious failure cases involving mkdir.
if (value.trim() === "") {
return "Project name cannot be empty.";
} else if (value.includes("/") || value.includes("\\")) {
return "Project name cannot contain '/' or '\\' characters.";
} else if (value === "." || value === "..") {
return "Project name cannot be '.' or '..'.";
}
// Ensure there are no name collisions
if (existingNames.includes(value)) {
return "A file/folder with this name already exists.";
}
return undefined;
},
});
if (projectName === undefined) {
return undefined;
}
// Create the folder that will store the new project
const projectUri = vscode.Uri.joinPath(selectedFolder[0], projectName);
await fs.mkdir(projectUri.fsPath);
// Use swift package manager to initialize the swift project
await withDelayedProgress(
{
location: vscode.ProgressLocation.Notification,
title: `Creating swift project ${projectName}`,
},
async () => {
await execSwift(
["package", "init", "--type", projectType, "--name", projectName],
toolchain,
{
cwd: projectUri.fsPath,
}
);
},
1000
);
// Prompt the user whether or not they want to open the newly created project
const isWorkspaceOpened = !!vscode.workspace.workspaceFolders;
const openAfterCreate = configuration.openAfterCreateNewProject;
let action: "open" | "openNewWindow" | "addToWorkspace" | undefined;
if (openAfterCreate === "always") {
action = "open";
} else if (openAfterCreate === "alwaysNewWindow") {
action = "openNewWindow";
} else if (openAfterCreate === "whenNoFolderOpen" && !isWorkspaceOpened) {
action = "open";
}
if (action === undefined) {
let message = `Would you like to open ${projectName}?`;
const open = "Open";
const openNewWindow = "Open in New Window";
const choices = [open, openNewWindow];
const addToWorkspace = "Add to Workspace";
if (isWorkspaceOpened) {
message = `Would you like to open ${projectName}, or add it to the current workspace?`;
choices.push(addToWorkspace);
}
const result = await vscode.window.showInformationMessage(
message,
{ modal: true, detail: "The default action can be configured in settings" },
...choices
);
if (result === open) {
action = "open";
} else if (result === openNewWindow) {
action = "openNewWindow";
} else if (result === addToWorkspace) {
action = "addToWorkspace";
}
}
if (action === "open") {
await vscode.commands.executeCommand("vscode.openFolder", projectUri, {
forceReuseWindow: true,
});
} else if (action === "openNewWindow") {
await vscode.commands.executeCommand("vscode.openFolder", projectUri, {
forceNewWindow: true,
});
} else if (action === "addToWorkspace") {
const index = vscode.workspace.workspaceFolders?.length ?? 0;
await vscode.workspace.updateWorkspaceFolders(index, 0, { uri: projectUri });
}
}