extensionManagementUtil.ts 4.1 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { ILocalExtension, IGalleryExtension, IExtensionIdentifier, IReportedExtension, IExtensionIdentifierWithVersion } from 'vs/platform/extensionManagement/common/extensionManagement';
S
Sandeep Somavarapu 已提交
7
import { compareIgnoreCase } from 'vs/base/common/strings';
A
Alex Dima 已提交
8
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
9

S
Sandeep Somavarapu 已提交
10 11 12 13
export function areSameExtensions(a: IExtensionIdentifier, b: IExtensionIdentifier): boolean {
	if (a.uuid && b.uuid) {
		return a.uuid === b.uuid;
	}
14 15 16
	if (a.id === b.id) {
		return true;
	}
S
Sandeep Somavarapu 已提交
17 18 19
	return compareIgnoreCase(a.id, b.id) === 0;
}

20 21 22 23 24
export class ExtensionIdentifierWithVersion implements IExtensionIdentifierWithVersion {

	readonly id: string;
	readonly uuid?: string;

A
Alex Dima 已提交
25
	constructor(
26
		identifier: IExtensionIdentifier,
A
Alex Dima 已提交
27
		readonly version: string
28 29 30 31
	) {
		this.id = identifier.id;
		this.uuid = identifier.uuid;
	}
A
Alex Dima 已提交
32 33

	key(): string {
34
		return `${this.id}-${this.version}`;
A
Alex Dima 已提交
35 36 37 38 39 40
	}

	equals(o: any): boolean {
		if (!(o instanceof ExtensionIdentifierWithVersion)) {
			return false;
		}
41
		return areSameExtensions(this, o) && this.version === o.version;
A
Alex Dima 已提交
42 43 44
	}
}

S
Sandeep Somavarapu 已提交
45 46
export function adoptToGalleryExtensionId(id: string): string {
	return id.toLocaleLowerCase();
47 48
}

49
export function getGalleryExtensionId(publisher: string, name: string): string {
S
Sandeep Somavarapu 已提交
50
	return `${publisher.toLocaleLowerCase()}.${name.toLocaleLowerCase()}`;
51 52
}

S
Sandeep Somavarapu 已提交
53 54
export function groupByExtension<T>(extensions: T[], getExtensionIdentifier: (t: T) => IExtensionIdentifier): T[][] {
	const byExtension: T[][] = [];
M
Matt Bierner 已提交
55
	const findGroup = (extension: T) => {
S
Sandeep Somavarapu 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
		for (const group of byExtension) {
			if (group.some(e => areSameExtensions(getExtensionIdentifier(e), getExtensionIdentifier(extension)))) {
				return group;
			}
		}
		return null;
	};
	for (const extension of extensions) {
		const group = findGroup(extension);
		if (group) {
			group.push(extension);
		} else {
			byExtension.push([extension]);
		}
	}
	return byExtension;
}

74 75
export function getLocalExtensionTelemetryData(extension: ILocalExtension): any {
	return {
76
		id: extension.identifier.id,
77
		name: extension.manifest.name,
78
		galleryId: null,
79
		publisherId: extension.publisherId,
80
		publisherName: extension.manifest.publisher,
81
		publisherDisplayName: extension.publisherDisplayName,
82
		dependencies: extension.manifest.extensionDependencies && extension.manifest.extensionDependencies.length > 0
83 84 85
	};
}

K
kieferrm 已提交
86 87

/* __GDPR__FRAGMENT__
K
kieferrm 已提交
88 89 90 91
	"GalleryExtensionTelemetryData" : {
		"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
		"name": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
		"galleryId": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
92 93 94
		"publisherId": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
		"publisherName": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
		"publisherDisplayName": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
R
Ramya Achutha Rao 已提交
95
		"dependencies": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true },
K
kieferrm 已提交
96 97 98 99 100
		"${include}": [
			"${GalleryExtensionTelemetryData2}"
		]
	}
*/
101 102
export function getGalleryExtensionTelemetryData(extension: IGalleryExtension): any {
	return {
S
Sandeep Somavarapu 已提交
103
		id: extension.identifier.id,
104
		name: extension.name,
S
Sandeep Somavarapu 已提交
105
		galleryId: extension.identifier.uuid,
106 107 108
		publisherId: extension.publisherId,
		publisherName: extension.publisher,
		publisherDisplayName: extension.publisherDisplayName,
109
		dependencies: !!(extension.properties.dependencies && extension.properties.dependencies.length > 0),
110
		...extension.telemetryData
111
	};
112 113
}

A
Alex Dima 已提交
114
export const BetterMergeId = new ExtensionIdentifier('pprice.better-merge');
J
Joao Moreno 已提交
115 116 117 118 119 120 121 122 123 124 125

export function getMaliciousExtensionsSet(report: IReportedExtension[]): Set<string> {
	const result = new Set<string>();

	for (const extension of report) {
		if (extension.malicious) {
			result.add(extension.id.id);
		}
	}

	return result;
126
}