-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
1283 lines (941 loc) · 41 KB
/
extension.js
File metadata and controls
1283 lines (941 loc) · 41 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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const spawn = require('child_process').spawn;
const fs = require('fs');
const path = require('path');
const vscode = require('vscode');
const chokidar = require('chokidar');
const { LanguageClient } = require('vscode-languageclient/node');
const {Cscope,queryCscope,vscodeQuery} = require('./Tools/cscope');
const Client = require('ssh2-sftp-client')
// const FileExplorer = require('./Tools/Fileprovider').AixExplorerProvider; //Explorer to manage the files openes from the remote.
const AixFSProvider = require('./Tools/Fileprovider').AixFSProvider; //FileManager that reads/write the file from th remote.
const hash_path = require('./Tools/Fileprovider').hash_path;
// Handling downtime and restart the servers
class ServerReload {
constructor(context, restartFunc) {
this.context = context;
this.restartFunc = restartFunc;
// load servers from state
const stored = context.globalState.get("connectedServers") || [];
this.servers = stored.map(obj => Server.fromJSON(obj));
}
getAll() {
return this.servers;
}
async restartAll() {
for (const server of this.servers) {
const value = `${server.AIX_USER}@${server.AIX_HOST}:`;
try {
await this.restartFunc(this.context, value, server);
console.log(`Restarted server ${value}`);
} catch (err) {
console.error(`Failed to restart ${value}`, err);
}
}
}
async add(server) {
this.servers.push(server);
await this.context.globalState.update(
"connectedServers",
this.servers.map(s => s.toJSON())
);
}
async remove(server) {
this.servers = this.servers.filter(
s => !(s.AIX_USER === server.AIX_USER && s.AIX_HOST === server.AIX_HOST)
);
await this.context.globalState.update(
"connectedServers",
this.servers.map(s => s.toJSON())
);
}
}
// Global map to store all server instances
const Servers = new Map(); // Map to map hostip id to the Server class
const Listeners = new Map(); // Map tp store the pythonlistener that I have created for each of the terminal to stop them at last
const uriLocaltoRemote = new Map(); // Map to store the local uri to remote uri.
const cclsclients = new Map(); // Map to store the ccls clients for each proj.
// common to all servers
let LOCAL_SYNC_DIR = ''; // My local dir where I am keeping the command
let mount_dir = ''; // My mount_Dir where all these servers dir will be placed.
let watcherScript = ''; // Watcherscript python which do the checking on command.txt
let safeCode =''; // code to be pasted on the remote to make this code command work.
// let Explorer = null;
class Server
{
constructor(LOCAL_COMMAND_FILE,AIX_HOST,AIX_USER,TEMP_DIR,Provider)
{
this.LOCAL_COMMAND_FILE = LOCAL_COMMAND_FILE;
this.AIX_HOST = AIX_HOST;
this.AIX_USER = AIX_USER;
this.TEMP_DIR = TEMP_DIR;
this.rsync_path = 'opt/freeware/bin/rsync';
this.sed_path = '';
this.ccls_path = '';
this.osname = '';
this.rtport = 0;
this.localtoRemote = new Map();
this._lastModified = new Map();
this._lastModified_size = new Map();
this.sftp = new Client();
this.FS_Provider = Provider;
this.localfoldertoRemote = new Map(); // Map to store the local folder to remote path files.
}
updateLocalToRemote(remotePath,fileName)
{
this.localtoRemote.set(fileName,remotePath);
fs.writeFileSync(path.join(this.TEMP_DIR,`${this.AIX_HOST}_files.json`),JSON.stringify(this.localtoRemote));
}
// SetPort(port)
// {
// this.port = port;
// }
CreatingTempDir()
{
fs.mkdirSync(this.TEMP_DIR, { recursive: true });
}
async Setkeypath(keyPath)
{
this.keyPath = keyPath;
try {
await this.sftp.connect({
host: this.AIX_HOST,
username: this.AIX_USER,
privateKey: fs.readFileSync(this.keyPath)
});
vscode.window.showInformationMessage("SFTP connected successfully");
} catch (err) {
vscode.window.showErrorMessage(`SFTP connect failed: ${err.message}`);
throw err;
}
}
// // --- NEW: serialize/deserialize helpers ---
toJSON() {
return {
LOCAL_COMMAND_FILE: this.LOCAL_COMMAND_FILE,
AIX_HOST: this.AIX_HOST,
AIX_USER: this.AIX_USER,
TEMP_DIR: this.TEMP_DIR,
// port: this.port,
keyPath: this.keyPath,
localtoRemote: [...this.localtoRemote]
};
}
static fromJSON(obj) {
const s = new Server(obj.LOCAL_COMMAND_FILE, obj.AIX_HOST, obj.AIX_USER, obj.TEMP_DIR);
// s.port = obj.port;
s.keyPath = obj.keyPath;
s.localtoRemote = new Map(obj.localtoRemote || []);
return s;
}
}
function activate(context) {
let activeWatcher = null;
const Explorer_watcher = vscode.workspace.createFileSystemWatcher("**/*"); //for when some local files are deleted
// //Defining the custom file explorer to be run for the AIX
// Explorer = new FileExplorer(getServer,AllServers);
// vscode.window.createTreeView('AIX-explorer', { treeDataProvider: Explorer });
// Defining the filesystem for the AIX
const provider = new AixFSProvider(AllServers); // pass your server map
// fake aix schema so that I can create fake uri for things like peek definition/peek refrences etc in c / c++ code.
const virtualProvider = {
provideTextDocumentContent: async (uri) => {
const retieve_frag = new URLSearchParams(uri.fragment);
const cscope_Dir = retieve_frag.get('cscope_rootdir');
const remotepath = retieve_frag.get('path');
vscode.window.showInformationMessage(`Fetching content for ${uri.path} from AIX server ${uri.authority}`);
const localpath = uri.path; // gettting the local path to see if the file is already there.
const local_uri = vscode.Uri.file(localpath);
if(!uriLocaltoRemote.has(local_uri.toString()))
{
let aix_uri = vscode.Uri.from({
scheme: "aix",
authority: uri.authority,
path: remotepath,
});
// Creating the fragment for the uri
const frag = new URLSearchParams({
cscope_dir: cscope_Dir,
}).toString();
aix_uri = aix_uri.with({fragment: frag});
await provider.readFile(aix_uri);
// await provider.readFile(aix_uri); // Ensure file is downloaded and stored locally
// updating the uriLocaltoRemote map
uriLocaltoRemote.set(local_uri.toString(),aix_uri);
}
vscode.workspace.openTextDocument(localpath); // Open the file in vscode
const content = fs.readFileSync(localpath, 'utf8');
return content;
}
};
context.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider("fake_aix", virtualProvider)
);
// // fucntiond for the new epxplorere
// context.subscriptions.push(
// vscode.commands.registerCommand("OpenFile",async(file)=>
// {
// await Explorer.OpenFile(file);
// }
// )
// );
// 👇 Register listener globally during activation, not inside a command
// const open_document = vscode.workspace.onDidOpenTextDocument(async (document) => {
// // if(!editor) return;
// // const document = editor.document;
// if(!document) return;
// const uri = document.uri;
// // if(uri.scheme === 'fake_aix')
// // {
// // vscode.workspace.openTextDocument(uri.path);
// // vscode.window.showTextDocument(vscode.Uri.file(uri.path), { preview: true, preserveFocus: true });
// // }
// const fragment = new URLSearchParams(uri.fragment)
// if(fragment.get('cscope_rootdir'))
// {
// const cscope_Dir = fragment.get('cscope_rootdir');
// const remote_path = fragment.get('path');
// const server = fragment.get('authority');
// let aix_uri = vscode.Uri.from({
// scheme: "aix",
// authority: server,
// path: remote_path,
// });
// // Creating the fragment for the uri
// const frag = new URLSearchParams({
// cscope_dir: cscope_Dir,
// }).toString();
// aix_uri = aix_uri.with({fragment: frag});
// await provider.readFile(aix_uri); // reading the file from the remote if not present locally.
// vscode.window.showInformationMessage("Preview or open triggered");
// const local_path = await hash_path(remote_path,Servers.get(server)); // gettting the local path to see if the file is already there.
// const local_uri = vscode.Uri.file(local_path);
// vscode.window.showInformationMessage(`URI: ${local_path}`);
// await vscode.window.showTextDocument(local_uri); //showing the local file.
// }
// });
const saving_Doc = //Saving the files which will be done by my custom fs.
vscode.workspace.onDidSaveTextDocument(async (event) => {
const local_uri = event.uri;
const remote_uri = uriLocaltoRemote.get(local_uri.toString()); // as the local_uri has changed it doesn't have that fragment hence will not be able to find it.need to resolve this.
if (!remote_uri) return; // not an AIX-mapped file
console.log("Saving: " + local_uri.fsPath + " to " + remote_uri.path);
await provider.writeFile(remote_uri, Buffer.from(event.getText()), {
create: true,
overwrite: true
});
});
const editorChangeDisposable = vscode.window.onDidChangeActiveTextEditor( async (editor) => {
// console.log(' editor change')
vscode.window.showInformationMessage("Active file changed");
if (activeWatcher) {
activeWatcher.dispose();
activeWatcher = null;
}
const local_uri = editor?.document.uri;
if (!local_uri) return;
const remote_uri = uriLocaltoRemote.get(local_uri.toString());
if (!remote_uri) return; // not an AIX-mapped file
vscode.window.showInformationMessage("Yes it gonna watch u!!")
activeWatcher = provider.watch(remote_uri);
});
// Setting up the command to search for the functions using cscope
const def_search = vscode.languages.registerDefinitionProvider(['cpp','c'],{
async provideDefinition(document, position, token) {
const remote_uri = uriLocaltoRemote.get(document.uri.toString());
if (!remote_uri) {
return null; // Not a remote file, skip
}
const result = await vscodeQuery(provider,remote_uri,document,position,Servers,"define"); //provider is the class that do all backend work of file seinzing and other things.
return result;
}
});
const def_refrence = vscode.languages.registerReferenceProvider(['cpp','c'],
{
async provideReferences(document, position, token) {
const remote_uri = uriLocaltoRemote.get(document.uri.toString());
if (!remote_uri) {
return null; // Not a remote file, skip
}
const result = await vscodeQuery(provider,remote_uri,document,position,Servers,"refrences");
return result;
}
}
);
//To defined it globally so that disposable can use .
const disposable = vscode.commands.registerCommand('remote-rsync.Remote-Rsync', async function () {
// Creating an reload_Manager instance
const reloadManager = new ServerReload(context, mountSSHFS);
// Restart all servers on activation
// reloadManager.restartAll().then(() => {
// console.log("All servers restarted");
// }).catch(err => {
// console.error("Failed to restart servers:", err);
// });
//defining variables here
let LOCAL_COMMAND_FILE = '';
let AIX_USER = '';
let AIX_HOST = '';
let TEMP_DIR = '';
// Check if workspace folder exists (Bob editor compatibility)
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) {
vscode.window.showErrorMessage('Please open a workspace folder first to use Remote-Rsync');
return;
}
mount_dir = vscode.workspace.workspaceFolders[0].uri.fsPath;
fs.mkdirSync(path.join(mount_dir, '.sshfs'), { recursive: true });
LOCAL_SYNC_DIR = path.join(mount_dir, '.sshfs');
const logins = loadconfig(context) || [];
let value = await new Promise((resolve) => {
const quickPick = vscode.window.createQuickPick();
quickPick.items = logins.map(login => ({ label: login }));
quickPick.placeholder = 'Select a saved login or type a new one';
quickPick.ignoreFocusOut = true;
quickPick.onDidAccept(() => {
const selectedValue =
quickPick.selectedItems.length > 0
? quickPick.selectedItems[0].label
: quickPick.value;
quickPick.hide();
quickPick.dispose();
console.log("values",selectedValue);
resolve(selectedValue ? selectedValue.trim() : undefined);
});
// quickPick.onDidHide(() => {
// quickPick.dispose();
// resolve(undefined);
// });
quickPick.show();
});
if (!value) return;
console.log("Selected login:", value);
value = value.trim(); // removing unnecessary spaces
console.log("Selected login:", value);
// Validate format: user@host
if (!value.includes('@')) {
vscode.window.showErrorMessage('Invalid format. Please use: user@host');
return;
}
[AIX_USER, AIX_HOST] = value.split('@');
// Validate both parts exist
if (!AIX_USER || !AIX_HOST) {
vscode.window.showErrorMessage('Invalid format. Both username and hostname are required');
return;
}
TEMP_DIR = path.join(mount_dir, `temp_${AIX_HOST}`);
LOCAL_COMMAND_FILE = path.join(LOCAL_SYNC_DIR, `command_${AIX_HOST}.txt`);
let Remote_server = new Server(LOCAL_COMMAND_FILE,AIX_HOST,AIX_USER,TEMP_DIR,provider);
await mountSSHFS(context, value,Remote_server); //Setting up the server
fs.writeFileSync(LOCAL_COMMAND_FILE,'');
Servers.set(AIX_HOST, Remote_server);
// Explorer.refresh();
// if (Remote_server.ccls_path !== '')
// {
// const client = new LanguageClient(
// "remote-ccls",
// "Remote CCLS",
// {
// command: "node",
// args: ["/Tools/lsp_proxy.js"],
// options: { env: process.env }
// },
// {
// documentSelector: [
// { scheme: "file", language: "cpp" },
// { scheme: "file", language: "c" }
// ]
// }
// );
// client.start();
// }
reloadManager.add(Remote_server); // For reloading of the server when refresh or server is lost
// Register the virtual file system provider for aix:
// const provider = new AixFSProvider(AIX_USER, AIX_HOST, Remote_server.keyPath,TEMP_DIR);
// context.subscriptions.push(
// vscode.workspace.registerFileSystemProvider(`aix_${AIX_HOST}`, provider, { isCaseSensitive: true })
// );
// for the case when we load the files from local temp
// vscode.workspace.onDidOpenTextDocument(async doc => {
// const filePath = doc.uri.fsPath;
// // Only intercept TEMP files, not VFS URIs
// if (filePath.startsWith(TEMP_DIR)) {
// const fileName = path.basename(filePath);
// const remotePath = Remote_server.localtoRemote.get(fileName);
// const vfsUri = vscode.Uri.parse(`aix_${AIX_HOST}:${remotePath}`);
// // Defer to let VSCode finish opening first
// setTimeout(async () => {
// // Find the editor showing this TEMP file
// const editor = vscode.window.visibleTextEditors.find(e => e.document.uri.fsPath === filePath);
// if (editor) {
// await vscode.window.showTextDocument(editor.document); // make it active
// await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
// }
// // Now open the VFS one
// const file = await vscode.workspace.openTextDocument(vfsUri);
// await vscode.window.showTextDocument(file, { preview: true, preserveFocus: false });
// }, 50);
// }
// });
//Running watch from custom AIXfs to see any updates from remote to local
// Opening the file from the local editor
// vscode.workspace.onDidOpenTextDocument((doc) => {
// const remote_uri = uriLocaltoRemote.get(doc.uri.toString());
// if (!remote_uri) return;
// const key = remote_uri.toString();
// if (!watchers.has(key)) {
// const watcher = (remote_uri);
// watchers.set(key, watcher);
// }
// });
// Closing the listener who's terminal have stopped and they have nothing to do.
vscode.window.onDidCloseTerminal((closedTerm) => {
const proc = Listeners.get(closedTerm);
if (proc) {
proc.kill(); // stop python listener
Listeners.delete(closedTerm);
}
});
// Clean up when doc is closed
// vscode.workspace.onDidCloseTextDocument((doc) => {
// const local_uri = doc.uri;
// const remote_uri = uriLocaltoRemote.get(local_uri.toString());
// const key = remote_uri.toString();
// watchers.get(key)?.dispose();
// watchers.delete(key);
// // if (doc.uri.toString() === local_uri.toString()) {
// // watchers.delete(key);
// // }
// });
// vscode.window.onDidChangeActiveTextEditor((editor) => {
// // Dispose previous watcher
// vscode.window.showInformationMessage("Active file changed");
// if (activeWatcher) {
// activeWatcher.dispose();
// activeWatcher = null;
// }
// const local_uri = editor?.document.uri;
// if (!local_uri) return;
// const remote_uri = uriLocaltoRemote.get(local_uri.toString());
// if (!remote_uri) return; // not an AIX-mapped file
// // Create a new watcher for the currently active file
// activeWatcher = provider.watch(remote_uri);
// });
watchCommandFile(Remote_server);
});
// Local files are deleted and we have to remove the cache of the aixfs
// let FS_Explorer= Explorer_watcher.onDidDelete(async uri => {
// const remote_uri = uriLocaltoRemote.has(uri.toString());
// if (remote_uri) {
// console.log("Local file deleted, clearing cache:", uri.fsPath);
// uriLocaltoRemote.delete(uri.toString());
// }
// const remote_doc = vscode.window.visibleTextEditors.find(
// editor => editor.document.uri.toString() === uri.toString()
// );
// if (remote_doc) {
// console.log("Closing deleted file:", uri.fsPath);
// await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
// }
// });
let Terminal_loader = vscode.commands.registerCommand("AIX_Terminals", async function() {
// Turn the Servers map into QuickPick items
const term_quickPickItems = Array.from(Servers.entries()).map(([host, server]) => ({
label: host, // shown in QuickPick
description: server.TEMP_DIR || '' // optional: show extra info
}));
if (term_quickPickItems.length === 0) {
vscode.window.showErrorMessage("No active servers available");
return;
}
const selected = await vscode.window.showQuickPick(term_quickPickItems, {
placeHolder: 'Select a connected server',
ignoreFocusOut: true
});
if (!selected) return;
const chosenHost = selected.label;
const chosenServer = Servers.get(chosenHost);
vscode.window.showInformationMessage(chosenHost," ");
if (!chosenServer) {
vscode.window.showErrorMessage(`No active server found for ${chosenHost}`);
return;
}
Boot(`${chosenServer.AIX_USER}@${chosenHost}`, chosenServer);
});
context.subscriptions.push(editorChangeDisposable);
context.subscriptions.push(disposable);
context.subscriptions.push(Terminal_loader);
context.subscriptions.push(def_search);
context.subscriptions.push(saving_Doc);
// context.subscriptions.push(open_document);
context.subscriptions.push(def_refrence);
// context.subscriptions.push(FS_Explorer);
}
// function to getorcreate the ccls client per project
async function getorstartcclsclient(Remote_Server,remote_dir)
{
if(cclsclients.has(remote_dir))
{
return cclsclients.get(remote_dir);
}
if(Remote_Server.ccls_path === '')
{
Remote_Server.ccls_path = '/Kushal/lsp/ccls/Release/ccls'; // default path if not found
}// to make sure ccls path is set
vscode.window.showInformationMessage(`Starting ccls for project: ${remote_dir}`);
const client = new LanguageClient(
`ccls-${remote_dir}`,
`ccls (${remote_dir})`,
{
command: `ssh`,
args: [`${Remote_Server.AIX_USER}@${Remote_Server.AIX_HOST}`,`${Remote_Server.ccls_path}`],
options: {
env:process.env }
},
{
documentSelector: [
{ scheme: 'file', language: 'cpp'},
{ scheme: 'file', language: 'c'}
]
,
traceOutputChannel: vscode.window.createOutputChannel('ccls-trace'),
}
);
client.traceOutputChannel.show(true);
client.start();
cclsclients.set(remote_dir, client);
return client;
}
// Dynamically assoscaiting the file explorer with the new servers
async function getServer()
{
return Array.from(Servers.entries()).map(([hostname,Remote_server])=>
(
{
"name": hostname,
"folder": Remote_server.TEMP_DIR,
}
)
);
}
//Returning all the Server map
async function AllServers()
{
return Servers;
}
function watchCommandFile(Remote_server) {
chokidar.watch(Remote_server.LOCAL_COMMAND_FILE).on('change', () => {
let target = fs.readFileSync(Remote_server.LOCAL_COMMAND_FILE, 'utf8').trim();
let remote_dir = '';
let newtarget = '';
if(target && target.includes('::'))
{
const parts = target.split('::');
if (parts.length >= 2) {
newtarget = parts[1].trim();
remote_dir = parts[0].trim();
pullFromAix(newtarget,Remote_server,remote_dir);
// getorstartcclsclient(Remote_server,remote_dir);
// Cscope(Remote_server,remote_dir);
}
}
else if (target) {
pullFromAix(target,Remote_server);
}
});
}
// remote_dir is needed to see where that cscope_dir has been stored by the package.
async function pullFromAix(remotePath,Remote_server,remote_dir="") {
remotePath = path.posix.normalize(remotePath);
// Explorer.refresh();
// fs.writeFileSync(path.join(Remote_server.TEMP_DIR,`${Remote_server.AIX_HOST}_files.json`),JSON.stringify(Remote_server.localtoRemote));
let uri = vscode.Uri.from({
scheme: "aix",
authority: Remote_server.AIX_HOST,
path: remotePath,
});
const localFile = await hash_path(remotePath,Remote_server);
// Remote_server.updateLocalToRemote(path.basename(remotePath),remotePath);
// Creating the fragment for the uri
const frag = new URLSearchParams({
cscope_dir: remote_dir,
server: Remote_server // storing this remote_server so that ccls can get the approach to run the commands later.
}).toString();
uri = uri.with({ fragment: frag }); // Setting up the uri for the files to be get the cscope_dir later when needed.
// setting up the local uri
let local_uri = vscode.Uri.file(localFile);
// local_uri = local_uri.with({fragment: frag});
let local_file=null;
if(uriLocaltoRemote.get(local_uri.toString()))
{
local_file = await vscode.workspace.openTextDocument(local_uri);
}
else
{
uriLocaltoRemote.set(local_uri.toString(),uri);
await Remote_server.FS_Provider.readFile(uri);
local_file = await vscode.workspace.openTextDocument(local_uri);
}
try{
await vscode.window.showTextDocument(local_file, { preview: true, preserveFocus: true });
}
catch (err) {
if(err.message.includes("Failed to reload: CodeExpectedError: cannot open file"))
{
await vscode.commands.executeCommand('vscode.open',local_uri);
}
}
}
function saveConfig(context, login) {
const configPath = vscode.Uri.joinPath(context.globalStorageUri, 'sshfs-config.json').fsPath;
vscode.window.showInformationMessage(`Saving login: ${configPath}`);
let logins = [];
if (fs.existsSync(configPath)) {
try { logins = JSON.parse(fs.readFileSync(configPath, 'utf8')); } catch {}
}
if (!logins.includes(login)) logins.push(login);
if(!fs.existsSync(context.globalStorageUri.fsPath))
{fs.mkdirSync(context.globalStorageUri.fsPath, { recursive: true });}
fs.writeFileSync(configPath, JSON.stringify(logins, null, 2));
}
function loadconfig(context) {
const configPath = vscode.Uri.joinPath(context.globalStorageUri, 'sshfs-config.json').fsPath;
if (fs.existsSync(configPath)) {
try { return JSON.parse(fs.readFileSync(configPath, 'utf8')); } catch { return []; }
}
return [];
}
function startWatcher(watcherScript,Remote_server) {
return new Promise((resolve, reject) => {
fs.chmodSync(watcherScript, "755");
const pyProc = spawn("python3", [watcherScript,path.join(LOCAL_SYNC_DIR,`command_${Remote_server.AIX_HOST}.txt`)], {
detached: true, // allows it to keep running in background
stdio: ["ignore", "pipe", "pipe"]
});
let port;
pyProc.stdout.on("data", data => {
const output = data.toString().trim();
const parsedPort = parseInt(output, 10);
if (!isNaN(parsedPort)) {
port = parsedPort;
console.log("Watcher assigned PORT:", port);
resolve({ port, process: pyProc });
}
});
pyProc.stderr.on("data", err => {
console.error(`Watcher error: ${err}`);
});
pyProc.on("error", reject);
// Don't tie Node's lifecycle to the child
pyProc.unref();
});
}
function TerminalLoader(userHost,Remote_server,Pyproc,Remote_server_port)
{
// Create (or reuse) a VS Code terminal
const terminal = vscode.window.createTerminal({ name: `${Remote_server.AIX_HOST}_Terminal` });
terminal.show();
// Send reverse SSH tunnel command
const forwardCmd = `ssh -R ${Remote_server_port}:localhost:${Remote_server_port} \
-o StrictHostKeyChecking=no \
-o ServerAliveInterval=10 -o ServerAliveCountMax=3 ${userHost}`;
terminal.sendText(forwardCmd, true);
// Storing the terminal sessions listener so that if close the terminal remove those listeners
Listeners.set(terminal, Pyproc);
}
// function to find the path of the different tools on the remote aix server
function ToolPath(Remote_server,tool)
{
return new Promise((resolve, reject) => {
let default_path = '';
if(Remote_server.osname === "AIX")
{
default_path = '/opt/freeware/bin';
}
else if(Remote_server.osname === "Linux")
{
default_path = '/usr/bin';
}
const whichRsync = spawn("ssh", [`${Remote_server.AIX_USER}@${Remote_server.AIX_HOST}`, `which ${tool}`]);
let toolPath = "";
let stderr = "";
whichRsync.stdout.on('data', (data) => {
toolPath = data.toString();
// Now ru
});
whichRsync.stderr.on('data', (data) => {
stderr += data.toString();
});
whichRsync.on('close', (code) => {
if(code !== 0) {
let foundit = false;
if(toolPath === "")
{
let Path= spawn("ssh", [`${Remote_server.AIX_USER}@${Remote_server.AIX_HOST}`, `find ${default_path} -name ${tool} | head -n 1`]);
Path.stdout.on('data', (data) => {
toolPath = data.toString();
// Now ru
});
Path.stderr.on('data', (data) => {
stderr += data.toString();
});
Path.on('close', (code) => {
if(code !== 0)
{
vscode.window.showErrorMessage(`Error finding ${tool}`);
reject(new Error(`Error: No such file or directory: ${stderr}`));
}
else{
foundit = true;
resolve(toolPath);
console.log(`${tool} path set to: ${toolPath}`);
}
});
}
if(!foundit)
{
vscode.window.showErrorMessage(`Error finding ${tool}`);
reject(new Error(`Error: No such file or directory: ${stderr}`));
}
}
toolPath = toolPath.split('\n').find(p => p.trim().endsWith(`${tool}`)) || '';
if (toolPath ) {
if(tool === 'rsync')
{
Remote_server.rsync_path = toolPath.trim();
resolve(Remote_server.rsync_path);
console.log(`${tool} path set to: ${Remote_server.rsync_path}`);
}
else if(tool === 'sed'){
toolPath = toolPath.trim();
Remote_server.sed_path = toolPath;
resolve(toolPath);
console.log(`${tool} path set to: ${toolPath}`);
}
else if (tool === 'ccls'){
toolPath = toolPath.trim();
Remote_server.ccls_path = toolPath;
resolve(toolPath);
console.log(`${tool} path ccls to: ${toolPath}`);
}
} else {
console.warn(`${tool} not found on remote, using default`);
resolve("");
}
});
});
}
async function Boot(userHost,Remote_server) {
try {