Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Commit

Permalink
Simplified global profile
Browse files Browse the repository at this point in the history
  • Loading branch information
da-the-dev committed Dec 28, 2021
1 parent ab8b19b commit 08d0831
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 112 deletions.
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@
"command": "vscode-extension-profiles.Import",
"title": "Extension profiles: Import profile",
"shortTitle": "Import profile"
},
{
"command": "vscode-extension-profiles.UpdateGlobal",
"title": "Extension profiles: Update Global Profile",
"shortTitle": "Update Global Profile"
}
]
},
Expand All @@ -93,7 +88,6 @@
"onCommand:vscode-extension-profiles.Refresh",
"onCommand:vscode-extension-profiles.Export",
"onCommand:vscode-extension-profiles.Import",
"onCommand:vscode-extension-profiles.UpdateGlobal",
"onStartupFinished"
],
"dependencies": {
Expand Down
113 changes: 12 additions & 101 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { setGlobalStorageValue, setWorkspaceStorageValue } from "./storage";
import { ExtensionList, ExtensionValue } from "./types";
import { getAllExtensions, getExtensionList, getPathToDocuments, getProfileList, getUserWorkspaceStorageUUID, getWorkspacesUUID } from "./utils";

// VSCode EXtention PRofiles Global Profile. User should not be able to update/delete/create a profile with this name
const vscexprgp = 'vscexprgp';
// User should not be able to update/delete/create a profile with this name
const globalProfileName = 'Global Profile';

// Select and apply profile ...
export async function applyProfile() {
Expand All @@ -24,18 +24,15 @@ export async function applyProfile() {
// Generate items
let itemsProfiles: vscode.QuickPickItem[] = [];
for (const item in profiles)
if (item !== vscexprgp)
itemsProfiles.push({
label: item,
});

itemsProfiles.push({
label: item,
});

// Selected profile
let profileName = (await vscode.window.showQuickPick(itemsProfiles, { placeHolder: "Search", title: "Select a profile" }))?.label;
if (!profileName)
return;


// Check and refresh extension list
let extensions = await getExtensionList();
if (Object.keys(extensions).length === 0)
Expand All @@ -49,7 +46,7 @@ export async function applyProfile() {
let item: ExtensionValue = { id: key, uuid: extensions[key].uuid };

// Set enabled and disabled extensions for workspace
if (profiles[profileName][key] !== undefined || profiles[vscexprgp][key] !== undefined)
if (profiles[profileName][key] !== undefined || (profiles[globalProfileName] && profiles[globalProfileName][key] !== undefined))
enabledList.push(item);
else
disabledList.push(item);
Expand All @@ -66,7 +63,6 @@ export async function applyProfile() {
} else
uuid = await getUserWorkspaceStorageUUID(folders[0].uri);


// write in workspace
await setWorkspaceStorageValue(uuid, "enabled", enabledList);
await setWorkspaceStorageValue(uuid, "disabled", disabledList);
Expand All @@ -76,50 +72,6 @@ export async function applyProfile() {
return;
}

// Update global profile ...
export async function updateGlobalProfile() {
const profiles = await getProfileList();

// set name profile
const profileName = vscexprgp;

// Get extension list of cache
let extensions = await getExtensionList();

// update if not exist
if (Object.keys(extensions).length === 0)
extensions = await refreshExtensionList({ isCache: true });

// create extension list
let itemsWorkspace: vscode.QuickPickItem[] = [];
for (const key in extensions) {
let item = extensions[key];
itemsWorkspace.push({
label: item.label || key,
description: item.label ? key : undefined,
detail: item.description || " - - - - - ",
});
}

// show and select extensions
let selected = await vscode.window.showQuickPick(itemsWorkspace, {
canPickMany: true,
placeHolder: "The selected extensions will be enabled globally in every workspace.",
title: `Select extensions for the Global Profile`,
});

// set enabled extensions for profile
profiles[profileName] = {};

if (selected)
for (const { description: key } of selected)
profiles[profileName][key!] = extensions[key!];

await setGlobalStorageValue("vscodeExtensionProfiles/profiles", profiles);

return vscode.window.showInformationMessage(`Global Profile successfully updated!`);
}

// Create profile ...
export async function createProfile() {
const profiles = await getProfileList();
Expand All @@ -133,7 +85,7 @@ export async function createProfile() {
if (profileName && Object.keys(profiles).includes(profileName)) {
placeHolder = `The profile \"${profileName}\" already exists, think of another name`;
continue; //go next step
} else if (profileName && profileName === vscexprgp)
} else if (profileName && profileName === globalProfileName)
placeHolder = 'This profile name is reserved, please use another one';
else if (!profileName)
return; // close input box
Expand Down Expand Up @@ -193,18 +145,16 @@ export async function editProfile() {
// Generate items
let itemsProfiles: vscode.QuickPickItem[] = [];
for (const item in profiles)
if (item !== vscexprgp)
itemsProfiles.push({
label: item,
});
itemsProfiles.push({
label: item
});


// Selected profile
let profileName = (await vscode.window.showQuickPick(itemsProfiles, { placeHolder: "Search", title: "Select a profile to edit" }))?.label;
if (!profileName)
return;


// Check and refresh extension list
let extensions = await getExtensionList();
if (Object.keys(extensions).length === 0)
Expand Down Expand Up @@ -259,12 +209,10 @@ export async function deleteProfile() {
if (Object.keys(profiles).length === 0)
return vscode.window.showInformationMessage("All right, no profiles to delete! 😌");



// Generate items
let itemsProfiles: vscode.QuickPickItem[] = [];
for (const item in profiles)
if (item !== vscexprgp)
if (item !== globalProfileName)
itemsProfiles.push({
label: item,
});
Expand All @@ -275,7 +223,6 @@ export async function deleteProfile() {
if (!profileName)
return;


delete profiles[profileName];

await setGlobalStorageValue("vscodeExtensionProfiles/profiles", profiles);
Expand All @@ -293,17 +240,14 @@ export async function exportProfile() {
let itemsProfiles: vscode.QuickPickItem[] = [];
for (const item in profiles)
itemsProfiles.push({
label: item === vscexprgp ? 'Global Profile' : item
label: item
});



// Selected profile
let profileName = (await vscode.window.showQuickPick(itemsProfiles, { placeHolder: "Search", title: "Select a profile to export" }))?.label;
if (!profileName)
return;


const resource = await vscode.window.showSaveDialog({
title: 'Select a place and file name to save the exported profile',
saveLabel: 'Export',
Expand Down Expand Up @@ -351,39 +295,6 @@ export async function importProfile() {
return vscode.window.showInformationMessage(`Profile "${profileName}" successfully imported!`);
}

export async function importGlobalProfile() {
const profiles = await getProfileList();

// Use showSaveDialog to get a path to the profile
const resource = await vscode.window.showOpenDialog({
title: 'Select a Global Profile to import',
openLabel: 'Import',
canSelectMany: false,
filters: {
'JSON files': ['json']
},
defaultUri: getPathToDocuments()
});

if (!resource)
return vscode.window.showErrorMessage(`Couldn't locate the path to the Global Profile! Try again.`);
const profileName = vscexprgp;

// Get extension list of cache
let extensions = await getExtensionList();

// update if not exist
if (Object.keys(extensions).length === 0)
extensions = await refreshExtensionList({ isCache: true });

// Add the imported profile
profiles[profileName] = JSON.parse((await readFile(resource[0].fsPath)).toString());

await setGlobalStorageValue("vscodeExtensionProfiles/profiles", profiles);

return vscode.window.showInformationMessage(`Global Profile successfully imported!`);
}

export async function refreshExtensionList({ isCache = false }) {
let oldExtensionList = await getExtensionList();
let newExtensionList: ExtensionList = {};
Expand Down
9 changes: 6 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";
import * as vscode from "vscode";
import { applyProfile, createProfile, deleteProfile, editProfile, exportProfile, importProfile, refreshExtensionList, updateGlobalProfile } from "./commands";
import { applyProfile, createProfile, deleteProfile, editProfile, exportProfile, importProfile, refreshExtensionList } from "./commands";
import { CommandType } from "./types";
import { checkGlobalProfile } from "./utils";

export async function activate(ctx: vscode.ExtensionContext) {
// Refreshing the list of extensions after startup
Expand All @@ -15,9 +16,11 @@ export async function activate(ctx: vscode.ExtensionContext) {
vscode.commands.registerCommand("vscode-extension-profiles.Edit" as CommandType, editProfile),
vscode.commands.registerCommand("vscode-extension-profiles.Delete" as CommandType, deleteProfile),
vscode.commands.registerCommand("vscode-extension-profiles.Export" as CommandType, exportProfile),
vscode.commands.registerCommand("vscode-extension-profiles.Import" as CommandType, importProfile),
vscode.commands.registerCommand("vscode-extension-profiles.UpdateGlobal" as CommandType, updateGlobalProfile),
vscode.commands.registerCommand("vscode-extension-profiles.Import" as CommandType, importProfile)
);

// Check for global profile
await checkGlobalProfile();
}

export function deactivate() { }
18 changes: 16 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { homedir } from "os";
import { promisify } from "util";
import * as vscode from "vscode";
import { getGlobalStorageValue } from "./storage";
import { getGlobalStorageValue, setGlobalStorageValue } from "./storage";
import { ExtensionList, ExtensionValue, PackageJson, ProfileList } from "./types";

import path = require("path");
Expand Down Expand Up @@ -246,9 +246,23 @@ export function getExtensionLocaleValue(extPath: string, key: string): string {
return key;
}

// Return a path to a profile export file that will be in a 'Documents' folder or just the 'Documents'
/**
* Return a path to a profile export file that will be in a 'Documents' folder or just the 'Documents'.
*/
export function getPathToDocuments(profileName?: string): vscode.Uri {
let basePath = homedir();
// Return the URI either with a file name appended (export) or without it (import)
return vscode.Uri.file(profileName ? `${basePath}${platformSlash}Documents${platformSlash}${profileName}.json` : `${basePath}${platformSlash}Documents${platformSlash}`);
}

/**
* Check if a global profile exists. It will create one if there isn't any.
*/
export async function checkGlobalProfile() {
const globalProfileName = 'Global Profile';
const profiles = await getProfileList();
if (profiles[globalProfileName] === undefined)
profiles[globalProfileName] = {};

await setGlobalStorageValue("vscodeExtensionProfiles/profiles", profiles);
}

0 comments on commit 08d0831

Please sign in to comment.