Skip to content

Commit 73971f2

Browse files
committed
Optionally provide build tasks for library products
1 parent 40bdb78 commit 73971f2

File tree

7 files changed

+82
-2
lines changed

7 files changed

+82
-2
lines changed

assets/test/defaultPackage/Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ let package = Package(
99
.library(
1010
name: "PackageLib",
1111
targets: ["PackageLib"]),
12+
.library(
13+
name: "PackageLib2",
14+
type: .dynamic,
15+
targets: ["PackageLib"]),
1216
],
1317
targets: [
1418
// Targets are the basic building blocks of a package. A target can define a module or a test suite.

assets/test/defaultPackage/Package@swift-6.0.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ let package = Package(
1212
.library(
1313
name: "PackageLib",
1414
targets: ["PackageLib"]),
15+
.library(
16+
name: "PackageLib2",
17+
type: .dynamic,
18+
targets: ["PackageLib"]),
1519
],
1620
targets: [
1721
// Targets are the basic building blocks of a package. A target can define a module or a test suite.

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,12 @@
651651
],
652652
"scope": "application"
653653
},
654+
"swift.createTasksForLibraryProducts": {
655+
"type": "boolean",
656+
"default": false,
657+
"markdownDescription": "When enabled, the extension will create \"swift\" build tasks for library products in the package manifest. Note that automatic library products will not be included.",
658+
"scope": "machine-overridable"
659+
},
654660
"swift.showCreateSwiftProjectInWelcomePage": {
655661
"type": "boolean",
656662
"default": true,

src/SwiftPackage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export interface Product {
3737
type: { executable?: null; library?: string[] };
3838
}
3939

40+
export function isAutomatic(product: Product): boolean {
41+
return (product.type.library || []).includes("automatic");
42+
}
43+
4044
/** Swift Package Manager target */
4145
export interface Target {
4246
name: string;

src/configuration.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,12 @@ const configuration = {
405405
.getConfiguration("swift")
406406
.get<ShowBuildStatusOptions>("showBuildStatus", "swiftStatus");
407407
},
408+
/** create build tasks for the library products of the package(s) */
409+
get createTasksForLibraryProducts(): boolean {
410+
return vscode.workspace
411+
.getConfiguration("swift")
412+
.get<boolean>("createTasksForLibraryProducts", false);
413+
},
408414
/** background compilation */
409415
get backgroundCompilation(): boolean {
410416
return vscode.workspace

src/tasks/SwiftTaskProvider.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import * as vscode from "vscode";
1616
import { WorkspaceContext } from "../WorkspaceContext";
1717
import { FolderContext } from "../FolderContext";
18-
import { Product } from "../SwiftPackage";
18+
import { isAutomatic, Product } from "../SwiftPackage";
1919
import configuration, {
2020
ShowBuildStatusOptions,
2121
substituteVariablesInString,
@@ -425,6 +425,16 @@ export class SwiftTaskProvider implements vscode.TaskProvider {
425425
for (const executable of executables) {
426426
tasks.push(...createBuildTasks(executable, folderContext));
427427
}
428+
429+
if (configuration.createTasksForLibraryProducts) {
430+
const libraries = await folderContext.swiftPackage.libraryProducts;
431+
for (const lib of libraries) {
432+
if (isAutomatic(lib)) {
433+
continue;
434+
}
435+
tasks.push(...createBuildTasks(lib, folderContext));
436+
}
437+
}
428438
}
429439
return tasks;
430440
}

test/integration-tests/tasks/SwiftTaskProvider.test.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import { executeTaskAndWaitForResult, waitForEndTaskProcess } from "../../utilit
2626
import { Version } from "../../../src/utilities/version";
2727
import { FolderContext } from "../../../src/FolderContext";
2828
import { mockGlobalObject } from "../../MockUtils";
29-
import { activateExtensionForSuite, folderInRootWorkspace } from "../utilities/testutilities";
29+
import {
30+
activateExtensionForSuite,
31+
folderInRootWorkspace,
32+
updateSettings,
33+
} from "../utilities/testutilities";
3034

3135
suite("SwiftTaskProvider Test Suite", () => {
3236
let workspaceContext: WorkspaceContext;
@@ -92,6 +96,13 @@ suite("SwiftTaskProvider Test Suite", () => {
9296
});
9397

9498
suite("provideTasks", () => {
99+
let resetSettings: (() => Promise<void>) | undefined;
100+
teardown(async () => {
101+
if (resetSettings) {
102+
await resetSettings();
103+
}
104+
});
105+
95106
suite("includes build all task from extension", () => {
96107
let task: vscode.Task | undefined;
97108

@@ -150,6 +161,41 @@ suite("SwiftTaskProvider Test Suite", () => {
150161
expect(task?.detail).to.include("swift build --product PackageExe");
151162
});
152163

164+
test("includes library build tasks task", async () => {
165+
const taskProvider = workspaceContext.taskProvider;
166+
let tasks = await taskProvider.provideTasks(new vscode.CancellationTokenSource().token);
167+
let task = tasks.find(t => t.name === "Build Debug PackageLib2 (defaultPackage)");
168+
expect(task).to.be.undefined;
169+
task = tasks.find(t => t.name === "Build Release PackageLib2 (defaultPackage)");
170+
expect(task).to.be.undefined;
171+
172+
resetSettings = await updateSettings({
173+
"swift.createTasksForLibraryProducts": true,
174+
});
175+
176+
tasks = await taskProvider.provideTasks(new vscode.CancellationTokenSource().token);
177+
task = tasks.find(t => t.name === "Build Debug PackageLib2 (defaultPackage)");
178+
expect(
179+
task,
180+
'expected to find a task named "Build Debug PackageLib2 (defaultPackage)", instead found ' +
181+
tasks.map(t => t.name)
182+
).to.not.be.undefined;
183+
expect(task?.detail).to.include("swift build --product PackageLib2");
184+
task = tasks.find(t => t.name === "Build Release PackageLib2 (defaultPackage)");
185+
expect(
186+
task,
187+
'expected to find a task named "Build Release PackageLib2 (defaultPackage)", instead found ' +
188+
tasks.map(t => t.name)
189+
).to.not.be.undefined;
190+
expect(task?.detail).to.include("swift build -c release --product PackageLib2");
191+
192+
// Don't include automatic products
193+
task = tasks.find(t => t.name === "Build Debug PackageLib (defaultPackage)");
194+
expect(task).to.be.undefined;
195+
task = tasks.find(t => t.name === "Build Release PackageLib (defaultPackage)");
196+
expect(task).to.be.undefined;
197+
});
198+
153199
test("includes product release task", async () => {
154200
const taskProvider = workspaceContext.taskProvider;
155201
const tasks = await taskProvider.provideTasks(

0 commit comments

Comments
 (0)