-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtasksTreeDataProvider.ts
More file actions
executable file
·423 lines (380 loc) · 13.9 KB
/
tasksTreeDataProvider.ts
File metadata and controls
executable file
·423 lines (380 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { ActiveEditorTracker } from './activeEditorTracker';
import { TextEditorComparer } from './comparers';
export class TasksTreeDataProvider implements vscode.TreeDataProvider<TaskTreeItem> {
private sha1 = require('sha1');
private _onDidChangeTreeData: vscode.EventEmitter<TaskTreeItem | undefined> = new vscode.EventEmitter<TaskTreeItem | undefined>();
readonly onDidChangeTreeData: vscode.Event<TaskTreeItem | undefined> = this._onDidChangeTreeData.event;
private packageJsonPath = null;
private commitMessagePath = null;
private emptySettings = { lock: false, active: null, sets: { default: { source: 'default', 'name': 'Default', tasks: {} } } };
taskStatusBarItem: vscode.StatusBarItem;
constructor(private workspaceRoot: string) {
if (this.workspaceRoot) {
var vsCodePath = path.join(this.workspaceRoot, ".vscode");
if (!this.pathExists(vsCodePath)) fs.mkdirSync(vsCodePath);
this.packageJsonPath = path.join(this.workspaceRoot, ".vscode", "tasks-and-contexts.json");
this.commitMessagePath = path.join(this.workspaceRoot, ".vscode", "tasks-and-contexts-commit-msg.txt");
this.packageJson();
}
}
private packageJson() {
if (this.pathExists(this.packageJsonPath)) {
return JSON.parse(fs.readFileSync(this.packageJsonPath, 'utf-8'));
} else {
const emptySettings = JSON.stringify(this.emptySettings, null, 2);
fs.writeFileSync(this.packageJsonPath, emptySettings, { encoding: 'utf-8' });
return JSON.parse(emptySettings);
}
}
private refresh(): void {
this._onDidChangeTreeData.fire();
}
private fileNames = [];
addTask(): void {
vscode.window.showInputBox({ placeHolder: 'Enter a task name' })
.then(taskName => {
if (taskName !== null && taskName !== undefined) {
var packageJson = this.packageJson();
const taskId = this.sha1(taskName);
if (packageJson.sets.default.tasks[taskId] == undefined) {
packageJson.sets.default.tasks[taskId] = { 'name': taskName, data: [] };
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
this.refresh();
} else {
vscode.window.showErrorMessage("A task with this name already exists");
}
}
});
}
async addFromTrello() {
/*
"extensionTaskTreeItem": [
"Ho-Wan.vscode-trello-viewer"
],
*/
var trelloExtension = vscode.extensions.getExtension('Ho-Wan.vscode-trello-viewer');
if (trelloExtension.isActive) {
let trelloApi = trelloExtension.exports;
const boards = await trelloApi.boards();
let picks = [];
for (var i in boards) {
picks.push(boards[i].name);
}
const boardName = await vscode.window.showQuickPick(picks, { placeHolder: "Select a Trello Board" });
if (boardName == undefined) return;
let boardId = "";
for (var i in boards) {
if (boardName == boards[i].name) {
boardId = boards[i].id;
break;
}
}
const lists = await trelloApi.lists(boardId);
picks = [];
for (var i in lists) {
picks.push(lists[i].name);
}
const listName = await vscode.window.showQuickPick(picks, { placeHolder: "Select a Trello List" });
if (listName == undefined) return;
let listId = "";
for (var i in lists) {
if (listName == lists[i].name) {
listId = lists[i].id;
break;
}
}
const cards = await trelloApi.cards(listId);
if (cards != undefined) {
const packageJson = this.packageJson();
const set = "trello-" + listId;
for (var i in cards) {
if (packageJson.sets[set] == undefined)
packageJson.sets[set] = { source: 'trello', name: boardName + " - " + listName, id: listId, tasks: {} };
if (packageJson.sets[set].tasks[cards[i].id] == undefined) {
packageJson.sets[set].tasks[cards[i].id] = { name: cards[i].name, link: cards[i].shortLink, data: [] };
}
}
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
this.refresh();
}
}
}
deleteTask(task: TaskTreeItem): void {
var packageJson = this.packageJson();
if (packageJson.sets[task.set].source != 'default') {
vscode.window.showErrorMessage("You can not delete this type of task");
return;
}
vscode.commands.executeCommand('workbench.action.closeAllEditors');
const taskName = task.label;
if (packageJson) {
if (packageJson.sets[task.set].tasks[task.id] != undefined) {
delete packageJson.sets[task.set].tasks[task.id];
packageJson.active = null;
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
this.refresh();
}
}
}
async refreshTasks() {
var addCount = 0;
var delCount = 0;
var packageJson = this.packageJson();
if (packageJson) {
var trelloExtension = vscode.extensions.getExtension('Ho-Wan.vscode-trello-viewer');
for (var set in packageJson.sets) {
if (packageJson.sets[set].source == 'trello') {
if (trelloExtension.isActive) {
let trelloApi = trelloExtension.exports;
var listId = packageJson.sets[set].id;
var cards = await trelloApi.cards(listId);
if (cards != undefined) {
var cardIds = [];
console.log(cards);
for (var i in cards) {
cardIds.push(cards[i].id);
if (packageJson.sets[set].tasks[cards[i].id] == undefined) {
packageJson.sets[set].tasks[cards[i].id] = { name: cards[i].name, link: cards[i].shortLink, data: [] };
addCount++;
} else if (packageJson.sets[set].tasks[cards[i].id].link == undefined) {
packageJson.sets[set].tasks[cards[i].id].link = cards[i].shortLink;
}
}
for (var taskId in packageJson.sets[set].tasks) {
if (cardIds.indexOf(taskId) == -1) {
delCount++;
delete packageJson.sets[set].tasks[taskId];
}
}
}
}
}
}
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
this.refresh();
vscode.window.showInformationMessage(addCount + " tasks added, " + delCount + " tasks deleted");
}
}
renameEntry(task: TaskTreeItem): void {
var packageJson = this.packageJson();
if (packageJson.sets[task.set].source != 'default') {
vscode.window.showErrorMessage("You can not rename this type of task");
return;
}
vscode.window.showInputBox({ value: task.label })
.then(taskName => {
if (taskName !== null && taskName !== undefined) {
if (taskName == task.label) {
vscode.window.showErrorMessage("The task name was not changed");
return;
}
var newTaskId = this.sha1(taskName);
packageJson.sets[task.set].tasks[newTaskId] = packageJson.sets[task.set].tasks[task.id];
delete packageJson.sets[task.set].tasks[task.id];
packageJson.sets[task.set].tasks[newTaskId].name = taskName;
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
this.refresh();
}
});
}
async activateTask(taskId: string) {
var packageJson = this.packageJson();
if (packageJson.active) {
try {
const editorTracker = new ActiveEditorTracker();
let active = vscode.window.activeTextEditor;
let editor = active;
const openEditors: vscode.TextEditor[] = [];
do {
if (editor != null) {
// If we didn't start with a valid editor, set one once we find it
if (active === undefined) {
active = editor;
}
openEditors.push(editor);
}
editor = await editorTracker.awaitNext(500);
if (editor !== undefined && openEditors.some(_ => TextEditorComparer.equals(_, editor, { useId: true, usePosition: true }))) break;
} while ((active === undefined && editor === undefined) || !TextEditorComparer.equals(active, editor, { useId: true, usePosition: true }));
editorTracker.dispose();
for (var set in packageJson.sets) {
if (packageJson.sets[set].tasks[packageJson.active] != undefined) {
packageJson.sets[set].tasks[packageJson.active].data = [];
for (var i in openEditors) {
packageJson.sets[set].tasks[packageJson.active].data.push(openEditors[i].document.fileName);
}
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
break;
}
}
}
catch (ex) {
//Logger.error(ex, 'DocumentManager.save');
}
}
var packageJson = this.packageJson();
if (packageJson.active == taskId) {
this.taskStatusBarItem.hide();
packageJson.active = '';
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
this.refresh();
await fs.unlink(this.commitMessagePath, (err) => { });
} else {
packageJson.active = taskId;
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
this.refresh();
for (var set in packageJson.sets) {
if (packageJson.sets[set].tasks[taskId] != undefined) {
this.taskStatusBarItem.text = packageJson.sets[set].tasks[taskId].name;
this.taskStatusBarItem.tooltip = packageJson.sets[set].name + ': ' + packageJson.sets[set].tasks[taskId].name;
this.taskStatusBarItem.show();
for (var i in packageJson.sets[set].tasks[taskId].data) {
var fileName = packageJson.sets[set].tasks[taskId].data[i];
if (this.pathExists(fileName)) {
let doc = await vscode.workspace.openTextDocument(fileName);
await vscode.window.showTextDocument(doc, { preview: false });
}
}
var commitMessage = packageJson.sets[set].tasks[taskId].name;
if (packageJson.sets[set].tasks[taskId].link != undefined)
commitMessage += "\n\n#" + packageJson.sets[set].source + ":" + packageJson.sets[set].tasks[taskId].link;
fs.writeFileSync(this.commitMessagePath, commitMessage, { encoding: 'utf-8' });
}
}
}
}
private getRealFileName(document): string {
let fileName = document.fileName;
const ext = fileName.split('.').pop();
if (ext == 'git')
fileName = fileName.substr(0, fileName.lastIndexOf("."));
return fileName;
}
addDocument(document: vscode.TextDocument): void {
const baseName = document.fileName.replace(/^.*[\\\/]/, '');
if (document.uri.scheme == 'file') {
const fileName = this.getRealFileName(document);
var packageJson = this.packageJson();
const activeTask = packageJson.active;
for (var set in packageJson.sets) {
if (activeTask != null && packageJson.sets[set].tasks[activeTask] != undefined && packageJson.sets[set].tasks[activeTask].data.indexOf(fileName) == -1) {
packageJson.sets[set].tasks[activeTask].data.push(fileName);
fs.writeFileSync(this.packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
this.refresh();
return;
}
}
}
}
getTreeItem(element: TaskTreeItem): vscode.TreeItem {
return element;
}
getChildren(element?: TaskTreeItem): Thenable<TaskTreeItem[]> {
if (!this.workspaceRoot) {
vscode.window.showWarningMessage('Tasks and Contexts needs a workspace to run');
return Promise.resolve([]);
}
var packageJson = this.packageJson();
var activeSet = '';
if (packageJson.active != '') {
for (var set in packageJson.sets) {
if (packageJson.sets[set].tasks[packageJson.active] != undefined) {
activeSet = set;
this.taskStatusBarItem.text = packageJson.sets[set].tasks[packageJson.active].name;
this.taskStatusBarItem.tooltip = packageJson.sets[set].name + ': ' + packageJson.sets[set].tasks[packageJson.active].name;
this.taskStatusBarItem.show();
break;
}
}
}
if (element) {
return Promise.resolve(this.getTasksinPackageJson(element.id));
} else {
if (this.pathExists(this.packageJsonPath)) {
return Promise.resolve(this.getSetsinPackageJson(activeSet));
} else {
vscode.window.showInformationMessage('Workspace has no config file for Tasks and Contexts');
return Promise.resolve([]);
}
}
}
private getSetsinPackageJson(activeSet): TaskTreeItem[] {
const packageJson = this.packageJson();
if (packageJson) {
const toDep = (set: string): TaskTreeItem => {
return new TaskTreeItem(packageJson.sets[set].name,
false,
null,
null,
set,
'set',
activeSet != set ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.Expanded,
null);
}
var deps = Object.keys(packageJson.sets).map(dep => toDep(dep));
return deps;
}
}
private getTasksinPackageJson(set: string): TaskTreeItem[] {
const packageJson = this.packageJson();
if (packageJson) {
const toDep = (taskId: string, set: string): TaskTreeItem => {
var taskName = packageJson.sets[set].tasks[taskId].name;
if (taskId == packageJson.active) taskName = "* " + taskName;
return new TaskTreeItem(taskName,
taskId == packageJson.active,
set,
packageJson.sets[set].name,
taskId,
'task_' + packageJson.sets[set].source,
vscode.TreeItemCollapsibleState.None,
{
command: 'taskManager.activateTask',
title: '',
arguments: [taskId]
}
);
}
return Object.keys(packageJson.sets[set].tasks).map(dep => toDep(dep, set));
}
}
private pathExists(p: string): boolean {
try {
fs.accessSync(p);
} catch (err) {
return false;
}
return true;
}
}
export class TaskTreeItem extends vscode.TreeItem {
constructor(
public readonly label: string,
private active: boolean,
public set: string,
public setName: string,
public id: string,
public context: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly command?: vscode.Command
) {
super(label, collapsibleState);
this.contextValue = context;
if (context != 'set') {
this.iconPath = {
light: path.join(__filename, '..', '..', 'images', 'light', 'document.svg'),
dark: path.join(__filename, '..', '..', 'images', 'dark', 'document.svg')
};
}
}
get tooltip(): string {
return `${this.label} (${this.setName})`;
}
get description(): string {
return '';
}
}