Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@
},
"devDependencies": {
"@types/chai": "4.2.1",
"@types/express": "^4.16.1",
"@types/lodash": "^4.14.123",
"@types/node-fetch": "^2.3.3",
"@types/express": "4.16.1",
"@types/express-serve-static-core": "4.17.28",
"@types/lodash": "4.14.123",
"@types/node-fetch": "2.3.3",
"@types/fs-extra": "^8.0.0",
"@types/mocha": "^5.2.6",
"@types/node": "^12.0.9",
Expand All @@ -175,15 +176,15 @@
"webpack-merge": "^4.2.1"
},
"dependencies": {
"@octokit/rest": "^16.23.2",
"@octokit/rest": "16.23.2",
"adm-zip": "^0.4.13",
"const": "^1.0.0",
"express": "^4.16.4",
"fs-extra": "^8.0.1",
"https-proxy-agent": "^2.2.1",
"lockfile": "^1.0.4",
"lodash": "^4.17.15",
"node-fetch": "^2.5.0",
"lodash": "4.17.15",
"node-fetch": "2.5.0",
"recursive-readdir": "^2.2.2",
"temp": "^0.9.0",
"vscode-chokidar": "^2.1.6"
Expand Down
111 changes: 111 additions & 0 deletions src/service/plugin.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"use strict";
import { execFileSync } from "child_process";
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";

export class ExtensionInformation {
Expand All @@ -20,6 +23,7 @@ export class ExtensionInformation {
item.name = obj.name;
item.publisher = obj.publisher;
item.version = obj.version;
item.disabled = !!obj.disabled;
return item;
} catch (err) {
throw new Error(err);
Expand All @@ -46,6 +50,7 @@ export class ExtensionInformation {
item.name = obj.name;
item.publisher = obj.publisher;
item.version = obj.version;
item.disabled = !!obj.disabled;

if (item.name !== "code-settings-sync") {
extList.push(item);
Expand All @@ -62,6 +67,11 @@ export class ExtensionInformation {
public name: string;
public version: string;
public publisher: string;
public disabled: boolean = false;

public get extensionId(): string {
return `${this.publisher}.${this.name}`;
}
}

export class ExtensionMetadata {
Expand All @@ -76,6 +86,63 @@ export class ExtensionMetadata {
}

export class PluginService {
public static GetEnabledExtensionIds(): string[] {
return vscode.extensions.all
.filter(ext => !ext.packageJSON.isBuiltin)
.map(ext => ext.id);
}

public static GetCodeCliCandidates(): string[] {
const candidates: string[] = [];
try {
const codeCliName = process.platform === "win32" ? "code.cmd" : "code";
// vscode.env.appRoot is usually ".../resources/app"
const bundledCli = path.join(vscode.env.appRoot, "bin", codeCliName);
candidates.push(bundledCli);
} catch (err) {
// ignore
}

candidates.push("code");
candidates.push("code-insiders");
return candidates;
}

public static TryGetInstalledExtensionIds(): string[] {
for (const candidate of PluginService.GetCodeCliCandidates()) {
try {
if (candidate.includes(path.sep) && !fs.existsSync(candidate)) {
continue;
}

const stdout = execFileSync(candidate, ["--list-extensions"], {
encoding: "utf8",
windowsHide: true,
timeout: 15000
});

return stdout
.split(/\r?\n/)
.map(l => l.trim())
.filter(Boolean);
} catch (err) {
// try next candidate
}
}

return [];
}

public static GetDisabledExtensionIds(): string[] {
const installed = PluginService.TryGetInstalledExtensionIds();
if (installed.length === 0) {
return [];
}

const enabled = new Set(PluginService.GetEnabledExtensionIds());
return installed.filter(id => !enabled.has(id));
}

public static GetMissingExtensions(
remoteExt: string,
ignoredExtensions: string[]
Expand Down Expand Up @@ -123,6 +190,7 @@ export class PluginService {
}

public static CreateExtensionList() {
const disabled = new Set(PluginService.GetDisabledExtensionIds());
return vscode.extensions.all
.filter(ext => !ext.packageJSON.isBuiltin)
.map(ext => {
Expand All @@ -144,10 +212,53 @@ export class PluginService {
info.name = ext.packageJSON.name;
info.publisher = ext.packageJSON.publisher;
info.version = ext.packageJSON.version;
info.disabled = disabled.has(ext.id);
return info;
});
}

public static async ApplyExtensionEnablement(
extensionsJson: string,
ignoredExtensions: string[],
notificationCallBack: (...data: any[]) => void
): Promise<void> {
const remoteExtensions = ExtensionInformation.fromJSONList(extensionsJson);
const ignored = new Set(ignoredExtensions || []);

for (const ext of remoteExtensions) {
if (ext.name === "code-settings-sync") {
continue;
}
if (ignored.has(ext.name)) {
continue;
}

try {
if (ext.disabled) {
notificationCallBack(
`[ ] - EXTENSION: ${ext.extensionId} - DISABLING`,
false
);
await vscode.commands.executeCommand(
"workbench.extensions.action.disableExtension",
ext.extensionId
);
} else {
notificationCallBack(
`[ ] - EXTENSION: ${ext.extensionId} - ENABLING`,
false
);
await vscode.commands.executeCommand(
"workbench.extensions.action.enableExtension",
ext.extensionId
);
}
} catch (err) {
// Best-effort: do not fail full sync if enablement can’t be applied.
}
}
}

public static async DeleteExtension(
extension: ExtensionInformation
): Promise<boolean> {
Expand Down
12 changes: 12 additions & 0 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,18 @@ export class Sync {
}
}
);

await PluginService.ApplyExtensionEnablement(
content,
ignoredExtensions,
(message: string) => {
if (!syncSetting.quietSync) {
Commons.outputChannel.appendLine(message);
} else {
console.log(message);
}
}
);
} catch (err) {
throw new Error(err);
}
Expand Down