Skip to content

Commit a2b566f

Browse files
committed
Slighty better code organization
1 parent 9db94a0 commit a2b566f

7 files changed

+46
-49
lines changed

src/GradeTreeItem.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Command, TreeItem, TreeItemCollapsibleState } from 'vscode';
2-
import { join as pathJoin } from 'path';
32

43
export default class GradleTreeItem extends TreeItem {
54
constructor(

src/Gradle.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { window, workspace, Disposable, OutputChannel } from 'vscode';
22

33
import ProcessRegistry from './ProcessRegistry';
4-
import { GradleTask } from './TaskRegistry';
4+
import GradleTask from './GradleTask';
5+
6+
const TASK_REGEX: RegExp = /$\s*([a-z0-9]+)\s-\s(.*)$/gim;
57

68
function getCommand(): string {
79
return workspace.getConfiguration().get('gradle.useCommand', 'gradlew');
@@ -11,14 +13,31 @@ function getTasksArgs(): string {
1113
return workspace.getConfiguration().get('gradle.tasks.args', '');
1214
}
1315

16+
function getTasks(): Thenable<GradleTask[]> {
17+
const cmd = `${getCommand()} --console plain tasks ${getTasksArgs()}`;
18+
const { rootPath: cwd } = workspace;
19+
return ProcessRegistry.create(cmd, { cwd }).then(stdout => {
20+
let match: RegExpExecArray | null = null;
21+
const tasks: GradleTask[] = [];
22+
while ((match = TASK_REGEX.exec(stdout)) !== null) {
23+
tasks.push(new GradleTask(match[1], match[2]));
24+
}
25+
return tasks.sort((a, b) => a.label.localeCompare(b.label));
26+
});
27+
}
28+
1429
function runTask(
1530
task: GradleTask,
16-
outputChannel?: OutputChannel
31+
outputChannel: OutputChannel
1732
): Thenable<void> {
1833
const cmd = `${getCommand()} ${task.label}`;
19-
const statusbar: Disposable = window.setStatusBarMessage(`Running ${cmd}`);
34+
const feedback = `Running ${cmd}`;
35+
const statusbar: Disposable = window.setStatusBarMessage(feedback);
2036
const { rootPath: cwd } = workspace;
2137

38+
outputChannel.show();
39+
outputChannel.append(`${feedback}\n`);
40+
2241
return ProcessRegistry.create(cmd, { cwd }, outputChannel).then(
2342
() => statusbar.dispose(),
2443
err => {
@@ -28,4 +47,4 @@ function runTask(
2847
);
2948
}
3049

31-
export default { getCommand, getTasksArgs, runTask };
50+
export default { getTasks, runTask };

src/GradleNodeProvider.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {
66
TreeItemCollapsibleState
77
} from 'vscode';
88

9-
import { GradleTask } from './TaskRegistry';
10-
119
import TaskRegistry from './TaskRegistry';
1210
import GradleTreeItem from './GradeTreeItem';
1311
import WorkspaceTreeItem from './WorkspaceTreeItem';
@@ -22,7 +20,7 @@ export default class TreeProvider
2220

2321
constructor() {}
2422

25-
refresh(): void {
23+
refresh() {
2624
this._onDidChangeTreeData.fire();
2725
}
2826

src/GradleTask.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { QuickPickItem } from 'vscode';
2+
3+
export default class GradleTask implements QuickPickItem {
4+
constructor(
5+
public readonly label: string,
6+
public readonly description?: string
7+
) {
8+
this.label = label;
9+
this.description = description;
10+
}
11+
}

src/ProcessRegistry.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { ChildProcess, ExecOptions, exec } from 'child_process';
33

44
const processes: Set<ChildProcess> = new Set();
55

6-
function add(process: ChildProcess): void {
6+
function add(process: ChildProcess) {
77
processes.add(process);
88
}
99

10-
function remove(process: ChildProcess): void {
10+
function remove(process: ChildProcess) {
1111
processes.delete(process);
1212
}
1313

14-
function killAll(): void {
14+
function killAll() {
1515
processes.forEach((process: ChildProcess, _: ChildProcess) => {
1616
process.kill('SIGINT');
1717
});
@@ -25,7 +25,6 @@ function writeOutput(
2525
{ stdout, stderr }: ChildProcess,
2626
outputChannel: OutputChannel
2727
) {
28-
outputChannel.show();
2928
if (stdout) {
3029
stdout.on('data', data => outputChannel.append(data.toString()));
3130
}

src/TaskRegistry.ts

+6-36
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,32 @@
1-
import {
2-
window,
3-
workspace,
4-
Disposable,
5-
OutputChannel,
6-
QuickPickItem
7-
} from 'vscode';
1+
import { window, Disposable, QuickPickItem } from 'vscode';
82

93
import Gradle from './Gradle';
10-
import ProcessRegistry from './ProcessRegistry';
4+
import GradleTask from './GradleTask';
115

12-
export class GradleTask implements QuickPickItem {
13-
label: string;
14-
description: string | undefined;
15-
16-
constructor(label: string, description?: string) {
17-
this.label = label;
18-
this.description = description;
19-
}
20-
}
21-
22-
const TASK_REGEX: RegExp = /$\s*([a-z0-9]+)\s-\s(.*)$/gim;
236
const tasks: Set<GradleTask> = new Set();
247
const changeHandlers: Array<() => void> = [];
258

26-
function add(task: GradleTask): void {
9+
function add(task: GradleTask) {
2710
tasks.add(task);
2811
}
2912

30-
function addAll(tasks: GradleTask[]): void {
13+
function addAll(tasks: GradleTask[]) {
3114
tasks.forEach(add);
3215
}
3316

34-
function clear(): void {
17+
function clear() {
3518
tasks.clear();
3619
}
3720

3821
function getTasks(): GradleTask[] {
3922
return Array.from(tasks);
4023
}
4124

42-
function getTasksFromGradle(): Thenable<GradleTask[]> {
43-
const cmd = `${Gradle.getCommand()} --console plain tasks ${Gradle.getTasksArgs()}`;
44-
const { rootPath: cwd } = workspace;
45-
return ProcessRegistry.create(cmd, { cwd }).then(stdout => {
46-
let match: RegExpExecArray | null = null;
47-
const tasks: GradleTask[] = [];
48-
while ((match = TASK_REGEX.exec(stdout)) !== null) {
49-
tasks.push(new GradleTask(match[1], match[2]));
50-
}
51-
return tasks.sort((a, b) => a.label.localeCompare(b.label));
52-
});
53-
}
54-
5525
function refresh(): Thenable<void> {
5626
const statusbar: Disposable = window.setStatusBarMessage(
5727
'Refreshing gradle tasks'
5828
);
59-
return getTasksFromGradle().then(
29+
return Gradle.getTasks().then(
6030
gradleTasks => {
6131
clear();
6232
addAll(gradleTasks);

src/extension.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
} from 'vscode';
88

99
import ProcessRegistry from './ProcessRegistry';
10-
import TaskRegistry, { GradleTask } from './TaskRegistry';
10+
import TaskRegistry from './TaskRegistry';
11+
import GradleTask from './GradleTask';
1112
import Gradle from './Gradle';
1213
import GradleTreeProvider from './GradleNodeProvider';
1314

0 commit comments

Comments
 (0)