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

Commit

Permalink
update 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
evald24 committed Jun 16, 2021
1 parent 4efec73 commit 41b8c93
Show file tree
Hide file tree
Showing 9 changed files with 948 additions and 68 deletions.
22 changes: 16 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@
"contributes": {
"commands": [
{
"command": "vscode-extension-profiles.Select",
"title": "Extension profiles - Select profile"
"command": "vscode-extension-profiles.Apply",
"title": "Extension profiles: Select and apply profile"
},
{
"command": "vscode-extension-profiles.Edite",
"title": "Extension profiles - Edite profile"
"title": "Extension profiles: Edite profile"
},
{
"command": "vscode-extension-profiles.Refresh",
"title": "Extension profiles: Refresh the storage of installed extensions"
},
{
"command": "vscode-extension-profiles.Delete",
"title": "Extension profiles - Delete profile"
"title": "Extension profiles: Delete profile"
}
],
"configuration": {
Expand Down Expand Up @@ -97,14 +101,16 @@
}
},
"activationEvents": [
"onCommand:vscode-extension-profiles.Select",
"onCommand:vscode-extension-profiles.Refresh",
"onCommand:vscode-extension-profiles.Apply",
"onCommand:vscode-extension-profiles.Edite",
"onCommand:vscode-extension-profiles.Delete"
],
"devDependencies": {
"@types/glob": "^7.1.3",
"@types/mocha": "^8.2.2",
"@types/node": "14.x",
"@types/sqlite3": "^3.1.7",
"@types/vscode": "^1.57.0",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
Expand All @@ -117,5 +123,9 @@
"engines": {
"vscode": "^1.57.0"
},
"preview": true
"preview": true,
"dependencies": {
"sqlite": "^4.0.23",
"sqlite3": "^5.0.2"
}
}
158 changes: 125 additions & 33 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,162 @@
/* eslint-disable @typescript-eslint/naming-convention */

import path = require("path");
import * as vscode from "vscode";
import { getProfiles } from "./config";
import { getExtensionsPath, getVSCodePath } from "./utils";

export const CommandTypes = ["vscode-extension-profiles.Select", "vscode-extension-profiles.Edite", "vscode-extension-profiles.Delete"] as const;
import { getExtensionList, getProfiles, setExtensionList } from "./config";
import { getAllExtensions } from "./storage";
import { ExtensionList } from "./types";
export const CommandTypes = [
"vscode-extension-profiles.Apply",
"vscode-extension-profiles.Refresh",
"vscode-extension-profiles.Edite",
"vscode-extension-profiles.Delete",
] as const;

type Args = { ctx: vscode.ExtensionContext };

export const Commands: Record<typeof CommandTypes[number], (args: Args) => any> = {
"vscode-extension-profiles.Select": selectProfile,
"vscode-extension-profiles.Apply": applyProfile,
"vscode-extension-profiles.Edite": editeProfile,
"vscode-extension-profiles.Refresh": refreshExtensionList,
"vscode-extension-profiles.Delete": deleteProfile,
};

