Skip to content

Commit

Permalink
feat(CommandLineArgs): Support passing Args to Tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxCheetham committed Sep 2, 2023
1 parent c5ac2e9 commit 438172f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 15 deletions.
27 changes: 24 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,24 @@
"category": "Task",
"icon": "$(play)"
},
{
"command": "vscode-task.runTaskWithArgs",
"title": "Run Task With Args",
"category": "Task",
"icon": "$(run-all)"
},
{
"command": "vscode-task.runTaskPicker",
"title": "Run Task",
"category": "Task",
"icon": "$(play)"
},
{
"command": "vscode-task.runTaskPickerWithArgs",
"title": "Run Task With Args",
"category": "Task",
"icon": "$(play)"
},
{
"command": "vscode-task.runLastTask",
"title": "Run Last Task",
Expand Down Expand Up @@ -155,6 +167,10 @@
"command": "vscode-task.runTask",
"when": "false"
},
{
"command": "vscode-task.runTaskWithArgs",
"when": "false"
},
{
"command": "vscode-task.goToDefinition",
"when": "false"
Expand Down Expand Up @@ -183,15 +199,20 @@
}
],
"view/item/context": [
{
"command": "vscode-task.goToDefinition",
"when": "view == vscode-task.tasks && viewItem == taskTreeItem",
"group": "inline@1"
},
{
"command": "vscode-task.runTask",
"when": "view == vscode-task.tasks && viewItem == taskTreeItem",
"group": "inline"
"group": "inline@2"
},
{
"command": "vscode-task.goToDefinition",
"command": "vscode-task.runTaskWithArgs",
"when": "view == vscode-task.tasks && viewItem == taskTreeItem",
"group": "inline"
"group": "inline@3"
}
]
},
Expand Down
23 changes: 13 additions & 10 deletions src/services/taskfile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Endpoints } from "@octokit/types";
import * as cp from 'child_process';
import * as vscode from 'vscode';
import * as models from '../models';
import * as path from 'path';
import * as fs from 'fs';
import { Octokit } from 'octokit';
import * as path from 'path';
import * as semver from 'semver';
import * as vscode from 'vscode';
import * as models from '../models';
import { log, settings } from '../utils';
import { Octokit } from 'octokit';
import { Endpoints } from "@octokit/types";
import stripAnsi = require('strip-ansi');

const octokit = new Octokit();
Expand Down Expand Up @@ -48,11 +48,14 @@ class TaskfileService {
return this._instance ?? (this._instance = new this());
}

private command(command?: string): string {
private command(command?: string, cliArgs?: string): string {
if (command === undefined) {
return settings.path;
}
return `${settings.path} ${command}`;
if (cliArgs === undefined) {
return `${settings.path} ${command}`;
}
return `${settings.path} ${command} -- ${cliArgs}`;
}

public async checkInstallation(checkForUpdates?: boolean): Promise<string> {
Expand Down Expand Up @@ -225,17 +228,17 @@ class TaskfileService {
await this.runTask(this.lastTaskName, this.lastTaskDir);
}

public async runTask(taskName: string, dir?: string): Promise<void> {
public async runTask(taskName: string, dir?: string, cliArgs?: string): Promise<void> {
if (settings.outputTo === "terminal") {
log.info(`Running task: "${taskName}" in: "${dir}"`);
log.info(`Running task: "${taskName} ${cliArgs}" in: "${dir}"`);
var terminal: vscode.Terminal;
if (vscode.window.activeTerminal !== undefined) {
terminal = vscode.window.activeTerminal;
} else {
terminal = vscode.window.createTerminal("Task");
}
terminal.show();
terminal.sendText(this.command(taskName));
terminal.sendText(this.command(taskName, cliArgs));
} else {
return await new Promise((resolve) => {
log.info(`Running task: "${taskName}" in: "${dir}"`);
Expand Down
55 changes: 53 additions & 2 deletions src/task.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import * as elements from './elements';
import * as services from './services';
import * as models from './models';
import * as services from './services';
import { log, settings } from './utils';

export class TaskExtension {
Expand Down Expand Up @@ -35,7 +35,7 @@ export class TaskExtension {
});
return Promise.allSettled(p);

// If there are no valid taskfiles, set the status to "noTaskfile"
// If there are no valid taskfiles, set the status to "noTaskfile"
}).then(results => {
this._taskfiles = results
.filter(result => result.status === "fulfilled")
Expand Down Expand Up @@ -68,6 +68,8 @@ export class TaskExtension {
}

public registerCommands(context: vscode.ExtensionContext): void {
const RUNTASKWITHARGS_PROMPT = "Enter Command Line Arguments:";
const RUNTASKWITHARGS_PLACEHOLDER = "<arg1> <arg2> ...";

// Initialise Taskfile
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.init', () => {
Expand Down Expand Up @@ -116,6 +118,19 @@ export class TaskExtension {
}
}));

// Run task with args
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runTaskWithArgs', (treeItem?: elements.TaskTreeItem) => {
log.info("vscode-task.runTaskWithArgs");
if (treeItem?.task) {
vscode.window.showInputBox({
prompt: RUNTASKWITHARGS_PROMPT,
placeHolder: RUNTASKWITHARGS_PLACEHOLDER
}).then((cliArgsInput) => {
services.taskfile.runTask(treeItem.task.name, treeItem.workspace);
});
}
}));

// Run task picker
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runTaskPicker', () => {
log.info("Command: vscode-task.runTaskPicker");
Expand All @@ -128,17 +143,53 @@ export class TaskExtension {
});
}
});

if (items.length === 0) {
vscode.window.showInformationMessage('No tasks found');
return;
}

vscode.window.showQuickPick(items).then((item) => {
if (item && item instanceof elements.QuickPickTaskItem) {
services.taskfile.runTask(item.label, item.taskfile.workspace);
}
});
}));

// Run task picker with args
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runTaskPickerWithArgs', () => {
log.info("Command: vscode-task.runTaskPickerWithArgs");
let items: vscode.QuickPickItem[] = [];
this._taskfiles.forEach(taskfile => {
if (taskfile.tasks.length > 0) {
items = items.concat(new elements.QuickPickTaskSeparator(taskfile));
taskfile.tasks.forEach(task => {
items = items.concat(new elements.QuickPickTaskItem(taskfile, task));
});
}
});

if (items.length === 0) {
vscode.window.showInformationMessage('No tasks found');
return;
}

vscode.window.showQuickPick(items).then((item) => {
vscode.window.showInputBox({
prompt: RUNTASKWITHARGS_PROMPT,
placeHolder: RUNTASKWITHARGS_PLACEHOLDER
}).then((cliArgsInput) => {
if (cliArgsInput === undefined) {
vscode.window.showInformationMessage('No Args Supplied');
return;
}
if (item && item instanceof elements.QuickPickTaskItem) {
services.taskfile.runTask(item.label, item.taskfile.workspace, cliArgsInput);
}
});
});
}));

// Run last task
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runLastTask', () => {
log.info("Command: vscode-task.runLastTask");
Expand Down

0 comments on commit 438172f

Please sign in to comment.