mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 04:49:35 +00:00
Make grunt auto detect multi folder aware
This commit is contained in:
parent
575efe5a5d
commit
48b2d7a964
|
@ -31,6 +31,7 @@
|
||||||
"title": "Grunt",
|
"title": "Grunt",
|
||||||
"properties": {
|
"properties": {
|
||||||
"grunt.autoDetect": {
|
"grunt.autoDetect": {
|
||||||
|
"scope": "resource",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": [
|
"enum": [
|
||||||
"off",
|
"off",
|
||||||
|
|
|
@ -13,49 +13,6 @@ import * as nls from 'vscode-nls';
|
||||||
const localize = nls.config(process.env.VSCODE_NLS_CONFIG)();
|
const localize = nls.config(process.env.VSCODE_NLS_CONFIG)();
|
||||||
|
|
||||||
type AutoDetect = 'on' | 'off';
|
type AutoDetect = 'on' | 'off';
|
||||||
let taskProvider: vscode.Disposable | undefined;
|
|
||||||
|
|
||||||
export function activate(_context: vscode.ExtensionContext): void {
|
|
||||||
let workspaceRoot = vscode.workspace.rootPath;
|
|
||||||
if (!workspaceRoot) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let pattern = path.join(workspaceRoot, '[Gg]runtfile.js');
|
|
||||||
let detectorPromise: Thenable<vscode.Task[]> | undefined = undefined;
|
|
||||||
let fileWatcher = vscode.workspace.createFileSystemWatcher(pattern);
|
|
||||||
fileWatcher.onDidChange(() => detectorPromise = undefined);
|
|
||||||
fileWatcher.onDidCreate(() => detectorPromise = undefined);
|
|
||||||
fileWatcher.onDidDelete(() => detectorPromise = undefined);
|
|
||||||
|
|
||||||
function onConfigurationChanged() {
|
|
||||||
let autoDetect = vscode.workspace.getConfiguration('grunt').get<AutoDetect>('autoDetect');
|
|
||||||
if (taskProvider && autoDetect === 'off') {
|
|
||||||
detectorPromise = undefined;
|
|
||||||
taskProvider.dispose();
|
|
||||||
taskProvider = undefined;
|
|
||||||
} else if (!taskProvider && autoDetect === 'on') {
|
|
||||||
taskProvider = vscode.workspace.registerTaskProvider('grunt', {
|
|
||||||
provideTasks: () => {
|
|
||||||
if (!detectorPromise) {
|
|
||||||
detectorPromise = getGruntTasks();
|
|
||||||
}
|
|
||||||
return detectorPromise;
|
|
||||||
},
|
|
||||||
resolveTask(_task: vscode.Task): vscode.Task | undefined {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
vscode.workspace.onDidChangeConfiguration(onConfigurationChanged);
|
|
||||||
onConfigurationChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
export function deactivate(): void {
|
|
||||||
if (taskProvider) {
|
|
||||||
taskProvider.dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function exists(file: string): Promise<boolean> {
|
function exists(file: string): Promise<boolean> {
|
||||||
return new Promise<boolean>((resolve, _reject) => {
|
return new Promise<boolean>((resolve, _reject) => {
|
||||||
|
@ -76,19 +33,6 @@ function exec(command: string, options: cp.ExecOptions): Promise<{ stdout: strin
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let _channel: vscode.OutputChannel;
|
|
||||||
function getOutputChannel(): vscode.OutputChannel {
|
|
||||||
if (!_channel) {
|
|
||||||
_channel = vscode.window.createOutputChannel('Grunt Auto Detection');
|
|
||||||
}
|
|
||||||
return _channel;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface GruntTaskDefinition extends vscode.TaskDefinition {
|
|
||||||
task: string;
|
|
||||||
file?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const buildNames: string[] = ['build', 'compile', 'watch'];
|
const buildNames: string[] = ['build', 'compile', 'watch'];
|
||||||
function isBuildTask(name: string): boolean {
|
function isBuildTask(name: string): boolean {
|
||||||
for (let buildName of buildNames) {
|
for (let buildName of buildNames) {
|
||||||
|
@ -109,97 +53,287 @@ function isTestTask(name: string): boolean {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getGruntTasks(): Promise<vscode.Task[]> {
|
let _channel: vscode.OutputChannel;
|
||||||
let workspaceRoot = vscode.workspace.rootPath;
|
function getOutputChannel(): vscode.OutputChannel {
|
||||||
let emptyTasks: vscode.Task[] = [];
|
if (!_channel) {
|
||||||
if (!workspaceRoot) {
|
_channel = vscode.window.createOutputChannel('Gulp Auto Detection');
|
||||||
return emptyTasks;
|
|
||||||
}
|
}
|
||||||
if (!await exists(path.join(workspaceRoot, 'gruntfile.js')) && !await exists(path.join(workspaceRoot, 'Gruntfile.js'))) {
|
return _channel;
|
||||||
return emptyTasks;
|
}
|
||||||
|
|
||||||
|
interface GruntTaskDefinition extends vscode.TaskDefinition {
|
||||||
|
task: string;
|
||||||
|
file?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
class FolderDetector {
|
||||||
|
|
||||||
|
private fileWatcher: vscode.FileSystemWatcher;
|
||||||
|
private promise: Thenable<vscode.Task[]> | undefined;
|
||||||
|
|
||||||
|
constructor(private _workspaceFolder: vscode.WorkspaceFolder) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let command: string;
|
public get workspaceFolder(): vscode.WorkspaceFolder {
|
||||||
let platform = process.platform;
|
return this._workspaceFolder;
|
||||||
if (platform === 'win32' && await exists(path.join(workspaceRoot!, 'node_modules', '.bin', 'grunt.cmd'))) {
|
|
||||||
command = path.join('.', 'node_modules', '.bin', 'grunt.cmd');
|
|
||||||
} else if ((platform === 'linux' || platform === 'darwin') && await exists(path.join(workspaceRoot!, 'node_modules', '.bin', 'grunt'))) {
|
|
||||||
command = path.join('.', 'node_modules', '.bin', 'grunt');
|
|
||||||
} else {
|
|
||||||
command = 'grunt';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let commandLine = `${command} --help --no-color`;
|
public isEnabled(): boolean {
|
||||||
try {
|
return vscode.workspace.getConfiguration('grunt', this._workspaceFolder.uri).get<AutoDetect>('autoDetect') === 'on';
|
||||||
let { stdout, stderr } = await exec(commandLine, { cwd: workspaceRoot });
|
}
|
||||||
if (stderr) {
|
|
||||||
getOutputChannel().appendLine(stderr);
|
public start(): void {
|
||||||
getOutputChannel().show(true);
|
let pattern = path.join(this._workspaceFolder.uri.fsPath, '[Gg]runtfile.js');
|
||||||
|
this.fileWatcher = vscode.workspace.createFileSystemWatcher(pattern);
|
||||||
|
this.fileWatcher.onDidChange(() => this.promise = undefined);
|
||||||
|
this.fileWatcher.onDidCreate(() => this.promise = undefined);
|
||||||
|
this.fileWatcher.onDidDelete(() => this.promise = undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getTasks(): Promise<vscode.Task[]> {
|
||||||
|
if (!this.promise) {
|
||||||
|
this.promise = this.computeTasks();
|
||||||
}
|
}
|
||||||
let result: vscode.Task[] = [];
|
return this.promise;
|
||||||
if (stdout) {
|
}
|
||||||
// grunt lists tasks as follows (description is wrapped into a new line if too long):
|
|
||||||
// ...
|
|
||||||
// Available tasks
|
|
||||||
// uglify Minify files with UglifyJS. *
|
|
||||||
// jshint Validate files with JSHint. *
|
|
||||||
// test Alias for "jshint", "qunit" tasks.
|
|
||||||
// default Alias for "jshint", "qunit", "concat", "uglify" tasks.
|
|
||||||
// long Alias for "eslint", "qunit", "browserify", "sass",
|
|
||||||
// "autoprefixer", "uglify", tasks.
|
|
||||||
//
|
|
||||||
// Tasks run in the order specified
|
|
||||||
|
|
||||||
let lines = stdout.split(/\r{0,1}\n/);
|
private async computeTasks(): Promise<vscode.Task[]> {
|
||||||
let tasksStart = false;
|
let rootPath = this._workspaceFolder.uri.scheme === 'file' ? this._workspaceFolder.uri.fsPath : undefined;
|
||||||
let tasksEnd = false;
|
let emptyTasks: vscode.Task[] = [];
|
||||||
for (let line of lines) {
|
if (!rootPath) {
|
||||||
if (line.length === 0) {
|
return emptyTasks;
|
||||||
continue;
|
}
|
||||||
}
|
if (!await exists(path.join(rootPath, 'gruntfile.js')) && !await exists(path.join(rootPath, 'Gruntfile.js'))) {
|
||||||
if (!tasksStart && !tasksEnd) {
|
return emptyTasks;
|
||||||
if (line.indexOf('Available tasks') === 0) {
|
}
|
||||||
tasksStart = true;
|
|
||||||
|
let command: string;
|
||||||
|
let platform = process.platform;
|
||||||
|
if (platform === 'win32' && await exists(path.join(rootPath!, 'node_modules', '.bin', 'grunt.cmd'))) {
|
||||||
|
command = path.join('.', 'node_modules', '.bin', 'grunt.cmd');
|
||||||
|
} else if ((platform === 'linux' || platform === 'darwin') && await exists(path.join(rootPath!, 'node_modules', '.bin', 'grunt'))) {
|
||||||
|
command = path.join('.', 'node_modules', '.bin', 'grunt');
|
||||||
|
} else {
|
||||||
|
command = 'grunt';
|
||||||
|
}
|
||||||
|
|
||||||
|
let commandLine = `${command} --help --no-color`;
|
||||||
|
try {
|
||||||
|
let { stdout, stderr } = await exec(commandLine, { cwd: rootPath });
|
||||||
|
if (stderr) {
|
||||||
|
getOutputChannel().appendLine(stderr);
|
||||||
|
getOutputChannel().show(true);
|
||||||
|
}
|
||||||
|
let result: vscode.Task[] = [];
|
||||||
|
if (stdout) {
|
||||||
|
// grunt lists tasks as follows (description is wrapped into a new line if too long):
|
||||||
|
// ...
|
||||||
|
// Available tasks
|
||||||
|
// uglify Minify files with UglifyJS. *
|
||||||
|
// jshint Validate files with JSHint. *
|
||||||
|
// test Alias for "jshint", "qunit" tasks.
|
||||||
|
// default Alias for "jshint", "qunit", "concat", "uglify" tasks.
|
||||||
|
// long Alias for "eslint", "qunit", "browserify", "sass",
|
||||||
|
// "autoprefixer", "uglify", tasks.
|
||||||
|
//
|
||||||
|
// Tasks run in the order specified
|
||||||
|
|
||||||
|
let lines = stdout.split(/\r{0,1}\n/);
|
||||||
|
let tasksStart = false;
|
||||||
|
let tasksEnd = false;
|
||||||
|
for (let line of lines) {
|
||||||
|
if (line.length === 0) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
} else if (tasksStart && !tasksEnd) {
|
if (!tasksStart && !tasksEnd) {
|
||||||
if (line.indexOf('Tasks run in the order specified') === 0) {
|
if (line.indexOf('Available tasks') === 0) {
|
||||||
tasksEnd = true;
|
tasksStart = true;
|
||||||
} else {
|
}
|
||||||
let regExp = /^\s*(\S.*\S) \S/g;
|
} else if (tasksStart && !tasksEnd) {
|
||||||
let matches = regExp.exec(line);
|
if (line.indexOf('Tasks run in the order specified') === 0) {
|
||||||
if (matches && matches.length === 2) {
|
tasksEnd = true;
|
||||||
let name = matches[1];
|
} else {
|
||||||
let kind: GruntTaskDefinition = {
|
let regExp = /^\s*(\S.*\S) \S/g;
|
||||||
type: 'grunt',
|
let matches = regExp.exec(line);
|
||||||
task: name
|
if (matches && matches.length === 2) {
|
||||||
};
|
let name = matches[1];
|
||||||
let source = 'grunt';
|
let kind: GruntTaskDefinition = {
|
||||||
let task = name.indexOf(' ') === -1
|
type: 'grunt',
|
||||||
? new vscode.Task(kind, name, source, new vscode.ShellExecution(`${command} ${name}`))
|
task: name
|
||||||
: new vscode.Task(kind, name, source, new vscode.ShellExecution(`${command} "${name}"`));
|
};
|
||||||
result.push(task);
|
let source = 'grunt';
|
||||||
let lowerCaseTaskName = name.toLowerCase();
|
let options: vscode.ShellExecutionOptions = { cwd: this.workspaceFolder.uri.fsPath };
|
||||||
if (isBuildTask(lowerCaseTaskName)) {
|
let task = name.indexOf(' ') === -1
|
||||||
task.group = vscode.TaskGroup.Build;
|
? new vscode.Task(kind, this.workspaceFolder, name, source, new vscode.ShellExecution(`${command} ${name}`, options))
|
||||||
} else if (isTestTask(lowerCaseTaskName)) {
|
: new vscode.Task(kind, this.workspaceFolder, name, source, new vscode.ShellExecution(`${command} "${name}"`, options));
|
||||||
task.group = vscode.TaskGroup.Test;
|
result.push(task);
|
||||||
|
let lowerCaseTaskName = name.toLowerCase();
|
||||||
|
if (isBuildTask(lowerCaseTaskName)) {
|
||||||
|
task.group = vscode.TaskGroup.Build;
|
||||||
|
} else if (isTestTask(lowerCaseTaskName)) {
|
||||||
|
task.group = vscode.TaskGroup.Test;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
|
} catch (err) {
|
||||||
|
let channel = getOutputChannel();
|
||||||
|
if (err.stderr) {
|
||||||
|
channel.appendLine(err.stderr);
|
||||||
|
}
|
||||||
|
if (err.stdout) {
|
||||||
|
channel.appendLine(err.stdout);
|
||||||
|
}
|
||||||
|
channel.appendLine(localize('execFailed', 'Auto detecting Grunt failed with error: {0}', err.error ? err.error.toString() : 'unknown'));
|
||||||
|
channel.show(true);
|
||||||
|
return emptyTasks;
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
} catch (err) {
|
|
||||||
let channel = getOutputChannel();
|
|
||||||
if (err.stderr) {
|
|
||||||
channel.appendLine(err.stderr);
|
|
||||||
}
|
|
||||||
if (err.stdout) {
|
|
||||||
channel.appendLine(err.stdout);
|
|
||||||
}
|
|
||||||
channel.appendLine(localize('execFailed', 'Auto detecting Grunt failed with error: {0}', err.error ? err.error.toString() : 'unknown'));
|
|
||||||
channel.show(true);
|
|
||||||
return emptyTasks;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public dispose() {
|
||||||
|
this.promise = undefined;
|
||||||
|
if (this.fileWatcher) {
|
||||||
|
this.fileWatcher.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TaskDetector {
|
||||||
|
|
||||||
|
private taskProvider: vscode.Disposable | undefined;
|
||||||
|
private detectors: Map<string, FolderDetector> = new Map();
|
||||||
|
private promise: Promise<vscode.Task[]> | undefined;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public start(): void {
|
||||||
|
let folders = vscode.workspace.workspaceFolders;
|
||||||
|
if (folders) {
|
||||||
|
this.updateWorkspaceFolders(folders, []);
|
||||||
|
}
|
||||||
|
vscode.workspace.onDidChangeWorkspaceFolders((event) => this.updateWorkspaceFolders(event.added, event.removed));
|
||||||
|
vscode.workspace.onDidChangeConfiguration(this.updateConfiguration, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public dispose(): void {
|
||||||
|
if (this.taskProvider) {
|
||||||
|
this.taskProvider.dispose();
|
||||||
|
this.taskProvider = undefined;
|
||||||
|
}
|
||||||
|
this.detectors.clear();
|
||||||
|
this.promise = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
private updateWorkspaceFolders(added: vscode.WorkspaceFolder[], removed: vscode.WorkspaceFolder[]): void {
|
||||||
|
let changed = false;
|
||||||
|
for (let remove of removed) {
|
||||||
|
let detector = this.detectors.get(remove.uri.toString());
|
||||||
|
if (detector) {
|
||||||
|
changed = true;
|
||||||
|
detector.dispose();
|
||||||
|
this.detectors.delete(remove.uri.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let add of added) {
|
||||||
|
let detector = new FolderDetector(add);
|
||||||
|
if (detector.isEnabled()) {
|
||||||
|
changed = true;
|
||||||
|
this.detectors.set(add.uri.toString(), detector);
|
||||||
|
detector.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (changed) {
|
||||||
|
this.promise = undefined;
|
||||||
|
}
|
||||||
|
this.updateProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
private updateConfiguration(): void {
|
||||||
|
let changed = false;
|
||||||
|
for (let detector of this.detectors.values()) {
|
||||||
|
if (!detector.isEnabled()) {
|
||||||
|
changed = true;
|
||||||
|
detector.dispose();
|
||||||
|
this.detectors.delete(detector.workspaceFolder.uri.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let folders = vscode.workspace.workspaceFolders;
|
||||||
|
if (folders) {
|
||||||
|
for (let folder of folders) {
|
||||||
|
if (!this.detectors.has(folder.uri.toString())) {
|
||||||
|
let detector = new FolderDetector(folder);
|
||||||
|
if (detector.isEnabled()) {
|
||||||
|
changed = true;
|
||||||
|
this.detectors.set(folder.uri.toString(), detector);
|
||||||
|
detector.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (changed) {
|
||||||
|
this.promise = undefined;
|
||||||
|
}
|
||||||
|
this.updateProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
private updateProvider(): void {
|
||||||
|
if (!this.taskProvider && this.detectors.size > 0) {
|
||||||
|
this.taskProvider = vscode.workspace.registerTaskProvider('gulp', {
|
||||||
|
provideTasks: () => {
|
||||||
|
return this.getTasks();
|
||||||
|
},
|
||||||
|
resolveTask(_task: vscode.Task): vscode.Task | undefined {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (this.taskProvider && this.detectors.size === 0) {
|
||||||
|
this.taskProvider.dispose();
|
||||||
|
this.taskProvider = undefined;
|
||||||
|
this.promise = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getTasks(): Promise<vscode.Task[]> {
|
||||||
|
if (!this.promise) {
|
||||||
|
this.promise = this.computeTasks();
|
||||||
|
}
|
||||||
|
return this.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
private computeTasks(): Promise<vscode.Task[]> {
|
||||||
|
if (this.detectors.size === 0) {
|
||||||
|
return Promise.resolve([]);
|
||||||
|
} else if (this.detectors.size === 1) {
|
||||||
|
return this.detectors.values().next().value.getTasks();
|
||||||
|
} else {
|
||||||
|
let promises: Promise<vscode.Task[]>[] = [];
|
||||||
|
for (let detector of this.detectors.values()) {
|
||||||
|
promises.push(detector.getTasks().then((value) => value, () => []));
|
||||||
|
}
|
||||||
|
return Promise.all(promises).then((values) => {
|
||||||
|
let result: vscode.Task[] = [];
|
||||||
|
for (let tasks of values) {
|
||||||
|
if (tasks && tasks.length > 0) {
|
||||||
|
result.push(...tasks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let detector: TaskDetector;
|
||||||
|
export function activate(_context: vscode.ExtensionContext): void {
|
||||||
|
detector = new TaskDetector();
|
||||||
|
detector.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function deactivate(): void {
|
||||||
|
detector.dispose();
|
||||||
}
|
}
|
|
@ -139,7 +139,8 @@ class FolderDetector {
|
||||||
type: 'gulp',
|
type: 'gulp',
|
||||||
task: line
|
task: line
|
||||||
};
|
};
|
||||||
let task = new vscode.Task(kind, this.workspaceFolder, line, 'gulp', new vscode.ShellExecution(`${gulpCommand} ${line}`));
|
let options: vscode.ShellExecutionOptions = { cwd: this.workspaceFolder.uri.fsPath };
|
||||||
|
let task = new vscode.Task(kind, this.workspaceFolder, line, 'gulp', new vscode.ShellExecution(`${gulpCommand} ${line}`, options));
|
||||||
result.push(task);
|
result.push(task);
|
||||||
let lowerCaseLine = line.toLowerCase();
|
let lowerCaseLine = line.toLowerCase();
|
||||||
if (isBuildTask(lowerCaseLine)) {
|
if (isBuildTask(lowerCaseLine)) {
|
||||||
|
|
Loading…
Reference in a new issue