-
Notifications
You must be signed in to change notification settings - Fork 19
/
Common.ts
530 lines (520 loc) · 19 KB
/
Common.ts
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import * as cp from 'child_process';
import { join } from 'path';
import { ProgressLocation, Selection, Uri, ViewColumn, commands, window, workspace } from 'vscode';
import Output from './utils/Output';
export interface Command {
name: string;
description: string;
arguments: any[];
options: {
name: string;
shortcut: string;
accept_value: boolean;
is_value_required: boolean;
is_multiple: boolean;
description: string;
default: any;
}[];
}
export interface CommandInfo {
err?: Error;
stdout?: string;
stderr?: string;
artisan: {
dir: string;
path: string;
};
}
export default interface Common {
run(): void;
dispose?(): void;
}
export default class Common {
public static readonly artisanFileList: Uri[] = [];
/**
* Get a list of all artisan files in the workspace.
*/
protected static async listArtisanPaths() {
let config = workspace.getConfiguration('artisan');
let additionalLocations = config.get<string | null | string[]>('location');
additionalLocations = typeof additionalLocations === 'string' ? new Array(1).concat(additionalLocations) : additionalLocations;
let list = this.artisanFileList.concat(additionalLocations.map(i => Uri.parse(i)));
if (list.length === 1 && list[0].fsPath.length) return list[0].fsPath;
else if (list.length === 0) return 'artisan';
let artisanToUse = await Common.getListInput(
'Which artisan should execute this command?',
list
// Get the fs path from the URI
.map(i => i.fsPath)
// Remove Non-String values
.filter(String)
// Remove Duplicates
.filter((v, i, a) => a.indexOf(v) === i)
);
return artisanToUse;
}
/**
* Gets an artisan file from the workspace.
* @param artisan The artisan path to use. If not specified then the user will be prompted to select one.
*/
protected static async getArtisanRoot(artisan?: string) {
const artisanToUse = artisan ? artisan : await this.listArtisanPaths();
return artisanToUse.replace(/artisan$/, '').replace(/\\$/g, '');
}
/**
* Executes an artisan command.
* @param command The command to execute.
* @param callback The callback to execute when the command is finished.
* @param artisan The artisan path to use. If not specified then the user will be prompted to select one.
*/
protected static async execCmd(command: string, callback: (info: CommandInfo) => void, artisan?: string) {
const artisanToUse = artisan ? artisan : await this.listArtisanPaths();
const artisanRoot = await this.getArtisanRoot(artisan);
// Try an get a custom php location
const config = workspace.getConfiguration('artisan');
const phpLocation = config.get<string | null>('php.location', 'php');
const dockerEnabled = config.get<boolean>('docker.enabled', false);
const dockerCommand = config.get<string>('docker.command', null);
const maxBuffer = config.get<number>('maxBuffer', 1024 * 200);
const wsl = config.get<boolean>('wsl.enabled', false);
let cmd = '';
if (dockerEnabled) {
command = `php artisan ${command}`;
cmd = `${dockerCommand} ${command}`;
} else {
if (phpLocation === 'php') {
command = `php artisan ${command}`;
} else {
// Location is in quotes so that it can support spaces in the path
command = `"${phpLocation}" artisan ${command}`;
}
cmd = command;
}
Output.command(command.trim());
window.withProgress(
{
location: ProgressLocation.Window,
cancellable: false,
title: 'Executing Artisan Command',
},
async progress => {
progress.report({ increment: 0 });
const result = wsl
? await this.wslCommand(artisanRoot, artisanToUse, cmd)
: await this.nonWslCommand(artisanRoot, artisanToUse, cmd, maxBuffer);
callback(result);
progress.report({ increment: 100 });
}
);
}
/**
* Executes an artisan command when not using WSL.
* @param callback The callback to execute when the command is finished.
* @param artisanRoot The root directory of the artisan file.
* @param artisanToUse The artisan file to use.
* @param cmd The command to execute.
* @param maxBuffer The max buffer size to use.
*/
protected static async nonWslCommand(artisanRoot: string, artisanToUse: string, cmd: string, maxBuffer: number) {
return new Promise<CommandInfo>(resolve => {
cp.exec(cmd, { cwd: artisanRoot, maxBuffer: maxBuffer }, (err, stdout, stderr) => {
Output.command(stdout.trim());
Output.showConsole();
if (err) {
Output.command('-----------------------------------');
Output.error(err.message.trim());
Output.showConsole();
}
resolve({
err,
stdout,
stderr,
artisan: {
dir: artisanRoot,
path: artisanToUse,
},
});
});
});
}
/**
* Executes an artisan command when using WSL.
* @param callback The callback to execute when the command is finished.
* @param artisanRoot The root directory of the artisan file.
* @param artisanToUse The artisan file to use.
* @param cmd The command to execute.
*/
protected static async wslCommand(artisanRoot: string, artisanToUse: string, cmd: string) {
return new Promise<CommandInfo>((resolve, reject) => {
const child = cp.spawn('wsl', [cmd], { cwd: artisanRoot, shell: true });
const childData: string[] = [];
child.stdout.on('data', data => childData.push(data.toString()));
child.stdout.on('end', () => {
resolve({
err: undefined,
stdout: childData.join(''),
stderr: undefined,
artisan: {
dir: artisanRoot,
path: artisanToUse,
},
});
});
child.stderr.on('error', err => {
Output.command('-----------------------------------');
Output.error(err.message.trim());
Output.showConsole();
reject({
err,
stdout: undefined,
stderr: undefined,
artisan: {
dir: artisanRoot,
path: artisanToUse,
},
});
});
});
}
/**
* Opens a file in the editor.
* @param root The root directory.
* @param filename The filename to open.
*/
protected static async openFile(root: string, filename: string) {
try {
// let doc = await workspace.openTextDocument(this.artisanRoot + '/' + filename)
let doc = await workspace.openTextDocument(join(root, filename));
window.showTextDocument(doc);
this.refreshFilesExplorer();
} catch (e) {
console.log(e.message);
}
}
/**
* Parses a cli table generated by artisan.
* @param cliTable The cli table to parse.
*/
protected static parseCliTable(cliTable: string) {
let cliRows = cliTable.split(/\r\n|\n/g);
let headers: string[] = [];
let rows: string[][] = [];
// Parse the cli table
for (let i = 0, len = cliRows.length; i < len; i++) {
if (i === 0 || i === 2) {
continue;
} else if (i === 1) {
(headers = cliRows[i].split('|')).forEach((v, k) => {
headers[k] = v.replace(/[\u001b\u009b][[()#?]*(?:[0-9]{1,4}(?:[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '').trim();
if (headers[k] === '') {
delete headers[k];
}
});
} else {
if (cliRows[i].indexOf('|') > -1) {
let row: string[] = [];
cliRows[i].split(/ \| /g).forEach(v => {
row.push(v.replace(/^\||\|$/g, '').trim());
});
rows.push(row);
}
}
}
return { headers: headers, rows: rows };
}
private static get tableStyle(): string {
return `<style>
* { box-sizing: border-box; }
body { padding: 0; margin: 0; }
table { border-collapse: collapse; width: 95vw; margin: auto; }
table thead { font-size: 16px; text-align: left; }
table tbody { font-size: 14px; }
table td, table th { padding: 10px; }
table tbody tr:nth-child(odd) { background-color: rgba(0,0,0,0.25); }
table td a { color: #4080d0; cursor: pointer; }
.hidden { display: none; }
.search { padding-top: 15px; padding-bottom: 15px; width: 95vw; margin: auto; }
#filter { display: block; padding: 5px; width: 100%; }
.loading { text-align: center; }
.filters { display: flex; flex-direction: row; gap: 20px; justify-content: start; }
.text-filter-wrapper { display: flex; flex-direction: row; gap: 20px; place-items: center; }
.text-filter-wrapper label { width: 150px }
label { display: flex; flex-direction: row; gap: 5px; place-items: center; cursor: pointer; }
</style>`;
}
/**
* Opens a virtual Laravel routes list.
* @param openPath The path to open.
* @param title The title of the file.
* @param headers The headers of the table.
* @param rows The rows of the table.
* @param artisanRoot The root directory of the artisan file.
*/
protected static async openVirtualHtmlFile(openPath: string, title: string, headers: string[], rows: string[][], artisanRoot: string) {
let html: string = `
<div class="search">
<div class="text-filter-wrapper">
<input type="text" id="filter" placeholder="Search for an item (RegExp Supported)">
<label>
<input type="checkbox" id="filter-invert"> Invert Search
</label>
</div>
<p class="filters">`;
headers.forEach(header => {
html += `
<label>
<input type="checkbox" id="filter-${header.toLowerCase()}" checked> Display ${header}
</label>`;
});
html += `</p></div>`;
html += '<h2 class="loading">Loading Route Information...</h2>';
html += `${this.tableStyle}<table class="hidden">`;
html += '<thead><tr>';
headers.forEach(header => {
html += `<th id="header_${header}">${header}</th>`;
});
html += '</tr></thead><tbody>';
rows.forEach((row, rowIdx) => {
html += '<tr>';
row.forEach((item, colIdx) => {
if ((item ?? '').match(/app\\/i)) {
html +=
`<td id="cell_${rowIdx}_${headers[colIdx]}">
<a href="file:///${workspace.rootPath}/${item.replace(/@.+$/, '').replace(/^App/g, 'app')}.php" data-method="${item.replace(
/^.+@/,
''
)}" class="app-item">` +
item +
'</a></td>';
} else {
html += '<td id="cell_' + rowIdx + '_' + headers[colIdx] + '">' + item + '</td>';
}
});
html += '</tr>';
});
html += '</tbody></table>';
html += `<script>
const filter = document.querySelector('#filter')
const body = document.querySelector('table tbody')
const invertCheckbox = document.querySelector('#filter-invert')
const rootPath = '${artisanRoot.replace(/\\/g, '/')}'
const headers = ${JSON.stringify(headers)}
const vscode = acquireVsCodeApi()
filter.focus()
const table = document.querySelector('table')
const loading = document.querySelector('.loading')
invertCheckbox.addEventListener('change', e => filterItems())
`;
headers.forEach(header => {
html += `
const ${header}Checkbox = document.querySelector('#filter-${header.toLowerCase()}')
let shouldShow${header} = true
${header}Checkbox.addEventListener('change', e => {
shouldShow${header} = e.currentTarget.checked
filterItems()
});
`;
});
// Begin filter items function
html += `
function filterItems() {
let v = filter.value
let shouldInvert = invertCheckbox.checked
document.querySelectorAll('tbody > tr').forEach(row => {
let txt = row.textContent
let reg = new RegExp(v, 'ig')
// If the row matches the regular expression or the value is empty, show the row
// If the search is inverted, hide the row if it matches the regular expression
if(v.length === 0) {
row.classList.remove('hidden')
} else {
if (!shouldInvert) {
reg.test(txt)?row.classList.remove('hidden'):row.classList.add('hidden')
} else {
!reg.test(txt)?row.classList.remove('hidden'):row.classList.add('hidden')
}
}
})
`;
headers.forEach(header => {
html += `
const ${header}Header = document.querySelector('#header_${header}')
const ${header}Checkbox = document.querySelector('#filter-${header.toLowerCase()}')
${header}Checkbox.addEventListener('change', e => {
if (e.currentTarget.checked) {
${header}Header.classList.remove('hidden')
} else {
${header}Header.classList.add('hidden')
}
});
`;
});
headers.forEach(header => {
html += `
document.querySelectorAll('tbody > tr').forEach((row, rowIdx) => {
const ${header} = row.querySelector('#cell_' + rowIdx + '_${header}')
if (${header} && !shouldShow${header}) {
${header}.classList.add('hidden')
} else if (${header} && shouldShow${header}) {
${header}.classList.remove('hidden')
}
});
`;
});
// End filter items function
html += `}`;
html += `
function routeEvents(){
Array.from(body.querySelectorAll('a')).forEach(item => {
item.addEventListener('click', e => {
e.preventDefault()
let target = e.currentTarget
vscode.postMessage({ file: target.href, method: target.getAttribute('data-method') })
})
})
}
filter.addEventListener('input', filterItems)
window.addEventListener('message', msg => {
let rows = msg.data.rows
if(rows.length >0){
table.classList.remove('hidden')
loading.classList.add('hidden')
}
let html = ''
rows.forEach((row, rowIdx) => {
html += '<tr>'
row.forEach((item, colIdx) => {
if ((item??'').match(/app\\\\/i)) {
let file = \`\${rootPath}/\${item.replace(/@.+$/, '').replace(/^App/, 'app')}.php\`.replace(/\\\\/g, '/')
html += \`<td id="cell_\${rowIdx}_\${headers[colIdx]}"><a href="\${file}" data-method="\${item.replace(/^.+@/, '')}" class="app-item">\` + item + '</a></td>'
} else {
html += '<td id="cell_' + rowIdx + '_' + headers[colIdx] + '">' + item + '</td>'
}
})
html += '</tr>'
})
body.innerHTML = html
filterItems()
routeEvents()
})
routeEvents()
</script>`;
const panel = window.createWebviewPanel(openPath, title, ViewColumn.Active, {
enableScripts: true,
retainContextWhenHidden: true,
});
panel.webview.html = html;
panel.webview.onDidReceiveMessage(async msg => {
if (msg.file) {
let uri = Uri.parse(msg.file);
let method = msg.method || '';
let doc = await workspace.openTextDocument(uri);
let activeDoc = await window.showTextDocument(doc);
if (method.length > 0) {
let idx = doc.getText().indexOf(`function ${method}`);
if (idx > -1) {
let pos = doc.positionAt(idx + 9);
activeDoc.selection = new Selection(pos, pos);
}
}
}
});
return panel;
}
/**
* Shows an input box and returns the value.
* @param placeHolder The placeholder text.
*/
protected static async getInput(placeHolder: string) {
let name = await window.showInputBox({
placeHolder: placeHolder.replace(/\s\s+/g, ' ').trim(),
});
name = typeof name === 'undefined' ? '' : name;
// if (name.length === 0) {
// window.showErrorMessage('Invalid ' + placeHolder)
// return ''
// }
return name;
}
/**
* Shows a list of items and returns the selected item.
* @param placeHolder The placeholder text.
* @param list The list of items to choose from.
*/
protected static async getListInput(placeHolder: string, list: string[]) {
let name = await window.showQuickPick(list, { placeHolder: placeHolder });
name = typeof name === 'undefined' ? '' : name;
return name;
}
/**
* Shows a Yes/No dialog (where `yes` is first in the list) and returns the result.
* @param placeHolder The placeholder text.
*/
protected static async getYesNo(placeHolder: string): Promise<boolean> {
let value = await window.showQuickPick(['Yes', 'No'], { placeHolder });
return value.toLowerCase() === 'yes' ? true : false;
}
/**
* Shows a No/Yes dialog (where `no` is first in the list) and returns the result.
* @param placeHolder The placeholder text.
*/
protected static async getNoYes(placeHolder: string): Promise<boolean> {
let value = await window.showQuickPick(['No', 'Yes'], { placeHolder });
return value.toLowerCase() === 'yes' ? true : false;
}
/**
* Shows snackbar message to the user.
* @param message The message to show.
*/
protected static async showMessage(message: string) {
window.showInformationMessage(message);
return true;
}
/**
* Shows snackbar error message to the user.
* @param message The message to show.
* @param consoleErr The message to show in console.
*/
protected static async showError(message: string, consoleErr = null) {
if (consoleErr !== null) {
message += ' (See output console for more details)';
console.error(consoleErr + ' (See output console for more details)');
}
window.showErrorMessage(message);
return false;
}
/**
* Refreshes the files explorer.
*/
protected static refreshFilesExplorer() {
commands.executeCommand('workbench.files.action.refreshFilesExplorer');
}
/**
* Get a list of all artisan commands.
*/
protected static getCommandList(): Promise<Command[]> {
return new Promise(resolve => {
this.execCmd(`list --format=json`, info => {
let commands: any[] = JSON.parse(info.stdout).commands;
let commandList: Command[] = [];
commands.forEach(command => {
let commandItem = {
name: command.name,
description: command.description,
options: [],
arguments: [],
};
for (let i in command.definition.options) {
if (['help', 'quiet', 'verbose', 'version', 'ansi', 'no-ansi', 'no-interaction', 'env'].indexOf(i) > -1) continue;
commandItem.options.push(command.definition.options[i]);
}
for (let i in command.definition.arguments) {
commandItem.arguments.push(command.definition.arguments[i]);
}
commandList.push(commandItem);
});
resolve(commandList);
});
});
}
}