Skip to content

Commit a457ba3

Browse files
authored
feat(CommandLineArgs): Support passing Args to Tasks (#89)
1 parent a5416bc commit a457ba3

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

package.json

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,24 @@
6767
"category": "Task",
6868
"icon": "$(play)"
6969
},
70+
{
71+
"command": "vscode-task.runTaskWithArgs",
72+
"title": "Run Task With Args",
73+
"category": "Task",
74+
"icon": "$(run-all)"
75+
},
7076
{
7177
"command": "vscode-task.runTaskPicker",
7278
"title": "Run Task",
7379
"category": "Task",
7480
"icon": "$(play)"
7581
},
82+
{
83+
"command": "vscode-task.runTaskPickerWithArgs",
84+
"title": "Run Task With Args",
85+
"category": "Task",
86+
"icon": "$(play)"
87+
},
7688
{
7789
"command": "vscode-task.runLastTask",
7890
"title": "Run Last Task",
@@ -155,6 +167,10 @@
155167
"command": "vscode-task.runTask",
156168
"when": "false"
157169
},
170+
{
171+
"command": "vscode-task.runTaskWithArgs",
172+
"when": "false"
173+
},
158174
{
159175
"command": "vscode-task.goToDefinition",
160176
"when": "false"
@@ -183,15 +199,20 @@
183199
}
184200
],
185201
"view/item/context": [
202+
{
203+
"command": "vscode-task.goToDefinition",
204+
"when": "view == vscode-task.tasks && viewItem == taskTreeItem",
205+
"group": "inline@1"
206+
},
186207
{
187208
"command": "vscode-task.runTask",
188209
"when": "view == vscode-task.tasks && viewItem == taskTreeItem",
189-
"group": "inline"
210+
"group": "inline@2"
190211
},
191212
{
192-
"command": "vscode-task.goToDefinition",
213+
"command": "vscode-task.runTaskWithArgs",
193214
"when": "view == vscode-task.tasks && viewItem == taskTreeItem",
194-
"group": "inline"
215+
"group": "inline@3"
195216
}
196217
]
197218
},

src/services/taskfile.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import { Endpoints } from "@octokit/types";
12
import * as cp from 'child_process';
2-
import * as vscode from 'vscode';
3-
import * as models from '../models';
4-
import * as path from 'path';
53
import * as fs from 'fs';
4+
import { Octokit } from 'octokit';
5+
import * as path from 'path';
66
import * as semver from 'semver';
7+
import * as vscode from 'vscode';
8+
import * as models from '../models';
79
import { log, settings } from '../utils';
8-
import { Octokit } from 'octokit';
9-
import { Endpoints } from "@octokit/types";
1010
import stripAnsi = require('strip-ansi');
1111

1212
const octokit = new Octokit();
@@ -48,11 +48,14 @@ class TaskfileService {
4848
return this._instance ?? (this._instance = new this());
4949
}
5050

51-
private command(command?: string): string {
51+
private command(command?: string, cliArgs?: string): string {
5252
if (command === undefined) {
5353
return settings.path;
5454
}
55-
return `${settings.path} ${command}`;
55+
if (cliArgs === undefined) {
56+
return `${settings.path} ${command}`;
57+
}
58+
return `${settings.path} ${command} -- ${cliArgs}`;
5659
}
5760

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

228-
public async runTask(taskName: string, dir?: string): Promise<void> {
231+
public async runTask(taskName: string, dir?: string, cliArgs?: string): Promise<void> {
229232
if (settings.outputTo === "terminal") {
230-
log.info(`Running task: "${taskName}" in: "${dir}"`);
233+
log.info(`Running task: "${taskName} ${cliArgs}" in: "${dir}"`);
231234
var terminal: vscode.Terminal;
232235
if (vscode.window.activeTerminal !== undefined) {
233236
terminal = vscode.window.activeTerminal;
234237
} else {
235238
terminal = vscode.window.createTerminal("Task");
236239
}
237240
terminal.show();
238-
terminal.sendText(this.command(taskName));
241+
terminal.sendText(this.command(taskName, cliArgs));
239242
} else {
240243
return await new Promise((resolve) => {
241244
log.info(`Running task: "${taskName}" in: "${dir}"`);

src/task.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export class TaskExtension {
7070
}
7171

7272
public registerCommands(context: vscode.ExtensionContext): void {
73+
const RUNTASKWITHARGS_PROMPT = "Enter Command Line Arguments:";
74+
const RUNTASKWITHARGS_PLACEHOLDER = "<arg1> <arg2> ...";
75+
7376
// Initialise Taskfile
7477
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.init', () => {
7578
log.info("Command: vscode-task.init");
@@ -117,6 +120,19 @@ export class TaskExtension {
117120
}
118121
}));
119122

123+
// Run task with args
124+
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runTaskWithArgs', (treeItem?: elements.TaskTreeItem) => {
125+
log.info("vscode-task.runTaskWithArgs");
126+
if (treeItem?.task) {
127+
vscode.window.showInputBox({
128+
prompt: RUNTASKWITHARGS_PROMPT,
129+
placeHolder: RUNTASKWITHARGS_PLACEHOLDER
130+
}).then((cliArgsInput) => {
131+
services.taskfile.runTask(treeItem.task.name, treeItem.workspace);
132+
});
133+
}
134+
}));
135+
120136
// Run task picker
121137
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runTaskPicker', () => {
122138
log.info("Command: vscode-task.runTaskPicker");
@@ -134,6 +150,32 @@ export class TaskExtension {
134150
});
135151
}));
136152

153+
// Run task picker with args
154+
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runTaskPickerWithArgs', () => {
155+
log.info("Command: vscode-task.runTaskPickerWithArgs");
156+
let items: vscode.QuickPickItem[] = this._loadTasksFromTaskfile();
157+
158+
if (items.length === 0) {
159+
vscode.window.showInformationMessage('No tasks found');
160+
return;
161+
}
162+
163+
vscode.window.showQuickPick(items).then((item) => {
164+
vscode.window.showInputBox({
165+
prompt: RUNTASKWITHARGS_PROMPT,
166+
placeHolder: RUNTASKWITHARGS_PLACEHOLDER
167+
}).then((cliArgsInput) => {
168+
if (cliArgsInput === undefined) {
169+
vscode.window.showInformationMessage('No Args Supplied');
170+
return;
171+
}
172+
if (item && item instanceof elements.QuickPickTaskItem) {
173+
services.taskfile.runTask(item.label, item.taskfile.workspace, cliArgsInput);
174+
}
175+
});
176+
});
177+
}));
178+
137179
// Run last task
138180
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runLastTask', () => {
139181
log.info("Command: vscode-task.runLastTask");

0 commit comments

Comments
 (0)