Skip to content

Commit ade9b24

Browse files
author
WebFreak001
committed
Added build/run to the status bar
1 parent 3074dad commit ade9b24

4 files changed

Lines changed: 134 additions & 10 deletions

File tree

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@
112112
{
113113
"command": "code-d.reloadImports",
114114
"title": "code-d: Reload import paths"
115+
},
116+
{
117+
"command": "code-d.run",
118+
"title": "code-d: Run project"
119+
},
120+
{
121+
"command": "code-d.build",
122+
"title": "code-d: Build project"
123+
},
124+
{
125+
"command": "code-d.stop",
126+
"title": "code-d: Stop project"
115127
}
116128
],
117129
"jsonValidation": [

src/compile-buttons.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import * as vscode from "vscode"
2+
import * as ChildProcess from "child_process"
3+
import { WorkspaceD } from "./workspace-d"
4+
5+
export class CompileButtons implements vscode.Disposable {
6+
buildButton: vscode.StatusBarItem;
7+
startButton: vscode.StatusBarItem;
8+
stopButton: vscode.StatusBarItem;
9+
child: ChildProcess.ChildProcess;
10+
workspaced: WorkspaceD;
11+
output: vscode.OutputChannel;
12+
13+
constructor(workspaced: WorkspaceD) {
14+
this.workspaced = workspaced;
15+
16+
this.output = vscode.window.createOutputChannel("Run output");
17+
18+
this.buildButton = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0.99);
19+
this.startButton = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0.98);
20+
this.stopButton = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0.97);
21+
22+
this.buildButton.text = "$(file-binary)";
23+
this.startButton.text = " $(triangle-right) ";
24+
this.stopButton.text = "$(primitive-square)";
25+
26+
this.buildButton.tooltip = "Build project";
27+
this.startButton.tooltip = "Run project";
28+
this.stopButton.tooltip = "Stop running project";
29+
30+
this.buildButton.command = "code-d.build";
31+
this.startButton.command = "code-d.run";
32+
this.stopButton.command = "code-d.stop";
33+
34+
this.buildButton.show();
35+
this.startButton.show();
36+
37+
vscode.commands.registerCommand("code-d.build", this.build, this);
38+
vscode.commands.registerCommand("code-d.run", this.run, this);
39+
vscode.commands.registerCommand("code-d.stop", this.stop, this);
40+
}
41+
42+
handleData(data) {
43+
let lines = data.toString("utf8").split('\n');
44+
for (var i = 0; i < lines.length - 1; i++) {
45+
this.output.appendLine(lines[i]);
46+
}
47+
this.output.append(lines[lines.length - 1]);
48+
}
49+
50+
run() {
51+
this.startProc("run");
52+
}
53+
54+
build() {
55+
this.startProc("build");
56+
}
57+
58+
startProc(cmd) {
59+
if (!this.child) {
60+
this.output.show(vscode.ViewColumn.Three);
61+
this.output.clear();
62+
this.buildButton.hide();
63+
this.startButton.hide();
64+
Promise.all([this.workspaced.getConfiguration(), this.workspaced.getBuildType()]).then(values => {
65+
let args = [cmd, "--config=" + values[0], "--build=" + values[1]];
66+
this.output.appendLine("> dub " + args.join(" "));
67+
this.child = ChildProcess.spawn("dub", args, { cwd: vscode.workspace.rootPath, detached: true });
68+
this.child.stderr.on("data", this.handleData.bind(this));
69+
this.child.stdout.on("data", this.handleData.bind(this));
70+
this.child.once("close", (code) => {
71+
code = (code || 0);
72+
if (code === 0)
73+
this.output.appendLine(cmd + " succeeded");
74+
else
75+
this.output.appendLine("dub stopped with error code " + code);
76+
this.handleStop();
77+
});
78+
this.child.once("error", (err) => {
79+
this.output.appendLine("dub crashed:");
80+
this.output.appendLine(err.toString());
81+
this.handleStop();
82+
});
83+
84+
this.stopButton.show();
85+
});
86+
}
87+
}
88+
89+
handleStop() {
90+
this.child = null;
91+
this.buildButton.show();
92+
this.startButton.show();
93+
this.stopButton.hide();
94+
}
95+
96+
stop() {
97+
if (this.child) {
98+
process.kill(-this.child.pid);
99+
this.handleStop();
100+
}
101+
}
102+
103+
dispose() {
104+
this.stop();
105+
}
106+
}

src/extension.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
import { D_MODE } from "./dmode"
33
import { WorkspaceD } from "./workspace-d"
4+
import { CompileButtons } from "./compile-buttons"
45
import * as statusbar from "./statusbar"
56

