Skip to content

Commit c9f19d5

Browse files
committed
chore: added logging everywhere
1 parent 3461383 commit c9f19d5

File tree

6 files changed

+67
-6
lines changed

6 files changed

+67
-6
lines changed

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as vscode from 'vscode';
2+
import { log } from './utils/log';
23
import { TaskExtension } from './task';
34

45
export function activate(context: vscode.ExtensionContext) {
6+
log.info("Extension activated");
57

68
// Create a new instance of Tagger
79
let taskExtension: TaskExtension = new TaskExtension();

src/services/taskfile.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as models from '../models';
44
import * as path from 'path';
55
import * as fs from 'fs';
66
import * as semver from 'semver';
7-
import { settings } from '../settings';
7+
import { log, settings } from '../utils';
88
import { Octokit } from 'octokit';
99
import { Endpoints } from "@octokit/types";
1010

@@ -42,6 +42,7 @@ class TaskfileService {
4242

4343
// If the version is a devel version, ignore all version checks
4444
if (stdout.includes("devel")) {
45+
log.info("Using development version of task");
4546
return resolve("ready");
4647
}
4748

@@ -50,13 +51,15 @@ class TaskfileService {
5051

5152
// If there is an error fetching the version, assume task is not installed
5253
if (stderr !== "" || version === undefined) {
54+
log.error(version ? stderr : "Version is undefined");
5355
vscode.window.showErrorMessage("Task command not found.", "Install").then(this.buttonCallback);
5456
return resolve("notInstalled");
5557
}
5658

5759
// If the current version is older than the minimum required version, show an error
5860
if (version && version.compare(minimumRequiredVersion) < 0) {
59-
vscode.window.showErrorMessage(`Task v${minimumRequiredVersion} is required to run this extension. Your current version is v${version}.`, "Update").then(this.buttonCallback);
61+
log.error(`Task v${minimumRequiredVersion} is required to run this extension. The current version is v${version}`);
62+
vscode.window.showErrorMessage(`Task v${minimumRequiredVersion} is required to run this extension. The current version is v${version}.`, "Update").then(this.buttonCallback);
6063
return resolve("outOfDate");
6164
}
6265

@@ -65,11 +68,12 @@ class TaskfileService {
6568
if (settings.checkForUpdates) {
6669
this.getLatestVersion().then((latestVersion) => {
6770
if (version && latestVersion && version.compare(latestVersion) < 0) {
71+
log.info(`A new version of Task is available. Current version: v${version}, Latest version: v${latestVersion}`);
6872
vscode.window.showInformationMessage(`A new version of Task is available. Current version: v${version}, Latest version: v${latestVersion}`, "Update").then(this.buttonCallback);
6973
}
7074
return resolve("ready");
7175
}).catch((err) => {
72-
console.error(err);
76+
log.error(err);
7377
return resolve("notInstalled");
7478
});
7579
}
@@ -88,6 +92,7 @@ class TaskfileService {
8892
}
8993

9094
async getLatestVersion(): Promise<semver.SemVer | null> {
95+
log.info(`Calling GitHub to get the latest version`);
9196
let request: ReleaseRequest = {
9297
owner: 'go-task',
9398
repo: 'task'
@@ -111,6 +116,7 @@ class TaskfileService {
111116
}
112117

113118
public async init(dir: string): Promise<void> {
119+
log.info(`Initialising taskfile in: "${dir}"`);
114120
return await new Promise((resolve) => {
115121
let command = this.command('--init');
116122
cp.exec(command, { cwd: dir }, (_, stdout: string, stderr: string) => {
@@ -129,23 +135,28 @@ class TaskfileService {
129135
for (let i = 0; i < filenames.length; i++) {
130136
let filename = path.join(dir, filenames[i]);
131137
if (fs.existsSync(filename)) {
138+
log.info(`Opening taskfile: "${filename}"`);
132139
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(filename), { preview: false });
133140
return;
134141
}
135142
}
136143
}
137144

138145
public async read(dir: string): Promise<models.Taskfile> {
146+
log.info(`Searching for taskfile in: "${dir}"`);
139147
return await new Promise((resolve, reject) => {
140148
let command = this.command('--list-all --json');
141149
cp.exec(command, { cwd: dir }, (err: cp.ExecException | null, stdout: string) => {
142150
if (err) {
151+
log.error(err);
143152
return reject();
144153
}
145154
var taskfile: models.Taskfile = JSON.parse(stdout);
146155
if (path.dirname(taskfile.location) !== dir) {
156+
log.info(`Ignoring taskfile: "${taskfile.location}" (outside of workspace)`);
147157
return reject();
148158
}
159+
log.info(`Found taskfile: "${taskfile.location}"`);
149160
taskfile.workspace = dir;
150161
return resolve(taskfile);
151162
});
@@ -162,6 +173,8 @@ class TaskfileService {
162173

163174
public async runTask(taskName: string, dir?: string): Promise<void> {
164175
return await new Promise((resolve) => {
176+
log.info(`Running task: "${taskName}" in: "${dir}"`);
177+
165178
// Spawn a child process
166179
let child = cp.spawn(this.command(), [taskName], { cwd: dir });
167180

@@ -183,6 +196,7 @@ class TaskfileService {
183196

184197
// When the task finishes, print the exit code and resolve the promise
185198
child.on('close', code => {
199+
log.info(`Task completed with code ${code}`);
186200
TaskfileService.outputChannel.append(`task: completed with code ${code}\n`);
187201
this.lastTaskName = taskName;
188202
this.lastTaskDir = dir;
@@ -192,6 +206,8 @@ class TaskfileService {
192206
}
193207

194208
public async goToDefinition(task: models.Task, preview: boolean = false): Promise<void> {
209+
log.info(`Navigating to "${task.name}" definition in: "${task.location.taskfile}"`);
210+
195211
let position = new vscode.Position(task.location.line - 1, task.location.column - 1);
196212
let range = new vscode.Range(position, position);
197213

@@ -208,7 +224,7 @@ class TaskfileService {
208224
try {
209225
await vscode.commands.executeCommand('vscode.open', file, options);
210226
} catch (err) {
211-
console.error(err);
227+
log.error(err);
212228
}
213229
}
214230
}

src/task.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
22
import * as elements from './elements';
33
import * as services from './services';
44
import * as models from './models';
5-
import { settings } from './settings';
5+
import { log, settings } from './utils';
66

77
export class TaskExtension {
88
private _taskfiles: models.Taskfile[] = [];
@@ -51,7 +51,7 @@ export class TaskExtension {
5151
await this.update().then(() => {
5252
this.refresh();
5353
}).catch((err: string) => {
54-
console.error(err);
54+
log.error(err);
5555
});
5656
}
5757

@@ -64,6 +64,7 @@ export class TaskExtension {
6464

6565
// Initialise Taskfile
6666
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.init', () => {
67+
log.info("Command: vscode-task.init");
6768
if (vscode.workspace.workspaceFolders?.length === 1) {
6869
services.taskfile.init(vscode.workspace.workspaceFolders[0].uri.fsPath);
6970
return;
@@ -84,28 +85,33 @@ export class TaskExtension {
8485

8586
// Refresh tasks
8687
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.refresh', () => {
88+
log.info("Command: vscode-task.refresh");
8789
this.updateAndRefresh();
8890
}));
8991

9092
// View tasks as list
9193
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.viewAsList', () => {
94+
log.info("Command: vscode-task.viewAsList");
9295
this.setTreeNesting(false);
9396
}));
9497

9598
// View tasks as tree
9699
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.viewAsTree', () => {
100+
log.info("Command: vscode-task.viewAsTree");
97101
this.setTreeNesting(true);
98102
}));
99103

100104
// Run task
101105
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runTask', (treeItem?: elements.TaskTreeItem) => {
106+
log.info("Command: vscode-task.runTask");
102107
if (treeItem?.task) {
103108
services.taskfile.runTask(treeItem.task.name, treeItem.workspace);
104109
}
105110
}));
106111

107112
// Run task picker
108113
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runTaskPicker', () => {
114+
log.info("Command: vscode-task.runTaskPicker");
109115
let items: vscode.QuickPickItem[] = [];
110116
this._taskfiles.forEach(taskfile => {
111117
if (taskfile.tasks.length > 0) {
@@ -128,11 +134,13 @@ export class TaskExtension {
128134

129135
// Run last task
130136
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.runLastTask', () => {
137+
log.info("Command: vscode-task.runLastTask");
131138
services.taskfile.runLastTask();
132139
}));
133140

134141
// Go to definition
135142
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.goToDefinition', (task: elements.TaskTreeItem | models.Task, preview: boolean = false) => {
143+
log.info("Command: vscode-task.goToDefinition");
136144
if (task instanceof elements.TaskTreeItem) {
137145
if (task.task === undefined) {
138146
return;
@@ -144,6 +152,7 @@ export class TaskExtension {
144152

145153
// Go to definition picker
146154
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.goToDefinitionPicker', () => {
155+
log.info("Command: vscode-task.goToDefinitionPicker");
147156
let items: vscode.QuickPickItem[] = [];
148157
this._taskfiles.forEach(taskfile => {
149158
if (taskfile.tasks.length > 0) {
@@ -166,11 +175,13 @@ export class TaskExtension {
166175

167176
// Open installation
168177
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.openInstallation', () => {
178+
log.info("Command: vscode-task.openInstallation");
169179
vscode.env.openExternal(vscode.Uri.parse('https://taskfile.dev/installation'));
170180
}));
171181

172182
// Open usage
173183
context.subscriptions.push(vscode.commands.registerCommand('vscode-task.openUsage', () => {
184+
log.info("Command: vscode-task.openUsage");
174185
vscode.env.openExternal(vscode.Uri.parse('https://taskfile.dev/usage'));
175186
}));
176187
}
@@ -186,13 +197,15 @@ export class TaskExtension {
186197
}
187198

188199
private async _onDidTaskfileChange() {
200+
log.info("Detected changes to taskfile");
189201
// If manual updating is turned off (update on save)
190202
if (settings.updateOn !== "manual") {
191203
await this.updateAndRefresh();
192204
}
193205
}
194206

195207
private _onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
208+
log.info("Detected changes to configuration");
196209
if (event.affectsConfiguration("task")) {
197210
settings.update();
198211
}

src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './log';
2+
export * from './settings';

src/utils/log.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+
3+
class Log {
4+
private static _instance: Log;
5+
6+
constructor(
7+
private _channel: vscode.OutputChannel = vscode.window.createOutputChannel("Task (Debug)")
8+
) { }
9+
10+
public static get instance() {
11+
return this._instance ?? (this._instance = new this());
12+
}
13+
14+
info(v: any) {
15+
console.log(v);
16+
this._channel.appendLine(v);
17+
}
18+
19+
error(err: any) {
20+
console.error(err);
21+
this._channel.appendLine(err);
22+
}
23+
}
24+
25+
export const log = Log.instance;

src/settings.ts renamed to src/utils/settings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from 'vscode';
2+
import { log } from './';
23

34
class Settings {
45
private static _instance: Settings;
@@ -17,6 +18,8 @@ class Settings {
1718

1819
// Fetches all the settings from the workspace configuration file
1920
public update() {
21+
log.info("Updating settings");
22+
2023
// Get the workspace config
2124
let config = vscode.workspace.getConfiguration("task");
2225

0 commit comments

Comments
 (0)