From 08d0831b39f334eb7f93332b1f1402dcf0f14ef1 Mon Sep 17 00:00:00 2001 From: da-the-dev Date: Tue, 28 Dec 2021 19:41:52 +0300 Subject: [PATCH] Simplified global profile --- package.json | 6 --- src/commands.ts | 113 +++++------------------------------------------ src/extension.ts | 9 ++-- src/utils.ts | 18 +++++++- 4 files changed, 34 insertions(+), 112 deletions(-) diff --git a/package.json b/package.json index 4d954bd..d1d5f3d 100644 --- a/package.json +++ b/package.json @@ -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" } ] }, @@ -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": { diff --git a/src/commands.ts b/src/commands.ts index e5f63bd..87efc42 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -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() { @@ -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) @@ -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); @@ -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); @@ -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(); @@ -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 @@ -193,10 +145,9 @@ 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 @@ -204,7 +155,6 @@ export async function editProfile() { if (!profileName) return; - // Check and refresh extension list let extensions = await getExtensionList(); if (Object.keys(extensions).length === 0) @@ -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, }); @@ -275,7 +223,6 @@ export async function deleteProfile() { if (!profileName) return; - delete profiles[profileName]; await setGlobalStorageValue("vscodeExtensionProfiles/profiles", profiles); @@ -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', @@ -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 = {}; diff --git a/src/extension.ts b/src/extension.ts index 11ffcff..ba58c00 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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 @@ -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() { } diff --git a/src/utils.ts b/src/utils.ts index 835679b..89094b2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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"); @@ -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); +} \ No newline at end of file