forked from swiftlang/vscode-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftTaskProvider.ts
459 lines (425 loc) · 16.9 KB
/
SwiftTaskProvider.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2021-2024 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 { WorkspaceContext } from "../WorkspaceContext";
import { FolderContext } from "../FolderContext";
import { Product } from "../SwiftPackage";
import configuration, { ShowBuildStatusOptions } from "../configuration";
import { swiftRuntimeEnv } from "../utilities/utilities";
import { Version } from "../utilities/version";
import { SwiftToolchain } from "../toolchain/toolchain";
import { SwiftExecution } from "../tasks/SwiftExecution";
import { resolveTaskCwd } from "../utilities/tasks";
import { BuildConfigurationFactory } from "../debugger/buildConfig";
/**
* References:
*
* - General information on tasks:
* https://code.visualstudio.com/docs/editor/tasks
* - Contributing task definitions:
* https://code.visualstudio.com/api/references/contribution-points#contributes.taskDefinitions
* - Implementing task providers:
* https://code.visualstudio.com/api/extension-guides/task-provider
*/
// Interface class for defining task configuration
interface TaskConfig {
cwd: vscode.Uri;
scope: vscode.TaskScope | vscode.WorkspaceFolder;
group?: vscode.TaskGroup;
presentationOptions?: vscode.TaskPresentationOptions;
prefix?: string;
disableTaskQueue?: boolean;
dontTriggerTestDiscovery?: boolean;
showBuildStatus?: ShowBuildStatusOptions;
}
interface TaskPlatformSpecificConfig {
args?: string[];
cwd?: string;
env?: { [name: string]: unknown };
}
export interface SwiftTask extends vscode.Task {
execution: SwiftExecution;
}
/** arguments for generating debug builds */
export function platformDebugBuildOptions(toolchain: SwiftToolchain): string[] {
if (process.platform === "win32") {
if (toolchain.swiftVersion.isGreaterThanOrEqual(new Version(5, 9, 0))) {
return ["-Xlinker", "-debug:dwarf"];
} else {
return ["-Xswiftc", "-g", "-Xswiftc", "-use-ld=lld", "-Xlinker", "-debug:dwarf"];
}
}
return [];
}
/** arguments for setting diagnostics style */
export function diagnosticsStyleOptions(): string[] {
if (configuration.diagnosticsStyle !== "default") {
return ["-Xswiftc", `-diagnostic-style=${configuration.diagnosticsStyle}`];
}
return [];
}
/** Return swift build options */
export function buildOptions(toolchain: SwiftToolchain, debug = true): string[] {
const args: string[] = [];
if (debug) {
args.push(...platformDebugBuildOptions(toolchain));
}
args.push(...diagnosticsStyleOptions());
const sanitizer = toolchain.sanitizer(configuration.sanitizer);
if (sanitizer) {
args.push(...sanitizer.buildFlags);
}
args.push(...configuration.buildArguments);
return args;
}
/**
* Get task reveal kind based off configuration
*/
function getBuildRevealOption(): vscode.TaskRevealKind {
return configuration.actionAfterBuildError === "Focus Terminal"
? vscode.TaskRevealKind.Silent
: vscode.TaskRevealKind.Never;
}
const buildAllTaskCache = (() => {
const cache = new Map<string, SwiftTask>();
const key = (name: string, folderContext: FolderContext, task: SwiftTask) => {
return `${name}:${folderContext.folder}:${buildOptions(folderContext.workspaceContext.toolchain).join(",")}:${task.definition.args.join(",")}`;
};
return {
get(name: string, folderContext: FolderContext, task: SwiftTask): SwiftTask {
const cached = cache.get(key(name, folderContext, task));
if (!cached) {
this.set(name, folderContext, task);
}
return cached ?? task;
},
set(name: string, folderContext: FolderContext, task: SwiftTask) {
cache.set(key(name, folderContext, task), task);
},
};
})();
function buildAllTaskName(folderContext: FolderContext, release: boolean): string {
let buildTaskName = release
? `${SwiftTaskProvider.buildAllName} - Release`
: SwiftTaskProvider.buildAllName;
if (folderContext.relativePath.length > 0) {
buildTaskName += ` (${folderContext.relativePath})`;
}
return buildTaskName;
}
/**
* Creates a {@link vscode.Task Task} to build all targets in this package.
*/
export function createBuildAllTask(
folderContext: FolderContext,
release: boolean = false
): SwiftTask {
const args = BuildConfigurationFactory.buildAll(folderContext, false, release).args;
const buildTaskName = buildAllTaskName(folderContext, release);
const task = createSwiftTask(
args,
buildTaskName,
{
group: vscode.TaskGroup.Build,
cwd: folderContext.folder,
scope: folderContext.workspaceFolder,
presentationOptions: {
reveal: getBuildRevealOption(),
},
disableTaskQueue: true,
},
folderContext.workspaceContext.toolchain
);
// Ensures there is one Build All task per folder context, since this can be called multiple
// times and we want the same instance each time. Otherwise, VS Code may try and execute
// one instance while our extension code tries to listen to events on an instance created earlier/later.
return buildAllTaskCache.get(buildTaskName, folderContext, task);
}
/**
* Return build all task for a folder
* @param folderContext Folder to get Build All Task for
* @returns Build All Task
*/
export async function getBuildAllTask(
folderContext: FolderContext,
release: boolean = false
): Promise<vscode.Task> {
const buildTaskName = buildAllTaskName(folderContext, release);
const folderWorkingDir = folderContext.workspaceFolder.uri.fsPath;
// search for build all task in task.json first, that are valid for folder
const workspaceTasks = (await vscode.tasks.fetchTasks()).filter(task => {
if (task.source !== "Workspace" || task.scope !== folderContext.workspaceFolder) {
return false;
}
const swiftExecutionOptions = (task.execution as SwiftExecution).options;
let cwd = swiftExecutionOptions?.cwd;
if (cwd === "${workspaceFolder}" || cwd === undefined) {
cwd = folderWorkingDir;
}
return cwd === folderContext.folder.fsPath;
});
// find default build task
let task = workspaceTasks.find(
task => task.group?.id === vscode.TaskGroup.Build.id && task.group?.isDefault === true
);
if (task) {
return task;
}
// find task with name "swift: Build All"
task = workspaceTasks.find(task => task.name === `swift: ${buildTaskName}`);
if (task) {
return task;
}
// search for generated tasks
const swiftTasks = await vscode.tasks.fetchTasks({ type: "swift" });
task = swiftTasks.find(
task =>
task.name === buildTaskName &&
(task.execution as SwiftExecution).options?.cwd === folderContext.folder.fsPath &&
task.source === "swift"
);
if (!task) {
task = createBuildAllTask(folderContext, release);
}
return task;
}
/**
* Creates a {@link vscode.Task Task} to run an executable target.
*/
function createBuildTasks(product: Product, folderContext: FolderContext): vscode.Task[] {
const toolchain = folderContext.workspaceContext.toolchain;
let buildTaskNameSuffix = "";
if (folderContext.relativePath.length > 0) {
buildTaskNameSuffix = ` (${folderContext.relativePath})`;
}
const buildDebugName = `Build Debug ${product.name}${buildTaskNameSuffix}`;
const buildDebugTask = createSwiftTask(
["build", "--product", product.name, ...buildOptions(toolchain)],
buildDebugName,
{
group: vscode.TaskGroup.Build,
cwd: folderContext.folder,
scope: folderContext.workspaceFolder,
presentationOptions: {
reveal: getBuildRevealOption(),
},
disableTaskQueue: true,
dontTriggerTestDiscovery: true,
},
folderContext.workspaceContext.toolchain
);
const buildDebug = buildAllTaskCache.get(buildDebugName, folderContext, buildDebugTask);
const buildReleaseName = `Build Release ${product.name}${buildTaskNameSuffix}`;
const buildReleaseTask = createSwiftTask(
["build", "-c", "release", "--product", product.name, ...buildOptions(toolchain, false)],
`Build Release ${product.name}${buildTaskNameSuffix}`,
{
group: vscode.TaskGroup.Build,
cwd: folderContext.folder,
scope: folderContext.workspaceFolder,
presentationOptions: {
reveal: getBuildRevealOption(),
},
disableTaskQueue: true,
dontTriggerTestDiscovery: true,
},
folderContext.workspaceContext.toolchain
);
const buildRelease = buildAllTaskCache.get(buildReleaseName, folderContext, buildReleaseTask);
return [buildDebug, buildRelease];
}
/**
* Helper function to create a {@link vscode.Task Task} with the given parameters.
*/
export function createSwiftTask(
args: string[],
name: string,
config: TaskConfig,
toolchain: SwiftToolchain,
cmdEnv: { [key: string]: string } = {}
): SwiftTask {
const swift = toolchain.getToolchainExecutable("swift");
args = toolchain.buildFlags.withSwiftPackageFlags(toolchain.buildFlags.withSwiftSDKFlags(args));
// Add relative path current working directory
const cwd = config.cwd.fsPath;
const fullCwd = config.cwd.fsPath;
/* Currently there seems to be a bug in vscode where kicking off two tasks
with the same definition but different scopes messes with the task
completion code. When that is resolved we will go back to the code below
where we only store the relative cwd instead of the full cwd
const scopeWorkspaceFolder = config.scope as vscode.WorkspaceFolder;
if (scopeWorkspaceFolder.uri.fsPath) {
cwd = path.relative(scopeWorkspaceFolder.uri.fsPath, config.cwd.fsPath);
} else {
cwd = config.cwd.fsPath;
}*/
const env = { ...configuration.swiftEnvironmentVariables, ...swiftRuntimeEnv(), ...cmdEnv };
const presentation = config?.presentationOptions ?? {};
const task = new vscode.Task(
{
type: "swift",
args: args,
env: env,
cwd: cwd,
...(config.showBuildStatus !== undefined
? { showBuildStatus: config.showBuildStatus }
: {}),
...(config.disableTaskQueue !== undefined
? { disableTaskQueue: config.disableTaskQueue }
: {}),
...(config.dontTriggerTestDiscovery !== undefined
? { dontTriggerTestDiscovery: config.dontTriggerTestDiscovery }
: {}),
},
config?.scope ?? vscode.TaskScope.Workspace,
name,
"swift",
new SwiftExecution(swift, args, {
cwd: fullCwd,
env: env,
presentation,
})
);
// This doesn't include any quotes added by VS Code.
// See also: https://github.com/microsoft/vscode/issues/137895
let prefix: string;
if (config?.prefix) {
prefix = `(${config.prefix}) `;
} else {
prefix = "";
}
task.detail = `${prefix}swift ${args.join(" ")}`;
task.group = config?.group;
task.presentationOptions = presentation;
return task as SwiftTask;
}
/**
* A {@link vscode.TaskProvider TaskProvider} for tasks that match the definition
* in **package.json**: `{ type: 'swift'; args: string[], cwd: string? }`.
*
* See {@link SwiftTaskProvider.provideTasks provideTasks} for a list of provided tasks.
*/
export class SwiftTaskProvider implements vscode.TaskProvider {
static buildAllName = "Build All";
static cleanBuildName = "Clean Build";
static resolvePackageName = "Resolve Package Dependencies";
static updatePackageName = "Update Package Dependencies";
constructor(private workspaceContext: WorkspaceContext) {}
/**
* Provides tasks to run the following commands:
*
* - `swift build`
* - `swift package clean`
* - `swift package resolve`
* - `swift package update`
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async provideTasks(token: vscode.CancellationToken): Promise<vscode.Task[]> {
if (this.workspaceContext.folders.length === 0) {
return [];
}
const tasks = [];
for (const folderContext of this.workspaceContext.folders) {
if (!folderContext.swiftPackage.foundPackage) {
continue;
}
const activeOperation = folderContext.taskQueue.activeOperation;
// if there is an active task running on the folder task queue (eg resolve or update)
// then don't add build tasks for this folder instead create a dummy task indicating why
// the build tasks are unavailable
//
// Ignore an active build task, it could be the build task that has just been
// initiated.
//
// This is only required in Swift toolchains before v6 as SwiftPM in newer toolchains
// will block multiple processes accessing the .build folder at the same time
if (
this.workspaceContext.toolchain.swiftVersion.isLessThan(new Version(6, 0, 0)) &&
activeOperation &&
!activeOperation.operation.isBuildOperation
) {
let buildTaskName = "Build tasks disabled";
if (folderContext.relativePath.length > 0) {
buildTaskName += ` (${folderContext.relativePath})`;
}
const task = new vscode.Task(
{
type: "swift",
args: [],
},
folderContext.workspaceFolder,
buildTaskName,
"swift",
new vscode.CustomExecution(() => {
throw Error("Task disabled.");
})
);
task.group = vscode.TaskGroup.Build;
task.detail = `While ${activeOperation.operation.name} is running.`;
task.presentationOptions = { reveal: vscode.TaskRevealKind.Never, echo: false };
tasks.push(task);
continue;
}
// Create debug Build All task.
tasks.push(createBuildAllTask(folderContext, false));
const executables = folderContext.swiftPackage.executableProducts;
for (const executable of executables) {
tasks.push(...createBuildTasks(executable, folderContext));
}
}
return tasks;
}
/**
* Resolves a {@link vscode.Task Task} specified in **tasks.json**.
*
* Other than its definition, this `Task` may be incomplete,
* so this method should fill in the blanks.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resolveTask(task: vscode.Task, token: vscode.CancellationToken): vscode.Task {
// We need to create a new Task object here.
// Reusing the task parameter doesn't seem to work.
const swift = this.workspaceContext.toolchain.getToolchainExecutable("swift");
// platform specific
let platform: TaskPlatformSpecificConfig | undefined;
if (process.platform === "win32") {
platform = task.definition.windows;
} else if (process.platform === "linux") {
platform = task.definition.linux;
} else if (process.platform === "darwin") {
platform = task.definition.macos;
}
// get args and cwd values from either platform specific block or base
const args = platform?.args ?? task.definition.args;
const env = platform?.env ?? task.definition.env;
const fullCwd = resolveTaskCwd(task, platform?.cwd ?? task.definition.cwd);
const presentation = task.definition.presentation ?? task.presentationOptions ?? {};
const newTask = new vscode.Task(
task.definition,
task.scope ?? vscode.TaskScope.Workspace,
task.name ?? "Swift Custom Task",
"swift",
new SwiftExecution(swift, args, {
cwd: fullCwd,
env: { ...env, ...swiftRuntimeEnv() },
presentation,
}),
task.problemMatchers
);
newTask.detail = task.detail ?? `swift ${args.join(" ")}`;
newTask.group = task.group;
newTask.presentationOptions = presentation;
return newTask;
}
}