Skip to content

Commit f79a5a1

Browse files
committed
Feature: Add pickCommand command
filePicker.pickCommand will prompt the user for a file (as in filePicker.pick) and then run a shell command. The file resulting from the file prompt can be substituted into the command by inserting "${file}" in the command string, similar to other variable substitutions.
1 parent ee4a2b8 commit f79a5a1

File tree

5 files changed

+106
-5
lines changed

5 files changed

+106
-5
lines changed

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
File picker is simple tool, helping you to select file while running vs code task.
44

5-
## Usage
5+
# filePicker.pick
66
Example 'tasks.json' file to build project in monorepo with lerna
77
```json
88
{
@@ -64,3 +64,48 @@ Example 'tasks.json' file to build project in monorepo with lerna
6464
* `json` `<string>` Path to property of json file
6565
* `description` `<PresentationConfig>` Rule to get file description (to show in vs code picker)
6666
* `detail` `<PresentationConfig>` Rule to get file details (to show in vs code picker)
67+
68+
69+
# filePicker.pickCommand
70+
This command operates similarly to [`filePicker.pick`](#filePicker.pick) except the resulting file name or property can be used in a shell command for further processing. For example, the user can select a file to test and use a shell command to generate appropriate debugging args for the test.
71+
72+
## Usage
73+
Example `launch.json` which uses a shell script to generate args for a user-specified test.
74+
```json
75+
{
76+
"version": "0.2.0",
77+
"configurations": [
78+
{
79+
"name": "Debug File",
80+
"request": "launch",
81+
"program": "process_to_debug",
82+
"args": ["${input:getDebugArgs}"],
83+
},
84+
],
85+
"inputs": [
86+
{
87+
"id": "getDebugArgs",
88+
"type": "command",
89+
"command": "filePicker.pickCommand",
90+
"args": {
91+
"masks": "**/*.test",
92+
"display": "filePath",
93+
"output": "filePath",
94+
"command": {
95+
"command": "generate_args.sh ${file}",
96+
"cwd": ".",
97+
}
98+
}
99+
}
100+
]
101+
}
102+
103+
```
104+
105+
## Arguments
106+
Same as `filePicker.pick` except with the addition of a `command` section:
107+
* `command` `<CommandConfig>`
108+
109+
`CommandConfig`:
110+
* `command` `string` shell command to be run. No variable substitution is currently supported except for `"${file}"` being replaced with the results of the file pick.
111+
* `cwd` `string` Directory to execute the command in.

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "file-picker",
33
"displayName": "file-picker",
44
"description": "Simple tool select file from your project by name or json content",
5-
"version": "0.1.3",
5+
"version": "0.2.0",
66
"publisher": "dfarley1",
77
"repository": {
88
"type": "git",
@@ -20,7 +20,8 @@
2020
"inputs"
2121
],
2222
"activationEvents": [
23-
"onCommand:filePicker.pick"
23+
"onCommand:filePicker.pick",
24+
"onCommand:filePicker.pickCommand"
2425
],
2526
"main": "./out/extension.js",
2627
"icon": "images/icon.png",
@@ -30,6 +31,11 @@
3031
"command": "filePicker.pick",
3132
"title": "Pick",
3233
"category": "FilePicker"
34+
},
35+
{
36+
"command": "filePicker.pickCommand",
37+
"title": "Pick",
38+
"category": "FilePicker"
3339
}
3440
]
3541
},
@@ -49,4 +55,4 @@
4955
"@types/glob": "^7.1.1",
5056
"glob": "^7.1.3"
5157
}
52-
}
58+
}

src/Args.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ export type DisplayConfig = DisplayType | PresentationConfig & {
2323
detail: PresentationConfig;
2424
};
2525

26+
export type CommandConfig = {
27+
cwd: string | undefined;
28+
command: string;
29+
};
30+
2631
export interface Args {
2732
masks: string | string[];
2833
display: DisplayConfig;
2934
output: PresentationConfig;
35+
command: CommandConfig;
3036
}

src/RunCommand.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as vscode from 'vscode';
2+
import * as subprocess from 'child_process';
3+
import { Args, CommandConfig } from './Args';
4+
5+
6+
export class RunCommand {
7+
private command: string;
8+
private cwd: string | undefined;
9+
10+
constructor(args: CommandConfig, file: string) {
11+
this.cwd = args.cwd;
12+
13+
this.command = this.resolveFile(args.command, file);
14+
}
15+
16+
private resolveFile(command: string, file: string): string {
17+
const regex: RegExp = /\$\{(file)\}/gm;
18+
return command.replace(regex, file);
19+
}
20+
21+
run(): string {
22+
const options: subprocess.ExecSyncOptionsWithStringEncoding = {
23+
encoding: 'utf8',
24+
cwd: this.cwd,
25+
};
26+
try {
27+
return subprocess.execSync(this.command!, options);
28+
} catch (error) {
29+
console.error(error);
30+
return error;
31+
}
32+
}
33+
}

src/extension.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import * as vscode from 'vscode';
22
import { Args } from './Args';
33
import { PickCommand } from './PickCommand';
4+
import { RunCommand } from './RunCommand';
45

5-
function pick(args: Args) {
6+
async function pick(args: Args): Promise<string | undefined> {
67
return new PickCommand(args).Invoke();
78
}
89

10+
async function pickCommand(args: Args): Promise<string | undefined> {
11+
const picker = new PickCommand(args);
12+
const result = picker.Invoke().then(file => {
13+
const run_result = new RunCommand(args.command, file).run();
14+
return run_result;
15+
});
16+
return result;
17+
}
18+
919
export function activate(context: vscode.ExtensionContext) {
1020
context.subscriptions.push(
1121
vscode.commands.registerCommand('filePicker.pick', pick),
22+
vscode.commands.registerCommand('filePicker.pickCommand', pickCommand),
1223
);
1324
}
1425

0 commit comments

Comments
 (0)