-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
extension.ts
557 lines (461 loc) · 20.7 KB
/
extension.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
553
554
555
556
557
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as pluginService from './pluginService'
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
var openurl = require('open');
var fs = require('fs');
var GitHubApi = require("github");
var github = new GitHubApi({
// required
version: "3.0.0"
});
var TOKEN: string = null;
var GIST: string = null;
var tokenChecked: boolean = false;
var gistChecked: boolean = false;
var tempValue: string = "";
var PATH: string = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : '/var/local');
var FILE_GIST: string = PATH.concat("/Code/User/gist_sync.txt");
var FILE_TOKEN: string = PATH.concat("/Code/User/token.txt");
var FILE_SETTING: string = PATH.concat("/Code/User/settings.json");
var FILE_LAUNCH: string = PATH.concat("/Code/User/launch.json");
var FILE_KEYBINDING: string = PATH.concat("/Code/User/keybindings.json");
var FOLDER_SNIPPETS: string = PATH.concat("/Code/User/snippets/");
var ERROR_MESSAGE: string = "ERROR ! Logged In Console. Please open an issue in Github Repo."
var GIST_JSON: any = {
"description": "Visual Studio code settings",
"public": false,
"files": {
"settings": {
"content": ""
},
"launch": {
"content": ""
},
"keybindings": {
"content": ""
},
"extensions": {
"content": ""
}
}
};
var disposable = vscode.commands.registerCommand('extension.updateSettings', () => {
tempValue = "";
vscode.window.setStatusBarMessage("Checking for Github Token and GIST.", 2000);
tokenChecked = false;
gistChecked = false;
function GetInputBox(token: boolean) {
if (token) {
let options: vscode.InputBoxOptions = {
placeHolder: "Enter Github Personal Access Token",
password: false,
prompt: "Link is opened to get the github token."
};
return options;
}
else {
let options: vscode.InputBoxOptions = {
placeHolder: "Enter GIST ID",
password: false,
prompt: "If you never upload the files in any machine before then upload it before."
};
return options;
}
};
function ReadTokenFileResult(err: any, data: any) {
if (!data) {
var opt = GetInputBox(true);
openurl("https://github.com/settings/tokens");
vscode.window.showInputBox(opt).then((value) => {
value = value.trim();
if (value) {
tempValue = value;
fs.writeFile(FILE_TOKEN, value, WriteTokenFileResult);
}
});
}
else {
TOKEN = data;
ReadGist();
}
};
function WriteTokenFileResult(err: any, data: any) {
if (err) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
TOKEN = tempValue;
ReadGist();
};
function ReadGist() {
fs.readFile(FILE_GIST, { encoding: 'utf8' }, ReadGistFileResult);
};
function ReadGistFileResult(err: any, data: any) {
if (err) {
if (err.code != "ENOENT") {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
}
if (data) {
GIST = data;
}
else {
GIST = null;
}
vscode.window.setStatusBarMessage("Uploading / Updating Your Settings In Github.", 2000);
startGitProcess();
};
function CreateNewGist(settingtext: string, launchtext: string, keybindingtext: string, extensiontext: string) {
github.authenticate({
type: "oauth",
token: TOKEN
});
if (fs.existsSync(FOLDER_SNIPPETS)) {
//create new gist and upload all files there
var list = fs.readdirSync(FOLDER_SNIPPETS);
for (var i: number = 0; i < list.length; i++) {
var fileName = list[i];
var filePath = FOLDER_SNIPPETS.concat(fileName);
var fileText: string = fs.readFileSync(filePath, { encoding: 'utf8' });
var jsonObjName = fileName.split('.')[0];
var obj = {};
obj[jsonObjName] = {};
obj[jsonObjName].content = fileText;
GIST_JSON.files[jsonObjName] = {};
GIST_JSON.files[jsonObjName].content = fileText;
//debugger;
}
}
GIST_JSON.files.settings.content = settingtext;
GIST_JSON.files.launch.content = launchtext;
GIST_JSON.files.keybindings.content = keybindingtext;
GIST_JSON.files.extensions.content = extensiontext;
github.getGistsApi().create(GIST_JSON
, function(err, res) {
if (err) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
vscode.window.showInformationMessage("Uploaded Successfully." + " GIST ID : " + res.id + " . Please copy and use this ID in other machines to sync all settings.");
fs.writeFile(FILE_GIST, res.id, function(err, data) {
if (err) {
vscode.window.showErrorMessage("ERROR ! Unable to Save GIST ID In this machine. You need to enter it manually from Download Settings.");
console.log(err);
return false;
}
vscode.window.showInformationMessage("GIST ID Saved in your machine.");
});
});
};
function ExistingGist(settingtext: string, launchtext: string, keybindingtext: string, extensiontext: string) {
github.authenticate({
type: "oauth",
token: TOKEN
});
github.getGistsApi().get({ id: GIST }, function(er, res) {
if (er) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(er);
return false;
}
else {
if (fs.existsSync(FOLDER_SNIPPETS)) {
var list = fs.readdirSync(FOLDER_SNIPPETS);
for (var i: number = 0; i < list.length; i++) {
var fileName = list[i];
var filePath = FOLDER_SNIPPETS.concat(fileName);
var fileText: string = fs.readFileSync(filePath, { encoding: 'utf8' });
var jsonObjName = fileName.split('.')[0];
res.files[jsonObjName] = {};
res.files[jsonObjName].content = fileText;
}
}
res.files.settings.content = settingtext;
res.files.launch.content = launchtext;
res.files.keybindings.content = keybindingtext;
if (res.files.extensions) {
res.files.extensions.content = extensiontext;
}
else {
vscode.window.showInformationMessage("Announcement : Extension Sync feature has been Added. You need to Reset Settings Or Manually Remove GIST ID File in order to sync your extensions.");
}
github.getGistsApi().edit(res, function(ere, ress) {
if (ere) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(ere);
return false;
}
vscode.window.showInformationMessage("Settings Updated Successfully");
});
}
});
};
function startGitProcess() {
if (TOKEN != null) {
var settingtext: string = "//setting";
var launchtext: string = "//lanuch";
var keybindingtext: string = "//keybinding";
var extensiontext = "";
if (fs.existsSync(FILE_SETTING)) {
settingtext = fs.readFileSync(FILE_SETTING, { encoding: 'utf8' });
}
if (fs.existsSync(FILE_LAUNCH)) {
launchtext = fs.readFileSync(FILE_LAUNCH, { encoding: 'utf8' });
}
if (fs.existsSync(FILE_KEYBINDING)) {
keybindingtext = fs.readFileSync(FILE_KEYBINDING, { encoding: 'utf8' });
}
extensiontext = JSON.stringify(pluginService.PluginService.CreateExtensionList());
if (GIST == null) {
CreateNewGist(settingtext, launchtext, keybindingtext, extensiontext);
}
else if (GIST != null) {
ExistingGist(settingtext, launchtext, keybindingtext, extensiontext);
}
}
else {
vscode.window.showErrorMessage("ERROR ! Github Account Token Not Set");
}
}
function Initialize() {
if (fs.existsSync(FILE_TOKEN)) {
fs.readFile(FILE_TOKEN, { encoding: 'utf8' }, ReadTokenFileResult);
}
else {
openurl("https://github.com/settings/tokens");
var opt = GetInputBox(true);
vscode.window.showInputBox(opt).then((value) => {
if (value) {
value = value.trim();
tempValue = value;
fs.writeFile(FILE_TOKEN, value, WriteTokenFileResult);
}
});
}
}
//// start here
Initialize();
});
var disposable = vscode.commands.registerCommand('extension.downloadSettings', () => {
vscode.window.setStatusBarMessage("Downloading Your Settings...", 2000);
var tokenChecked: boolean = false;
var gistChecked: boolean = false;
function GetInputBox(token: boolean) {
if (token) {
let options: vscode.InputBoxOptions = {
placeHolder: "Enter Github Personal Access Token",
password: false,
prompt: "Link is opened to get the github token."
};
return options;
}
else {
let options: vscode.InputBoxOptions = {
placeHolder: "Enter GIST ID",
password: false,
prompt: "If you never upload the files in any machine before then upload it before."
};
return options;
}
};
function ReadTokenFileResult(err: any, data: any) {
if (err) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
if (!data) {
openurl("https://github.com/settings/tokens");
var opt = GetInputBox(false);
vscode.window.showInputBox(opt).then((value) => {
if (value) {
value = value.trim();
tempValue = value;
fs.writeFile(FILE_TOKEN, value, WriteTokenFileResult);
}
});
}
else {
TOKEN = data;
ReadGist();
}
};
function WriteTokenFileResult(err: any, data: any) {
if (err) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
TOKEN = tempValue;
ReadGist();
}
function ReadGist() {
fs.readFile(FILE_GIST, { encoding: 'utf8' }, ReadGistFileResult);
};
function ReadGistFileResult(err: any, data: any) {
if (!data) {
var opt = GetInputBox(false);
vscode.window.showInputBox(opt).then((value) => {
if (value) {
value = value.trim();
tempValue = value;
fs.writeFile(FILE_GIST, value, WriteGistFileResult);
}
});
}
else {
GIST = data;
StartDownload();
}
};
function WriteGistFileResult(err: any, data: any) {
if (err) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
GIST = tempValue;
StartDownload();
};
function StartDownload() {
github.getGistsApi().get({ id: GIST }, function(er, res) {
if (er) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(er);
return false;
}
var keys = Object.keys(res.files);
for (var i: number = 0; i < keys.length; i++) {
switch (keys[i]) {
case "launch": {
fs.writeFile(FILE_LAUNCH, res.files.launch.content, function(err, data) {
if (err) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
vscode.window.showInformationMessage("Launch Settings downloaded Successfully");
// console.log("launch");
// console.log(data);
});
break;
}
case "settings": {
fs.writeFile(FILE_SETTING, res.files.settings.content, function(err, data) {
if (err) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
vscode.window.showInformationMessage("Editor Settings downloaded Successfully");
// console.log("setting");
// console.log(data);
});
break;
}
case "keybindings": {
fs.writeFile(FILE_KEYBINDING, res.files.keybindings.content, function(err, data) {
if (err) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
vscode.window.showInformationMessage("Keybinding Settings downloaded Successfully");
});
break;
}
case "extensions": {
var remoteList = pluginService.ExtensionInformation.fromJSONList(res.files.extensions.content);
var missingList = pluginService.PluginService.GetMissingExtensions(remoteList);
if (missingList.length == 0) {
vscode.window.showInformationMessage("No extension need to be installed");
}
else {
var actionList = new Array<Promise<void>>();
vscode.window.setStatusBarMessage("Installing Extensions in background.",4000);
missingList.forEach(element => {
actionList.push(pluginService.PluginService.InstallExtension(element)
.then(function() {
var name = element.publisher + '.' + element.name + '-' + element.version;
vscode.window.showInformationMessage("Extension " + name + " installed Successfully");
}));
});
Promise.all(actionList)
.then(function() {
vscode.window.showInformationMessage("Extension installed Successfully, please restart");
})
.catch(function(e) {
console.log(e);
vscode.window.showErrorMessage("Extension download failed." + ERROR_MESSAGE)
});
}
break;
}
default: {
if (i < keys.length) {
if (!fs.existsSync(FOLDER_SNIPPETS)) {
fs.mkdirSync(FOLDER_SNIPPETS);
}
var file = FOLDER_SNIPPETS.concat(keys[i]).concat(".json");
var fileName = keys[i].concat(".json");
fs.writeFile(file, res.files[keys[i]].content, function(err, data) {
if (err) {
vscode.window.showErrorMessage(ERROR_MESSAGE);
console.log(err);
return false;
}
vscode.window.showInformationMessage(fileName + " snippet added successfully.");
});
}
break;
}
}
}
});
}
function Initialize() {
if (fs.existsSync(FILE_TOKEN)) {
fs.readFile(FILE_TOKEN, { encoding: 'utf8' }, ReadTokenFileResult);
}
else {
openurl("https://github.com/settings/tokens");
var opt = GetInputBox(true);
vscode.window.showInputBox(opt).then((value) => {
if (value) {
value = value.trim();
tempValue = value;
fs.writeFile(FILE_TOKEN, value, WriteTokenFileResult);
}
});
}
}
Initialize();
});
var disposable = vscode.commands.registerCommand('extension.resetSettings', () => {
vscode.window.setStatusBarMessage("Resetting Your Settings.", 2000);
try {
if (fs.existsSync(FILE_GIST)) {
fs.unlinkSync(FILE_GIST);
}
if (fs.existsSync(FILE_TOKEN)) {
fs.unlinkSync(FILE_TOKEN);
}
vscode.window.showInformationMessage("GIST ID and Github Token Cleared.");
}
catch (err) {
console.log(err);
vscode.window.showErrorMessage("Unable to clear settings. Error Logged on console. Please open an issue.");
}
});
context.subscriptions.push(disposable);
}