Skip to content
This repository was archived by the owner on Jun 22, 2024. It is now read-only.

Commit 129761f

Browse files
committed
Encapsulate swift toolchain
- add other build-related commands - build on deman - run - run TARGET - clean - clean up diagnostic parsing
1 parent 2062e36 commit 129761f

File tree

5 files changed

+317
-177
lines changed

5 files changed

+317
-177
lines changed

package.json

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@
3636
"activationEvents": [
3737
"onLanguage:swift",
3838
"workspaceContains:**/*swift",
39-
"onCommand:sde.commands.buildPackage"
39+
"onCommand:sde.commands.build",
40+
"onCommand:sde.commands.run",
41+
"onCommand:sde.commands.clean",
42+
"onCommand:sde.commands.selectRun"
4043
],
4144
"main": "./out/clientMain",
4245
"contributes": {
4346
"commands": [
4447
{
45-
"command": "sde.commands.buildPackage",
48+
"command": "sde.commands.build",
4649
"title": "Build Package",
4750
"category": "SDE"
4851
},
@@ -52,16 +55,55 @@
5255
"category": "SDE"
5356
},
5457
{
55-
"command": "sde.commands.runPackage",
56-
"title": "Run Package",
58+
"command": "sde.commands.run",
59+
"title": "Run Default Target",
60+
"category": "SDE",
61+
"enablement": "sde:running == false"
62+
},
63+
{
64+
"command": "sde.commands.selectRun",
65+
"title": "Run Target…",
66+
"category": "SDE",
67+
"enablement": "sde:running == false"
68+
},
69+
{
70+
"command": "sde.commands.restartRun",
71+
"title": "Restart Target",
72+
"category": "SDE",
73+
"enablement": "sde:running == true"
74+
},
75+
{
76+
"command": "sde.commands.stop",
77+
"title": "Stop Running Target",
78+
"category": "SDE",
79+
"enablement": "sde:running == true"
80+
},
81+
{
82+
"command": "sde.commands.clean",
83+
"title": "Clean Package",
5784
"category": "SDE"
5885
}
5986
],
6087
"keybindings": [
6188
{
62-
"command": "sde.commands.buildPackage",
63-
"key": "alt+b",
64-
"mac": "alt+b"
89+
"command": "sde.commands.build",
90+
"key": "alt+b"
91+
},
92+
{
93+
"command": "sde.commands.run",
94+
"key": "alt+r"
95+
},
96+
{
97+
"command": "sde.commands.selectRun",
98+
"key": "alt+shift+r"
99+
},
100+
{
101+
"command": "sde.commands.restart",
102+
"key": "alt+shift+r"
103+
},
104+
{
105+
"command": "sde.commands.stop",
106+
"key": "alt+s"
65107
}
66108
],
67109
"configuration": {

src/clientMain.ts

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,78 @@
11
"use strict";
22

33
import * as fs from "fs";
4-
import * as tools from "./SwiftTools";
54
import * as path from "path";
6-
import {
7-
commands,
8-
DiagnosticCollection,
9-
ExtensionContext,
10-
languages,
11-
TextDocument,
12-
window,
13-
workspace,
14-
} from "vscode";
5+
import { commands, DiagnosticCollection, ExtensionContext, window, workspace } from "vscode";
156
import { absolutePath } from "./helpers/AbsolutePath";
7+
import * as tools from "./toolchain/SwiftTools";
168
import * as config from "./vscode/config-helpers";
179
import lsp from "./vscode/lsp-interop";
1810
import output from "./vscode/output-channels";
1911
import { statusBarItem } from "./vscode/status-bar";
2012

2113
let swiftBinPath: string | null = null;
2214
let swiftBuildParams: string[] = ["build"];
23-
let swiftPackageManifestPath: string | null = null;
2415
let skProtocolProcess: string | null = null;
2516
let skProtocolProcessAsShellCmd: string | null = null;
2617
export let diagnosticCollection: DiagnosticCollection;
2718

19+
let mostRecentRunTarget = "";
20+
2821
export function activate(context: ExtensionContext) {
2922
output.init(context);
3023

3124
if (workspace.getConfiguration().get<boolean>("sde.enable") === false) {
3225
output.build.log("SDE Disabled", false);
3326
return;
3427
}
28+
tools.setRunning(false);
3529
output.build.log("Activating SDE");
3630

3731
initConfig();
3832
lsp.startLSPClient(context);
3933

40-
diagnosticCollection = languages.createDiagnosticCollection("swift");
41-
context.subscriptions.push(diagnosticCollection);
42-
4334
//commands
35+
let toolchain = new tools.Toolchain(
36+
swiftBinPath,
37+
workspace.workspaceFolders[0].uri.fsPath,
38+
swiftBuildParams
39+
);
40+
context.subscriptions.push(toolchain.diagnostics);
41+
context.subscriptions.push(toolchain.start());
4442
context.subscriptions.push(
45-
commands.registerCommand("sde.commands.buildPackage", buildSPMPackage),
43+
commands.registerCommand("sde.commands.build", () => toolchain.build()),
4644
commands.registerCommand("sde.commands.restartLanguageServer", () =>
4745
lsp.restartLSPClient(context)
4846
),
49-
commands.registerCommand("sde.commands.runPackage", () => {
50-
output.build.log("sde.commands.runPackage");
51-
})
47+
commands.registerCommand("sde.commands.run", () => toolchain.runStart()),
48+
commands.registerCommand("sde.commands.selectRun", () => {
49+
window
50+
.showInputBox({ prompt: "Run which target?", value: mostRecentRunTarget })
51+
.then(target => {
52+
if (!target) {
53+
return;
54+
}
55+
mostRecentRunTarget = target;
56+
toolchain.runStart(target);
57+
});
58+
}),
59+
commands.registerCommand("sde.commands.restart", () => {
60+
toolchain.runStop();
61+
toolchain.runStart(mostRecentRunTarget);
62+
}),
63+
commands.registerCommand("sde.commands.stop", () => toolchain.runStop()),
64+
commands.registerCommand("sde.commands.clean", () => toolchain.clean())
5265
);
5366

54-
workspace.onDidSaveTextDocument(onSave, null, context.subscriptions);
67+
workspace.onDidSaveTextDocument(
68+
document => {
69+
if (tools.shouldBuildOnSave() && document.languageId === "swift") {
70+
toolchain.build();
71+
}
72+
},
73+
null,
74+
context.subscriptions
75+
);
5576

5677
// respond to settings changes
5778
workspace.onDidChangeConfiguration(event => {
@@ -61,33 +82,11 @@ export function activate(context: ExtensionContext) {
6182
});
6283

6384
// build on startup
64-
buildSPMPackage();
85+
toolchain.build();
6586
}
6687

6788
function initConfig() {
6889
checkToolsAvailability();
69-
70-
//FIXME rootPath may be undefined for adhoc file editing mode???
71-
swiftPackageManifestPath = workspace.workspaceFolders[0]
72-
? path.join(workspace.workspaceFolders[0].uri.fsPath, "Package.swift")
73-
: null;
74-
}
75-
76-
function buildSPMPackage() {
77-
if (isSPMProject()) {
78-
statusBarItem.start();
79-
tools.buildPackage(swiftBinPath, workspace.rootPath, swiftBuildParams);
80-
}
81-
}
82-
83-
function onSave(document: TextDocument) {
84-
if (config.buildOnSave() && document.languageId === "swift") {
85-
buildSPMPackage();
86-
}
87-
}
88-
89-
function isSPMProject(): boolean {
90-
return swiftPackageManifestPath ? fs.existsSync(swiftPackageManifestPath) : false;
9190
}
9291

9392
function checkToolsAvailability() {
@@ -105,7 +104,7 @@ function checkToolsAvailability() {
105104

106105
if (!swiftBinPath || !fs.existsSync(swiftBinPath)) {
107106
window.showErrorMessage(
108-
'missing dependent swift tool, please configure correct "swift.path.swift_driver_bin"'
107+
'missing dependent `swift` tool, please configure correct "swift.path.swift_driver_bin"'
109108
);
110109
}
111110
if (!sourcekitePathEnableShCmd) {

0 commit comments

Comments
 (0)