SQL Operations Studio Public Preview 1 (0.23) release source code

This commit is contained in:
Karl Burtram
2017-11-09 14:30:27 -08:00
parent b88ecb8d93
commit 3cdac41339
8829 changed files with 759707 additions and 286 deletions

View File

@@ -0,0 +1,140 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { TPromise } from 'vs/base/common/winjs.base';
import { distinct } from 'vs/base/common/arrays';
import Event, { Emitter } from 'vs/base/common/event';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { IExtensionManagementService, DidUninstallExtensionEvent, IExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionManagement';
import { adoptToGalleryExtensionId, getIdAndVersionFromLocalExtensionId } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
const DISABLED_EXTENSIONS_STORAGE_PATH = 'extensions/disabled';
export class ExtensionEnablementService implements IExtensionEnablementService {
_serviceBrand: any;
private disposables: IDisposable[] = [];
private _onEnablementChanged = new Emitter<string>();
public onEnablementChanged: Event<string> = this._onEnablementChanged.event;
constructor(
@IStorageService private storageService: IStorageService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@IEnvironmentService private environmentService: IEnvironmentService,
@IExtensionManagementService private extensionManagementService: IExtensionManagementService
) {
extensionManagementService.onDidUninstallExtension(this.onDidUninstallExtension, this, this.disposables);
}
private get hasWorkspace(): boolean {
return this.contextService.hasWorkspace();
}
public getGloballyDisabledExtensions(): string[] {
return this.getDisabledExtensions(StorageScope.GLOBAL);
}
public getWorkspaceDisabledExtensions(): string[] {
return this.getDisabledExtensions(StorageScope.WORKSPACE);
}
public canEnable(identifier: string): boolean {
if (this.environmentService.disableExtensions) {
return false;
}
if (this.getGloballyDisabledExtensions().indexOf(identifier) !== -1) {
return true;
}
if (this.getWorkspaceDisabledExtensions().indexOf(identifier) !== -1) {
return true;
}
return false;
}
public setEnablement(identifier: string, enable: boolean, workspace: boolean = false): TPromise<boolean> {
if (workspace && !this.hasWorkspace) {
return TPromise.wrapError<boolean>(new Error(localize('noWorkspace', "No workspace.")));
}
if (this.environmentService.disableExtensions) {
return TPromise.wrap(false);
}
if (enable) {
if (workspace) {
return this.enableExtension(identifier, StorageScope.WORKSPACE);
} else {
return this.enableExtension(identifier, StorageScope.GLOBAL);
}
} else {
if (workspace) {
return this.disableExtension(identifier, StorageScope.WORKSPACE);
} else {
return this.disableExtension(identifier, StorageScope.GLOBAL);
}
}
}
private disableExtension(identifier: string, scope: StorageScope): TPromise<boolean> {
let disabledExtensions = this.getDisabledExtensions(scope);
const index = disabledExtensions.indexOf(identifier);
if (index === -1) {
disabledExtensions.push(identifier);
this.setDisabledExtensions(disabledExtensions, scope, identifier);
return TPromise.wrap(true);
}
return TPromise.wrap(false);
}
private enableExtension(identifier: string, scope: StorageScope, fireEvent = true): TPromise<boolean> {
let disabledExtensions = this.getDisabledExtensions(scope);
const index = disabledExtensions.indexOf(identifier);
if (index !== -1) {
disabledExtensions.splice(index, 1);
this.setDisabledExtensions(disabledExtensions, scope, identifier, fireEvent);
return TPromise.wrap(true);
}
return TPromise.wrap(false);
}
private getDisabledExtensions(scope: StorageScope): string[] {
if (scope === StorageScope.WORKSPACE && !this.hasWorkspace) {
return [];
}
const value = this.storageService.get(DISABLED_EXTENSIONS_STORAGE_PATH, scope, '');
return value ? distinct(value.split(',')).map(id => adoptToGalleryExtensionId(id)) : [];
}
private setDisabledExtensions(disabledExtensions: string[], scope: StorageScope, extension: string, fireEvent = true): void {
if (disabledExtensions.length) {
this.storageService.store(DISABLED_EXTENSIONS_STORAGE_PATH, disabledExtensions.join(','), scope);
} else {
this.storageService.remove(DISABLED_EXTENSIONS_STORAGE_PATH, scope);
}
if (fireEvent) {
this._onEnablementChanged.fire(extension);
}
}
private onDidUninstallExtension({ id, error }: DidUninstallExtensionEvent): void {
if (!error) {
id = getIdAndVersionFromLocalExtensionId(id).id;
if (id) {
this.enableExtension(id, StorageScope.WORKSPACE, false);
this.enableExtension(id, StorageScope.GLOBAL, false);
}
}
}
dispose(): void {
this.disposables = dispose(this.disposables);
}
}

View File

@@ -0,0 +1,303 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { localize } from 'vs/nls';
import { TPromise } from 'vs/base/common/winjs.base';
import Event from 'vs/base/common/event';
import { IPager } from 'vs/base/common/paging';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
export const EXTENSION_IDENTIFIER_PATTERN = '^([a-z0-9A-Z][a-z0-9\-A-Z]*)\\.([a-z0-9A-Z][a-z0-9\-A-Z]*)$';
export const EXTENSION_IDENTIFIER_REGEX = new RegExp(EXTENSION_IDENTIFIER_PATTERN);
export interface ICommand {
command: string;
title: string;
category?: string;
}
export interface IConfigurationProperty {
description: string;
type: string | string[];
default?: any;
}
export interface IConfiguration {
properties: { [key: string]: IConfigurationProperty; };
}
export interface IDebugger {
label?: string;
type: string;
runtime: string;
}
export interface IGrammar {
language: string;
}
export interface IJSONValidation {
fileMatch: string;
}
export interface IKeyBinding {
command: string;
key: string;
when?: string;
mac?: string;
linux?: string;
win?: string;
}
export interface ILanguage {
id: string;
extensions: string[];
aliases: string[];
}
export interface IMenu {
command: string;
alt?: string;
when?: string;
group?: string;
}
export interface ISnippet {
language: string;
}
export interface ITheme {
label: string;
}
export interface IView {
id: string;
name: string;
}
export interface IExtensionContributions {
commands?: ICommand[];
configuration?: IConfiguration;
debuggers?: IDebugger[];
grammars?: IGrammar[];
jsonValidation?: IJSONValidation[];
keybindings?: IKeyBinding[];
languages?: ILanguage[];
menus?: { [context: string]: IMenu[] };
snippets?: ISnippet[];
themes?: ITheme[];
views?: { [location: string]: IView[] };
}
export interface IExtensionManifest {
name: string;
publisher: string;
version: string;
engines: { vscode: string };
displayName?: string;
description?: string;
main?: string;
icon?: string;
categories?: string[];
activationEvents?: string[];
extensionDependencies?: string[];
contributes?: IExtensionContributions;
}
export interface IGalleryExtensionProperties {
dependencies?: string[];
engine?: string;
}
export interface IGalleryExtensionAsset {
uri: string;
fallbackUri: string;
}
export interface IGalleryExtensionAssets {
manifest: IGalleryExtensionAsset;
readme: IGalleryExtensionAsset;
changelog: IGalleryExtensionAsset;
download: IGalleryExtensionAsset;
icon: IGalleryExtensionAsset;
license: IGalleryExtensionAsset;
}
export interface IGalleryExtension {
uuid: string;
id: string;
name: string;
version: string;
date: string;
displayName: string;
publisherId: string;
publisher: string;
publisherDisplayName: string;
description: string;
installCount: number;
rating: number;
ratingCount: number;
assets: IGalleryExtensionAssets;
properties: IGalleryExtensionProperties;
telemetryData: any;
}
export interface IGalleryMetadata {
id: string;
publisherId: string;
publisherDisplayName: string;
}
export enum LocalExtensionType {
System,
User
}
export interface ILocalExtension {
type: LocalExtensionType;
id: string;
manifest: IExtensionManifest;
metadata: IGalleryMetadata;
path: string;
readmeUrl: string;
changelogUrl: string;
}
export const IExtensionManagementService = createDecorator<IExtensionManagementService>('extensionManagementService');
export const IExtensionGalleryService = createDecorator<IExtensionGalleryService>('extensionGalleryService');
export enum SortBy {
NoneOrRelevance = 0,
LastUpdatedDate = 1,
Title = 2,
PublisherName = 3,
InstallCount = 4,
PublishedDate = 5,
AverageRating = 6,
WeightedRating = 12
}
export enum SortOrder {
Default = 0,
Ascending = 1,
Descending = 2
}
export interface IQueryOptions {
text?: string;
ids?: string[];
names?: string[];
pageSize?: number;
sortBy?: SortBy;
sortOrder?: SortOrder;
source?: string;
}
export enum StatisticType {
Uninstall = 'uninstall'
}
export interface IExtensionGalleryService {
_serviceBrand: any;
isEnabled(): boolean;
query(options?: IQueryOptions): TPromise<IPager<IGalleryExtension>>;
download(extension: IGalleryExtension): TPromise<string>;
reportStatistic(publisher: string, name: string, version: string, type: StatisticType): TPromise<void>;
getReadme(extension: IGalleryExtension): TPromise<string>;
getManifest(extension: IGalleryExtension): TPromise<IExtensionManifest>;
getChangelog(extension: IGalleryMetadata): TPromise<string>;
loadCompatibleVersion(extension: IGalleryExtension): TPromise<IGalleryExtension>;
getAllDependencies(extension: IGalleryExtension): TPromise<IGalleryExtension[]>;
}
export interface InstallExtensionEvent {
id: string;
zipPath?: string;
gallery?: IGalleryExtension;
}
export interface DidInstallExtensionEvent {
id: string;
zipPath?: string;
gallery?: IGalleryExtension;
local?: ILocalExtension;
error?: Error;
}
export interface DidUninstallExtensionEvent {
id: string;
error?: Error;
}
export interface IExtensionManagementService {
_serviceBrand: any;
onInstallExtension: Event<InstallExtensionEvent>;
onDidInstallExtension: Event<DidInstallExtensionEvent>;
onUninstallExtension: Event<string>;
onDidUninstallExtension: Event<DidUninstallExtensionEvent>;
install(zipPath: string): TPromise<void>;
installFromGallery(extension: IGalleryExtension, promptToInstallDependencies?: boolean): TPromise<void>;
uninstall(extension: ILocalExtension, force?: boolean): TPromise<void>;
getInstalled(type?: LocalExtensionType): TPromise<ILocalExtension[]>;
}
export const IExtensionEnablementService = createDecorator<IExtensionEnablementService>('extensionEnablementService');
// TODO: @sandy: Merge this into IExtensionManagementService when we have a storage service available in Shared process
export interface IExtensionEnablementService {
_serviceBrand: any;
/**
* Event to listen on for extension enablement changes
*/
onEnablementChanged: Event<string>;
/**
* Returns all globally disabled extension identifiers.
* Returns an empty array if none exist.
*/
getGloballyDisabledExtensions(): string[];
/**
* Returns all workspace disabled extension identifiers.
* Returns an empty array if none exist or workspace does not exist.
*/
getWorkspaceDisabledExtensions(): string[];
/**
* Returns `true` if given extension can be enabled by calling `setEnablement`, otherwise false`.
*/
canEnable(identifier: string): boolean;
/**
* Enable or disable the given extension.
* if `workspace` is `true` then enablement is done for workspace, otherwise globally.
*
* Returns a promise that resolves to boolean value.
* if resolves to `true` then requires restart for the change to take effect.
*
* Throws error if enablement is requested for workspace and there is no workspace
*/
setEnablement(identifier: string, enable: boolean, workspace?: boolean): TPromise<boolean>;
}
export const IExtensionTipsService = createDecorator<IExtensionTipsService>('extensionTipsService');
export interface IExtensionTipsService {
_serviceBrand: any;
getRecommendations(): string[];
getWorkspaceRecommendations(): TPromise<string[]>;
getKeymapRecommendations(): string[];
getKeywordsForExtension(extension: string): string[];
getRecommendationsForExtension(extension: string): string[];
}
export const ExtensionsLabel = localize('extensions', "Extensions");
export const ExtensionsChannelId = 'extensions';
export const PreferencesLabel = localize('preferences', "Preferences");

View File

@@ -0,0 +1,87 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { TPromise } from 'vs/base/common/winjs.base';
import { IChannel, eventToCall, eventFromCall } from 'vs/base/parts/ipc/common/ipc';
import { IExtensionManagementService, ILocalExtension, InstallExtensionEvent, DidInstallExtensionEvent, IGalleryExtension, LocalExtensionType, DidUninstallExtensionEvent } from './extensionManagement';
import Event, { buffer } from 'vs/base/common/event';
export interface IExtensionManagementChannel extends IChannel {
call(command: 'event:onInstallExtension'): TPromise<void>;
call(command: 'event:onDidInstallExtension'): TPromise<void>;
call(command: 'event:onUninstallExtension'): TPromise<void>;
call(command: 'event:onDidUninstallExtension'): TPromise<void>;
call(command: 'install', path: string): TPromise<void>;
call(command: 'installFromGallery', extension: IGalleryExtension): TPromise<void>;
call(command: 'uninstall', args: [ILocalExtension, boolean]): TPromise<void>;
call(command: 'getInstalled'): TPromise<ILocalExtension[]>;
call(command: string, arg?: any): TPromise<any>;
}
export class ExtensionManagementChannel implements IExtensionManagementChannel {
onInstallExtension: Event<InstallExtensionEvent>;
onDidInstallExtension: Event<DidInstallExtensionEvent>;
onUninstallExtension: Event<string>;
onDidUninstallExtension: Event<DidUninstallExtensionEvent>;
constructor(private service: IExtensionManagementService) {
this.onInstallExtension = buffer(service.onInstallExtension, true);
this.onDidInstallExtension = buffer(service.onDidInstallExtension, true);
this.onUninstallExtension = buffer(service.onUninstallExtension, true);
this.onDidUninstallExtension = buffer(service.onDidUninstallExtension, true);
}
call(command: string, arg?: any): TPromise<any> {
switch (command) {
case 'event:onInstallExtension': return eventToCall(this.onInstallExtension);
case 'event:onDidInstallExtension': return eventToCall(this.onDidInstallExtension);
case 'event:onUninstallExtension': return eventToCall(this.onUninstallExtension);
case 'event:onDidUninstallExtension': return eventToCall(this.onDidUninstallExtension);
case 'install': return this.service.install(arg);
case 'installFromGallery': return this.service.installFromGallery(arg[0], arg[1]);
case 'uninstall': return this.service.uninstall(arg[0], arg[1]);
case 'getInstalled': return this.service.getInstalled(arg);
}
return undefined;
}
}
export class ExtensionManagementChannelClient implements IExtensionManagementService {
_serviceBrand: any;
constructor(private channel: IExtensionManagementChannel) { }
private _onInstallExtension = eventFromCall<InstallExtensionEvent>(this.channel, 'event:onInstallExtension');
get onInstallExtension(): Event<InstallExtensionEvent> { return this._onInstallExtension; }
private _onDidInstallExtension = eventFromCall<DidInstallExtensionEvent>(this.channel, 'event:onDidInstallExtension');
get onDidInstallExtension(): Event<DidInstallExtensionEvent> { return this._onDidInstallExtension; }
private _onUninstallExtension = eventFromCall<string>(this.channel, 'event:onUninstallExtension');
get onUninstallExtension(): Event<string> { return this._onUninstallExtension; }
private _onDidUninstallExtension = eventFromCall<DidUninstallExtensionEvent>(this.channel, 'event:onDidUninstallExtension');
get onDidUninstallExtension(): Event<DidUninstallExtensionEvent> { return this._onDidUninstallExtension; }
install(zipPath: string): TPromise<void> {
return this.channel.call('install', zipPath);
}
installFromGallery(extension: IGalleryExtension, promptToInstallDependencies: boolean = true): TPromise<void> {
return this.channel.call('installFromGallery', [extension, promptToInstallDependencies]);
}
uninstall(extension: ILocalExtension, force = false): TPromise<void> {
return this.channel.call('uninstall', [extension, force]);
}
getInstalled(type: LocalExtensionType = null): TPromise<ILocalExtension[]> {
return this.channel.call('getInstalled', type);
}
}

View File

@@ -0,0 +1,97 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { ILocalExtension, IGalleryExtension, IExtensionManifest, EXTENSION_IDENTIFIER_REGEX, IExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionManagement';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
export function areSameExtensions(a: { id: string }, b: { id: string }): boolean {
if (a.id === b.id) {
return true;
}
return adoptToGalleryExtensionId(a.id) === adoptToGalleryExtensionId(b.id);
}
export function getGalleryExtensionId(publisher: string, name: string): string {
return `${publisher}.${name.toLocaleLowerCase()}`;
}
export function getLocalExtensionIdFromGallery(extension: IGalleryExtension, version: string): string {
return getLocalExtensionId(extension.id, version);
}
export function getLocalExtensionIdFromManifest(manifest: IExtensionManifest): string {
return getLocalExtensionId(getGalleryExtensionId(manifest.publisher, manifest.name), manifest.version);
}
export function getGalleryExtensionIdFromLocal(local: ILocalExtension): string {
return getGalleryExtensionId(local.manifest.publisher, local.manifest.name);
}
export function getIdAndVersionFromLocalExtensionId(localExtensionId: string): { id: string, version: string } {
const matches = /^([^.]+\..+)-(\d+\.\d+\.\d+)$/.exec(localExtensionId);
if (matches && matches[1] && matches[2]) {
return { id: adoptToGalleryExtensionId(matches[1]), version: matches[2] };
}
return {
id: adoptToGalleryExtensionId(localExtensionId),
version: null
};
}
export function adoptToGalleryExtensionId(id: string): string {
return id.replace(EXTENSION_IDENTIFIER_REGEX, (match, publisher: string, name: string) => getGalleryExtensionId(publisher, name));
}
function getLocalExtensionId(id: string, version: string): string {
return `${id}-${version}`;
}
export function getLocalExtensionTelemetryData(extension: ILocalExtension): any {
return {
id: getGalleryExtensionIdFromLocal(extension),
name: extension.manifest.name,
galleryId: null,
publisherId: extension.metadata ? extension.metadata.publisherId : null,
publisherName: extension.manifest.publisher,
publisherDisplayName: extension.metadata ? extension.metadata.publisherDisplayName : null,
dependencies: extension.manifest.extensionDependencies && extension.manifest.extensionDependencies.length > 0
};
}
export function getGalleryExtensionTelemetryData(extension: IGalleryExtension): any {
return {
id: extension.id,
name: extension.name,
galleryId: extension.uuid,
publisherId: extension.publisherId,
publisherName: extension.publisher,
publisherDisplayName: extension.publisherDisplayName,
dependencies: extension.properties.dependencies.length > 0,
...extension.telemetryData
};
}
const BetterMergeCheckKey = 'extensions/bettermergecheck';
export const BetterMergeDisabledNowKey = 'extensions/bettermergedisablednow';
export const BetterMergeId = 'pprice.better-merge';
/**
* Globally disabled extensions, taking care of disabling obsolete extensions.
*/
export function getGloballyDisabledExtensions(extensionEnablementService: IExtensionEnablementService, storageService: IStorageService, installedExtensions: { id: string; }[]) {
const globallyDisabled = extensionEnablementService.getGloballyDisabledExtensions();
if (!storageService.getBoolean(BetterMergeCheckKey, StorageScope.GLOBAL, false)) {
storageService.store(BetterMergeCheckKey, true);
if (globallyDisabled.indexOf(BetterMergeId) === -1 && installedExtensions.some(d => d.id === BetterMergeId)) {
globallyDisabled.push(BetterMergeId);
extensionEnablementService.setEnablement(BetterMergeId, false);
storageService.store(BetterMergeDisabledNowKey, true);
}
}
return globallyDisabled;
}

View File

@@ -0,0 +1,33 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { cloneAndChange } from 'vs/base/common/objects';
import { IExtensionManifest } from 'vs/platform/extensionManagement/common/extensionManagement';
const nlsRegex = /^%([\w\d.]+)%$/i;
export interface ITranslations {
[key: string]: string;
}
export function localizeManifest(manifest: IExtensionManifest, translations: ITranslations): IExtensionManifest {
const patcher = value => {
if (typeof value !== 'string') {
return undefined;
}
const match = nlsRegex.exec(value);
if (!match) {
return undefined;
}
return translations[match[1]] || value;
};
return cloneAndChange(manifest, patcher);
}

View File

@@ -0,0 +1,642 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { tmpdir } from 'os';
import * as path from 'path';
import { TPromise } from 'vs/base/common/winjs.base';
import * as uuid from 'vs/base/common/uuid';
import { distinct } from 'vs/base/common/arrays';
import { getErrorMessage, isPromiseCanceledError } from 'vs/base/common/errors';
import { StatisticType, IGalleryExtension, IExtensionGalleryService, IGalleryExtensionAsset, IQueryOptions, SortBy, SortOrder, IExtensionManifest } from 'vs/platform/extensionManagement/common/extensionManagement';
import { getGalleryExtensionId, getGalleryExtensionTelemetryData, adoptToGalleryExtensionId } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
import { assign, getOrDefault } from 'vs/base/common/objects';
import { IRequestService } from 'vs/platform/request/node/request';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IPager } from 'vs/base/common/paging';
import { IRequestOptions, IRequestContext, download, asJson, asText } from 'vs/base/node/request';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import pkg from 'vs/platform/node/package';
import product from 'vs/platform/node/product';
import { isVersionValid } from 'vs/platform/extensions/node/extensionValidator';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
interface IRawGalleryExtensionFile {
assetType: string;
source: string;
}
interface IRawGalleryExtensionProperty {
key: string;
value: string;
}
interface IRawGalleryExtensionVersion {
version: string;
lastUpdated: string;
assetUri: string;
fallbackAssetUri: string;
files: IRawGalleryExtensionFile[];
properties?: IRawGalleryExtensionProperty[];
}
interface IRawGalleryExtensionStatistics {
statisticName: string;
value: number;
}
interface IRawGalleryExtension {
extensionId: string;
extensionName: string;
displayName: string;
shortDescription: string;
publisher: { displayName: string, publisherId: string, publisherName: string; };
versions: IRawGalleryExtensionVersion[];
statistics: IRawGalleryExtensionStatistics[];
}
interface IRawGalleryQueryResult {
results: {
extensions: IRawGalleryExtension[];
resultMetadata: {
metadataType: string;
metadataItems: {
name: string;
count: number;
}[];
}[]
}[];
}
enum Flags {
None = 0x0,
IncludeVersions = 0x1,
IncludeFiles = 0x2,
IncludeCategoryAndTags = 0x4,
IncludeSharedAccounts = 0x8,
IncludeVersionProperties = 0x10,
ExcludeNonValidated = 0x20,
IncludeInstallationTargets = 0x40,
IncludeAssetUri = 0x80,
IncludeStatistics = 0x100,
IncludeLatestVersionOnly = 0x200,
Unpublished = 0x1000
}
function flagsToString(...flags: Flags[]): string {
return String(flags.reduce((r, f) => r | f, 0));
}
enum FilterType {
Tag = 1,
ExtensionId = 4,
Category = 5,
ExtensionName = 7,
Target = 8,
Featured = 9,
SearchText = 10,
ExcludeWithFlags = 12
}
const AssetType = {
Icon: 'Microsoft.VisualStudio.Services.Icons.Default',
Details: 'Microsoft.VisualStudio.Services.Content.Details',
Changelog: 'Microsoft.VisualStudio.Services.Content.Changelog',
Manifest: 'Microsoft.VisualStudio.Code.Manifest',
VSIX: 'Microsoft.VisualStudio.Services.VSIXPackage',
License: 'Microsoft.VisualStudio.Services.Content.License',
};
const PropertyType = {
Dependency: 'Microsoft.VisualStudio.Code.ExtensionDependencies',
Engine: 'Microsoft.VisualStudio.Code.Engine'
};
interface ICriterium {
filterType: FilterType;
value?: string;
}
const DefaultPageSize = 10;
interface IQueryState {
pageNumber: number;
pageSize: number;
sortBy: SortBy;
sortOrder: SortOrder;
flags: Flags;
criteria: ICriterium[];
assetTypes: string[];
}
const DefaultQueryState: IQueryState = {
pageNumber: 1,
pageSize: DefaultPageSize,
sortBy: SortBy.NoneOrRelevance,
sortOrder: SortOrder.Default,
flags: Flags.None,
criteria: [],
assetTypes: []
};
class Query {
constructor(private state = DefaultQueryState) { }
get pageNumber(): number { return this.state.pageNumber; }
get pageSize(): number { return this.state.pageSize; }
get sortBy(): number { return this.state.sortBy; }
get sortOrder(): number { return this.state.sortOrder; }
get flags(): number { return this.state.flags; }
withPage(pageNumber: number, pageSize: number = this.state.pageSize): Query {
return new Query(assign({}, this.state, { pageNumber, pageSize }));
}
withFilter(filterType: FilterType, ...values: string[]): Query {
const criteria = [
...this.state.criteria,
...values.map(value => ({ filterType, value }))
];
return new Query(assign({}, this.state, { criteria }));
}
withSortBy(sortBy: SortBy): Query {
return new Query(assign({}, this.state, { sortBy }));
}
withSortOrder(sortOrder: SortOrder): Query {
return new Query(assign({}, this.state, { sortOrder }));
}
withFlags(...flags: Flags[]): Query {
return new Query(assign({}, this.state, { flags: flags.reduce((r, f) => r | f, 0) }));
}
withAssetTypes(...assetTypes: string[]): Query {
return new Query(assign({}, this.state, { assetTypes }));
}
get raw(): any {
const { criteria, pageNumber, pageSize, sortBy, sortOrder, flags, assetTypes } = this.state;
const filters = [{ criteria, pageNumber, pageSize, sortBy, sortOrder }];
return { filters, assetTypes, flags };
}
get searchText(): string {
const criterium = this.state.criteria.filter(criterium => criterium.filterType === FilterType.SearchText)[0];
return criterium ? criterium.value : '';
}
}
function getStatistic(statistics: IRawGalleryExtensionStatistics[], name: string): number {
const result = (statistics || []).filter(s => s.statisticName === name)[0];
return result ? result.value : 0;
}
function getVersionAsset(version: IRawGalleryExtensionVersion, type: string): IGalleryExtensionAsset {
const result = version.files.filter(f => f.assetType === type)[0];
if (!result) {
if (type === AssetType.Icon) {
const uri = require.toUrl('./media/defaultIcon.png');
return { uri, fallbackUri: uri };
}
return null;
}
if (type === AssetType.VSIX) {
return {
uri: `${version.fallbackAssetUri}/${type}?redirect=true&install=true`,
fallbackUri: `${version.fallbackAssetUri}/${type}?install=true`
};
}
return {
uri: `${version.assetUri}/${type}`,
fallbackUri: `${version.fallbackAssetUri}/${type}`
};
}
function getDependencies(version: IRawGalleryExtensionVersion): string[] {
const values = version.properties ? version.properties.filter(p => p.key === PropertyType.Dependency) : [];
const value = values.length > 0 && values[0].value;
return value ? value.split(',').map(v => adoptToGalleryExtensionId(v)) : [];
}
function getEngine(version: IRawGalleryExtensionVersion): string {
const values = version.properties ? version.properties.filter(p => p.key === PropertyType.Engine) : [];
return (values.length > 0 && values[0].value) || '';
}
function toExtension(galleryExtension: IRawGalleryExtension, extensionsGalleryUrl: string, index: number, query: Query, querySource?: string): IGalleryExtension {
const [version] = galleryExtension.versions;
const assets = {
manifest: getVersionAsset(version, AssetType.Manifest),
readme: getVersionAsset(version, AssetType.Details),
changelog: getVersionAsset(version, AssetType.Changelog),
download: getVersionAsset(version, AssetType.VSIX),
icon: getVersionAsset(version, AssetType.Icon),
license: getVersionAsset(version, AssetType.License)
};
return {
uuid: galleryExtension.extensionId,
id: getGalleryExtensionId(galleryExtension.publisher.publisherName, galleryExtension.extensionName),
name: galleryExtension.extensionName,
version: version.version,
date: version.lastUpdated,
displayName: galleryExtension.displayName,
publisherId: galleryExtension.publisher.publisherId,
publisher: galleryExtension.publisher.publisherName,
publisherDisplayName: galleryExtension.publisher.displayName,
description: galleryExtension.shortDescription || '',
installCount: getStatistic(galleryExtension.statistics, 'install'),
rating: getStatistic(galleryExtension.statistics, 'averagerating'),
ratingCount: getStatistic(galleryExtension.statistics, 'ratingcount'),
assets,
properties: {
dependencies: getDependencies(version),
engine: getEngine(version)
},
telemetryData: {
index: ((query.pageNumber - 1) * query.pageSize) + index,
searchText: query.searchText,
querySource
}
};
}
export class ExtensionGalleryService implements IExtensionGalleryService {
_serviceBrand: any;
private extensionsGalleryUrl: string;
private readonly commonHTTPHeaders: { [key: string]: string; };
constructor(
@IRequestService private requestService: IRequestService,
@IEnvironmentService private environmentService: IEnvironmentService,
@ITelemetryService private telemetryService: ITelemetryService,
@IConfigurationService private configurationService: IConfigurationService
) {
const config = product.extensionsGallery;
this.extensionsGalleryUrl = config && config.serviceUrl;
this.commonHTTPHeaders = {
'X-Market-Client-Id': `VSCode ${pkg.version}`,
'User-Agent': `VSCode ${pkg.version}`,
'X-Market-User-Id': this.environmentService.machineUUID
};
}
private api(path = ''): string {
return `${this.extensionsGalleryUrl}${path}`;
}
isEnabled(): boolean {
return !!this.extensionsGalleryUrl;
}
query(options: IQueryOptions = {}): TPromise<IPager<IGalleryExtension>> {
if (!this.isEnabled()) {
return TPromise.wrapError<IPager<IGalleryExtension>>(new Error('No extension gallery service configured.'));
}
const type = options.names ? 'ids' : (options.text ? 'text' : 'all');
let text = options.text || '';
const pageSize = getOrDefault(options, o => o.pageSize, 50);
this.telemetryService.publicLog('galleryService:query', { type, text });
let query = new Query()
.withFlags(Flags.IncludeLatestVersionOnly, Flags.IncludeAssetUri, Flags.IncludeStatistics, Flags.IncludeFiles, Flags.IncludeVersionProperties)
.withPage(1, pageSize)
.withFilter(FilterType.Target, 'Microsoft.VisualStudio.Code')
.withFilter(FilterType.ExcludeWithFlags, flagsToString(Flags.Unpublished))
.withAssetTypes(AssetType.Icon, AssetType.License, AssetType.Details, AssetType.Manifest, AssetType.VSIX, AssetType.Changelog);
if (text) {
// Use category filter instead of "category:themes"
text = text.replace(/\bcategory:("([^"]*)"|([^"]\S*))(\s+|\b|$)/g, (_, quotedCategory, category) => {
query = query.withFilter(FilterType.Category, category || quotedCategory);
return '';
});
// Use tag filter instead of "tag:debuggers"
text = text.replace(/\btag:("([^"]*)"|([^"]\S*))(\s+|\b|$)/g, (_, quotedTag, tag) => {
query = query.withFilter(FilterType.Tag, tag || quotedTag);
return '';
});
text = text.trim();
if (text) {
query = query.withFilter(FilterType.SearchText, text);
}
query = query.withSortBy(SortBy.NoneOrRelevance);
} else if (options.ids) {
query = query.withFilter(FilterType.ExtensionId, ...options.ids);
} else if (options.names) {
query = query.withFilter(FilterType.ExtensionName, ...options.names);
} else {
query = query.withSortBy(SortBy.InstallCount);
}
if (typeof options.sortBy === 'number') {
query = query.withSortBy(options.sortBy);
}
if (typeof options.sortOrder === 'number') {
query = query.withSortOrder(options.sortOrder);
}
return this.queryGallery(query).then(({ galleryExtensions, total }) => {
const extensions = galleryExtensions.map((e, index) => toExtension(e, this.extensionsGalleryUrl, index, query, options.source));
const pageSize = query.pageSize;
const getPage = pageIndex => {
const nextPageQuery = query.withPage(pageIndex + 1);
return this.queryGallery(nextPageQuery)
.then(({ galleryExtensions }) => galleryExtensions.map((e, index) => toExtension(e, this.extensionsGalleryUrl, index, nextPageQuery, options.source)));
};
return { firstPage: extensions, total, pageSize, getPage };
});
}
private async queryGallery(query: Query): TPromise<{ galleryExtensions: IRawGalleryExtension[], total: number; }> {
const commonHeaders = await this.commonHTTPHeaders;
const data = JSON.stringify(query.raw);
const headers = assign({}, commonHeaders, {
'Content-Type': 'application/json',
'Accept': 'application/json;api-version=3.0-preview.1',
'Accept-Encoding': 'gzip',
'Content-Length': data.length
});
const context = await this.requestService.request({
type: 'POST',
url: this.api('/extensionquery'),
data,
headers
});
if (context.res.statusCode >= 400 && context.res.statusCode < 500) {
return { galleryExtensions: [], total: 0 };
}
const result = await asJson<IRawGalleryQueryResult>(context);
const r = result.results[0];
const galleryExtensions = r.extensions;
const resultCount = r.resultMetadata && r.resultMetadata.filter(m => m.metadataType === 'ResultCount')[0];
const total = resultCount && resultCount.metadataItems.filter(i => i.name === 'TotalCount')[0].count || 0;
return { galleryExtensions, total };
}
async reportStatistic(publisher: string, name: string, version: string, type: StatisticType): TPromise<void> {
if (!this.isEnabled()) {
return;
}
try {
const headers = {
...await this.commonHTTPHeaders,
Accept: '*/*;api-version=4.0-preview.1'
};
await this.requestService.request({
type: 'POST',
url: this.api(`/publishers/${publisher}/extensions/${name}/${version}/stats?statType=${type}`),
headers
});
} catch (err) {
// noop
}
}
download(extension: IGalleryExtension): TPromise<string> {
return this.loadCompatibleVersion(extension).then(extension => {
const zipPath = path.join(tmpdir(), uuid.generateUuid());
const data = getGalleryExtensionTelemetryData(extension);
const startTime = new Date().getTime();
const log = duration => this.telemetryService.publicLog('galleryService:downloadVSIX', assign(data, { duration }));
return this.getAsset(extension.assets.download)
.then(context => download(zipPath, context))
.then(() => log(new Date().getTime() - startTime))
.then(() => zipPath);
});
}
getReadme(extension: IGalleryExtension): TPromise<string> {
return this.getAsset(extension.assets.readme)
.then(asText);
}
getManifest(extension: IGalleryExtension): TPromise<IExtensionManifest> {
return this.getAsset(extension.assets.manifest)
.then(asText)
.then(JSON.parse);
}
getChangelog(extension: IGalleryExtension): TPromise<string> {
return this.getAsset(extension.assets.changelog)
.then(asText);
}
getAllDependencies(extension: IGalleryExtension): TPromise<IGalleryExtension[]> {
return this.loadCompatibleVersion(<IGalleryExtension>extension)
.then(compatible => this.getDependenciesReccursively(compatible.properties.dependencies, [], extension));
}
loadCompatibleVersion(extension: IGalleryExtension): TPromise<IGalleryExtension> {
if (extension.properties.engine && this.isEngineValid(extension.properties.engine)) {
return TPromise.wrap(extension);
}
const query = new Query()
.withFlags(Flags.IncludeVersions, Flags.IncludeFiles, Flags.IncludeVersionProperties)
.withPage(1, 1)
.withFilter(FilterType.Target, 'Microsoft.VisualStudio.Code')
.withFilter(FilterType.ExcludeWithFlags, flagsToString(Flags.Unpublished))
.withAssetTypes(AssetType.Manifest, AssetType.VSIX)
.withFilter(FilterType.ExtensionId, extension.uuid);
return this.queryGallery(query).then(({ galleryExtensions }) => {
const [rawExtension] = galleryExtensions;
if (!rawExtension) {
return TPromise.wrapError<IGalleryExtension>(new Error(localize('notFound', "Extension not found")));
}
return this.getLastValidExtensionVersion(rawExtension, rawExtension.versions)
.then(rawVersion => {
extension.properties.dependencies = getDependencies(rawVersion);
extension.properties.engine = getEngine(rawVersion);
extension.assets.download = getVersionAsset(rawVersion, AssetType.VSIX);
extension.version = rawVersion.version;
return extension;
});
});
}
private loadDependencies(extensionNames: string[]): TPromise<IGalleryExtension[]> {
if (!extensionNames || extensionNames.length === 0) {
return TPromise.as([]);
}
let query = new Query()
.withFlags(Flags.IncludeLatestVersionOnly, Flags.IncludeAssetUri, Flags.IncludeStatistics, Flags.IncludeFiles, Flags.IncludeVersionProperties)
.withPage(1, extensionNames.length)
.withFilter(FilterType.Target, 'Microsoft.VisualStudio.Code')
.withFilter(FilterType.ExcludeWithFlags, flagsToString(Flags.Unpublished))
.withAssetTypes(AssetType.Icon, AssetType.License, AssetType.Details, AssetType.Manifest, AssetType.VSIX)
.withFilter(FilterType.ExtensionName, ...extensionNames);
return this.queryGallery(query).then(result => {
const dependencies = [];
const ids = [];
for (let index = 0; index < result.galleryExtensions.length; index++) {
const rawExtension = result.galleryExtensions[index];
if (ids.indexOf(rawExtension.extensionId) === -1) {
dependencies.push(toExtension(rawExtension, this.extensionsGalleryUrl, index, query));
ids.push(rawExtension.extensionId);
}
}
return dependencies;
});
}
private getDependenciesReccursively(toGet: string[], result: IGalleryExtension[], root: IGalleryExtension): TPromise<IGalleryExtension[]> {
if (!toGet || !toGet.length) {
return TPromise.wrap(result);
}
if (toGet.indexOf(`${root.publisher}.${root.name}`) !== -1 && result.indexOf(root) === -1) {
result.push(root);
}
toGet = result.length ? toGet.filter(e => !ExtensionGalleryService.hasExtensionByName(result, e)) : toGet;
if (!toGet.length) {
return TPromise.wrap(result);
}
return this.loadDependencies(toGet)
.then(loadedDependencies => {
const dependenciesSet = new Set<string>();
for (const dep of loadedDependencies) {
if (dep.properties.dependencies) {
dep.properties.dependencies.forEach(d => dependenciesSet.add(d));
}
}
result = distinct(result.concat(loadedDependencies), d => d.uuid);
const dependencies: string[] = [];
dependenciesSet.forEach(d => !ExtensionGalleryService.hasExtensionByName(result, d) && dependencies.push(d));
return this.getDependenciesReccursively(dependencies, result, root);
});
}
private getAsset(asset: IGalleryExtensionAsset, options: IRequestOptions = {}): TPromise<IRequestContext> {
const baseOptions = { type: 'GET' };
const headers = assign({}, this.commonHTTPHeaders, options.headers || {});
options = assign({}, options, baseOptions, { headers });
const url = asset.uri;
const fallbackUrl = asset.fallbackUri;
const firstOptions = assign({}, options, { url });
return this.requestService.request(firstOptions)
.then(context => {
if (context.res.statusCode === 200) {
return TPromise.as(context);
}
return asText(context)
.then(message => TPromise.wrapError<IRequestContext>(new Error(`Expected 200, got back ${context.res.statusCode} instead.\n\n${message}`)));
})
.then(null, err => {
if (isPromiseCanceledError(err)) {
return TPromise.wrapError<IRequestContext>(err);
}
const message = getErrorMessage(err);
this.telemetryService.publicLog('galleryService:requestError', { url, cdn: true, message });
this.telemetryService.publicLog('galleryService:cdnFallback', { url, message });
const fallbackOptions = assign({}, options, { url: fallbackUrl });
return this.requestService.request(fallbackOptions).then(null, err => {
if (isPromiseCanceledError(err)) {
return TPromise.wrapError<IRequestContext>(err);
}
const message = getErrorMessage(err);
this.telemetryService.publicLog('galleryService:requestError', { url: fallbackUrl, cdn: false, message });
return TPromise.wrapError<IRequestContext>(err);
});
});
}
private getLastValidExtensionVersion(extension: IRawGalleryExtension, versions: IRawGalleryExtensionVersion[]): TPromise<IRawGalleryExtensionVersion> {
const version = this.getLastValidExtensionVersionFromProperties(extension, versions);
if (version) {
return version;
}
return this.getLastValidExtensionVersionReccursively(extension, versions);
}
private getLastValidExtensionVersionFromProperties(extension: IRawGalleryExtension, versions: IRawGalleryExtensionVersion[]): TPromise<IRawGalleryExtensionVersion> {
for (const version of versions) {
const engine = getEngine(version);
if (!engine) {
return null;
}
if (this.isEngineValid(engine)) {
return TPromise.wrap(version);
}
}
return null;
}
private getLastValidExtensionVersionReccursively(extension: IRawGalleryExtension, versions: IRawGalleryExtensionVersion[]): TPromise<IRawGalleryExtensionVersion> {
if (!versions.length) {
return TPromise.wrapError<IRawGalleryExtensionVersion>(new Error(localize('noCompatible', "Couldn't find a compatible version of {0} with this version of Code.", extension.displayName || extension.extensionName)));
}
const version = versions[0];
const asset = getVersionAsset(version, AssetType.Manifest);
const headers = { 'Accept-Encoding': 'gzip' };
return this.getAsset(asset, { headers })
.then(context => asJson<IExtensionManifest>(context))
.then(manifest => {
const engine = manifest.engines.vscode;
if (!this.isEngineValid(engine)) {
return this.getLastValidExtensionVersionReccursively(extension, versions.slice(1));
}
version.properties = version.properties || [];
version.properties.push({ key: PropertyType.Engine, value: manifest.engines.vscode });
return version;
});
}
private isEngineValid(engine: string): boolean {
// TODO@joao: discuss with alex '*' doesn't seem to be a valid engine version
return engine === '*' || isVersionValid(pkg.version, engine);
}
private static hasExtensionByName(extensions: IGalleryExtension[], name: string): boolean {
for (const extension of extensions) {
if (`${extension.publisher}.${extension.name}` === name) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,601 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import nls = require('vs/nls');
import * as path from 'path';
import * as pfs from 'vs/base/node/pfs';
import * as errors from 'vs/base/common/errors';
import { assign } from 'vs/base/common/objects';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { flatten, distinct } from 'vs/base/common/arrays';
import { extract, buffer } from 'vs/base/node/zip';
import { TPromise } from 'vs/base/common/winjs.base';
import {
IExtensionManagementService, IExtensionGalleryService, ILocalExtension,
IGalleryExtension, IExtensionManifest, IGalleryMetadata,
InstallExtensionEvent, DidInstallExtensionEvent, DidUninstallExtensionEvent, LocalExtensionType,
StatisticType
} from 'vs/platform/extensionManagement/common/extensionManagement';
import { getLocalExtensionIdFromGallery, getLocalExtensionIdFromManifest, getGalleryExtensionIdFromLocal, getIdAndVersionFromLocalExtensionId, adoptToGalleryExtensionId } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
import { localizeManifest } from '../common/extensionNls';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { Limiter } from 'vs/base/common/async';
import Event, { Emitter } from 'vs/base/common/event';
import * as semver from 'semver';
import { groupBy, values } from 'vs/base/common/collections';
import URI from 'vs/base/common/uri';
import { IChoiceService, Severity } from 'vs/platform/message/common/message';
const SystemExtensionsRoot = path.normalize(path.join(URI.parse(require.toUrl('')).fsPath, '..', 'extensions'));
function parseManifest(raw: string): TPromise<{ manifest: IExtensionManifest; metadata: IGalleryMetadata; }> {
return new TPromise((c, e) => {
try {
const manifest = JSON.parse(raw);
const metadata = manifest.__metadata || null;
delete manifest.__metadata;
c({ manifest, metadata });
} catch (err) {
e(new Error(nls.localize('invalidManifest', "Extension invalid: package.json is not a JSON file.")));
}
});
}
function validate(zipPath: string): TPromise<IExtensionManifest> {
return buffer(zipPath, 'extension/package.json')
.then(buffer => parseManifest(buffer.toString('utf8')))
.then(({ manifest }) => TPromise.as(manifest));
}
function readManifest(extensionPath: string): TPromise<{ manifest: IExtensionManifest; metadata: IGalleryMetadata; }> {
const promises = [
pfs.readFile(path.join(extensionPath, 'package.json'), 'utf8')
.then(raw => parseManifest(raw)),
pfs.readFile(path.join(extensionPath, 'package.nls.json'), 'utf8')
.then(null, err => err.code !== 'ENOENT' ? TPromise.wrapError<string>(err) : '{}')
.then(raw => JSON.parse(raw))
];
return TPromise.join<any>(promises).then(([{ manifest, metadata }, translations]) => {
return {
manifest: localizeManifest(manifest, translations),
metadata
};
});
}
export class ExtensionManagementService implements IExtensionManagementService {
_serviceBrand: any;
private extensionsPath: string;
private obsoletePath: string;
private obsoleteFileLimiter: Limiter<void>;
private disposables: IDisposable[] = [];
private _onInstallExtension = new Emitter<InstallExtensionEvent>();
onInstallExtension: Event<InstallExtensionEvent> = this._onInstallExtension.event;
private _onDidInstallExtension = new Emitter<DidInstallExtensionEvent>();
onDidInstallExtension: Event<DidInstallExtensionEvent> = this._onDidInstallExtension.event;
private _onUninstallExtension = new Emitter<string>();
onUninstallExtension: Event<string> = this._onUninstallExtension.event;
private _onDidUninstallExtension = new Emitter<DidUninstallExtensionEvent>();
onDidUninstallExtension: Event<DidUninstallExtensionEvent> = this._onDidUninstallExtension.event;
constructor(
@IEnvironmentService private environmentService: IEnvironmentService,
@IChoiceService private choiceService: IChoiceService,
@IExtensionGalleryService private galleryService: IExtensionGalleryService
) {
this.extensionsPath = environmentService.extensionsPath;
this.obsoletePath = path.join(this.extensionsPath, '.obsolete');
this.obsoleteFileLimiter = new Limiter(1);
}
install(zipPath: string): TPromise<void> {
zipPath = path.resolve(zipPath);
return validate(zipPath).then<void>(manifest => {
const id = getLocalExtensionIdFromManifest(manifest);
return this.isObsolete(id).then(isObsolete => {
if (isObsolete) {
return TPromise.wrapError(new Error(nls.localize('restartCode', "Please restart Code before reinstalling {0}.", manifest.displayName || manifest.name)));
}
this._onInstallExtension.fire({ id, zipPath });
return this.installExtension(zipPath, id)
.then(
local => this._onDidInstallExtension.fire({ id, zipPath, local }),
error => { this._onDidInstallExtension.fire({ id, zipPath, error }); return TPromise.wrapError(error); }
);
});
});
}
installFromGallery(extension: IGalleryExtension, promptToInstallDependencies: boolean = true): TPromise<void> {
const id = getLocalExtensionIdFromGallery(extension, extension.version);
return this.isObsolete(id).then(isObsolete => {
if (isObsolete) {
return TPromise.wrapError<void>(new Error(nls.localize('restartCode', "Please restart Code before reinstalling {0}.", extension.displayName || extension.name)));
}
this._onInstallExtension.fire({ id, gallery: extension });
return this.installCompatibleVersion(extension, true, promptToInstallDependencies)
.then(
local => this._onDidInstallExtension.fire({ id, local, gallery: extension }),
error => {
this._onDidInstallExtension.fire({ id, gallery: extension, error });
return TPromise.wrapError(error);
}
);
});
}
private installCompatibleVersion(extension: IGalleryExtension, installDependencies: boolean, promptToInstallDependencies: boolean): TPromise<ILocalExtension> {
return this.galleryService.loadCompatibleVersion(extension)
.then(compatibleVersion => this.getDependenciesToInstall(extension, installDependencies)
.then(dependencies => {
if (!dependencies.length) {
return this.downloadAndInstall(compatibleVersion);
}
if (promptToInstallDependencies) {
const message = nls.localize('installDependeciesConfirmation', "Installing '{0}' also installs its dependencies. Would you like to continue?", extension.displayName);
const options = [
nls.localize('install', "Yes"),
nls.localize('doNotInstall', "No")
];
return this.choiceService.choose(Severity.Info, message, options, 1, true)
.then(value => {
if (value === 0) {
return this.installWithDependencies(compatibleVersion);
}
return TPromise.wrapError<ILocalExtension>(errors.canceled());
}, error => TPromise.wrapError<ILocalExtension>(errors.canceled()));
} else {
return this.installWithDependencies(compatibleVersion);
}
})
);
}
private getDependenciesToInstall(extension: IGalleryExtension, checkDependecies: boolean): TPromise<string[]> {
if (!checkDependecies) {
return TPromise.wrap([]);
}
// Filter out self
const dependencies = extension.properties.dependencies ? extension.properties.dependencies.filter(id => id !== extension.id) : [];
if (!dependencies.length) {
return TPromise.wrap([]);
}
// Filter out installed dependencies
return this.getInstalled().then(installed => {
return dependencies.filter(dep => installed.every(i => `${i.manifest.publisher}.${i.manifest.name}` !== dep));
});
}
private installWithDependencies(extension: IGalleryExtension): TPromise<ILocalExtension> {
return this.galleryService.getAllDependencies(extension)
.then(allDependencies => this.filterDependenciesToInstall(extension, allDependencies))
.then(toInstall => this.filterObsolete(...toInstall.map(i => getLocalExtensionIdFromGallery(i, i.version)))
.then((obsolete) => {
if (obsolete.length) {
return TPromise.wrapError<ILocalExtension>(new Error(nls.localize('restartCode', "Please restart Code before reinstalling {0}.", extension.displayName || extension.name)));
}
return this.bulkInstallWithDependencies(extension, toInstall);
})
);
}
private bulkInstallWithDependencies(extension: IGalleryExtension, dependecies: IGalleryExtension[]): TPromise<ILocalExtension> {
for (const dependency of dependecies) {
const id = getLocalExtensionIdFromGallery(dependency, dependency.version);
this._onInstallExtension.fire({ id, gallery: dependency });
}
return this.downloadAndInstall(extension)
.then(localExtension => {
return TPromise.join(dependecies.map((dep) => this.installCompatibleVersion(dep, false, false)))
.then(installedLocalExtensions => {
for (const installedLocalExtension of installedLocalExtensions) {
const gallery = this.getGalleryExtensionForLocalExtension(dependecies, installedLocalExtension);
this._onDidInstallExtension.fire({ id: installedLocalExtension.id, local: installedLocalExtension, gallery });
}
return localExtension;
}, error => {
return this.rollback(localExtension, dependecies).then(() => {
return TPromise.wrapError<ILocalExtension>(Array.isArray(error) ? error[error.length - 1] : error);
});
});
})
.then(localExtension => localExtension, error => {
for (const dependency of dependecies) {
this._onDidInstallExtension.fire({ id: getLocalExtensionIdFromGallery(dependency, dependency.version), gallery: dependency, error });
}
return TPromise.wrapError<ILocalExtension>(error);
});
}
private rollback(localExtension: ILocalExtension, dependecies: IGalleryExtension[]): TPromise<void> {
return this.doUninstall(localExtension)
.then(() => this.filterOutUninstalled(dependecies))
.then(installed => TPromise.join(installed.map((i) => this.doUninstall(i))))
.then(() => null);
}
private filterDependenciesToInstall(extension: IGalleryExtension, dependencies: IGalleryExtension[]): TPromise<IGalleryExtension[]> {
return this.getInstalled()
.then(local => {
return dependencies.filter(d => {
if (extension.uuid === d.uuid) {
return false;
}
const extensionId = getLocalExtensionIdFromGallery(d, d.version);
return local.every(local => local.id !== extensionId);
});
});
}
private filterOutUninstalled(extensions: IGalleryExtension[]): TPromise<ILocalExtension[]> {
return this.getInstalled()
.then(installed => installed.filter(local => !!this.getGalleryExtensionForLocalExtension(extensions, local)));
}
private getGalleryExtensionForLocalExtension(galleryExtensions: IGalleryExtension[], localExtension: ILocalExtension): IGalleryExtension {
const filtered = galleryExtensions.filter(galleryExtension => getLocalExtensionIdFromGallery(galleryExtension, galleryExtension.version) === localExtension.id);
return filtered.length ? filtered[0] : null;
}
private downloadAndInstall(extension: IGalleryExtension): TPromise<ILocalExtension> {
const id = getLocalExtensionIdFromGallery(extension, extension.version);
const metadata = {
id: extension.uuid,
publisherId: extension.publisherId,
publisherDisplayName: extension.publisherDisplayName,
};
return this.galleryService.download(extension)
.then(zipPath => validate(zipPath).then(() => zipPath))
.then(zipPath => this.installExtension(zipPath, id, metadata));
}
private installExtension(zipPath: string, id: string, metadata: IGalleryMetadata = null): TPromise<ILocalExtension> {
const extensionPath = path.join(this.extensionsPath, id);
return pfs.rimraf(extensionPath).then(() => {
return extract(zipPath, extensionPath, { sourcePath: 'extension', overwrite: true })
.then(() => readManifest(extensionPath))
.then(({ manifest }) => {
return pfs.readdir(extensionPath).then(children => {
const readme = children.filter(child => /^readme(\.txt|\.md|)$/i.test(child))[0];
const readmeUrl = readme ? URI.file(path.join(extensionPath, readme)).toString() : null;
const changelog = children.filter(child => /^changelog(\.txt|\.md|)$/i.test(child))[0];
const changelogUrl = changelog ? URI.file(path.join(extensionPath, changelog)).toString() : null;
const type = LocalExtensionType.User;
const local: ILocalExtension = { type, id, manifest, metadata, path: extensionPath, readmeUrl, changelogUrl };
const manifestPath = path.join(extensionPath, 'package.json');
return pfs.readFile(manifestPath, 'utf8')
.then(raw => parseManifest(raw))
.then(({ manifest }) => assign(manifest, { __metadata: metadata }))
.then(manifest => pfs.writeFile(manifestPath, JSON.stringify(manifest, null, '\t')))
.then(() => local);
});
});
});
}
uninstall(extension: ILocalExtension, force = false): TPromise<void> {
return this.removeOutdatedExtensions().then(() => {
return this.scanUserExtensions().then(installed => {
const promises = installed
.filter(e => e.manifest.publisher === extension.manifest.publisher && e.manifest.name === extension.manifest.name)
.map(e => this.checkForDependenciesAndUninstall(e, installed, force));
return TPromise.join(promises);
});
}).then(() => { /* drop resolved value */ });
}
private checkForDependenciesAndUninstall(extension: ILocalExtension, installed: ILocalExtension[], force: boolean): TPromise<void> {
return this.preUninstallExtension(extension)
.then(() => this.hasDependencies(extension, installed) ? this.promptForDependenciesAndUninstall(extension, installed, force) : this.promptAndUninstall(extension, installed, force))
.then(() => this.postUninstallExtension(extension),
error => {
this.postUninstallExtension(extension, error);
return TPromise.wrapError(error);
});
}
private hasDependencies(extension: ILocalExtension, installed: ILocalExtension[]): boolean {
if (extension.manifest.extensionDependencies && extension.manifest.extensionDependencies.length) {
return installed.some(i => extension.manifest.extensionDependencies.indexOf(getGalleryExtensionIdFromLocal(i)) !== -1);
}
return false;
}
private promptForDependenciesAndUninstall(extension: ILocalExtension, installed: ILocalExtension[], force: boolean): TPromise<void> {
if (force) {
const dependencies = distinct(this.getDependenciesToUninstallRecursively(extension, installed, [])).filter(e => e !== extension);
return this.uninstallWithDependencies(extension, dependencies, installed);
}
const message = nls.localize('uninstallDependeciesConfirmation', "Would you like to uninstall '{0}' only or its dependencies also?", extension.manifest.displayName || extension.manifest.name);
const options = [
nls.localize('uninstallOnly', "Only"),
nls.localize('uninstallAll', "All"),
nls.localize('cancel', "Cancel")
];
return this.choiceService.choose(Severity.Info, message, options, 2, true)
.then<void>(value => {
if (value === 0) {
return this.uninstallWithDependencies(extension, [], installed);
}
if (value === 1) {
const dependencies = distinct(this.getDependenciesToUninstallRecursively(extension, installed, [])).filter(e => e !== extension);
return this.uninstallWithDependencies(extension, dependencies, installed);
}
return TPromise.wrapError(errors.canceled());
}, error => TPromise.wrapError(errors.canceled()));
}
private promptAndUninstall(extension: ILocalExtension, installed: ILocalExtension[], force: boolean): TPromise<void> {
if (force) {
return this.uninstallWithDependencies(extension, [], installed);
}
const message = nls.localize('uninstallConfirmation', "Are you sure you want to uninstall '{0}'?", extension.manifest.displayName || extension.manifest.name);
const options = [
nls.localize('ok', "OK"),
nls.localize('cancel', "Cancel")
];
return this.choiceService.choose(Severity.Info, message, options, 1, true)
.then<void>(value => {
if (value === 0) {
return this.uninstallWithDependencies(extension, [], installed);
}
return TPromise.wrapError(errors.canceled());
}, error => TPromise.wrapError(errors.canceled()));
}
private uninstallWithDependencies(extension: ILocalExtension, dependencies: ILocalExtension[], installed: ILocalExtension[]): TPromise<void> {
const dependenciesToUninstall = this.filterDependents(extension, dependencies, installed);
let dependents = this.getDependents(extension, installed).filter(dependent => extension !== dependent && dependenciesToUninstall.indexOf(dependent) === -1);
if (dependents.length) {
return TPromise.wrapError<void>(new Error(this.getDependentsErrorMessage(extension, dependents)));
}
return TPromise.join([this.uninstallExtension(extension.id), ...dependenciesToUninstall.map(d => this.doUninstall(d))]).then(() => null);
}
private getDependentsErrorMessage(extension: ILocalExtension, dependents: ILocalExtension[]): string {
if (dependents.length === 1) {
return nls.localize('singleDependentError', "Cannot uninstall extension '{0}'. Extension '{1}' depends on this.",
extension.manifest.displayName || extension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name);
}
if (dependents.length === 2) {
return nls.localize('twoDependentsError', "Cannot uninstall extension '{0}'. Extensions '{1}' and '{2}' depend on this.",
extension.manifest.displayName || extension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);
}
return nls.localize('multipleDependentsError', "Cannot uninstall extension '{0}'. Extensions '{1}', '{2}' and others depend on this.",
extension.manifest.displayName || extension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);
}
private getDependenciesToUninstallRecursively(extension: ILocalExtension, installed: ILocalExtension[], checked: ILocalExtension[]): ILocalExtension[] {
if (checked.indexOf(extension) !== -1) {
return [];
}
checked.push(extension);
if (!extension.manifest.extensionDependencies || extension.manifest.extensionDependencies.length === 0) {
return [];
}
const dependenciesToUninstall = installed.filter(i => extension.manifest.extensionDependencies.indexOf(getGalleryExtensionIdFromLocal(i)) !== -1);
const depsOfDeps = [];
for (const dep of dependenciesToUninstall) {
depsOfDeps.push(...this.getDependenciesToUninstallRecursively(dep, installed, checked));
}
return [...dependenciesToUninstall, ...depsOfDeps];
}
private filterDependents(extension: ILocalExtension, dependencies: ILocalExtension[], installed: ILocalExtension[]): ILocalExtension[] {
installed = installed.filter(i => i !== extension && i.manifest.extensionDependencies && i.manifest.extensionDependencies.length > 0);
let result = dependencies.slice(0);
for (let i = 0; i < dependencies.length; i++) {
const dep = dependencies[i];
const dependents = this.getDependents(dep, installed).filter(e => dependencies.indexOf(e) === -1);
if (dependents.length) {
result.splice(i - (dependencies.length - result.length), 1);
}
}
return result;
}
private getDependents(extension: ILocalExtension, installed: ILocalExtension[]): ILocalExtension[] {
return installed.filter(e => e.manifest.extensionDependencies && e.manifest.extensionDependencies.indexOf(getGalleryExtensionIdFromLocal(extension)) !== -1);
}
private doUninstall(extension: ILocalExtension): TPromise<void> {
return this.preUninstallExtension(extension)
.then(() => this.uninstallExtension(extension.id))
.then(() => this.postUninstallExtension(extension),
error => {
this.postUninstallExtension(extension, error);
return TPromise.wrapError(error);
});
}
private preUninstallExtension(extension: ILocalExtension): TPromise<void> {
const extensionPath = path.join(this.extensionsPath, extension.id);
return pfs.exists(extensionPath)
.then(exists => exists ? null : TPromise.wrapError(new Error(nls.localize('notExists', "Could not find extension"))))
.then(() => this._onUninstallExtension.fire(extension.id));
}
private uninstallExtension(id: string): TPromise<void> {
const extensionPath = path.join(this.extensionsPath, id);
return this.setObsolete(id)
.then(() => pfs.rimraf(extensionPath))
.then(() => this.unsetObsolete(id));
}
private async postUninstallExtension(extension: ILocalExtension, error?: any): TPromise<void> {
if (!error) {
await this.galleryService.reportStatistic(extension.manifest.publisher, extension.manifest.name, extension.manifest.version, StatisticType.Uninstall);
}
this._onDidUninstallExtension.fire({ id: extension.id, error });
}
getInstalled(type: LocalExtensionType = null): TPromise<ILocalExtension[]> {
const promises = [];
if (type === null || type === LocalExtensionType.System) {
promises.push(this.scanSystemExtensions());
}
if (type === null || type === LocalExtensionType.User) {
promises.push(this.scanUserExtensions());
}
return TPromise.join<ILocalExtension[]>(promises).then(flatten);
}
private scanSystemExtensions(): TPromise<ILocalExtension[]> {
return this.scanExtensions(SystemExtensionsRoot, LocalExtensionType.System);
}
private scanUserExtensions(): TPromise<ILocalExtension[]> {
return this.scanExtensions(this.extensionsPath, LocalExtensionType.User).then(extensions => {
const byId = values(groupBy(extensions, p => getGalleryExtensionIdFromLocal(p)));
return byId.map(p => p.sort((a, b) => semver.rcompare(a.manifest.version, b.manifest.version))[0]);
});
}
private scanExtensions(root: string, type: LocalExtensionType): TPromise<ILocalExtension[]> {
const limiter = new Limiter(10);
return this.scanExtensionFolders(root)
.then(extensionIds => TPromise.join(extensionIds.map(id => {
const extensionPath = path.join(root, id);
const each = () => pfs.readdir(extensionPath).then(children => {
const readme = children.filter(child => /^readme(\.txt|\.md|)$/i.test(child))[0];
const readmeUrl = readme ? URI.file(path.join(extensionPath, readme)).toString() : null;
const changelog = children.filter(child => /^changelog(\.txt|\.md|)$/i.test(child))[0];
const changelogUrl = changelog ? URI.file(path.join(extensionPath, changelog)).toString() : null;
return readManifest(extensionPath)
.then<ILocalExtension>(({ manifest, metadata }) => {
if (manifest.extensionDependencies) {
manifest.extensionDependencies = manifest.extensionDependencies.map(id => adoptToGalleryExtensionId(id));
}
return { type, id, manifest, metadata, path: extensionPath, readmeUrl, changelogUrl };
});
}).then(null, () => null);
return limiter.queue(each);
})))
.then(result => result.filter(a => !!a));
}
private scanExtensionFolders(root: string): TPromise<string[]> {
return this.getObsoleteExtensions()
.then(obsolete => pfs.readdir(root).then(extensions => extensions.filter(id => !obsolete[id])));
}
removeDeprecatedExtensions(): TPromise<any> {
return TPromise.join([
this.removeOutdatedExtensions(),
this.removeObsoleteExtensions()
]);
}
private removeOutdatedExtensions(): TPromise<any> {
return this.getOutdatedExtensionIds()
.then(extensionIds => this.removeExtensions(extensionIds));
}
private removeObsoleteExtensions(): TPromise<any> {
return this.getObsoleteExtensions()
.then(obsolete => Object.keys(obsolete))
.then(extensionIds => this.removeExtensions(extensionIds));
}
private removeExtensions(extensionsIds: string[]): TPromise<any> {
return TPromise.join(extensionsIds.map(id => {
return pfs.rimraf(path.join(this.extensionsPath, id))
.then(() => this.withObsoleteExtensions(obsolete => delete obsolete[id]));
}));
}
private getOutdatedExtensionIds(): TPromise<string[]> {
return this.scanExtensionFolders(this.extensionsPath)
.then(folders => {
const galleryFolders = folders
.map(folder => (assign({ folder }, getIdAndVersionFromLocalExtensionId(folder))))
.filter(({ id, version }) => !!id && !!version);
const byId = values(groupBy(galleryFolders, p => p.id));
return flatten(byId.map(p => p.sort((a, b) => semver.rcompare(a.version, b.version)).slice(1)))
.map(a => a.folder);
});
}
private isObsolete(id: string): TPromise<boolean> {
return this.filterObsolete(id).then(obsolete => obsolete.length === 1);
}
private filterObsolete(...ids: string[]): TPromise<string[]> {
return this.withObsoleteExtensions(allObsolete => {
const obsolete = [];
for (const id of ids) {
if (!!allObsolete[id]) {
obsolete.push(id);
}
}
return obsolete;
});
}
private setObsolete(id: string): TPromise<void> {
return this.withObsoleteExtensions(obsolete => assign(obsolete, { [id]: true }));
}
private unsetObsolete(id: string): TPromise<void> {
return this.withObsoleteExtensions<void>(obsolete => delete obsolete[id]);
}
private getObsoleteExtensions(): TPromise<{ [id: string]: boolean; }> {
return this.withObsoleteExtensions(obsolete => obsolete);
}
private withObsoleteExtensions<T>(fn: (obsolete: { [id: string]: boolean; }) => T): TPromise<T> {
return this.obsoleteFileLimiter.queue(() => {
let result: T = null;
return pfs.readFile(this.obsoletePath, 'utf8')
.then(null, err => err.code === 'ENOENT' ? TPromise.as('{}') : TPromise.wrapError(err))
.then<{ [id: string]: boolean }>(raw => { try { return JSON.parse(raw); } catch (e) { return {}; } })
.then(obsolete => { result = fn(obsolete); return obsolete; })
.then(obsolete => {
if (Object.keys(obsolete).length === 0) {
return pfs.rimraf(this.obsoletePath);
} else {
const raw = JSON.stringify(obsolete);
return pfs.writeFile(this.obsoletePath, raw);
}
})
.then(() => result);
});
}
dispose() {
this.disposables = dispose(this.disposables);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@@ -0,0 +1,277 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { IExtensionManagementService, IExtensionEnablementService, DidUninstallExtensionEvent } from 'vs/platform/extensionManagement/common/extensionManagement';
import { ExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionEnablementService';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { Emitter } from 'vs/base/common/event';
import { StorageService, InMemoryLocalStorage } from 'vs/platform/storage/common/storageService';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
function storageService(instantiationService: TestInstantiationService): IStorageService {
let service = instantiationService.get(IStorageService);
if (!service) {
let workspaceContextService = instantiationService.get(IWorkspaceContextService);
if (!workspaceContextService) {
workspaceContextService = instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{
hasWorkspace: () => {
return true;
},
});
}
service = instantiationService.stub(IStorageService, instantiationService.createInstance(StorageService, new InMemoryLocalStorage(), new InMemoryLocalStorage()));
}
return service;
}
export class TestExtensionEnablementService extends ExtensionEnablementService {
constructor(instantiationService: TestInstantiationService) {
super(storageService(instantiationService), instantiationService.get(IWorkspaceContextService),
instantiationService.get(IEnvironmentService) || instantiationService.stub(IEnvironmentService, <IEnvironmentService>{}),
instantiationService.get(IExtensionManagementService) || instantiationService.stub(IExtensionManagementService, { onDidUninstallExtension: new Emitter() }));
}
public reset(): void {
this.getGloballyDisabledExtensions().forEach(d => this.setEnablement(d, true));
this.getWorkspaceDisabledExtensions().forEach(d => this.setEnablement(d, true, true));
}
}
suite('ExtensionEnablementService Test', () => {
let instantiationService: TestInstantiationService;
let testObject: IExtensionEnablementService;
const didUninstallEvent: Emitter<DidUninstallExtensionEvent> = new Emitter<DidUninstallExtensionEvent>();
setup(() => {
instantiationService = new TestInstantiationService();
instantiationService.stub(IExtensionManagementService, { onDidUninstallExtension: didUninstallEvent.event, });
testObject = new TestExtensionEnablementService(instantiationService);
});
teardown(() => {
(<ExtensionEnablementService>testObject).dispose();
});
test('test when no extensions are disabled globally', () => {
assert.deepEqual([], testObject.getGloballyDisabledExtensions());
});
test('test when no extensions are disabled for workspace', () => {
assert.deepEqual([], testObject.getWorkspaceDisabledExtensions());
});
test('test when no extensions are disabled for workspace when there is no workspace', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => {
instantiationService.stub(IWorkspaceContextService, 'hasWorkspace', false);
assert.deepEqual([], testObject.getWorkspaceDisabledExtensions());
})
.then(done, done);
});
test('test disable an extension globally', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => assert.deepEqual(['pub.a'], testObject.getGloballyDisabledExtensions()))
.then(done, done);
});
test('test disable an extension globally should return truthy promise', (done) => {
testObject.setEnablement('pub.a', false)
.then(value => assert.ok(value))
.then(done, done);
});
test('test disable an extension globally triggers the change event', (done) => {
const target = sinon.spy();
testObject.onEnablementChanged(target);
testObject.setEnablement('pub.a', false)
.then(() => assert.ok(target.calledWithExactly('pub.a')))
.then(done, done);
});
test('test disable an extension globally again should return a falsy promise', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', false))
.then(value => assert.ok(!value))
.then(done, done);
});
test('test disable an extension for workspace', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => assert.deepEqual(['pub.a'], testObject.getWorkspaceDisabledExtensions()))
.then(done, done);
});
test('test disable an extension for workspace returns a truthy promise', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(value => assert.ok(value))
.then(done, done);
});
test('test disable an extension for workspace again should return a falsy promise', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false, true))
.then(value => assert.ok(!value))
.then(done, done);
});
test('test disable an extension for workspace and then globally', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(() => {
assert.deepEqual(['pub.a'], testObject.getWorkspaceDisabledExtensions());
assert.deepEqual(['pub.a'], testObject.getGloballyDisabledExtensions());
})
.then(done, done);
});
test('test disable an extension for workspace and then globally return a truthy promise', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(value => assert.ok(value))
.then(done, done);
});
test('test disable an extension for workspace and then globally triggers the change event', (done) => {
const target = sinon.spy();
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.onEnablementChanged(target))
.then(() => testObject.setEnablement('pub.a', false))
.then(() => assert.ok(target.calledWithExactly('pub.a')))
.then(done, done);
});
test('test disable an extension globally and then for workspace', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', false, true))
.then(() => {
assert.deepEqual(['pub.a'], testObject.getWorkspaceDisabledExtensions());
assert.deepEqual(['pub.a'], testObject.getGloballyDisabledExtensions());
})
.then(done, done);
});
test('test disable an extension globally and then for workspace return a truthy promise', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', false, true))
.then(value => assert.ok(value))
.then(done, done);
});
test('test disable an extension globally and then for workspace triggers the change event', (done) => {
const target = sinon.spy();
testObject.setEnablement('pub.a', false)
.then(() => testObject.onEnablementChanged(target))
.then(() => testObject.setEnablement('pub.a', false, true))
.then(() => assert.ok(target.calledWithExactly('pub.a')))
.then(done, done);
});
test('test disable an extension for workspace when there is no workspace throws error', (done) => {
instantiationService.stub(IWorkspaceContextService, 'hasWorkspace', false);
testObject.setEnablement('pub.a', false, true)
.then(() => assert.fail('should throw an error'), error => assert.ok(error))
.then(done, done);
});
test('test enable an extension globally', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', true))
.then(() => assert.deepEqual([], testObject.getGloballyDisabledExtensions()))
.then(done, done);
});
test('test enable an extension globally return truthy promise', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', true))
.then(value => assert.ok(value))
.then(done, done);
});
test('test enable an extension globally triggers change event', (done) => {
const target = sinon.spy();
testObject.setEnablement('pub.a', false)
.then(() => testObject.onEnablementChanged(target))
.then(() => testObject.setEnablement('pub.a', true))
.then(() => assert.ok(target.calledWithExactly('pub.a')))
.then(done, done);
});
test('test enable an extension globally when already enabled return falsy promise', (done) => {
testObject.setEnablement('pub.a', true)
.then(value => assert.ok(!value))
.then(done, done);
});
test('test enable an extension for workspace', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', true, true))
.then(() => assert.deepEqual([], testObject.getWorkspaceDisabledExtensions()))
.then(done, done);
});
test('test enable an extension for workspace return truthy promise', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', true, true))
.then(value => assert.ok(value))
.then(done, done);
});
test('test enable an extension for workspace triggers change event', (done) => {
const target = sinon.spy();
testObject.setEnablement('pub.b', false, true)
.then(() => testObject.onEnablementChanged(target))
.then(() => testObject.setEnablement('pub.b', true, true))
.then(() => assert.ok(target.calledWithExactly('pub.b')))
.then(done, done);
});
test('test enable an extension for workspace when already enabled return falsy promise', (done) => {
testObject.setEnablement('pub.a', true, true)
.then(value => assert.ok(!value))
.then(done, done);
});
test('test enable an extension for workspace when disabled in workspace and gloablly', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(() => testObject.setEnablement('pub.a', true, true))
.then(() => {
assert.deepEqual(['pub.a'], testObject.getGloballyDisabledExtensions());
assert.deepEqual([], testObject.getWorkspaceDisabledExtensions());
})
.then(done, done);
});
test('test enable an extension globally when disabled in workspace and gloablly', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(() => testObject.setEnablement('pub.a', true))
.then(() => {
assert.deepEqual(['pub.a'], testObject.getWorkspaceDisabledExtensions());
assert.deepEqual([], testObject.getGloballyDisabledExtensions());
})
.then(done, done);
});
test('test remove an extension from disablement list when uninstalled', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(() => didUninstallEvent.fire({ id: 'pub.a-1.0.0' }))
.then(() => {
assert.deepEqual([], testObject.getWorkspaceDisabledExtensions());
assert.deepEqual([], testObject.getGloballyDisabledExtensions());
})
.then(done, done);
});
});

View File

@@ -0,0 +1,29 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import { EXTENSION_IDENTIFIER_PATTERN } from 'vs/platform/extensionManagement/common/extensionManagement';
suite('Extension Identifier Pattern', () => {
test('extension identifier pattern', () => {
const regEx = new RegExp(EXTENSION_IDENTIFIER_PATTERN);
assert.equal(true, regEx.test('publisher.name'));
assert.equal(true, regEx.test('publiSher.name'));
assert.equal(true, regEx.test('publisher.Name'));
assert.equal(true, regEx.test('PUBLISHER.NAME'));
assert.equal(true, regEx.test('PUBLISHEr.NAMe'));
assert.equal(true, regEx.test('PUBLISHEr.N-AMe'));
assert.equal(true, regEx.test('PUBLISH12Er90.N-A54Me123'));
assert.equal(true, regEx.test('111PUBLISH12Er90.N-1111A54Me123'));
assert.equal(false, regEx.test('publishername'));
assert.equal(false, regEx.test('-publisher.name'));
assert.equal(false, regEx.test('publisher.-name'));
assert.equal(false, regEx.test('-publisher.-name'));
assert.equal(false, regEx.test('publ_isher.name'));
assert.equal(false, regEx.test('publisher._name'));
});
});