Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/grunt/src/main.ts
3291 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as path from 'path';
7
import * as fs from 'fs';
8
import * as cp from 'child_process';
9
import * as vscode from 'vscode';
10
11
type AutoDetect = 'on' | 'off';
12
13
function exists(file: string): Promise<boolean> {
14
return new Promise<boolean>((resolve, _reject) => {
15
fs.exists(file, (value) => {
16
resolve(value);
17
});
18
});
19
}
20
21
function exec(command: string, options: cp.ExecOptions): Promise<{ stdout: string; stderr: string }> {
22
return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
23
cp.exec(command, options, (error, stdout, stderr) => {
24
if (error) {
25
reject({ error, stdout, stderr });
26
}
27
resolve({ stdout, stderr });
28
});
29
});
30
}
31
32
const buildNames: string[] = ['build', 'compile', 'watch'];
33
function isBuildTask(name: string): boolean {
34
for (const buildName of buildNames) {
35
if (name.indexOf(buildName) !== -1) {
36
return true;
37
}
38
}
39
return false;
40
}
41
42
const testNames: string[] = ['test'];
43
function isTestTask(name: string): boolean {
44
for (const testName of testNames) {
45
if (name.indexOf(testName) !== -1) {
46
return true;
47
}
48
}
49
return false;
50
}
51
52
let _channel: vscode.OutputChannel;
53
function getOutputChannel(): vscode.OutputChannel {
54
if (!_channel) {
55
_channel = vscode.window.createOutputChannel('Grunt Auto Detection');
56
}
57
return _channel;
58
}
59
60
function showError() {
61
vscode.window.showWarningMessage(vscode.l10n.t("Problem finding grunt tasks. See the output for more information."),
62
vscode.l10n.t("Go to output")).then(() => {
63
getOutputChannel().show(true);
64
});
65
}
66
interface GruntTaskDefinition extends vscode.TaskDefinition {
67
task: string;
68
args?: string[];
69
file?: string;
70
}
71
72
async function findGruntCommand(rootPath: string): Promise<string> {
73
let command: string;
74
const platform = process.platform;
75
if (platform === 'win32' && await exists(path.join(rootPath!, 'node_modules', '.bin', 'grunt.cmd'))) {
76
command = path.join('.', 'node_modules', '.bin', 'grunt.cmd');
77
} else if ((platform === 'linux' || platform === 'darwin') && await exists(path.join(rootPath!, 'node_modules', '.bin', 'grunt'))) {
78
command = path.join('.', 'node_modules', '.bin', 'grunt');
79
} else {
80
command = 'grunt';
81
}
82
return command;
83
}
84
85
class FolderDetector {
86
87
private fileWatcher: vscode.FileSystemWatcher | undefined;
88
private promise: Thenable<vscode.Task[]> | undefined;
89
90
constructor(
91
private _workspaceFolder: vscode.WorkspaceFolder,
92
private _gruntCommand: Promise<string>) {
93
}
94
95
public get workspaceFolder(): vscode.WorkspaceFolder {
96
return this._workspaceFolder;
97
}
98
99
public isEnabled(): boolean {
100
return vscode.workspace.getConfiguration('grunt', this._workspaceFolder.uri).get<AutoDetect>('autoDetect') === 'on';
101
}
102
103
public start(): void {
104
const pattern = path.join(this._workspaceFolder.uri.fsPath, '{node_modules,[Gg]runtfile.js}');
105
this.fileWatcher = vscode.workspace.createFileSystemWatcher(pattern);
106
this.fileWatcher.onDidChange(() => this.promise = undefined);
107
this.fileWatcher.onDidCreate(() => this.promise = undefined);
108
this.fileWatcher.onDidDelete(() => this.promise = undefined);
109
}
110
111
public async getTasks(): Promise<vscode.Task[]> {
112
if (this.isEnabled()) {
113
if (!this.promise) {
114
this.promise = this.computeTasks();
115
}
116
return this.promise;
117
} else {
118
return [];
119
}
120
}
121
122
public async getTask(_task: vscode.Task): Promise<vscode.Task | undefined> {
123
const taskDefinition = <any>_task.definition;
124
const gruntTask = taskDefinition.task;
125
if (gruntTask) {
126
const options: vscode.ShellExecutionOptions = { cwd: this.workspaceFolder.uri.fsPath };
127
const source = 'grunt';
128
const task = gruntTask.indexOf(' ') === -1
129
? new vscode.Task(taskDefinition, this.workspaceFolder, gruntTask, source, new vscode.ShellExecution(`${await this._gruntCommand}`, [gruntTask, ...taskDefinition.args], options))
130
: new vscode.Task(taskDefinition, this.workspaceFolder, gruntTask, source, new vscode.ShellExecution(`${await this._gruntCommand}`, [`"${gruntTask}"`, ...taskDefinition.args], options));
131
return task;
132
}
133
return undefined;
134
}
135
136
private async computeTasks(): Promise<vscode.Task[]> {
137
const rootPath = this._workspaceFolder.uri.scheme === 'file' ? this._workspaceFolder.uri.fsPath : undefined;
138
const emptyTasks: vscode.Task[] = [];
139
if (!rootPath) {
140
return emptyTasks;
141
}
142
if (!await exists(path.join(rootPath, 'gruntfile.js')) && !await exists(path.join(rootPath, 'Gruntfile.js'))) {
143
return emptyTasks;
144
}
145
146
const commandLine = `${await this._gruntCommand} --help --no-color`;
147
try {
148
const { stdout, stderr } = await exec(commandLine, { cwd: rootPath });
149
if (stderr) {
150
getOutputChannel().appendLine(stderr);
151
showError();
152
}
153
const result: vscode.Task[] = [];
154
if (stdout) {
155
// grunt lists tasks as follows (description is wrapped into a new line if too long):
156
// ...
157
// Available tasks
158
// uglify Minify files with UglifyJS. *
159
// jshint Validate files with JSHint. *
160
// test Alias for "jshint", "qunit" tasks.
161
// default Alias for "jshint", "qunit", "concat", "uglify" tasks.
162
// long Alias for "eslint", "qunit", "browserify", "sass",
163
// "autoprefixer", "uglify", tasks.
164
//
165
// Tasks run in the order specified
166
167
const lines = stdout.split(/\r{0,1}\n/);
168
let tasksStart = false;
169
let tasksEnd = false;
170
for (const line of lines) {
171
if (line.length === 0) {
172
continue;
173
}
174
if (!tasksStart && !tasksEnd) {
175
if (line.indexOf('Available tasks') === 0) {
176
tasksStart = true;
177
}
178
} else if (tasksStart && !tasksEnd) {
179
if (line.indexOf('Tasks run in the order specified') === 0) {
180
tasksEnd = true;
181
} else {
182
const regExp = /^\s*(\S.*\S) \S/g;
183
const matches = regExp.exec(line);
184
if (matches && matches.length === 2) {
185
const name = matches[1];
186
const kind: GruntTaskDefinition = {
187
type: 'grunt',
188
task: name
189
};
190
const source = 'grunt';
191
const options: vscode.ShellExecutionOptions = { cwd: this.workspaceFolder.uri.fsPath };
192
const task = name.indexOf(' ') === -1
193
? new vscode.Task(kind, this.workspaceFolder, name, source, new vscode.ShellExecution(`${await this._gruntCommand} ${name}`, options))
194
: new vscode.Task(kind, this.workspaceFolder, name, source, new vscode.ShellExecution(`${await this._gruntCommand} "${name}"`, options));
195
result.push(task);
196
const lowerCaseTaskName = name.toLowerCase();
197
if (isBuildTask(lowerCaseTaskName)) {
198
task.group = vscode.TaskGroup.Build;
199
} else if (isTestTask(lowerCaseTaskName)) {
200
task.group = vscode.TaskGroup.Test;
201
}
202
}
203
}
204
}
205
}
206
}
207
return result;
208
} catch (err) {
209
const channel = getOutputChannel();
210
if (err.stderr) {
211
channel.appendLine(err.stderr);
212
}
213
if (err.stdout) {
214
channel.appendLine(err.stdout);
215
}
216
channel.appendLine(vscode.l10n.t("Auto detecting Grunt for folder {0} failed with error: {1}', this.workspaceFolder.name, err.error ? err.error.toString() : 'unknown"));
217
showError();
218
return emptyTasks;
219
}
220
}
221
222
public dispose() {
223
this.promise = undefined;
224
if (this.fileWatcher) {
225
this.fileWatcher.dispose();
226
}
227
}
228
}
229
230
class TaskDetector {
231
232
private taskProvider: vscode.Disposable | undefined;
233
private detectors: Map<string, FolderDetector> = new Map();
234
235
constructor() {
236
}
237
238
public start(): void {
239
const folders = vscode.workspace.workspaceFolders;
240
if (folders) {
241
this.updateWorkspaceFolders(folders, []);
242
}
243
vscode.workspace.onDidChangeWorkspaceFolders((event) => this.updateWorkspaceFolders(event.added, event.removed));
244
vscode.workspace.onDidChangeConfiguration(this.updateConfiguration, this);
245
}
246
247
public dispose(): void {
248
if (this.taskProvider) {
249
this.taskProvider.dispose();
250
this.taskProvider = undefined;
251
}
252
this.detectors.clear();
253
}
254
255
private updateWorkspaceFolders(added: readonly vscode.WorkspaceFolder[], removed: readonly vscode.WorkspaceFolder[]): void {
256
for (const remove of removed) {
257
const detector = this.detectors.get(remove.uri.toString());
258
if (detector) {
259
detector.dispose();
260
this.detectors.delete(remove.uri.toString());
261
}
262
}
263
for (const add of added) {
264
const detector = new FolderDetector(add, findGruntCommand(add.uri.fsPath));
265
this.detectors.set(add.uri.toString(), detector);
266
if (detector.isEnabled()) {
267
detector.start();
268
}
269
}
270
this.updateProvider();
271
}
272
273
private updateConfiguration(): void {
274
for (const detector of this.detectors.values()) {
275
detector.dispose();
276
this.detectors.delete(detector.workspaceFolder.uri.toString());
277
}
278
const folders = vscode.workspace.workspaceFolders;
279
if (folders) {
280
for (const folder of folders) {
281
if (!this.detectors.has(folder.uri.toString())) {
282
const detector = new FolderDetector(folder, findGruntCommand(folder.uri.fsPath));
283
this.detectors.set(folder.uri.toString(), detector);
284
if (detector.isEnabled()) {
285
detector.start();
286
}
287
}
288
}
289
}
290
this.updateProvider();
291
}
292
293
private updateProvider(): void {
294
if (!this.taskProvider && this.detectors.size > 0) {
295
const thisCapture = this;
296
this.taskProvider = vscode.tasks.registerTaskProvider('grunt', {
297
provideTasks: (): Promise<vscode.Task[]> => {
298
return thisCapture.getTasks();
299
},
300
resolveTask(_task: vscode.Task): Promise<vscode.Task | undefined> {
301
return thisCapture.getTask(_task);
302
}
303
});
304
}
305
else if (this.taskProvider && this.detectors.size === 0) {
306
this.taskProvider.dispose();
307
this.taskProvider = undefined;
308
}
309
}
310
311
public getTasks(): Promise<vscode.Task[]> {
312
return this.computeTasks();
313
}
314
315
private computeTasks(): Promise<vscode.Task[]> {
316
if (this.detectors.size === 0) {
317
return Promise.resolve([]);
318
} else if (this.detectors.size === 1) {
319
return this.detectors.values().next().value!.getTasks();
320
} else {
321
const promises: Promise<vscode.Task[]>[] = [];
322
for (const detector of this.detectors.values()) {
323
promises.push(detector.getTasks().then((value) => value, () => []));
324
}
325
return Promise.all(promises).then((values) => {
326
const result: vscode.Task[] = [];
327
for (const tasks of values) {
328
if (tasks && tasks.length > 0) {
329
result.push(...tasks);
330
}
331
}
332
return result;
333
});
334
}
335
}
336
337
public async getTask(task: vscode.Task): Promise<vscode.Task | undefined> {
338
if (this.detectors.size === 0) {
339
return undefined;
340
} else if (this.detectors.size === 1) {
341
return this.detectors.values().next().value!.getTask(task);
342
} else {
343
if ((task.scope === vscode.TaskScope.Workspace) || (task.scope === vscode.TaskScope.Global)) {
344
return undefined;
345
} else if (task.scope) {
346
const detector = this.detectors.get(task.scope.uri.toString());
347
if (detector) {
348
return detector.getTask(task);
349
}
350
}
351
return undefined;
352
}
353
}
354
}
355
356
let detector: TaskDetector;
357
export function activate(_context: vscode.ExtensionContext): void {
358
detector = new TaskDetector();
359
detector.start();
360
}
361
362
export function deactivate(): void {
363
detector.dispose();
364
}
365
366