Skip to content

Commit a28b7f0

Browse files
committed
Allow using the default swift build task for background compilation
This is not related to background indexing, but instead the optional background compilation feature that runs a build task on save. Essentially when `swift.backgroundCompilation.useDefaultTask` is enabled, any `swift` task marked as the default "build" group task will be used instead of the Build All task. This way the whole world is not built for large projects. Issue: #1320
1 parent 04fba14 commit a28b7f0

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,13 @@
595595
"swift.backgroundCompilation": {
596596
"type": "boolean",
597597
"default": false,
598-
"markdownDescription": "**Experimental**: Run `swift build` in the background whenever a file is saved. It is possible the background compilation will already be running when you attempt a compile yourself, so this is disabled by default.",
598+
"markdownDescription": "Run `swift build` in the background whenever a file is saved. It is possible the background compilation will already be running when you attempt a compile yourself, so this is disabled by default.",
599+
"scope": "machine-overridable"
600+
},
601+
"swift.backgroundCompilation.useDefaultTask": {
602+
"type": "boolean",
603+
"default": false,
604+
"markdownDescription": "Use the default build task configured using the `Tasks: Configure Default Build Task` command when executing the background compilation. `#swift.backgroundCompilation#` must be enabled.",
599605
"scope": "machine-overridable"
600606
},
601607
"swift.actionAfterBuildError": {

src/BackgroundCompilation.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class BackgroundCompilation implements vscode.Disposable {
8686
*/
8787
async runTask() {
8888
// create compile task and execute it
89-
const backgroundTask = await getBuildAllTask(this.folderContext);
89+
const backgroundTask = await this.getTask();
9090
if (!backgroundTask) {
9191
return;
9292
}
@@ -96,4 +96,16 @@ export class BackgroundCompilation implements vscode.Disposable {
9696
// can ignore if running task fails
9797
}
9898
}
99+
100+
async getTask(): Promise<vscode.Task> {
101+
if (!configuration.useDefaultTask) {
102+
return await getBuildAllTask(this.folderContext);
103+
}
104+
105+
const tasks = await vscode.tasks.fetchTasks({ type: "swift" });
106+
const defaultTask = tasks.find(
107+
t => t.group?.id === vscode.TaskGroup.Build.id && t.group?.isDefault
108+
);
109+
return defaultTask || (await getBuildAllTask(this.folderContext));
110+
}
99111
}

src/configuration.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,12 @@ const configuration = {
419419
.getConfiguration("swift")
420420
.get<boolean>("backgroundCompilation", false);
421421
},
422+
/** use the default `swift` build task when background compilation is enabled */
423+
get useDefaultTask(): boolean {
424+
return vscode.workspace
425+
.getConfiguration("swift.backgroundCompilation")
426+
.get<boolean>("useDefaultTask", false);
427+
},
422428
/** background indexing */
423429
get backgroundIndexing(): "on" | "off" | "auto" {
424430
const value = vscode.workspace

test/integration-tests/BackgroundCompilation.test.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,39 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
import * as assert from "assert";
14+
import { expect } from "chai";
15+
import { match } from "sinon";
1516
import * as vscode from "vscode";
1617

18+
import { BackgroundCompilation } from "@src/BackgroundCompilation";
19+
import { FolderContext } from "@src/FolderContext";
1720
import { WorkspaceContext } from "@src/WorkspaceContext";
21+
import configuration from "@src/configuration";
22+
import { getBuildAllTask } from "@src/tasks/SwiftTaskProvider";
1823

24+
import { mockGlobalObject, mockGlobalValue } from "../MockUtils";
1925
import { testAssetUri } from "../fixtures";
2026
import { tag } from "../tags";
2127
import { closeAllEditors } from "../utilities/commands";
2228
import { waitForNoRunningTasks } from "../utilities/tasks";
23-
import { activateExtensionForTest, updateSettings } from "./utilities/testutilities";
29+
import {
30+
activateExtensionForTest,
31+
folderInRootWorkspace,
32+
updateSettings,
33+
} from "./utilities/testutilities";
2434

2535
tag("large").suite("BackgroundCompilation Test Suite", () => {
2636
let subscriptions: vscode.Disposable[];
2737
let workspaceContext: WorkspaceContext;
38+
let folderContext: FolderContext;
39+
let buildAllTask: vscode.Task;
2840

2941
activateExtensionForTest({
3042
async setup(ctx) {
3143
subscriptions = [];
3244
workspaceContext = ctx;
33-
assert.notEqual(workspaceContext.folders.length, 0);
45+
folderContext = await folderInRootWorkspace("defaultPackage", workspaceContext);
46+
buildAllTask = await getBuildAllTask(folderContext);
3447
return await updateSettings({
3548
"swift.backgroundCompilation": true,
3649
});
@@ -62,4 +75,65 @@ tag("large").suite("BackgroundCompilation Test Suite", () => {
6275
await taskStartPromise;
6376
await waitForNoRunningTasks();
6477
});
78+
79+
suite("getTask", () => {
80+
const tasksMock = mockGlobalObject(vscode, "tasks");
81+
let swiftTask: vscode.Task;
82+
let nonSwiftTask: vscode.Task;
83+
let backgroundConfiguration: BackgroundCompilation;
84+
const useDefaultTaskConfig = mockGlobalValue(configuration, "useDefaultTask");
85+
86+
setup(async () => {
87+
nonSwiftTask = new vscode.Task(
88+
{
89+
type: "shell",
90+
args: ["./build.sh"],
91+
cwd: "defaultPackage",
92+
group: {
93+
id: "build",
94+
isDefault: true,
95+
},
96+
label: "Default build",
97+
},
98+
folderContext.workspaceFolder,
99+
"Default build",
100+
"shell"
101+
);
102+
swiftTask = new vscode.Task(
103+
{
104+
type: "swift",
105+
args: ["build"],
106+
cwd: "defaultPackage",
107+
group: "build",
108+
label: "swift build",
109+
},
110+
folderContext.workspaceFolder,
111+
"Swift build",
112+
"swift"
113+
);
114+
backgroundConfiguration = new BackgroundCompilation(folderContext);
115+
tasksMock.fetchTasks.resolves([nonSwiftTask, swiftTask, buildAllTask]);
116+
tasksMock.fetchTasks.withArgs(match.object).resolves([swiftTask, buildAllTask]);
117+
useDefaultTaskConfig.setValue(true);
118+
});
119+
120+
teardown(() => {
121+
backgroundConfiguration.dispose();
122+
});
123+
124+
test("non-swift default task", async () => {
125+
expect(await backgroundConfiguration.getTask()).to.equal(buildAllTask);
126+
});
127+
128+
test("swift default task", async () => {
129+
swiftTask.group = { id: "build", isDefault: true };
130+
expect(await backgroundConfiguration.getTask()).to.equal(swiftTask);
131+
});
132+
133+
test("don't use default task", async () => {
134+
useDefaultTaskConfig.setValue(false);
135+
swiftTask.group = { id: "build", isDefault: true };
136+
expect(await backgroundConfiguration.getTask()).to.equal(buildAllTask);
137+
});
138+
});
65139
});

0 commit comments

Comments
 (0)