Skip to content
This repository was archived by the owner on Jul 18, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -124,6 +133,7 @@
"vscode": "^1.1.28"
},
"dependencies": {
"fs-extra": "^8.0.1"
"fs-extra": "^8.0.1",
"vsls": "^0.3.1291"
}
}
1 change: 1 addition & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
enum ContributedCommands {
SelectLiveShareProfile = "extension.selectLiveShareProfile",
SelectProfile = "extension.selectProfile",
SaveProfile = "extension.saveProfile",
DeleteProfile = "extension.deleteProfile"
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
95 changes: 67 additions & 28 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Contributor Author

@lostintangent lostintangent Jul 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the churn in this file are refactorings that allowed the profile selection code to be re-used. I split the selectProfile method into selectProfile (which is the command handler), promptProfile (which handles displaying the UI), and activateProfile (which actually activates the profile).

This allows the Select Live Share Profile command handler to re-use the profile prompting code, without actually activating the selected profile (since it's simply setting a profile for future use, as opposed to immediately activating it).


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);
await extensionsHelper.removeExtensions(extensions);
if (message === "Yes") {
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
}

msg.dispose();
async function promptProfile(config: Config): Promise<string | undefined> {
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);
};
}

Expand Down Expand Up @@ -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,
Expand All @@ -141,4 +176,8 @@ export async function activate(context: vscode.ExtensionContext) {
deleteProfile(config)
)
);

liveShare.initialize(context, config, (profile: string) => {
activateProfile(profile, config, settingsHelper, extensionsHelper);
});
}
36 changes: 35 additions & 1 deletion src/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
ConfigProfilesKey,
ConfigStorageKey,
ConfigExtensionsKey,
ConfigExtensionsIgnoreKey
ConfigExtensionsIgnoreKey,
ConfigLiveShareProfileKey,
ContextSettingCurrentProfile,
ContextSettingPreviousProfile
} from "../constants";
import { ExtensionInfo } from "./extensions";

Expand All @@ -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<string | null>(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<string>(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();

Expand Down
42 changes: 42 additions & 0 deletions src/services/liveShare.ts
Original file line number Diff line number Diff line change
@@ -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);
}
})
}