From ba4288dda2f9c1f886173a9e13711088c9b03dab Mon Sep 17 00:00:00 2001 From: Jonathan Carter Date: Tue, 2 Jul 2019 18:54:37 +0000 Subject: [PATCH] Initial Live Share support --- package.json | 12 +++++- src/commands.ts | 1 + src/constants.ts | 4 ++ src/extension.ts | 95 +++++++++++++++++++++++++++++------------- src/services/config.ts | 36 +++++++++++++++- 5 files changed, 118 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 5aff379..74d07a2 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,10 @@ "command": "extension.selectProfile", "title": "Profile Switcher: Select Profile" }, + { + "command": "extension.selectLiveShareProfile", + "title": "Profile Switcher: Select Live Share Profile" + }, { "command": "extension.saveProfile", "title": "Profile Switcher: Save Profile" @@ -74,6 +78,11 @@ } } }, + "profileSwitcher.liveShareProfile": { + "type": "string", + "description": "Specifies the profile you'd like to use when you start a Live Share session. Defaults to using the current profile", + "default": null + }, "profileSwitcher.extensions": { "type": "object", "description": "These are the extensions for each profile that has been saved. Probably don't hand-edit this", @@ -124,6 +133,7 @@ "vscode": "^1.1.28" }, "dependencies": { - "fs-extra": "^8.0.1" + "fs-extra": "^8.0.1", + "vsls": "^0.3.1291" } } diff --git a/src/commands.ts b/src/commands.ts index fe18f11..7d16c29 100755 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,4 +1,5 @@ enum ContributedCommands { + SelectLiveShareProfile = "extension.selectLiveShareProfile", SelectProfile = "extension.selectProfile", SaveProfile = "extension.saveProfile", DeleteProfile = "extension.deleteProfile" diff --git a/src/constants.ts b/src/constants.ts index a6cb74d..525895b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -2,6 +2,10 @@ export const ExtensionName = "vscode-profile-switcher"; export const ExtensionPublisher = "aaronpowell"; export const ExtensionId = `${ExtensionPublisher}.${ExtensionName}`; +export const ContextSettingCurrentProfile = "currentProfile"; +export const ContextSettingPreviousProfile = "previousProfile"; + +export const ConfigLiveShareProfileKey = "liveShareProfile"; export const ConfigKey = "profileSwitcher"; export const ConfigProfilesKey = "profiles"; export const ConfigStorageKey = "storage"; diff --git a/src/extension.ts b/src/extension.ts index eac8181..59da08a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,49 +3,77 @@ import SettingsHelper from "./settingsHelper"; import ContributedCommands from "./commands"; import Config from "./services/config"; import ExtensionHelper from "./services/extensions"; +import * as liveShare from "./services/liveShare"; -function selectProfile( +async function activateProfile( + profile: string, config: Config, settingsHelper: SettingsHelper, extensionsHelper: ExtensionHelper ) { - return async () => { - let profiles = config.getProfiles(); + let msg = vscode.window.setStatusBarMessage("Switching profiles."); - if (!profiles.length) { - await vscode.window.showInformationMessage( - "There are no profiles saved to switch to. First save a profile and then you can pick it" - ); - return; - } + config.setCurrentProfile(profile); - let profile = await vscode.window.showQuickPick(profiles, { - placeHolder: "Select a profile" - }); + let profileSettings = config.getProfileSettings(profile); + await settingsHelper.updateUserSettings(profileSettings); - if (!profile) { - return; - } + let extensions = config.getProfileExtensions(profile); + await extensionsHelper.installExtensions(extensions); + await extensionsHelper.removeExtensions(extensions); - let msg = vscode.window.setStatusBarMessage("Switching profiles."); + msg.dispose(); - let profileSettings = config.getProfileSettings(profile); - await settingsHelper.updateUserSettings(profileSettings); + const message = await vscode.window.showInformationMessage( + "Do you want to reload and activate the extensions?", + "Yes" + ); - let extensions = config.getProfileExtensions(profile); - await extensionsHelper.installExtensions(extensions); - await extensionsHelper.removeExtensions(extensions); + if (message === "Yes") { + vscode.commands.executeCommand("workbench.action.reloadWindow"); + } +} - msg.dispose(); +async function promptProfile(config: Config): Promise { + let profiles = config.getProfiles(); - const message = await vscode.window.showInformationMessage( - "Do you want to reload and activate the extensions?", - "Yes" + if (!profiles.length) { + await vscode.window.showInformationMessage( + "There are no profiles saved to switch to. First save a profile and then you can pick it" ); + return; + } - if (message === "Yes") { - vscode.commands.executeCommand("workbench.action.reloadWindow"); + let profile = await vscode.window.showQuickPick(profiles, { + placeHolder: "Select a profile" + }); + + return profile; +} + +function selectProfile( + config: Config, + settingsHelper: SettingsHelper, + extensionsHelper: ExtensionHelper +) { + return async () => { + const profile = await promptProfile(config); + if (!profile) { + return; } + + activateProfile(profile, config, settingsHelper, extensionsHelper); + }; +} + +function selectLiveShareProfile(config: Config) { + return async () => { + const profile = await promptProfile(config); + if (!profile) { + return; + } + + await config.setLiveShareProfile(profile); }; } @@ -117,10 +145,17 @@ function deleteProfile(config: Config) { } export async function activate(context: vscode.ExtensionContext) { - let config = new Config(); + let config = new Config(context); let settingsHelper = new SettingsHelper(context); let extensionsHelper = new ExtensionHelper(context, settingsHelper, config); + context.subscriptions.push( + vscode.commands.registerCommand( + ContributedCommands.SelectLiveShareProfile, + selectLiveShareProfile(config) + ) + ); + context.subscriptions.push( vscode.commands.registerCommand( ContributedCommands.SelectProfile, @@ -141,4 +176,8 @@ export async function activate(context: vscode.ExtensionContext) { deleteProfile(config) ) ); + + liveShare.initialize(context, config, (profile: string) => { + activateProfile(profile, config, settingsHelper, extensionsHelper); + }); } diff --git a/src/services/config.ts b/src/services/config.ts index c535974..4680c7b 100755 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -4,7 +4,10 @@ import { ConfigProfilesKey, ConfigStorageKey, ConfigExtensionsKey, - ConfigExtensionsIgnoreKey + ConfigExtensionsIgnoreKey, + ConfigLiveShareProfileKey, + ContextSettingCurrentProfile, + ContextSettingPreviousProfile } from "../constants"; import { ExtensionInfo } from "./extensions"; @@ -21,10 +24,41 @@ interface ExtensionStorage { } class Config { + constructor(private context?: vscode.ExtensionContext) {} + private getConfig() { return vscode.workspace.getConfiguration(ConfigKey); } + public getLiveShareProfile() { + let config = this.getConfig(); + + return config.get(ConfigLiveShareProfileKey, null); + } + + public setLiveShareProfile(profile: string) { + let config = this.getConfig(); + + return config.update(ConfigLiveShareProfileKey, profile, vscode.ConfigurationTarget.Global); + } + + public setCurrentProfile(profile: string) { + if (this.context) { + const previousProfile = this.context.globalState.get(ContextSettingCurrentProfile); + this.setPreviousProfile(previousProfile); + + this.context.globalState.update(ContextSettingCurrentProfile, profile); + } + } + + public getPreviousProfile(): string | undefined { + return this.context && this.context.globalState.get(ContextSettingPreviousProfile); + } + + public setPreviousProfile(profile: string | undefined) { + this.context && this.context.globalState.update(ContextSettingPreviousProfile, profile); + } + public getProfiles() { let config = this.getConfig();