const isDev = true;
// Select profile ...
function selectProfile({ ctx }: Args): any {
console.log(ctx);
// vscode.commands.executeCommand('workbench.extensions.installExtension', 'extensionId');
// let list = vscode.commands.executeCommand("workbench.extensions.search", "@installed ");
// console.log(list);
console.log("extension", getExtensionsPath());
console.log("code path", getVSCodePath());
// console.log(vscode.extensions.all.filter((e) => /^(?!\/Applications\/Visual Studio Code.app\/).*$/g.test(e.extensionPath)));
let items: vscode.QuickPickItem[] = [];
async function applyProfile({ ctx }: Args) {
const profiles = getProfiles();
const profilesKeys = Object.keys(profiles);
if (profilesKeys.length <= 0) {
vscode.window.showErrorMessage("Extension profiles: No profiles found, please create a profile first.", { modal: true });
return;
}

console.log(Object.keys(getProfiles()));
let fsPath;
if (vscode.workspace.workspaceFolders !== undefined) {
fsPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
} else {
vscode.window.showErrorMessage("Extension profiles: Working folder not found, open a folder an try again", { modal: true });
return;
}

// console.log(vscode.extensions.all.filter((e) => /^(?!\/Applications\/Visual Studio Code.app\/).*$/g.test(e.extensionPath)));
let itemsProfiles: vscode.QuickPickItem[] = [];

for (const item of Object.keys(getProfiles())) {
items.push({
for (const item of profilesKeys) {
itemsProfiles.push({
label: item,
});
}

// for (const item of getAllExtensions()) {
// items.push({
// label: item.packageJSON.displayName || item.packageJSON.name || item.id,
// description: item.id,
// detail: item.packageJSON.description,
// picked: true,
// });
// }

vscode.window.showQuickPick(items).then((selection) => {
let profile = "";
vscode.window.showQuickPick(itemsProfiles, { placeHolder: "Select profile" }).then((selection) => {
// the user canceled the selection
if (!selection) {
return;
}

console.log(selection);
profile = selection.label;
});
console.log(profile);

let itemsWorkspace: vscode.QuickPickItem[] = [];

// let uuid = await getWorkspaceUUID(vscode.Uri.parse(fsPath));
}

async function refreshExtensionList() {
let oldConfig = getExtensionList(),
keysConfig = Object.keys(oldConfig);
let newConfig: ExtensionList = {};

for (const item of await getAllExtensions()) {
if (!item.label) {
item.label = item.id;
if (keysConfig.length > 0) {
for (const key of keysConfig) {
if (item.id === key) {
if (oldConfig[key].label) {
// Если в конфиге указано имя расширения
item.label === oldConfig[key].label;
}
break;
}
}
}
}

newConfig[item.id] = {
uuid: item.uuid,
label: item.label,
};
}

await setExtensionList(newConfig);

vscode.window.showInformationMessage("Extension profiles: Updated the list of installed extensions");
}

// Select profile ...
// async function selectProfile({ ctx }: Args) {
// console.log(ctx);
// // vscode.commands.executeCommand('workbench.extensions.installExtension', 'extensionId');
// // vscode.commands.executeCommand("workbench.action.reloadWindow");
// // let list = vscode.commands.executeCommand("workbench.extensions.search", "@installed ");
// // let list = vscode.commands.executeCommand("workbench.action.openDefaultKeybindingsFile");
// // console.log(list);
// let fsPath;
// if (vscode.workspace.workspaceFolders !== undefined) {
// fsPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
// } else {
// vscode.window.showErrorMessage("vscode-extension-profiles: Working folder not found, open a folder an try again");
// return;
// }

// console.log(await getWorkspaceUUID(vscode.Uri.parse(fsPath)));

// // console.log(vscode.extensions.all.filter((e) => /^(?!\/Applications\/Visual Studio Code.app\/).*$/g.test(e.extensionPath)));
// let items: vscode.QuickPickItem[] = [];

// console.log(Object.keys(getProfiles()));

// for (const item of Object.keys(getProfiles())) {
// items.push({
// label: item,
// });
// }

// // for (const item of getAllExtensions()) {
// // items.push({
// // label: item.packageJSON.displayName || item.packageJSON.name || item.id,
// // description: item.id,
// // detail: item.packageJSON.description,
// // picked: true,
// // });
// // }

// vscode.window
// .showQuickPick(items, {
// canPickMany: true,
// })
// .then((selection) => {
// // the user canceled the selection
// if (!selection) {
// return;
// }

// console.log(selection);
// });
// }
// Edite profile ...
function editeProfile({ ctx }: Args): any {
async function editeProfile({ ctx }: Args) {
console.log(ctx);
vscode.window.showInformationMessage("editeProfile");
}

// Delete profile ...
function deleteProfile({ ctx }: Args): any {
async function deleteProfile({ ctx }: Args) {
console.log(ctx);
vscode.window.showInformationMessage("deleteProfile");
}

function ReloadWindow() {
async function ReloadWindow() {
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
15 changes: 10 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import * as vscode from "vscode";
import { ConfigList, ExtensionList } from "./types";

export const CONFIG_KEY = "vscode-extension-profiles";
export const CONFIG_KEY_LIST = "list";
export const CONFIG_LIST_EXTENSIONS = "extensions";

type ConfigList = {
[key: string]: String[];
};

export function getProfiles(): ConfigList[] {
export function getProfiles(): ConfigList {
return vscode.workspace.getConfiguration(CONFIG_KEY).get<any>(CONFIG_KEY_LIST);
}

export function getExtensionList(): ExtensionList {
return vscode.workspace.getConfiguration(CONFIG_KEY).get<any>(CONFIG_LIST_EXTENSIONS);
}

export async function setExtensionList(value: ExtensionList) {
await vscode.workspace.getConfiguration(CONFIG_KEY).update(CONFIG_LIST_EXTENSIONS, value, vscode.ConfigurationTarget.Global);
}
3 changes: 0 additions & 3 deletions src/constans.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Commands, CommandTypes } from "./commands";
export async function activate(ctx: vscode.ExtensionContext) {
// Registration commands
for (const key in Commands) {
ctx.subscriptions.push(vscode.commands.registerCommand(key, () => Commands[key as typeof CommandTypes[number]]({ ctx })));
ctx.subscriptions.push(vscode.commands.registerCommand(key, async () => Commands[key as typeof CommandTypes[number]]({ ctx })));
}
}

Expand Down
45 changes: 45 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { open } from "sqlite";
import * as sqlite3 from "sqlite3";
import * as vscode from "vscode";
import { ExtensionValue, StorageValue } from "./types";
import { getGlobalStorage } from "./utils";


export async function getDisabledExtensionsInStorage() {
let pathDB = getGlobalStorage() + "/state.vscdb";

const db = await open({
filename: pathDB,
driver: sqlite3.Database,
});

let data = (await db.get("SELECT key, value FROM ItemTable WHERE key = ?", "extensionsIdentifiers/disabled")) as StorageValue;
let value = JSON.parse(data.value) as ExtensionValue[];

return value;
}

export async function getAllExtensions() {
const disabled = await getDisabledExtensionsInStorage();
const enabled = getEnabledExtensions();

// https://github.com/microsoft/vscode/issues/15466
// vscode.extensions.getExtension not working disabled extensions
// for (const item of disabled) {
// let ext = vscode.extensions.getExtension(item.id);
// item.label = ext?.packageJSON.displayName || item.id;
// }

return [...disabled, ...enabled];
}

export function getEnabledExtensions() {

return vscode.extensions.all
.filter((e) => /^(?!\/Applications\/Visual Studio Code.app\/).*$/g.test(e.extensionPath))
.map(item =>({
id: item.id,
uuid: item?.packageJSON.uuid,
label: item?.packageJSON.displayName
}));
}
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type StorageValue = { key: string; value: string };
export type ExtensionValue = { id: string; uuid: string; label?: string };

export type ConfigList = {
[key: string]: [string];
};

export type ExtensionList = {
[key: string]: {
uuid: string;
label?: string;
};
};
Loading

0 comments on commit 41b8c93

Please sign in to comment.