Skip to content

Commit da09646

Browse files
committed
chore: code layout
1 parent 7201aff commit da09646

File tree

8 files changed

+113
-64
lines changed

8 files changed

+113
-64
lines changed

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './runTask';

src/commands/runTask.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as vscode from 'vscode';
2+
import * as services from '../services';
3+
import * as models from '../models';
4+
5+
// RunTask will show a quick pick with all the tasks and run the selected one
6+
export function runTask(taskfile?: models.Taskfile) {
7+
if (!taskfile || taskfile.tasks.length === 0) {
8+
vscode.window.showInformationMessage('No tasks found');
9+
return;
10+
}
11+
vscode.window.showQuickPick(taskfile.tasks.map(t => t.name)).then((taskName) => {
12+
if (taskName) {
13+
services.taskfile.runTask(taskName);
14+
}
15+
});
16+
}

src/extension.ts

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,18 @@
1-
// The module 'vscode' contains the VS Code extensibility API
2-
// Import the module and reference it with the alias vscode in your code below
31
import * as vscode from 'vscode';
4-
import * as cp from 'child_process';
2+
import { TaskExtension } from './task';
53

6-
var outputChannel: vscode.OutputChannel;
7-
var taskList: TaskList;
8-
9-
// This method is called when your extension is activated
10-
// Your extension is activated the very first time the command is executed
114
export function activate(context: vscode.ExtensionContext) {
125

13-
// Create an output channel for the task output
14-
outputChannel = vscode.window.createOutputChannel('task');
15-
16-
// Register commands
17-
let disposable = vscode.commands.registerCommand('vscode-task.runTask', runTask);
18-
19-
// Initialise the task list
20-
refreshTaskList();
21-
22-
context.subscriptions.push(disposable);
23-
}
24-
25-
// This method is called when your extension is deactivated
26-
export function deactivate() { }
27-
28-
function runTask() {
29-
refreshTaskList();
6+
// Create a new instance of Tagger
7+
let taskExtension: TaskExtension = new TaskExtension();
308

31-
// Show a quick pick with all the tasks
32-
vscode.window.showQuickPick(taskList.tasks.map(t => t.name)).then((taskName) => {
9+
// Registration
10+
taskExtension.registerCommands(context);
3311

34-
// If the user selected a task, run it
35-
if (taskName) {
36-
cp.exec(`task ${taskName}`, (err: cp.ExecException | null, stdout: string, stderr: string) => {
37-
if (err) {
38-
console.log('error: ' + err);
39-
return;
40-
}
41-
outputChannel.append(stderr);
42-
outputChannel.append(stdout);
43-
outputChannel.show();
44-
});
45-
}
12+
// Refresh the tasks list
13+
taskExtension.update().catch((err: string) => {
14+
console.error(err);
4615
});
4716
}
4817

49-
async function refreshTaskList() {
50-
return await new Promise((resolve, reject) => {
51-
// Get a list of all tasks
52-
cp.exec('task --list-all --json', (err: cp.ExecException | null, stdout: string, stderr: string) => {
53-
if (err) {
54-
console.log('error: ' + err);
55-
return;
56-
}
57-
// Parse the JSON output
58-
taskList = JSON.parse(stdout);
59-
resolve(stdout);
60-
});
61-
});
62-
}
63-
64-
interface Task {
65-
name: string;
66-
desc: string;
67-
summary: string;
68-
up_to_date: boolean;
69-
}
70-
71-
interface TaskList {
72-
tasks: Task[];
73-
}
18+
export function deactivate() { }

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './taskfile';

src/models/taskfile.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface Taskfile {
2+
tasks: Task[];
3+
}
4+
5+
export interface Task {
6+
name: string;
7+
desc: string;
8+
summary: string;
9+
up_to_date: boolean;
10+
}

src/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './taskfile';

src/services/taskfile.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as cp from 'child_process';
2+
import * as vscode from 'vscode';
3+
import * as models from '../models';
4+
5+
class TaskfileService {
6+
private static _instance: TaskfileService;
7+
private static outputChannel: vscode.OutputChannel;
8+
9+
private constructor() {
10+
TaskfileService.outputChannel = vscode.window.createOutputChannel('Task');
11+
}
12+
13+
public static get Instance() {
14+
return this._instance || (this._instance = new this());
15+
}
16+
17+
public async read(): Promise<models.Taskfile> {
18+
return await new Promise((resolve, reject) => {
19+
let command = 'task --list-all --json';
20+
cp.exec(command, (err: cp.ExecException | null, stdout: string, stderr: string) => {
21+
if (err) {
22+
console.log('error: ' + err);
23+
reject();
24+
return;
25+
}
26+
var taskfile: models.Taskfile = JSON.parse(stdout);
27+
resolve(taskfile);
28+
});
29+
});
30+
}
31+
32+
public async runTask(taskName: string): Promise<void> {
33+
return await new Promise((resolve, reject) => {
34+
let command = `task ${taskName}`;
35+
cp.exec(command, (err: cp.ExecException | null, stdout: string, stderr: string) => {
36+
if (err) {
37+
console.log('error: ' + err);
38+
reject();
39+
return;
40+
}
41+
TaskfileService.outputChannel.append(stderr);
42+
TaskfileService.outputChannel.append(stdout);
43+
TaskfileService.outputChannel.show();
44+
resolve();
45+
});
46+
});
47+
}
48+
}
49+
50+
export const taskfile = TaskfileService.Instance;

src/task.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as vscode from 'vscode';
2+
import * as commands from './commands';
3+
import * as services from './services';
4+
import * as models from './models';
5+
6+
export class TaskExtension {
7+
private _taskfile?: models.Taskfile;
8+
9+
public async update(): Promise<void> {
10+
await services.taskfile.read().then((taskfile: models.Taskfile) => {
11+
this._taskfile = taskfile;
12+
});
13+
}
14+
15+
public registerCommands(context: vscode.ExtensionContext): void {
16+
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runTask', () => {
17+
commands.runTask(this._taskfile);
18+
}));
19+
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.refreshTasks', () => {
20+
this.update().catch((err: string) => {
21+
console.error(err);
22+
});
23+
}));
24+
}
25+
}

0 commit comments

Comments
 (0)