Skip to content

Commit 9bc34be

Browse files
committed
Add support for setting custom VSCode executable name and VSCode user settings path
See #41
1 parent 97c149a commit 9bc34be

File tree

6 files changed

+155
-43
lines changed

6 files changed

+155
-43
lines changed

package.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "code-sync",
33
"displayName": "CodeSync",
44
"description": "Sync VSCode extensions using your favorite file synchronization service (OneDrive, Dropbox, Google Drive, etc.)",
5-
"version": "2.6.2",
5+
"version": "2.7.0",
66
"publisher": "golf1052",
77
"keywords": [
88
"sync",
@@ -157,6 +157,16 @@
157157
"category": "CodeSync",
158158
"command": "codeSync.toggleStatusBar",
159159
"title": "Toggle status bar icon"
160+
},
161+
{
162+
"category": "CodeSync",
163+
"command": "codeSync.setCodeExecutableName",
164+
"title": "Set VSCode executable name"
165+
},
166+
{
167+
"category": "CodeSync",
168+
"command": "codeSync.setCodeSettingsPath",
169+
"title": "Set VSCode settings path"
160170
}
161171
]
162172
},

src/cs.ts

+94-20
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const KEYBINDINGS = 'keybindings.json';
1818
export const SNIPPETS = 'snippets';
1919
export const LOCAL_SETTINGS = 'local-settings.json';
2020

21-
export const currentVersion: string = '2.6.2';
21+
export const currentVersion: string = '2.7.0';
2222
export let vsCodeExtensionDir: string = helpers.getExtensionDir();
2323
export let codeSyncExtensionDir: string = path.join(vsCodeExtensionDir, 'golf1052.code-sync-' + currentVersion);
2424

