diff --git a/.gitignore b/.gitignore index 2e94e98..bdbd5cd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ node_modules *.vsix .ionide test-results.xml -.test-profiles \ No newline at end of file +.test-profiles +*.orig \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 3fd50af..71db575 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1799,6 +1799,11 @@ "https-proxy-agent": "^2.2.1" } }, + "vsls": { + "version": "0.3.1291", + "resolved": "https://registry.npmjs.org/vsls/-/vsls-0.3.1291.tgz", + "integrity": "sha512-8yJPN9p7k+XYyczOVtQmpun4K1CRDsw/hdnIzT/c40r5bIkpptfsBlHmmLemoIV+CAHvrTLdWKEf5OtRvdcn9A==" + }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", diff --git a/package.json b/package.json index 3932550..3db492f 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", @@ -126,6 +135,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 1c9aa12..2a07943 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,50 +3,78 @@ import SettingsHelper from "./settingsHelper"; import ContributedCommands from "./commands"; import Config from "./services/config"; import ExtensionHelper from "./services/extensions"; -import logger from './services/logger'; +import logger from "./services/logger"; +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; - } + await 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, logger); - await extensionsHelper.removeExtensions(extensions, logger); + 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); }; } @@ -118,10 +146,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, @@ -142,4 +177,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..1d68a2b 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 { + public 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 async setCurrentProfile(profile: string) { + if (this.context) { + const previousProfile = this.context.globalState.get(ContextSettingCurrentProfile); + this.setPreviousProfile(previousProfile); + + await 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(); diff --git a/src/services/liveShare.ts b/src/services/liveShare.ts new file mode 100644 index 0000000..3768a9c --- /dev/null +++ b/src/services/liveShare.ts @@ -0,0 +1,42 @@ +import * as vscode from "vscode"; +import * as vsls from "vsls"; +import Config from "./config"; + +export async function initialize( + context: vscode.ExtensionContext, + config: Config, + activateProfileHandler: (profile: string) => void +) { + // Check to see if the end-user has the Live Share + // extension installed, and if not, exit early. + const liveShare = await vsls.getApi(); + if (!liveShare) { + return; + } + + // Begin listening for the beginning and end of Live + // Share sessions, in case we need to switch profiles. + liveShare.onDidChangeSession(e => { + // If the end-user never set a Live Share profile, then + // there's nothing we need to do. Note that we're calling + // this here, instead of as part of the activation flow, + // so that the end-user can set their profile any time + // and have it take effect immediately. + const liveShareProfile = config.getLiveShareProfile(); + if (!liveShareProfile) { + return; + } + + if (e.session.id) { + activateProfileHandler(liveShareProfile); + } else { + const previousProfile = config.getPreviousProfile(); + if (!previousProfile) { + return; + } + + config.setPreviousProfile(undefined); + activateProfileHandler(previousProfile); + } + }) +} \ No newline at end of file