This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
extensionFunctions.ts
552 lines (506 loc) · 18.8 KB
/
extensionFunctions.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as vscode from 'vscode';
import * as utils from './utils';
import * as fs from 'fs';
import {DidactWebviewPanel} from './didactWebView';
import * as querystring from 'querystring';
import * as path from 'path';
import * as child_process from 'child_process';
import {getMDParser} from './markdownUtils';
import {parseADtoHTML} from './asciidocUtils';
import * as scaffoldUtils from './scaffoldUtils';
import { TreeNode } from './nodeProvider';
let didactOutputChannel: vscode.OutputChannel | undefined = undefined;
const fetch = require('node-fetch');
const DOMParser = require('xmldom').DOMParser;
// command IDs
export const SCAFFOLD_PROJECT_COMMAND = 'vscode.didact.scaffoldProject';
export const OPEN_TUTORIAL_COMMAND = 'vscode.didact.openTutorial';
export const START_DIDACT_COMMAND = 'vscode.didact.startDidact';
export const START_TERMINAL_COMMAND = 'vscode.didact.startTerminalWithName';
export const SEND_TERMINAL_SOME_TEXT_COMMAND = 'vscode.didact.sendNamedTerminalAString';
export const REQUIREMENT_CHECK_COMMAND = 'vscode.didact.requirementCheck';
export const EXTENSION_REQUIREMENT_CHECK_COMMAND = 'vscode.didact.extensionRequirementCheck';
export const WORKSPACE_FOLDER_EXISTS_CHECK_COMMAND = 'vscode.didact.workspaceFolderExistsCheck';
export const CREATE_WORKSPACE_FOLDER_COMMAND = 'vscode.didact.createWorkspaceFolder';
export const RELOAD_DIDACT_COMMAND = 'vscode.didact.reload';
export const VALIDATE_ALL_REQS_COMMAND = 'vscode.didact.validateAllRequirements';
export const GATHER_ALL_REQS_COMMAND = 'vscode.didact.gatherAllRequirements';
export const GATHER_ALL_COMMANDS = 'vscode.didact.gatherAllCommands';
export const VIEW_OPEN_TUTORIAL_MENU = 'vscode.didact.view.tutorial.open';
export const REGISTER_TUTORIAL = 'vscode.didact.register'; // name, uri, category
export const REFRESH_DIDACT_VIEW = 'vscode.didact.view.refresh';
export const SEND_TERMINAL_KEY_SEQUENCE = 'vscode.didact.sendNamedTerminalCtrlC';
export const CLOSE_TERMINAL = 'vscode.didact.closeNamedTerminal';
export const CLI_SUCCESS_COMMAND = 'vscode.didact.cliCommandSuccessful';
export const DIDACT_OUTPUT_CHANNEL = 'Didact Activity';
// stash the extension context for use by the commands
export function initializeContext(inContext: vscode.ExtensionContext) {
extensionFunctions.setContext(inContext);
utils.setContext(inContext);
// set up the didact output channel
didactOutputChannel = vscode.window.createOutputChannel(DIDACT_OUTPUT_CHANNEL);
}
// contain all the various command functions in one spot
export namespace extensionFunctions {
// stashed extension context
let context : vscode.ExtensionContext;
// stashed markdown URI
let _mdFileUri : vscode.Uri | undefined = undefined;
// stash the context so we have it for use by the command functions without passing it each time
export function setContext(inContext: vscode.ExtensionContext) {
context = inContext;
}
// use the json to model the folder/file structure to be created in the vscode workspace
export async function scaffoldProjectFromJson(jsonpath:vscode.Uri) {
sendTextToOutputChannel(`Scaffolding project from json: ${jsonpath}`);
if (utils.getWorkspacePath()) {
let testJson : any;
if (jsonpath) {
var mdStr = fs.readFileSync(jsonpath.fsPath, 'utf8');
testJson = JSON.parse(mdStr);
} else {
testJson = scaffoldUtils.createSampleProject();
}
await scaffoldUtils.createFoldersFromJSON(testJson, jsonpath)
.catch( (error) => {
throw new Error(`Error found while scaffolding didact project: ${error}`);
});
} else {
throw new Error('No workspace folder. Workspace must have at least one folder before Didact scaffolding can begin.');
}
}
// quick and dirty workaround for an empty workspace - creates a folder in the user's temporary store
export async function createTemporaryFolderAsWorkspaceRoot(requirement: string | undefined) {
sendTextToOutputChannel(`Creating temporary folder as workspace root`);
var tmp = require('tmp');
// if the workspace is empty, we will create a temporary one for the user
var tmpobj = tmp.dirSync();
let rootUri : vscode.Uri = vscode.Uri.parse(`file://${tmpobj.name}`);
vscode.workspace.updateWorkspaceFolders(0,undefined, {uri: rootUri});
sendTextToOutputChannel(`-- created ${tmpobj.name}`);
if (requirement) {
if (rootUri) {
postRequirementsResponseMessage(requirement, true);
} else {
postRequirementsResponseMessage(requirement, false);
}
}
}
// utility command to start a named terminal so we have a handle to it
export async function startTerminal(...rest: any[]) { //name:string, path: vscode.Uri) {
let name : string | undefined = undefined;
let uri : vscode.Uri | undefined = undefined;
if (rest) {
try {
for(let arg of rest) {
if (typeof arg === 'string' ) {
name = arg;
} else if (typeof arg === 'object' ) {
uri = arg as vscode.Uri;
}
}
} catch (error) {
throw new Error(error);
}
}
if (name) {
if (findTerminal(name)) {
throw new Error(`Terminal ${name} already exists`);
}
}
let terminal : vscode.Terminal | undefined = undefined;
if (name && uri) {
sendTextToOutputChannel(`Starting terminal ${name} with uri ${uri}`);
terminal = vscode.window.createTerminal({
name: `${name}`,
cwd: `${uri.fsPath}`
});
} else if (name) {
terminal = vscode.window.createTerminal({
name: `${name}`
});
} else {
terminal = vscode.window.createTerminal();
}
if (terminal) {
terminal.show();
}
}
async function showAndSendText(terminal: vscode.Terminal, text:string) {
if (terminal) {
terminal.show();
terminal.sendText(text);
return;
}
}
async function showAndSendCtrlC(terminal: vscode.Terminal) {
if (terminal) {
terminal.show();
await vscode.commands.executeCommand("workbench.action.terminal.sendSequence", { text : "\x03" });
return;
}
}
async function killTerminal(terminal: vscode.Terminal) {
if (terminal) {
terminal.show();
await vscode.commands.executeCommand("workbench.action.terminal.kill");
return;
}
}
export function findTerminal(name: string) : vscode.Terminal | undefined {
try {
for(let localTerm of vscode.window.terminals){
if(localTerm.name === name){
return localTerm;
}
}
} catch {
return undefined;
}
return undefined;
}
// send a message to a named terminal
export async function sendTerminalText(name:string, text:string) {
const terminal : vscode.Terminal | undefined = findTerminal(name);
if (!terminal) {
const newterminal = vscode.window.createTerminal(name);
showAndSendText(newterminal, text);
}
if (terminal) {
showAndSendText(terminal, text);
}
sendTextToOutputChannel(`Sent terminal ${name} the text ${text}`);
}
export async function sendTerminalCtrlC(name:string) {
const terminal : vscode.Terminal | undefined = findTerminal(name);
if (!terminal) {
throw new Error(`No terminal found with name ${name} to send a Ctrl+C`);
} else {
showAndSendCtrlC(terminal);
sendTextToOutputChannel(`Sent terminal ${name} a Ctrl+C`);
}
}
export async function closeTerminal(name:string) {
const terminal : vscode.Terminal | undefined = findTerminal(name);
if (!terminal) {
throw new Error(`No terminal found with name ${name} to close`);
} else {
await killTerminal(terminal).then( () => {
if (terminal) {
terminal.dispose();
}
});
sendTextToOutputChannel(`Closed terminal ${name}`);
}
}
// reset the didact window to use the default set in the settings
export async function openDidactWithDefault() {
sendTextToOutputChannel(`Starting Didact window with default`);
DidactWebviewPanel.createOrShow(context.extensionPath);
DidactWebviewPanel.setContext(context);
_mdFileUri = undefined;
DidactWebviewPanel.hardReset();
}
// open the didact window with the markdown passed in via Uri
export async function startDidact(uri:vscode.Uri) {
if (!uri) {
uri = await utils.getCurrentFileSelectionPath();
}
sendTextToOutputChannel(`Starting Didact window with ${uri}`);
// handle extension, workspace, https, and http
if (uri) {
const query = querystring.parse(uri.query);
if (query.extension) {
const value = utils.getValue(query.extension);
if (value) {
if (context.extensionPath === undefined) {
return;
}
_mdFileUri = vscode.Uri.file(
path.join(context.extensionPath, value)
);
}
} else if (query.workspace) {
const value = utils.getValue(query.workspace);
if (value) {
if (vscode.workspace.workspaceFolders) {
var workspace = vscode.workspace.workspaceFolders[0];
let rootPath = workspace.uri.fsPath;
_mdFileUri = vscode.Uri.file(path.join(rootPath, value));
}
}
} else if (query.https) {
const value = utils.getValue(query.https);
if (value) {
_mdFileUri = vscode.Uri.parse(`https://${value}`);
}
} else if (query.http) {
const value = utils.getValue(query.http);
if (value) {
_mdFileUri = vscode.Uri.parse(`http://${value}`);
}
} else if (uri.fsPath) {
_mdFileUri = uri;
}
}
console.log(`--Retrieved file URI ${_mdFileUri}`);
sendTextToOutputChannel(`--Retrieved file URI ${_mdFileUri}`);
DidactWebviewPanel.createOrShow(context.extensionPath);
DidactWebviewPanel.setContext(context);
if (DidactWebviewPanel.currentPanel && _mdFileUri) {
DidactWebviewPanel.currentPanel.setMDPath(_mdFileUri);
}
}
// very basic requirements testing -- check to see if the results of a command executed at CLI returns a known result
// example: testCommand = mvn --version, testResult = 'Apache Maven'
export async function requirementCheck(requirement: string, testCommand: string, testResult: string) : Promise<boolean> {
try {
sendTextToOutputChannel(`Validating requirement ${testCommand} exists in VS Code workbench`);
let result = child_process.execSync(testCommand);
if (result.includes(testResult)) {
sendTextToOutputChannel(`--Requirement ${testCommand} exists in VS Code workbench: true`);
postRequirementsResponseMessage(requirement, true);
return true;
} else {
sendTextToOutputChannel(`--Requirement ${testCommand} exists in VS Code workbench: false`);
postRequirementsResponseMessage(requirement, false);
return false;
}
} catch (error) {
sendTextToOutputChannel(`--Requirement ${testCommand} exists in VS Code workbench: false`);
postRequirementsResponseMessage(requirement, false);
}
return false;
}
// even more basic CLI check - tests to see if CLI command returns zero meaning it executed successfully
export async function cliExecutionCheck(requirement: string, testCommand: string) : Promise<boolean> {
try {
sendTextToOutputChannel(`Validating requirement ${testCommand} exists in VS Code workbench`);
let result = child_process.execSync(testCommand).toString();
if (result) {
sendTextToOutputChannel(`--CLI command ${testCommand} returned code 0 and result ${result}`);
postRequirementsResponseMessage(requirement, true);
return true;
}
} catch (error) {
sendTextToOutputChannel(`--CLI command ${testCommand} failed with error ${error.status}`);
postRequirementsResponseMessage(requirement, false);
}
return false;
}
// very basic requirements testing -- check to see if the extension Id is installed in the user workspace
export async function extensionCheck(requirement: string, extensionId: string) : Promise<boolean> {
sendTextToOutputChannel(`Validating extension ${extensionId} exists in VS Code workbench`);
let testExt = vscode.extensions.getExtension(extensionId);
if (testExt) {
sendTextToOutputChannel(`--Extension ${extensionId} exists in VS Code workbench: true`);
postRequirementsResponseMessage(requirement, true);
return true;
} else {
sendTextToOutputChannel(`--Extension ${extensionId} exists in VS Code workbench: false`);
postRequirementsResponseMessage(requirement, false);
return false;
}
}
// very basic test -- check to see if the workspace has at least one root folder
export async function validWorkspaceCheck(requirement: string) : Promise<boolean> {
sendTextToOutputChannel(`Validating workspace has at least one root folder`);
let wsPath = utils.getWorkspacePath();
if (wsPath) {
sendTextToOutputChannel(`--Workspace has at least one root folder: true`);
postRequirementsResponseMessage(requirement, true);
return true;
} else {
sendTextToOutputChannel(`--Workspace has at least one root folder: false`);
postRequirementsResponseMessage(requirement, false);
return false;
}
}
// dispose of and reload the didact window with the latest Uri
export async function reloadDidact() {
sendTextToOutputChannel(`Reloading Didact window`);
if (DidactWebviewPanel.currentPanel) {
DidactWebviewPanel.currentPanel.dispose();
}
await vscode.commands.executeCommand(START_DIDACT_COMMAND, _mdFileUri);
}
// send a message back to the webview - used for requirements testing mostly
function postRequirementsResponseMessage(requirement: string, booleanResponse: boolean) {
if (requirement && DidactWebviewPanel.currentPanel) {
DidactWebviewPanel.postRequirementsResponseMessage(requirement, booleanResponse);
}
}
function showFileUnavailable(error : any) {
if (_mdFileUri) {
vscode.window.showErrorMessage(`File at ${_mdFileUri.toString()} is unavailable`);
}
console.log(error);
}
// retrieve the markdown content to render as HTML
export async function getWebviewContent() : Promise<string|void> {
if (!_mdFileUri) {
const configuredUri : string | undefined = vscode.workspace.getConfiguration().get('didact.defaultUrl');
if (configuredUri) {
_mdFileUri = vscode.Uri.parse(configuredUri);
}
}
if (_mdFileUri) {
if (_mdFileUri.scheme === 'file') {
return await getDataFromFile(_mdFileUri).catch( (error) => {
showFileUnavailable(error);
});
} else if (_mdFileUri.scheme === 'http' || _mdFileUri.scheme === 'https'){
const urlToFetch = _mdFileUri.toString();
return await getDataFromUrl(urlToFetch).catch( (error) => {
showFileUnavailable(error);
});
}
}
return undefined;
}
// retrieve markdown text from a file
async function getDataFromFile(uri:vscode.Uri) : Promise<string|undefined> {
try {
const content = fs.readFileSync(uri.fsPath, 'utf8');
const extname = path.extname(uri.fsPath);
let result : string;
if (extname.localeCompare('.adoc') === 0) {
result = parseADtoHTML(content);
return result;
} else if (extname.localeCompare('.md') === 0) {
const parser = getMDParser();
result = parser.render(content);
return result;
}
} catch (error) {
throw new Error(error);
}
}
// retrieve markdown text from a url
// TODO: figure out how to determine adoc vs. md on the fly from the url
async function getDataFromUrl(url:string) : Promise<string> {
try {
const response = await fetch(url);
const content = await response.text();
const parser = getMDParser();
const result = parser.render(content);
return result;
} catch (error) {
throw new Error(error);
}
}
export function validateAllRequirements() {
if (DidactWebviewPanel.currentPanel) {
sendTextToOutputChannel(`Validating all requirements specified in Didact tutorial`);
DidactWebviewPanel.postTestAllRequirementsMessage();
}
}
const commandPrefix = 'didact://?commandId';
function collectElements(tagname: string) : any[] {
var elements:any[] = [];
if (DidactWebviewPanel.currentPanel) {
let html : string | undefined = DidactWebviewPanel.currentPanel.getCurrentHTML();
if (html) {
var document = new DOMParser().parseFromString(html, 'text/html');
var links = document.getElementsByTagName(tagname);
for (let index = 0; index < links.length; index++) {
const element = links[index];
elements.push(element);
}
}
}
return elements;
}
const requirementCommandLinks = [
'didact://?commandId=vscode.didact.extensionRequirementCheck',
'didact://?commandId=vscode.didact.requirementCheck',
'didact://?commandId=vscode.didact.workspaceFolderExistsCheck'
];
export function gatherAllRequirementsLinks() : any[] {
var requirements = [];
if (DidactWebviewPanel.currentPanel) {
var links = collectElements("a");
for (let index = 0; index < links.length; index++) {
const element = links[index];
if (element.getAttribute('href')) {
const href = element.getAttribute('href');
for(let check of requirementCommandLinks) {
if (href.startsWith(check)) {
requirements.push(href);
break;
}
}
}
}
}
return requirements;
}
export function gatherAllCommandsLinks() {
var commandLinks = [];
if (DidactWebviewPanel.currentPanel) {
var links = collectElements("a");
for (let index = 0; index < links.length; index++) {
const element = links[index];
if (element.getAttribute('href')) {
const href = element.getAttribute('href');
if (href.startsWith(commandPrefix)) {
commandLinks.push(href);
}
}
}
}
return commandLinks;
}
export async function openTutorialFromView(node: TreeNode) : Promise<void> {
if (node && node.uri) {
sendTextToOutputChannel(`Opening tutorial from Didact view (${node.uri})`);
let vsUri = vscode.Uri.parse(node.uri);
await startDidact(vsUri);
}
}
export async function registerTutorial(name : string, sourceUri : string, category : string) : Promise<void> {
return new Promise<void>( (resolve, reject) => {
sendTextToOutputChannel(`Registering Didact tutorial with name (${name}), category (${category}, and sourceUri (${sourceUri})`);
utils.registerTutorial(name, sourceUri, category).then( () => {
resolve();
return;
}).catch ( (error) => {
reject(error);
return;
});
});
}
export async function sendTextToOutputChannel(msg: string, channel?: vscode.OutputChannel) : Promise<void> {
// set up the didact output channel if it's not set up
if (!didactOutputChannel) {
didactOutputChannel = vscode.window.createOutputChannel(DIDACT_OUTPUT_CHANNEL);
}
if (!channel && didactOutputChannel) {
channel = didactOutputChannel;
}
if (channel) {
if (!msg.endsWith('\n')) {
msg = `${msg} \n`;
}
channel.append(msg);
} else {
console.log('[' + msg + ']');
}
}
}