Skip to content

Commit

Permalink
Add support for setting custom VSCode executable name and VSCode user…
Browse files Browse the repository at this point in the history
… settings path

See #41
  • Loading branch information
golf1052 committed May 10, 2020
1 parent 97c149a commit 9bc34be
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 43 deletions.
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "code-sync",
"displayName": "CodeSync",
"description": "Sync VSCode extensions using your favorite file synchronization service (OneDrive, Dropbox, Google Drive, etc.)",
"version": "2.6.2",
"version": "2.7.0",
"publisher": "golf1052",
"keywords": [
"sync",
Expand Down Expand Up @@ -157,6 +157,16 @@
"category": "CodeSync",
"command": "codeSync.toggleStatusBar",
"title": "Toggle status bar icon"
},
{
"category": "CodeSync",
"command": "codeSync.setCodeExecutableName",
"title": "Set VSCode executable name"
},
{
"category": "CodeSync",
"command": "codeSync.setCodeSettingsPath",
"title": "Set VSCode settings path"
}
]
},
Expand Down
114 changes: 94 additions & 20 deletions src/cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const KEYBINDINGS = 'keybindings.json';
export const SNIPPETS = 'snippets';
export const LOCAL_SETTINGS = 'local-settings.json';

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

Expand Down Expand Up @@ -110,7 +110,9 @@ export class CodeSync {
excluded: {
installed: [],
external: []
}
},
executableName: '',
settingsPath: ''
};
this.Settings.Settings = tmpSettings;
this.Settings.save();
Expand Down Expand Up @@ -159,14 +161,14 @@ export class CodeSync {

startFileWatcher = () => {
let files: any = {};
if (fs.existsSync(helpers.getUserSettingsFilePath())) {
files[helpers.getUserSettingsFilePath()] = this.exportSettings.bind(this);
if (fs.existsSync(helpers.getUserSettingsFilePath(this.Settings.Settings))) {
files[helpers.getUserSettingsFilePath(this.Settings.Settings)] = this.exportSettings.bind(this);
}
if (fs.existsSync(helpers.getKeybindingsFilePath())) {
files[helpers.getKeybindingsFilePath()] = this.exportKeybindings.bind(this);
if (fs.existsSync(helpers.getKeybindingsFilePath(this.Settings.Settings))) {
files[helpers.getKeybindingsFilePath(this.Settings.Settings)] = this.exportKeybindings.bind(this);
}
if (fs.existsSync(helpers.getSnippetsFolderPath())) {
files[helpers.getSnippetsFolderPath()] = this.exportSnippets.bind(this);
if (fs.existsSync(helpers.getSnippetsFolderPath(this.Settings.Settings))) {
files[helpers.getSnippetsFolderPath(this.Settings.Settings)] = this.exportSnippets.bind(this);
}
this.fileWatcher = new FileWatcher(files, this.Settings);
}
Expand Down Expand Up @@ -204,8 +206,10 @@ export class CodeSync {
return;
}
if (helpers.isFileEmpty(settingsPath) == false &&
helpers.isFileContentEmpty(settingsPath) == false) {
this.localSettingsManager.import(settingsPath, helpers.getUserSettingsFilePath());
helpers.isFileContentEmpty(settingsPath) == false) {
const userSettingsFilePath = helpers.getUserSettingsFilePath(this.Settings.Settings);
this.logger.appendLine(`Importing settings to ${userSettingsFilePath}`);
this.localSettingsManager.import(settingsPath, userSettingsFilePath);
}
this.statusBar.reset();
this.logger.appendLine('Finished importing settings.');
Expand All @@ -216,13 +220,13 @@ export class CodeSync {
if (this.Settings.Settings.importSettings) {
this.logger.appendLine('Exporting settings.');
this.startSync('Exporting settings');
let settingsPath: string = helpers.getUserSettingsFilePath();
let settingsPath: string = helpers.getUserSettingsFilePath(this.Settings.Settings);
if (!fs.existsSync(settingsPath)) {
this.logger.appendLine(`Could not find settings path at ${settingsPath}. Giving up.`);
this.statusBar.reset();
return;
}
this.localSettingsManager.export(helpers.getUserSettingsFilePath(), path.join(this.codeSyncDir, SETTINGS));
this.localSettingsManager.export(settingsPath, path.join(this.codeSyncDir, SETTINGS));
this.statusBar.reset();
this.logger.appendLine('Finished exporting settings.');
}
Expand All @@ -239,8 +243,10 @@ export class CodeSync {
return;
}
if (helpers.isFileEmpty(keybindingsPath) == false &&
helpers.isFileContentEmpty(keybindingsPath) == false) {
await helpers.copy(keybindingsPath, helpers.getKeybindingsFilePath());
helpers.isFileContentEmpty(keybindingsPath) == false) {
const keybindingsFilePath = helpers.getKeybindingsFilePath(this.Settings.Settings)
this.logger.appendLine(`Importing keybindings to ${keybindingsFilePath}`);
await helpers.copy(keybindingsPath, keybindingsFilePath);
}
this.statusBar.reset();
this.logger.appendLine('Finished importing keybindings.');
Expand All @@ -250,10 +256,13 @@ export class CodeSync {
async exportKeybindings() {
if (this.Settings.Settings.importKeybindings) {
this.startSync('Exporting keybindings');
if (!fs.existsSync(helpers.getKeybindingsFilePath())) {
let keybindingsPath = helpers.getKeybindingsFilePath(this.Settings.Settings);
if (!fs.existsSync(keybindingsPath)) {
this.logger.appendLine(`Could not find keybindings path at ${keybindingsPath}. Giving up.`);
this.statusBar.reset();
return;
}
await helpers.copy(helpers.getKeybindingsFilePath(), path.join(this.codeSyncDir, KEYBINDINGS));
await helpers.copy(keybindingsPath, path.join(this.codeSyncDir, KEYBINDINGS));
this.statusBar.reset();
}
}
Expand All @@ -273,7 +282,7 @@ export class CodeSync {
let s = snippetFiles[i];if (fs.lstatSync(path.join(snippetsDirectory, s)).isFile()) {
if (helpers.isFileEmpty(path.join(snippetsDirectory, s)) == false &&
helpers.isFileContentEmpty(path.join(snippetsDirectory, s)) == false) {
await helpers.copy(path.join(snippetsDirectory, s), path.join(helpers.getSnippetsFolderPath(), s));
await helpers.copy(path.join(snippetsDirectory, s), path.join(helpers.getSnippetsFolderPath(this.Settings.Settings), s));
}
}
}
Expand All @@ -285,10 +294,13 @@ export class CodeSync {
async exportSnippets() {
if (this.Settings.Settings.importSnippets) {
this.startSync('Exporting snippets');
if (!fs.existsSync(helpers.getSnippetsFolderPath())) {
const snippetsFolderPath = helpers.getSnippetsFolderPath(this.Settings.Settings);
if (!fs.existsSync(snippetsFolderPath)) {
this.logger.appendLine(`Could not find snippets path at ${snippetsFolderPath}. Giving up.`);
this.statusBar.reset();
return;
}
await helpers.copy(helpers.getSnippetsFolderPath(), path.join(this.codeSyncDir, SNIPPETS));
await helpers.copy(snippetsFolderPath, path.join(this.codeSyncDir, SNIPPETS));
this.statusBar.reset();
}
}
Expand All @@ -303,7 +315,7 @@ export class CodeSync {
let installedAny: boolean = false;
extensions.forEach(e => {
if (installedExtensions.filter(i => i.id == e).length == 0) {
let val = helpers.installExtension(e);
let val = helpers.installExtension(e, this.Settings.Settings);
if (val) {
installedAny = true;
}
Expand Down Expand Up @@ -518,6 +530,68 @@ export class CodeSync {
}
}

async setCodeExecutableName(): Promise<void> {
let executableName: string = '';
executableName = await vscode.window.showInputBox({
prompt: 'Enter the VSCode executable name. NOTE: Do not include the file extension (.exe, .sh, etc.)',
placeHolder: 'code'
});

if (executableName == undefined) {
return;
} else {
vscode.window.showInformationMessage('Testing user defined VSCode executable name...');
let oldExecutableName;
if (this.Settings.Settings.executableName) {
oldExecutableName = this.Settings.Settings.executableName;
} else {
oldExecutableName = '';
}
let csSettings = this.Settings.Settings;
csSettings.executableName = executableName;
this.Settings.Settings = csSettings;
this.Settings.save();

if (helpers.isCodeOnPath(this.Settings.Settings)) {
vscode.window.showInformationMessage(`Test succeeded. Will now use ${executableName} as VSCode executable name.`);
} else {
csSettings.executableName = oldExecutableName;
this.Settings.Settings = csSettings;
this.Settings.save();
vscode.window.showInformationMessage('Test failed. Reverting back to old VSCode executable name.');
}
}
}

async setCodeSettingsPath(): Promise<void> {
let settingsPath: string = '';
settingsPath = await vscode.window.showInputBox({
prompt: 'Enter the VSCode user settings path. This must be an absolute path.',
placeHolder: helpers.getDefaultCodeSettingsFolderPath()
});

if (settingsPath == undefined) {
return;
} else {
vscode.window.showInformationMessage('Testing user defined VSCode user settings path...');
const oldSettingsPath = this.Settings.Settings.settingsPath;
this.Settings.Settings.settingsPath = settingsPath;
this.Settings.save();

if (!settingsPath) {
settingsPath = helpers.getDefaultCodeSettingsFolderPath();
}

if (fs.existsSync(settingsPath)) {
vscode.window.showInformationMessage(`Test succeeded. Will now use ${settingsPath} as VSCode user settings path.`);
} else {
this.Settings.Settings.settingsPath = oldSettingsPath;
this.Settings.save();
vscode.window.showInformationMessage('Test failed. Reverting back to old VSCode user settings path.');
}
}
}

private startSync(text: string) {
this.statusBar.StatusBarText = text;
this.statusBar.setSync();
Expand Down
14 changes: 11 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ var codeSync: cs.CodeSync;
export async function activate(context: vscode.ExtensionContext) {
logger = new Logger('extension');
codeSync = new cs.CodeSync(cs.vsCodeExtensionDir, cs.codeSyncExtensionDir, '');
helpers.isCodeASnapPackage(true);
codeSync.CanManageExtensions = helpers.isCodeOnPath();
helpers.isCodeASnapPackage(codeSync.Settings.Settings, true);
codeSync.CanManageExtensions = helpers.isCodeOnPath(codeSync.Settings.Settings);
if (!codeSync.CanManageExtensions) {
await vscode.window.showWarningMessage(helpers.getCodePathWarningMessage());
}
Expand Down Expand Up @@ -115,6 +115,12 @@ export async function activate(context: vscode.ExtensionContext) {
let toggleStatusBarDisposable = vscode.commands.registerCommand('codeSync.toggleStatusBar', function() {
codeSync.toggleStatusBarIcon();
});
const setCodeExecutableName = vscode.commands.registerCommand('codeSync.setCodeExecutableName', async function() {
await codeSync.setCodeExecutableName();
});
const setCodeSettingsPath = vscode.commands.registerCommand('codeSync.setCodeSettingsPath', async function() {
await codeSync.setCodeSettingsPath();
});

context.subscriptions.push(
importAllDisposable,
Expand All @@ -140,7 +146,9 @@ export async function activate(context: vscode.ExtensionContext) {
toggleImportSnippetsDisposable,
toggleImportExtensionsDisposable,
setSyncPathDisposable,
toggleStatusBarDisposable
toggleStatusBarDisposable,
setCodeExecutableName,
setCodeSettingsPath
);
}

Expand Down
8 changes: 6 additions & 2 deletions src/file-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import * as chokidar from 'chokidar';
import * as fs from 'fs';
import * as helpers from './helpers';
import * as settings from './settings';
import { Logger } from './logger';

export class FileWatcher {
private watchers: chokidar.FSWatcher[];
private files: any;
private codeSyncSettings: settings.CodeSyncSettings;
private logger: Logger;

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

change = (path: string, stats: fs.Stats) => {
if (this.codeSyncSettings.Settings.autoExport) {
this.logger.appendLine(`Detected file change at ${path}, syncing...`);
if (this.files[path]) {
this.files[path]();
}
else if (path.includes(helpers.getSnippetsFolderPath())) {
this.files[helpers.getSnippetsFolderPath()]();
else if (path.includes(helpers.getSnippetsFolderPath(this.codeSyncSettings.Settings))) {
this.files[helpers.getSnippetsFolderPath(this.codeSyncSettings.Settings)]();
}
}
}
Expand Down
Loading

0 comments on commit 9bc34be

Please sign in to comment.