Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
…ile-switcher into lostintangent-master
  • Loading branch information
aaronpowell committed Jul 3, 2019
2 parents 828c266 + 8ab0c2f commit 883fc69
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules
*.vsix
.ionide
test-results.xml
.test-profiles
.test-profiles
*.orig
5 changes: 5 additions & 0 deletions 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 @@ -126,6 +135,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
97 changes: 68 additions & 29 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -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,
Expand All @@ -142,4 +177,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);
}
})
}

0 comments on commit 883fc69

Please # to comment.