67
let diagnosticCollection: vscode.DiagnosticCollection;
@@ -14,6 +15,7 @@ export function activate(context: vscode.ExtensionContext) {
1415
console.warn("Could not initialize code-d");
1516
return;
1617
}
18+
1719
let workspaced = new WorkspaceD(vscode.workspace.rootPath);
1820
context.subscriptions.push(vscode.languages.registerSignatureHelpProvider(D_MODE, workspaced, "(", ","));
1921
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(D_MODE, workspaced));
@@ -22,7 +24,9 @@ export function activate(context: vscode.ExtensionContext) {
2224
context.subscriptions.push(vscode.languages.registerDefinitionProvider(D_MODE, workspaced));
2325
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(D_MODE, workspaced));
2426
context.subscriptions.push(workspaced);
27+
2528
context.subscriptions.push(statusbar.setup(workspaced));
29+
context.subscriptions.push(new CompileButtons(workspaced));
2630

2731
vscode.languages.setLanguageConfiguration(D_MODE.language, {
2832
__electricCharacterSupport: {
@@ -86,27 +90,27 @@ export function activate(context: vscode.ExtensionContext) {
8690
}
8791
}));
8892

89-
vscode.commands.registerCommand("code-d.switchConfiguration", () => {
93+
context.subscriptions.push(vscode.commands.registerCommand("code-d.switchConfiguration", () => {
9094
vscode.window.showQuickPick(workspaced.listConfigurations()).then((config) => {
9195
if (config)
9296
workspaced.setConfiguration(config);
9397
});
9498
}, (err) => {
9599
console.error(err);
96100
vscode.window.showErrorMessage("Failed to switch configuration. See console for details.");
97-
});
101+
}));
98102

99-
vscode.commands.registerCommand("code-d.switchBuildType", () => {
103+
context.subscriptions.push(vscode.commands.registerCommand("code-d.switchBuildType", () => {
100104
vscode.window.showQuickPick(workspaced.listBuildTypes()).then((config) => {
101105
if (config)
102106
workspaced.setBuildType(config);
103107
});
104108
}, (err) => {
105109
console.error(err);
106110
vscode.window.showErrorMessage("Failed to switch build type. See console for details.");
107-
});
111+
}));
108112

109-
vscode.commands.registerCommand("code-d.killServer", () => {
113+
context.subscriptions.push(vscode.commands.registerCommand("code-d.killServer", () => {
110114
workspaced.killServer().then((res) => {
111115
vscode.window.showInformationMessage("Killed DCD-Server", "Restart").then((pick) => {
112116
if (pick == "Restart")
@@ -116,18 +120,18 @@ export function activate(context: vscode.ExtensionContext) {
116120
console.error(err);
117121
vscode.window.showErrorMessage("Failed to kill DCD-Server. See console for details.");
118122
});
119-
});
123+
}));
120124

121-
vscode.commands.registerCommand("code-d.restartServer", () => {
125+
context.subscriptions.push(vscode.commands.registerCommand("code-d.restartServer", () => {
122126
workspaced.restartServer().then((res) => {
123127
vscode.window.showInformationMessage("Restarted DCD-Server");
124128
}, (err) => {
125129
console.error(err);
126130
vscode.window.showErrorMessage("Failed to kill DCD-Server. See console for details.");
127131
});
128-
});
132+
}));
129133

130-
vscode.commands.registerCommand("code-d.reloadImports", () => {
134+
context.subscriptions.push(vscode.commands.registerCommand("code-d.reloadImports", () => {
131135
workspaced.updateImports().then((success) => {
132136
if (success)
133137
vscode.window.showInformationMessage("Successfully reloaded import paths");
@@ -136,7 +140,7 @@ export function activate(context: vscode.ExtensionContext) {
136140
}, (err) => {
137141
vscode.window.showErrorMessage("Could not update imports. dub might not be initialized yet!");
138142
});
139-
});
143+
}));
140144

141145
console.log("Initialized code-d");
142146
}

src/statusbar.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ConfigSelector implements vscode.Disposable {
2222
private create() {
2323
this.item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 2);
2424
this.item.command = "code-d.switchConfiguration";
25+
this.item.tooltip = "Switch Configuration";
2526
this.item.show();
2627
this.workspaced.on("configuration-change", config => {
2728
this.item.text = config;
@@ -47,6 +48,7 @@ class BuildSelector implements vscode.Disposable {
4748
private create() {
4849
this.item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 1);
4950
this.item.command = "code-d.switchBuildType";
51+
this.item.tooltip = "Switch Build Type";
5052
this.item.show();
5153
this.workspaced.on("build-type-change", config => {
5254
this.item.text = config;

0 commit comments

Comments
 (0)