@@ -110,7 +110,9 @@ export class CodeSync {
110110
excluded: {
111111
installed: [],
112112
external: []
113-
}
113+
},
114+
executableName: '',
115+
settingsPath: ''
114116
};
115117
this.Settings.Settings = tmpSettings;
116118
this.Settings.save();
@@ -159,14 +161,14 @@ export class CodeSync {
159161

160162
startFileWatcher = () => {
161163
let files: any = {};
162-
if (fs.existsSync(helpers.getUserSettingsFilePath())) {
163-
files[helpers.getUserSettingsFilePath()] = this.exportSettings.bind(this);
164+
if (fs.existsSync(helpers.getUserSettingsFilePath(this.Settings.Settings))) {
165+
files[helpers.getUserSettingsFilePath(this.Settings.Settings)] = this.exportSettings.bind(this);
164166
}
165-
if (fs.existsSync(helpers.getKeybindingsFilePath())) {
166-
files[helpers.getKeybindingsFilePath()] = this.exportKeybindings.bind(this);
167+
if (fs.existsSync(helpers.getKeybindingsFilePath(this.Settings.Settings))) {
168+
files[helpers.getKeybindingsFilePath(this.Settings.Settings)] = this.exportKeybindings.bind(this);
167169
}
168-
if (fs.existsSync(helpers.getSnippetsFolderPath())) {
169-
files[helpers.getSnippetsFolderPath()] = this.exportSnippets.bind(this);
170+
if (fs.existsSync(helpers.getSnippetsFolderPath(this.Settings.Settings))) {
171+
files[helpers.getSnippetsFolderPath(this.Settings.Settings)] = this.exportSnippets.bind(this);
170172
}
171173
this.fileWatcher = new FileWatcher(files, this.Settings);
172174
}
@@ -204,8 +206,10 @@ export class CodeSync {
204206
return;
205207
}
206208
if (helpers.isFileEmpty(settingsPath) == false &&
207-
helpers.isFileContentEmpty(settingsPath) == false) {
208-
this.localSettingsManager.import(settingsPath, helpers.getUserSettingsFilePath());
209+
helpers.isFileContentEmpty(settingsPath) == false) {
210+
const userSettingsFilePath = helpers.getUserSettingsFilePath(this.Settings.Settings);
211+
this.logger.appendLine(`Importing settings to ${userSettingsFilePath}`);
212+
this.localSettingsManager.import(settingsPath, userSettingsFilePath);
209213
}
210214
this.statusBar.reset();
211215
this.logger.appendLine('Finished importing settings.');
@@ -216,13 +220,13 @@ export class CodeSync {
216220
if (this.Settings.Settings.importSettings) {
217221
this.logger.appendLine('Exporting settings.');
218222
this.startSync('Exporting settings');
219-
let settingsPath: string = helpers.getUserSettingsFilePath();
223+
let settingsPath: string = helpers.getUserSettingsFilePath(this.Settings.Settings);
220224
if (!fs.existsSync(settingsPath)) {
221225
this.logger.appendLine(`Could not find settings path at ${settingsPath}. Giving up.`);
222226
this.statusBar.reset();
223227
return;
224228
}
225-
this.localSettingsManager.export(helpers.getUserSettingsFilePath(), path.join(this.codeSyncDir, SETTINGS));
229+
this.localSettingsManager.export(settingsPath, path.join(this.codeSyncDir, SETTINGS));
226230
this.statusBar.reset();
227231
this.logger.appendLine('Finished exporting settings.');
228232
}
@@ -239,8 +243,10 @@ export class CodeSync {
239243
return;
240244
}
241245
if (helpers.isFileEmpty(keybindingsPath) == false &&
242-
helpers.isFileContentEmpty(keybindingsPath) == false) {
243-
await helpers.copy(keybindingsPath, helpers.getKeybindingsFilePath());
246+
helpers.isFileContentEmpty(keybindingsPath) == false) {
247+
const keybindingsFilePath = helpers.getKeybindingsFilePath(this.Settings.Settings)
248+
this.logger.appendLine(`Importing keybindings to ${keybindingsFilePath}`);
249+
await helpers.copy(keybindingsPath, keybindingsFilePath);
244250
}
245251
this.statusBar.reset();
246252
this.logger.appendLine('Finished importing keybindings.');
@@ -250,10 +256,13 @@ export class CodeSync {
250256
async exportKeybindings() {
251257
if (this.Settings.Settings.importKeybindings) {
252258
this.startSync('Exporting keybindings');
253-
if (!fs.existsSync(helpers.getKeybindingsFilePath())) {
259+
let keybindingsPath = helpers.getKeybindingsFilePath(this.Settings.Settings);
260+
if (!fs.existsSync(keybindingsPath)) {
261+
this.logger.appendLine(`Could not find keybindings path at ${keybindingsPath}. Giving up.`);
262+
this.statusBar.reset();
254263
return;
255264
}
256-
await helpers.copy(helpers.getKeybindingsFilePath(), path.join(this.codeSyncDir, KEYBINDINGS));
265+
await helpers.copy(keybindingsPath, path.join(this.codeSyncDir, KEYBINDINGS));
257266
this.statusBar.reset();
258267
}
259268
}
@@ -273,7 +282,7 @@ export class CodeSync {
273282
let s = snippetFiles[i];if (fs.lstatSync(path.join(snippetsDirectory, s)).isFile()) {
274283
if (helpers.isFileEmpty(path.join(snippetsDirectory, s)) == false &&
275284
helpers.isFileContentEmpty(path.join(snippetsDirectory, s)) == false) {
276-
await helpers.copy(path.join(snippetsDirectory, s), path.join(helpers.getSnippetsFolderPath(), s));
285+
await helpers.copy(path.join(snippetsDirectory, s), path.join(helpers.getSnippetsFolderPath(this.Settings.Settings), s));
277286
}
278287
}
279288
}
@@ -285,10 +294,13 @@ export class CodeSync {
285294
async exportSnippets() {
286295
if (this.Settings.Settings.importSnippets) {
287296
this.startSync('Exporting snippets');
288-
if (!fs.existsSync(helpers.getSnippetsFolderPath())) {
297+
const snippetsFolderPath = helpers.getSnippetsFolderPath(this.Settings.Settings);
298+
if (!fs.existsSync(snippetsFolderPath)) {
299+
this.logger.appendLine(`Could not find snippets path at ${snippetsFolderPath}. Giving up.`);
300+
this.statusBar.reset();
289301
return;
290302
}
291-
await helpers.copy(helpers.getSnippetsFolderPath(), path.join(this.codeSyncDir, SNIPPETS));
303+
await helpers.copy(snippetsFolderPath, path.join(this.codeSyncDir, SNIPPETS));
292304
this.statusBar.reset();
293305
}
294306
}
@@ -303,7 +315,7 @@ export class CodeSync {
303315
let installedAny: boolean = false;
304316
extensions.forEach(e => {
305317
if (installedExtensions.filter(i => i.id == e).length == 0) {
306-
let val = helpers.installExtension(e);
318+
let val = helpers.installExtension(e, this.Settings.Settings);
307319
if (val) {
308320
installedAny = true;
309321
}
@@ -518,6 +530,68 @@ export class CodeSync {
518530
}
519531
}
520532

533+
async setCodeExecutableName(): Promise<void> {
534+
let executableName: string = '';
535+
executableName = await vscode.window.showInputBox({
536+
prompt: 'Enter the VSCode executable name. NOTE: Do not include the file extension (.exe, .sh, etc.)',
537+
placeHolder: 'code'
538+
});
539+
540+
if (executableName == undefined) {
541+
return;
542+
} else {
543+
vscode.window.showInformationMessage('Testing user defined VSCode executable name...');
544+
let oldExecutableName;
545+
if (this.Settings.Settings.executableName) {
546+
oldExecutableName = this.Settings.Settings.executableName;
547+
} else {
548+
oldExecutableName = '';
549+
}
550+
let csSettings = this.Settings.Settings;
551+
csSettings.executableName = executableName;
552+
this.Settings.Settings = csSettings;
553+
this.Settings.save();
554+
555+
if (helpers.isCodeOnPath(this.Settings.Settings)) {
556+
vscode.window.showInformationMessage(`Test succeeded. Will now use ${executableName} as VSCode executable name.`);
557+
} else {
558+
csSettings.executableName = oldExecutableName;
559+
this.Settings.Settings = csSettings;
560+
this.Settings.save();
561+
vscode.window.showInformationMessage('Test failed. Reverting back to old VSCode executable name.');
562+
}
563+
}
564+
}
565+
566+
async setCodeSettingsPath(): Promise<void> {
567+
let settingsPath: string = '';
568+
settingsPath = await vscode.window.showInputBox({
569+
prompt: 'Enter the VSCode user settings path. This must be an absolute path.',
570+
placeHolder: helpers.getDefaultCodeSettingsFolderPath()
571+
});
572+
573+
if (settingsPath == undefined) {
574+
return;
575+
} else {
576+
vscode.window.showInformationMessage('Testing user defined VSCode user settings path...');
577+
const oldSettingsPath = this.Settings.Settings.settingsPath;
578+
this.Settings.Settings.settingsPath = settingsPath;
579+
this.Settings.save();
580+
581+
if (!settingsPath) {
582+
settingsPath = helpers.getDefaultCodeSettingsFolderPath();
583+
}
584+
585+
if (fs.existsSync(settingsPath)) {
586+
vscode.window.showInformationMessage(`Test succeeded. Will now use ${settingsPath} as VSCode user settings path.`);
587+
} else {
588+
this.Settings.Settings.settingsPath = oldSettingsPath;
589+
this.Settings.save();
590+
vscode.window.showInformationMessage('Test failed. Reverting back to old VSCode user settings path.');
591+
}
592+
}
593+
}
594+
521595
private startSync(text: string) {
522596
this.statusBar.StatusBarText = text;
523597
this.statusBar.setSync();

src/extension.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ var codeSync: cs.CodeSync;
1010
export async function activate(context: vscode.ExtensionContext) {
1111
logger = new Logger('extension');
1212
codeSync = new cs.CodeSync(cs.vsCodeExtensionDir, cs.codeSyncExtensionDir, '');
13-
helpers.isCodeASnapPackage(true);
14-
codeSync.CanManageExtensions = helpers.isCodeOnPath();
13+
helpers.isCodeASnapPackage(codeSync.Settings.Settings, true);
14+
codeSync.CanManageExtensions = helpers.isCodeOnPath(codeSync.Settings.Settings);
1515
if (!codeSync.CanManageExtensions) {
1616
await vscode.window.showWarningMessage(helpers.getCodePathWarningMessage());
1717
}
@@ -115,6 +115,12 @@ export async function activate(context: vscode.ExtensionContext) {
115115
let toggleStatusBarDisposable = vscode.commands.registerCommand('codeSync.toggleStatusBar', function() {
116116
codeSync.toggleStatusBarIcon();
117117
});
118+
const setCodeExecutableName = vscode.commands.registerCommand('codeSync.setCodeExecutableName', async function() {
119+
await codeSync.setCodeExecutableName();
120+
});
121+
const setCodeSettingsPath = vscode.commands.registerCommand('codeSync.setCodeSettingsPath', async function() {
122+
await codeSync.setCodeSettingsPath();
123+
});
118124

119125
context.subscriptions.push(
120126
importAllDisposable,
@@ -140,7 +146,9 @@ export async function activate(context: vscode.ExtensionContext) {
140146
toggleImportSnippetsDisposable,
141147
toggleImportExtensionsDisposable,
142148
setSyncPathDisposable,
143-
toggleStatusBarDisposable
149+
toggleStatusBarDisposable,
150+
setCodeExecutableName,
151+
setCodeSettingsPath
144152
);
145153
}
146154

src/file-watcher.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import * as chokidar from 'chokidar';
33
import * as fs from 'fs';
44
import * as helpers from './helpers';
55
import * as settings from './settings';
6+
import { Logger } from './logger';
67

78
export class FileWatcher {
89
private watchers: chokidar.FSWatcher[];
910
private files: any;
1011
private codeSyncSettings: settings.CodeSyncSettings;
12+
private logger: Logger;
1113

1214
constructor(files: any, codeSyncSettings: settings.CodeSyncSettings) {
15+
this.logger = new Logger('file-watcher');
1316
this.watchers = [];
1417
this.files = files;
1518
this.codeSyncSettings = codeSyncSettings;
@@ -31,11 +34,12 @@ export class FileWatcher {
3134

3235
change = (path: string, stats: fs.Stats) => {
3336
if (this.codeSyncSettings.Settings.autoExport) {
37+
this.logger.appendLine(`Detected file change at ${path}, syncing...`);
3438
if (this.files[path]) {
3539
this.files[path]();
3640
}
37-
else if (path.includes(helpers.getSnippetsFolderPath())) {
38-
this.files[helpers.getSnippetsFolderPath()]();
41+
else if (path.includes(helpers.getSnippetsFolderPath(this.codeSyncSettings.Settings))) {
42+
this.files[helpers.getSnippetsFolderPath(this.codeSyncSettings.Settings)]();
3943
}
4044
}
4145
}

0 commit comments

Comments
 (0)