Skip to content

Commit e089aa1

Browse files
Merge pull request #14 from salesforce/r-n-d/task-provider
Add Bazel task provider
2 parents 566a346 + f697365 commit e089aa1

File tree

5 files changed

+139
-3
lines changed

5 files changed

+139
-3
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
uses: actions/setup-node@v2
2626
with:
2727
node-version: '14'
28-
- run: sudo npm install -g vsce
28+
- run: sudo npm install -g vsce --unsafe-perm
2929
- run: npm install
3030
- run: npm run compile
3131
- run: vsce package

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,27 @@ You can download the latest build from the [Releases](https://github.com/salesfo
5252
}
5353
```
5454
10. Once installed, restart VS Code
55+
56+
57+
Task Provider
58+
--------------------
59+
60+
You can use the example from the bazelTaskProvider.ts to create VSCode tasks.
61+
62+
The example provides tasks:
63+
64+
* Run 'Build' and 'Test' to run bazel build an test for all targets.
65+
66+
* Run 'Dependencies' query to creat all targets dependency graph.
67+
68+
* Run 'Formatting' to formatt all BUILD and WORKSPACE files.
69+
70+
* Run 'Unused deps' to check unused dependencies.
71+
72+
NOTE! Tasks 'Formatting' and 'Unused deps' require [buildifier](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md) and [unused deps](https://github.com/bazelbuild/buildtools/blob/master/unused_deps/README.md) installed respectively.
73+
74+
75+
Remote Development
76+
--------------------
77+
78+
If you are using Visual Studio Code Remote Development for work, please note that 'Bazel' and other additional tools must be installed on the server.

package.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,26 @@
4242
"group": "MenuViewTitle"
4343
}
4444
]
45-
}
45+
},
46+
"taskDefinitions": [
47+
{
48+
"type": "bazel",
49+
"required": [
50+
"name",
51+
"task"
52+
],
53+
"properties": {
54+
"name": {
55+
"type": "string",
56+
"description": "The task name"
57+
},
58+
"task": {
59+
"type": "string",
60+
"description": "The task command"
61+
}
62+
}
63+
}
64+
]
4665
},
4766
"scripts": {
4867
"vscode:prepublish": "gulp build-plugin",

src/bazelTaskProvider.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
import * as vscode from 'vscode';
3+
4+
export class BazelTaskProvider implements vscode.TaskProvider {
5+
6+
static BazelType = 'bazel';
7+
private bazelPromise: Thenable<vscode.Task[]> | undefined = undefined;
8+
9+
public provideTasks(): Thenable<vscode.Task[]> | undefined {
10+
if (!this.bazelPromise) {
11+
this.bazelPromise = getBazelTasks();
12+
}
13+
return this.bazelPromise;
14+
}
15+
16+
public resolveTask(_task: vscode.Task): vscode.Task | undefined {
17+
const task = _task.definition.task;
18+
if (task) {
19+
// resolveTask requires that the same definition object be used.
20+
const definition: BazelTaskDefinition = <any>_task.definition;
21+
return new vscode.Task(definition, _task.scope ?? vscode.TaskScope.Workspace, definition.name, definition.type, new vscode.ShellExecution(`${definition.task}`));
22+
}
23+
return undefined;
24+
}
25+
26+
}
27+
28+
interface BazelTaskDefinition extends vscode.TaskDefinition {
29+
30+
}
31+
32+
async function getBazelTasks(): Promise<vscode.Task[]> {
33+
34+
const tasksDefenitions: BazelTaskDefinition[] = [];
35+
36+
tasksDefenitions.push(
37+
{
38+
type: 'bazel',
39+
name: 'Build',
40+
task: 'bazel build //...'
41+
}
42+
);
43+
44+
tasksDefenitions.push(
45+
{
46+
type: 'bazel',
47+
name: 'Test',
48+
task: 'bazel test //...'
49+
}
50+
);
51+
52+
tasksDefenitions.push(
53+
{
54+
type: 'bazel',
55+
name: 'Dependencies',
56+
task: 'bazel query --notool_deps --noimplicit_deps \"deps(//...)\" --output graph'
57+
}
58+
);
59+
60+
tasksDefenitions.push(
61+
{
62+
type: 'bazel',
63+
name: 'Formatting',
64+
task: 'buildifier -r . && echo \"Formatted\"'
65+
}
66+
);
67+
68+
tasksDefenitions.push(
69+
{
70+
type: 'bazel',
71+
name: 'Unused deps',
72+
task: 'unused_deps //...'
73+
}
74+
);
75+
76+
const result: vscode.Task[] = [];
77+
78+
tasksDefenitions.forEach(function (value) {
79+
result.push(new vscode.Task(value, vscode.TaskScope.Workspace, `${value.name}`, `${value.type}`, new vscode.ShellExecution(`${value.task}`)));
80+
});
81+
82+
return result;
83+
}

src/extension.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
33
import * as pieditor from './editor';
4+
import { BazelTaskProvider } from './bazelTaskProvider';
5+
6+
let bazelTaskProvider: vscode.Disposable | undefined;
47

58
export function activate(context: vscode.ExtensionContext) {
69
let panel: vscode.WebviewPanel | undefined = undefined;
@@ -49,7 +52,14 @@ export function activate(context: vscode.ExtensionContext) {
4952
});
5053

5154
context.subscriptions.push(viewTitleDisp);
55+
56+
bazelTaskProvider = vscode.tasks.registerTaskProvider(BazelTaskProvider.BazelType, new BazelTaskProvider());
57+
5258
}
5359

5460
// this method is called when your extension is deactivated
55-
export function deactivate() { }
61+
export function deactivate() {
62+
if (bazelTaskProvider) {
63+
bazelTaskProvider.dispose();
64+
}
65+
}

0 commit comments

Comments
 (0)