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

Commit

Permalink
First pass on switching extensions (#2)
Browse files Browse the repository at this point in the history
Should mostly switch fine
Backs up extensions to globalStorage when you remove them for faster reload
Install not working yet
  • Loading branch information
aaronpowell committed Jul 2, 2019
1 parent 92b3311 commit dc1d39b
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 13 deletions.
122 changes: 121 additions & 1 deletion .test-profiles/simple-profile/data/User/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,123 @@
{
"editor.fontSize": 24
"editor.fontSize": 24,
"profileSwitcher.profiles": ["Default"],
"profileSwitcher.storage": {
"Default": {
"sync.gist": "40c333588d64df34c6f36f196010e5ee",
"sync.lastUpload": "",
"sync.autoDownload": false,
"sync.autoUpload": false,
"sync.lastDownload": "2017-11-03T04:41:33.893Z",
"sync.forceDownload": false,
"sync.anonymousGist": false,
"sync.host": "",
"sync.pathPrefix": "",
"sync.quietSync": false,
"sync.askGistName": false,
"workbench.colorTheme": "1984 - Light",
"files.autoSave": "afterDelay",
"editor.renderWhitespace": "all",
"editor.fontFamily": "Fira Code",
"editor.fontLigatures": true,
"workbench.startupEditor": "newUntitledFile",
"files.autoSaveDelay": 5000,
"sync.removeExtensions": true,
"sync.syncExtensions": true,
"team.showWelcomeMessage": false,
"terminal.integrated.rendererType": "auto",
"explorer.confirmDelete": false,
"window.zoomLevel": 0,
"azureStorage.preview.staticWebsites": true,
"breadcrumbs.enabled": true,
"powershell.powerShellExePath": "C:\\WINDOWS\\SysWow64\\WindowsPowerShell\\v1.0\\powershell.exe",
"FSharp.enableReferenceCodeLens": true,
"FSharp.fsacRuntime": "netcore",
"workbench.iconTheme": "vscode-icons",
"peacock.favoriteColors": [
{
"name": "Angular Red",
"value": "#b52e31"
},
{
"name": "Auth0 Orange",
"value": "#eb5424"
},
{
"name": "Azure Blue",
"value": "#007fff"
},
{
"name": "C# Purple",
"value": "#68217A"
},
{
"name": "Gatsby Purple",
"value": "#639"
},
{
"name": "Go Cyan",
"value": "#5dc9e2"
},
{
"name": "Java Blue-Gray",
"value": "#557c9b"
},
{
"name": "JavaScript Yellow",
"value": "#f9e64f"
},
{
"name": "Mandalorian Blue",
"value": "#1857a4"
},
{
"name": "Node Green",
"value": "#215732"
},
{
"name": "React Blue",
"value": "#00b3e6"
},
{
"name": "Something Different",
"value": "#832561"
},
{
"name": "Vue Green",
"value": "#42b883"
}
],
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[jsonc]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.fontSize": 14,
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": true,
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"profileSwitcher.profiles": ["test1"],
"profileSwitcher.storage": {
"test1": {
"foo": "bar"
}
}
}
},
"profileSwitcher.extensions": {
"Default": [
{
"id": "96fa4707-6983-4489-b7c5-d5ffdfdcce90",
"publisherId": "esbenp.prettier-vscode",
"publisherName": "esbenp",
"version": "1.9.0",
"name": "prettier-vscode"
}
]
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "vscode-profile-switcher" extension will be documente

Check [Keep a Changelog](http://keepachan2gelog.com/) for recommendations on how to structure this file.

## 0.3.0 - Unreleased

- Working on support for enable/disable extensions as you change profiles (Issue [#2](https://github.com/aaronpowell/vscode-profile-switcher/issues/2))

## 0.2.0 - 2019-06-28

- Rewriting internals
Expand Down
23 changes: 21 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-profile-switcher",
"displayName": "Profile Switcher",
"description": "Allows you to switch between different profiles you have created",
"version": "0.2.0",
"version": "0.3.0",
"preview": true,
"license": "SEE LICENSE IN LICENSE.md",
"publisher": "aaronpowell",
Expand Down Expand Up @@ -67,12 +67,31 @@
},
"profileSwitcher.storage": {
"type": "object",
"description": "These are the details for each profile that has been saved. Probably don't hand-edit this",
"description": "These are the settings for each profile that has been saved. Probably don't hand-edit this",
"patternProperties": {
".*": {
"type": "object"
}
}
},
"profileSwitcher.extensions": {
"type": "object",
"description": "These are the extensions for each profile that has been saved. Probably don't hand-edit this",
"patternProperties": {
".*": {
"type": "array"
}
}
},
"profileSwitcher.extensionsIgnore": {
"type": "array",
"description": "Extensions that will not be removed when profiles are switches. Enter the profile ID like 'ms-vsliveshare.vsliveshare'.",
"default": [
"shan.code-settings-sync",
"ms-vsliveshare.vsliveshare",
"ms-vsliveshare.vsliveshare-audio",
"ms-vsliveshare.vsliveshare-pack"
]
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export const ExtensionId = `${ExtensionPublisher}.${ExtensionName}`;
export const ConfigKey = "profileSwitcher";
export const ConfigProfilesKey = "profiles";
export const ConfigStorageKey = "storage";
export const ConfigExtensionsKey = "extensions";
export const ConfigExtensionsIgnoreKey = "extensionsIgnore";
43 changes: 37 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import * as vscode from "vscode";
import SettingsHelper from "./settingsHelper";
import ContributedCommands from "./commands";
import Config from "./services/config";
import ExtensionHelper from "./services/extensions";

function selectProfile(config: Config, settingsHelper: SettingsHelper) {
function selectProfile(
config: Config,
settingsHelper: SettingsHelper,
extensionsHelper: ExtensionHelper
) {
return async () => {
let profiles = config.getProfiles();

Expand All @@ -22,13 +27,33 @@ function selectProfile(config: Config, settingsHelper: SettingsHelper) {
return;
}

let profileSettings = config.getProfileSettings(profile);
let msg = vscode.window.setStatusBarMessage("Switching profiles.");

let profileSettings = config.getProfileSettings(profile);
await settingsHelper.updateUserSettings(profileSettings);

let extensions = config.getProfileExtensions(profile);
await extensionsHelper.installExtensions(extensions);
await extensionsHelper.removeExtensions(extensions);

msg.dispose();

const message = await vscode.window.showInformationMessage(
"Do you want to reload and activate the extensions?",
"Yes"
);

if (message === "Yes") {
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
};
}

function saveProfile(config: Config, settingsHelper: SettingsHelper) {
function saveProfile(
config: Config,
settingsHelper: SettingsHelper,
extensionsHelper: ExtensionHelper
) {
return async () => {
let profiles = config.getProfiles();

Expand All @@ -52,7 +77,11 @@ function saveProfile(config: Config, settingsHelper: SettingsHelper) {
}

let userSettings = await settingsHelper.getUserSettings();

let extensions = extensionsHelper.getInstalled();

await config.addProfileSettings(profile, userSettings);
await config.addExtensions(profile, extensions);

vscode.window.showInformationMessage(`Profile ${profile} has been saved.`);
};
Expand All @@ -79,6 +108,7 @@ function deleteProfile(config: Config) {

await config.removeProfile(profile);
await config.removeProfileSettings(profile);
await config.removeProfileExtensions(profile);

await vscode.window.showInformationMessage(
`Profile ${profile} has been deleted.`
Expand All @@ -87,20 +117,21 @@ function deleteProfile(config: Config) {
}

export async function activate(context: vscode.ExtensionContext) {
let settingsHelper = new SettingsHelper(context);
let config = new Config();
let settingsHelper = new SettingsHelper(context);
let extensionsHelper = new ExtensionHelper(context, settingsHelper, config);

context.subscriptions.push(
vscode.commands.registerCommand(
ContributedCommands.SelectProfile,
selectProfile(config, settingsHelper)
selectProfile(config, settingsHelper, extensionsHelper)
)
);

context.subscriptions.push(
vscode.commands.registerCommand(
ContributedCommands.SaveProfile,
saveProfile(config, settingsHelper)
saveProfile(config, settingsHelper, extensionsHelper)
)
);

Expand Down
51 changes: 50 additions & 1 deletion src/services/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import * as vscode from "vscode";
import { ConfigKey, ConfigProfilesKey, ConfigStorageKey } from "../constants";
import {
ConfigKey,
ConfigProfilesKey,
ConfigStorageKey,
ConfigExtensionsKey,
ConfigExtensionsIgnoreKey
} from "../constants";
import { ExtensionInfo } from "./extensions";

export interface Settings {
[key: string]: number | string | boolean | object;
Expand All @@ -9,6 +16,10 @@ export interface Storage {
[key: string]: Settings;
}

interface ExtensionStorage {
[key: string]: ExtensionInfo[];
}

class Config {
private getConfig() {
return vscode.workspace.getConfiguration(ConfigKey);
Expand All @@ -24,6 +35,10 @@ class Config {
return this.getStorage()[profile];
}

public getProfileExtensions(profile: string) {
return this.getExtensions()[profile] || [];
}

private getStorage() {
let config = this.getConfig();

Expand Down Expand Up @@ -87,6 +102,40 @@ class Config {
vscode.ConfigurationTarget.Global
);
}

public addExtensions(profile: string, extensions: ExtensionInfo[]) {
let storage = this.getExtensions();
storage[profile] = extensions;
return this.updateExtensions(storage);
}

private getExtensions() {
let config = this.getConfig();

return config.get<ExtensionStorage>(ConfigExtensionsKey, {});
}

private updateExtensions(storage: ExtensionStorage) {
let config = this.getConfig();

return config.update(
ConfigExtensionsKey,
storage,
vscode.ConfigurationTarget.Global
);
}

public removeProfileExtensions(profile: string) {
let storage = this.getExtensions();
delete storage[profile];
return this.updateExtensions(storage);
}

public getIgnoredExtensions() {
let config = this.getConfig();

return config.get<string[]>(ConfigExtensionsIgnoreKey, []);
}
}

export default Config;
Loading

0 comments on commit dc1d39b

Please # to comment.