This repository was archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathextensionFunctions.ts
More file actions
976 lines (899 loc) · 32.6 KB
/
Copy pathextensionFunctions.ts
File metadata and controls
976 lines (899 loc) · 32.6 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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/**
* 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 fs from 'fs';
import * as querystring from 'querystring';
import * as path from 'path';
import * as child_process from 'child_process';
import {parseMDtoHTML} from './markdownUtils';
import {parseADtoHTML} from './asciidocUtils';
import * as scaffoldUtils from './scaffoldUtils';
import { TreeNode } from './nodeProvider';
import { handleExtFilePath, handleProjectFilePath } from './commandHandler';
import * as download from 'download';
import { didactManager } from './didactManager';
import { parse } from 'node-html-parser';
import { delay, DIDACT_DEFAULT_URL, getCachedOutputChannel, getCurrentFileSelectionPath, getValue, getWorkspacePath, registerTutorialWithCategory, rememberOutputChannel } from './utils';
const tmp = require('tmp');
const fetch = require('node-fetch');
const url = require('url-parse');
const EDITOR_OPENED_TIMEOUT = 3000;
// 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 OPEN_NAMED_OUTPUTCHANNEL_COMMAND = 'vscode.didact.openNamedOutputChannel';
export const SEND_TO_NAMED_OUTPUTCHANNEL_COMMAND = 'vscode.didact.sendTextToNamedOutputChannel';
export const VALIDATE_COMMAND_IDS = 'vscode.didact.verifyCommands';
export const TEXT_TO_CLIPBOARD_COMMAND = 'vscode.didact.copyToClipboardCommand';
export const COPY_FILE_URL_TO_WORKSPACE_COMMAND = 'vscode.didact.copyFileURLtoWorkspaceCommand';
export const DIDACT_OUTPUT_CHANNEL = 'Didact Activity';
export const FILE_TO_CLIPBOARD_COMMAND = 'vscode.didact.copyFileTextToClipboardCommand';
export const PASTE_TO_ACTIVE_EDITOR_COMMAND = 'vscode.didact.copyClipboardToActiveTextEditor';
export const PASTE_TO_EDITOR_FOR_FILE_COMMAND = 'vscode.didact.copyClipboardToEditorForFile';
export const PASTE_TO_NEW_FILE_COMMAND = 'vscode.didact.copyClipboardToNewFile';
export const REFRESH_DIDACT = 'vscode.didact.refresh';
export const EXTENSION_ID = "redhat.vscode-didact";
const commandPrefix = 'didact://?commandId';
// note that this MUST be also updated in the main.js file
const requirementCommandLinks = [
'didact://?commandId=vscode.didact.extensionRequirementCheck',
'didact://?commandId=vscode.didact.requirementCheck',
'didact://?commandId=vscode.didact.workspaceFolderExistsCheck',
'didact://?commandId=vscode.didact.cliCommandSuccessful'
];
// stashed extension context
let extContext : vscode.ExtensionContext;
export let didactOutputChannel: vscode.OutputChannel;
// stashed Didact URI
let _didactFileUri : vscode.Uri | undefined = undefined;
// exposed for testing purposes
export function getContext() : vscode.ExtensionContext {
return extContext;
}
export function initialize(inContext: vscode.ExtensionContext): void {
extContext = inContext;
// set up the didact output channel
didactOutputChannel = vscode.window.createOutputChannel(DIDACT_OUTPUT_CHANNEL);
}
// use the json to model the folder/file structure to be created in the vscode workspace
export async function scaffoldProjectFromJson(jsonpath:vscode.Uri): Promise<void> {
sendTextToOutputChannel(`Scaffolding project from json: ${jsonpath}`);
if (getWorkspacePath()) {
let testJson : any;
if (jsonpath) {
const jsoncontent = fs.readFileSync(jsonpath.fsPath, 'utf8');
testJson = JSON.parse(jsoncontent);
} 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): Promise<void> {
sendTextToOutputChannel(`Creating temporary folder as workspace root`);
// if the workspace is empty, we will create a temporary one for the user
const tmpobj = tmp.dirSync();
const rootUri : vscode.Uri = vscode.Uri.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[]): Promise<void>{ //name:string, path: vscode.Uri) {
let name : string | undefined = undefined;
let uri : vscode.Uri | undefined = undefined;
if (rest) {
try {
for(const 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) {
const oldTerm = findTerminal(name);
if (oldTerm) {
oldTerm.show();
return;
}
}
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();
}
}
export async function showAndSendText(terminal: vscode.Terminal, text:string): Promise<void> {
if (terminal) {
terminal.show();
terminal.sendText(text);
}
}
export async function showAndSendCtrlC(terminal: vscode.Terminal): Promise<void>{
if (terminal) {
terminal.show();
await vscode.commands.executeCommand("workbench.action.terminal.sendSequence", { text : "\x03" });
}
}
export async function killTerminal(terminal: vscode.Terminal): Promise<void>{
if (terminal) {
terminal.show();
await vscode.commands.executeCommand("workbench.action.terminal.kill");
}
}
export function findTerminal(name: string) : vscode.Terminal | undefined {
try {
for(const 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): Promise<void> {
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): Promise<void> {
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): Promise<void>{
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(): Promise<void>{
sendTextToOutputChannel(`Starting Didact window with default`);
didactManager.setContext(extContext);
const configuredPath : string | undefined = vscode.workspace.getConfiguration().get(DIDACT_DEFAULT_URL);
if (configuredPath) {
_didactFileUri = vscode.Uri.parse(configuredPath);
}
if (_didactFileUri) {
await didactManager.create(_didactFileUri);
} else {
const errStr = `No default didact URL provided when opening default tutorial. Check setting to ensure path is provided.`;
sendTextToOutputChannel(errStr);
vscode.window.showErrorMessage(errStr);
}
}
function processExtensionFilePath(value: string | undefined) : vscode.Uri | undefined {
if (value) {
const extUri = handleExtFilePath(value);
if (extUri) {
return extUri;
} else if (extContext.extensionPath === undefined) {
return undefined;
}
return vscode.Uri.file(
path.resolve(extContext.extensionPath, value)
);
}
return undefined;
}
export function handleVSCodeDidactUriParsingForPath(uri:vscode.Uri) : vscode.Uri | undefined {
let out : vscode.Uri | undefined = undefined;
// handle extension/extFilePath, workspace, https, and http
if (uri) {
const query = querystring.parse(uri.query);
if (query.extension) {
const value = getValue(query.extension);
out = processExtensionFilePath(value);
} else if (query.extFilePath) {
const value = getValue(query.extFilePath);
out = processExtensionFilePath(value);
} else if (query.workspace) {
const value = getValue(query.workspace);
if (value) {
if (vscode.workspace.workspaceFolders) {
const workspace = vscode.workspace.workspaceFolders[0];
const rootPath = workspace.uri.fsPath;
out = vscode.Uri.file(path.resolve(rootPath, value));
}
}
} else if (query.https) {
const value = getValue(query.https);
if (value) {
out = vscode.Uri.parse(`https://${value}`);
}
} else if (query.http) {
const value = getValue(query.http);
if (value) {
out = vscode.Uri.parse(`http://${value}`);
}
} else if (uri.fsPath) {
out = uri;
} else {
out = vscode.Uri.parse(uri.toString());
}
}
return out;
}
// open the didact window with the didact file passed in via Uri
export async function startDidact(uri: vscode.Uri, viewColumn?: string): Promise<void>{
if (!uri) {
uri = await getCurrentFileSelectionPath();
}
// if column passed, convert to viewcolumn enum
let actualColumn : vscode.ViewColumn = vscode.ViewColumn.Active;
if (viewColumn) {
actualColumn = (<any>vscode.ViewColumn)[viewColumn];
}
sendTextToOutputChannel(`Starting Didact window with ${uri}`);
const out : vscode.Uri | undefined = handleVSCodeDidactUriParsingForPath(uri);
if (!out) {
const errmsg = `--Error: No Didact file found when parsing URI ${uri}`;
sendTextToOutputChannel(errmsg);
vscode.window.showErrorMessage(errmsg);
return;
} else {
_didactFileUri = out;
}
console.log(`--Retrieved file URI ${_didactFileUri}`);
sendTextToOutputChannel(`--Retrieved file URI ${_didactFileUri}`);
didactManager.setContext(extContext);
await didactManager.create(_didactFileUri, actualColumn);
}
// 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`);
const 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`);
const options = {
timeout: 15000 // adding timeout for network calls
};
const result = child_process.execSync(testCommand, options);
if (result) {
sendTextToOutputChannel(`--CLI command ${testCommand} returned code 0 and result ${result.toString()}`);
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`);
const 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`);
const wsPath = 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(): Promise<void>{
sendTextToOutputChannel(`Reloading Didact window`);
didactManager.active()?.dispose();
await vscode.commands.executeCommand(START_DIDACT_COMMAND, _didactFileUri);
}
export async function refreshDidactWindow(): Promise<void>{
sendTextToOutputChannel(`Refreshing Didact window`);
await didactManager.active()?.refreshPanel();
}
// send a message back to the webview - used for requirements testing mostly
function postRequirementsResponseMessage(requirement: string, booleanResponse: boolean): void {
if (requirement) {
didactManager.active()?.postRequirementsResponseMessage(requirement, booleanResponse);
}
}
function showFileUnavailable(error : any): void {
if (_didactFileUri) {
vscode.window.showErrorMessage(`File at ${_didactFileUri.toString()} is unavailable`);
}
console.log(error);
}
// retrieve the didact content to render as HTML
export async function getWebviewContent() : Promise<string|void> {
if (!_didactFileUri) {
const configuredUri : string | undefined = vscode.workspace.getConfiguration().get('didact.defaultUrl');
if (configuredUri) {
_didactFileUri = vscode.Uri.parse(configuredUri);
}
}
if (_didactFileUri) {
if (_didactFileUri.scheme === 'file') {
return loadFileWithRetry(_didactFileUri);
} else if (_didactFileUri.scheme === 'http' || _didactFileUri.scheme === 'https'){
return loadFileFromHTTPWithRetry(_didactFileUri);
}
}
return undefined;
}
async function loadFileWithRetry ( uri:vscode.Uri ) : Promise<string | void | undefined> {
return getDataFromFile(uri).catch( async () => {
await delay(3000);
return getDataFromFile(uri).catch( async (error) => {
showFileUnavailable(error);
});
});
}
async function loadFileFromHTTPWithRetry ( uri:vscode.Uri ) : Promise<string | void | undefined> {
const urlToFetch = uri.toString();
return getDataFromUrl(urlToFetch).catch( async () => {
await delay(3000);
return getDataFromUrl(urlToFetch).catch( async (error) => {
showFileUnavailable(error);
});
});
}
export function isAsciiDoc() : boolean {
let uriToTest : vscode.Uri | undefined;
if (!_didactFileUri) {
const strToTest : string | undefined = vscode.workspace.getConfiguration().get('didact.defaultUrl');
if (strToTest) {
uriToTest = vscode.Uri.parse(strToTest);
}
} else {
uriToTest = _didactFileUri;
}
if (uriToTest && uriToTest.fsPath) {
const extname = path.extname(uriToTest.fsPath);
if (extname.localeCompare('.adoc') === 0) {
return true;
}
}
return false;
}
// retrieve didact text from a file - exported for test
export async function getDataFromFile(uri:vscode.Uri) : Promise<string|undefined> {
try {
const content = fs.readFileSync(uri.fsPath, 'utf8');
const extname = path.extname(uri.fsPath);
if (extname.localeCompare('.adoc') === 0) {
let baseDir : string | undefined = undefined;
if (uri.scheme.trim().startsWith('file')) {
baseDir = path.dirname(uri.fsPath);
}
return parseADtoHTML(content, baseDir);
} else if (extname.localeCompare('.md') === 0) {
return parseMDtoHTML(content);
} else {
throw new Error(`Unknown file type encountered: ${extname}`);
}
} catch (error) {
throw new Error(error);
}
}
// retrieve didact text from a url
export async function getDataFromUrl(inurl:string) : Promise<string> {
try {
const response = await fetch(inurl);
const content = await response.text();
const tempVSUri = vscode.Uri.parse(inurl);
const extname = path.extname(tempVSUri.fsPath);
if (extname.localeCompare('.adoc') === 0) {
return parseADtoHTML(content);
} else if (extname.localeCompare('.md') === 0) {
return parseMDtoHTML(content);
} else {
throw new Error(`Unknown file type encountered: ${extname}`);
}
} catch (error) {
throw new Error(error);
}
}
export function validateAllRequirements(): void {
sendTextToOutputChannel(`Validating all requirements specified in Didact tutorial`);
didactManager.active()?.postTestAllRequirementsMessage();
}
export function collectElements(tagname: string, html? : string | undefined) : any[] {
const elements: any[] = [];
if (!html) {
html = didactManager.active()?.getCurrentHTML();
}
if (html) {
const document = parse(html);
const links = document.querySelectorAll(tagname);
for (let element of links.values()) {
elements.push(element);
}
}
return elements;
}
export function gatherAllRequirementsLinks() : any[] {
const requirements = [];
if (didactManager.active()?.getCurrentHTML()) {
const links = collectElements("a", didactManager.active()?.getCurrentHTML());
for (let element of links.values()) {
if (element.getAttribute('href')) {
const href = element.getAttribute('href');
for(const check of requirementCommandLinks) {
if (href.startsWith(check)) {
requirements.push(href);
break;
}
}
}
}
}
return requirements;
}
export function gatherAllCommandsLinks(): any[] {
const commandLinks = [];
if (didactManager.active()?.getCurrentHTML()) {
const links = collectElements("a", didactManager.active()?.getCurrentHTML());
for (let element of links.values()) {
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})`);
const vsUri = vscode.Uri.parse(node.uri);
await startDidact(vsUri);
}
}
export async function registerTutorial(name : string, sourceUri : string, category : string) : Promise<void> {
sendTextToOutputChannel(`Registering Didact tutorial with name (${name}), category (${category}, and sourceUri (${sourceUri})`);
await registerTutorialWithCategory(name, sourceUri, category);
}
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 + ']');
}
}
async function openDidactOutputChannel() : Promise<void> {
if (!didactOutputChannel) {
didactOutputChannel = vscode.window.createOutputChannel(DIDACT_OUTPUT_CHANNEL);
}
didactOutputChannel.show();
}
// exported for testing
export async function validateDidactCommands(commands : any[], sendToConsole = false) : Promise<boolean> {
let allOk = true;
if (commands && commands.length > 0) {
sendTextToOutputChannel(`Starting validation...`);
const vsCommands : string[] = await vscode.commands.getCommands(true);
for(const command of commands) {
// validate all commands we found
const parsedUrl = new url(command, true);
const query = parsedUrl.query;
if (query.commandId) {
const commandId = getValue(query.commandId);
if (commandId) {
const foundCommand = validateCommand(commandId, vsCommands);
if (!foundCommand) {
// unexpected result - let the user know
const msg = `--Missing Command ID ${commandId}.`;
if (sendToConsole) {
console.log(msg);
} else {
await sendTextToOutputChannel(msg);
}
allOk = false;
}
}
}
}
}
return allOk;
}
// exported for testing
export function validateCommand(commandId:string, vsCommands:string[]) : boolean {
if (commandId) {
const foundCommand : string | undefined = vsCommands.find( function (command) {
return command === commandId;
});
if (foundCommand) {
return true;
}
}
return false;
}
export async function validateCommandIDsInSelectedFile(didactUri: vscode.Uri) : Promise<void> {
if (didactUri) {
await vscode.commands.executeCommand(START_DIDACT_COMMAND, didactUri);
}
const commands: any[] = gatherAllCommandsLinks();
let allOk = false;
await openDidactOutputChannel();
if (commands && commands.length > 0) {
allOk = await validateDidactCommands(commands);
if (allOk) {
sendTextToOutputChannel(`--Command IDs: OK`);
sendTextToOutputChannel(`Validation Result: SUCCESS (${commands.length} Commands Validated)`);
} else {
sendTextToOutputChannel(`Validation Result: FAILURE`);
sendTextToOutputChannel(`-- Note that command IDs not found may be due to a missing extension or simply an invalid ID.`);
}
} else {
sendTextToOutputChannel(`Validation Result: FAILURE - No command IDs found in current Didact file`);
}
}
export async function placeTextOnClipboard(clipText: string): Promise<void>{
await vscode.env.clipboard.writeText(clipText).then( () => {
sendTextToOutputChannel(`Text sent to clipboard: "${clipText}"`);
});
}
// exported for testing
export async function downloadAndUnzipFile(httpFileUrl : string, installFolder : string, dlFilename? : string, extractFlag = false, ignoreOverwrite = false): Promise<any> {
let filename = '';
if (!dlFilename) {
const fileUrl = new url(httpFileUrl);
const pathname = fileUrl.pathname;
if (pathname) {
filename = pathname.substring(pathname.lastIndexOf('/')+1);
}
} else {
filename = dlFilename;
}
const downloadFile : string = path.resolve(installFolder, filename);
let overwriteFile = false;
if (extractFlag && !ignoreOverwrite) {
const answer = await vscode.window.showQuickPick([
'Yes',
'No'
], {
canPickMany: false,
placeHolder: `The archive ${filename} may overwrite folders and files in the workspace. Are you sure?`
});
if (answer === 'No') {
sendTextToOutputChannel(`Copy and unzip of file ${filename} was canceled.`);
return null;
}
if (answer === 'Yes') {
overwriteFile = true;
}
} else if (!extractFlag && !ignoreOverwrite) {
try {
const pathUri = vscode.Uri.file(downloadFile);
try {
const contents = await vscode.workspace.fs.readFile(pathUri);
if (contents) {
const answer = await vscode.window.showQuickPick([
'Yes',
'No'
], {
canPickMany: false,
placeHolder: `The file ${filename} already exists. Do you want to overwrite it?`
});
if (answer === 'No') {
sendTextToOutputChannel(`Copy of file ${filename} was canceled.`);
return null;
}
if (answer === 'Yes') {
overwriteFile = true;
}
} else {
overwriteFile = true; // file doesn't exist
}
} catch (error) {
// ignore error, it means the file does not exist
overwriteFile = true;
}
} catch (error) {
// ignore error, it means the file does not exist
overwriteFile = true;
}
}
if (overwriteFile || ignoreOverwrite) {
try {
const downloadResult: boolean = await downloadAndExtract(httpFileUrl, installFolder, filename, extractFlag);
console.log(`Downloaded ${downloadFile} : ${downloadResult}`);
sendTextToOutputChannel(`Downloaded ${downloadFile}`);
return downloadFile;
} catch ( error ) {
console.log(error);
sendTextToOutputChannel(`Failed to download file: ${error}`);
return error;
}
}
}
export async function copyFileFromURLtoLocalURI(httpurl : any, fileName? : string, fileuri? : string, unzip = false): Promise<void>{
let projectFilePath = '';
if (fileuri && fileuri.length > 0) {
projectFilePath = fileuri.trim();
}
let dlFileName = '';
if (fileName) {
dlFileName = fileName;
}
const filepathUri = handleProjectFilePath(projectFilePath);
if (filepathUri) {
await downloadAndUnzipFile(httpurl, filepathUri.fsPath, dlFileName, unzip);
}
}
async function downloadAndExtract(link : string, installFolder : string, dlFilename? : string, extractFlag?: boolean) : Promise<boolean> {
const myStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
let downloadSettings;
if (dlFilename) {
downloadSettings = {
filename: `${dlFilename}`,
extract: extractFlag
};
} else {
downloadSettings = {
extract: extractFlag
};
}
sendTextToOutputChannel('Downloading from: ' + link);
await download(link, installFolder, downloadSettings)
.on('response', (response) => {
sendTextToOutputChannel(`Bytes to transfer: ${response.headers['content-length']}`);
}).on('downloadProgress', (progress) => {
const incr = progress.total > 0 ? Math.floor(progress.transferred / progress.total * 100) : 0;
const percent = Math.round(incr);
const message = `Download progress: ${progress.transferred} / ${progress.total} (${percent}%)`;
const tooltip = `Download progress for ${installFolder}`;
updateStatusBarItem(myStatusBarItem, message, tooltip);
}).then(async () => {
myStatusBarItem.dispose();
return true;
}).catch((error) => {
console.log(error);
});
myStatusBarItem.dispose();
return false;
}
function updateStatusBarItem(sbItem : vscode.StatusBarItem, text: string, tooltip : string): void {
if (text) {
sbItem.text = text;
sbItem.tooltip = tooltip;
sbItem.show();
} else {
sbItem.hide();
}
}
export function openNamedOutputChannel(name?: string | undefined): vscode.OutputChannel | undefined {
let channel: vscode.OutputChannel | undefined;
if (!name || name === DIDACT_OUTPUT_CHANNEL) {
if (!didactOutputChannel) {
didactOutputChannel = vscode.window.createOutputChannel(DIDACT_OUTPUT_CHANNEL);
}
channel = didactOutputChannel;
} else {
channel = getCachedOutputChannel(name);
if (!channel) {
channel = vscode.window.createOutputChannel(name);
rememberOutputChannel(channel);
}
}
if (channel) {
channel.show();
}
return channel;
}
export function sendTextToNamedOutputChannel(message: string, channelName?: string): void {
if (!message){
throw new Error('There was no text given for the output channel.');
}
let channel: vscode.OutputChannel | undefined = didactOutputChannel;
if (channelName) {
channel = openNamedOutputChannel(channelName);
}
sendTextToOutputChannel(message, channel);
if (!channelName || channelName === DIDACT_OUTPUT_CHANNEL) {
didactOutputChannel.show();
}
}
function showErrorMessage(msg : string) : void {
sendTextToOutputChannel(msg);
vscode.window.showErrorMessage(msg);
}
export async function copyFileTextToClipboard(uri: vscode.Uri) : Promise<void> {
const testUri : vscode.Uri = uri;
const out : vscode.Uri | undefined = handleVSCodeDidactUriParsingForPath(testUri);
if (!out) {
const errmsg = `ERROR: No file found when parsing path ${uri}`;
showErrorMessage(errmsg);
throw new Error(errmsg);
} else {
let content : string | undefined = undefined;
if (out.scheme === 'file') {
try {
content = fs.readFileSync(out.fsPath, 'utf8');
} catch(error) {
showFileUnavailable(error);
}
} else if (out.scheme === 'http' || out.scheme === 'https'){
const urlToFetch = out.toString();
try {
const response = await fetch(urlToFetch);
content = await response.text();
} catch(error) {
showFileUnavailable(error);
}
} else {
const errmsg = `ERROR: Unsupported scheme/protocol when parsing path ${uri}`;
showErrorMessage(errmsg);
throw new Error(errmsg);
}
if (content) {
await placeTextOnClipboard(content);
}
}
}
export async function pasteClipboardToActiveEditorOrPreviouslyUsedOne() : Promise<void> {
let currentEditor : vscode.TextEditor | undefined = vscode.window.activeTextEditor;
if (!currentEditor) {
await vscode.commands.executeCommand('workbench.action.openPreviousRecentlyUsedEditor');
currentEditor = vscode.window.activeTextEditor;
}
if (currentEditor) {
const textFromClipboard = await vscode.env.clipboard.readText();
if (textFromClipboard) {
await currentEditor.insertSnippet(new vscode.SnippetString(textFromClipboard));
} else {
await vscode.window.showWarningMessage(`No text found on clipboard`);
}
} else {
await vscode.window.showWarningMessage(`No active text editor found to paste from clipboard`);
}
}
export async function findOpenEditorForFileURI(uri: vscode.Uri) : Promise<vscode.TextEditor | undefined> {
for (let editor of vscode.window.visibleTextEditors.values()) {
if (editor.document.uri === uri) {
return editor;
}
}
return undefined;
}
export async function pasteClipboardToEditorForFile(uri: vscode.Uri) : Promise<void> {
let editorForFile : vscode.TextEditor | undefined = await findOpenEditorForFileURI(uri);
if (!editorForFile) {
try {
await vscode.commands.executeCommand('vscode.open', uri, vscode.ViewColumn.Beside);
editorForFile = vscode.window.activeTextEditor;
} catch (error) {
await vscode.window.showWarningMessage(`No editor found for file ${uri.fsPath}. ${error}`);
console.log(error);
}
}
if (editorForFile) {
await pasteClipboardToActiveEditorOrPreviouslyUsedOne();
}
}
export async function pasteClipboardToNewTextFile() : Promise<void> {
await vscode.commands.executeCommand('workbench.action.files.newUntitledFile');
await pasteClipboardToActiveEditorOrPreviouslyUsedOne();
}
export async function setDidactFileUri(newFileUri : vscode.Uri | undefined) {
_didactFileUri = newFileUri;
}