Extract isValidExtensionDescription out of extensionsRegistry

This commit is contained in:
Alex Dima 2016-10-26 17:06:53 +02:00
parent 949dc8c893
commit 7d3a902fff
3 changed files with 75 additions and 72 deletions

View file

@ -70,6 +70,7 @@ export interface IExtensionsRuntimeService {
/**
* Scans and returns only enabled extensions.
* **NOTE**: This call returns different results based on `setEnablement` calls!
*/
getExtensions(): TPromise<IExtensionDescription[]>;

View file

@ -7,7 +7,6 @@
import * as nls from 'vs/nls';
import { onUnexpectedError } from 'vs/base/common/errors';
import { IJSONSchema } from 'vs/base/common/jsonSchema';
import * as paths from 'vs/base/common/paths';
import Severity from 'vs/base/common/severity';
import { IActivationEventListener, IMessage, IExtensionDescription, IPointListener } from 'vs/platform/extensions/common/extensions';
import { Extensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
@ -51,66 +50,7 @@ class ExtensionMessageCollector implements IExtensionMessageCollector {
}
export function isValidExtensionDescription(extensionFolderPath: string, extensionDescription: IExtensionDescription, notices: string[]): boolean {
if (!extensionDescription) {
notices.push(nls.localize('extensionDescription.empty', "Got empty extension description"));
return false;
}
if (typeof extensionDescription.publisher !== 'string') {
notices.push(nls.localize('extensionDescription.publisher', "property `{0}` is mandatory and must be of type `string`", 'publisher'));
return false;
}
if (typeof extensionDescription.name !== 'string') {
notices.push(nls.localize('extensionDescription.name', "property `{0}` is mandatory and must be of type `string`", 'name'));
return false;
}
if (typeof extensionDescription.version !== 'string') {
notices.push(nls.localize('extensionDescription.version', "property `{0}` is mandatory and must be of type `string`", 'version'));
return false;
}
if (!extensionDescription.engines) {
notices.push(nls.localize('extensionDescription.engines', "property `{0}` is mandatory and must be of type `object`", 'engines'));
return false;
}
if (typeof extensionDescription.engines.vscode !== 'string') {
notices.push(nls.localize('extensionDescription.engines.vscode', "property `{0}` is mandatory and must be of type `string`", 'engines.vscode'));
return false;
}
if (typeof extensionDescription.extensionDependencies !== 'undefined') {
if (!_isStringArray(extensionDescription.extensionDependencies)) {
notices.push(nls.localize('extensionDescription.extensionDependencies', "property `{0}` can be omitted or must be of type `string[]`", 'extensionDependencies'));
return false;
}
}
if (typeof extensionDescription.activationEvents !== 'undefined') {
if (!_isStringArray(extensionDescription.activationEvents)) {
notices.push(nls.localize('extensionDescription.activationEvents1', "property `{0}` can be omitted or must be of type `string[]`", 'activationEvents'));
return false;
}
if (typeof extensionDescription.main === 'undefined') {
notices.push(nls.localize('extensionDescription.activationEvents2', "properties `{0}` and `{1}` must both be specified or must both be omitted", 'activationEvents', 'main'));
return false;
}
}
if (typeof extensionDescription.main !== 'undefined') {
if (typeof extensionDescription.main !== 'string') {
notices.push(nls.localize('extensionDescription.main1', "property `{0}` can be omitted or must be of type `string`", 'main'));
return false;
} else {
let normalizedAbsolutePath = paths.normalize(paths.join(extensionFolderPath, extensionDescription.main));
if (normalizedAbsolutePath.indexOf(extensionFolderPath)) {
notices.push(nls.localize('extensionDescription.main2', "Expected `main` ({0}) to be included inside extension's folder ({1}). This might make the extension non-portable.", normalizedAbsolutePath, extensionFolderPath));
// not a failure case
}
}
if (typeof extensionDescription.activationEvents === 'undefined') {
notices.push(nls.localize('extensionDescription.main3', "properties `{0}` and `{1}` must both be specified or must both be omitted", 'activationEvents', 'main'));
return false;
}
}
return true;
}
interface IExtensionDescriptionMap {
[extensionId: string]: IExtensionDescription;
@ -453,17 +393,6 @@ class ExtensionsRegistryImpl implements IExtensionsRegistry {
}
function _isStringArray(arr: string[]): boolean {
if (!Array.isArray(arr)) {
return false;
}
for (let i = 0, len = arr.length; i < len; i++) {
if (typeof arr[i] !== 'string') {
return false;
}
}
return true;
}
const PRExtensions = {
ExtensionsRegistry: 'ExtensionsRegistry'

View file

@ -6,8 +6,8 @@
import * as nls from 'vs/nls';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { isValidExtensionDescription as baseIsValidExtensionDescription } from 'vs/platform/extensions/common/extensionsRegistry';
import { valid } from 'semver';
import * as paths from 'vs/base/common/paths';
export interface IParsedVersion {
hasCaret: boolean;
@ -230,6 +230,79 @@ export function isVersionValid(currentVersion: string, requestedVersion: string,
return true;
}
function _isStringArray(arr: string[]): boolean {
if (!Array.isArray(arr)) {
return false;
}
for (let i = 0, len = arr.length; i < len; i++) {
if (typeof arr[i] !== 'string') {
return false;
}
}
return true;
}
function baseIsValidExtensionDescription(extensionFolderPath: string, extensionDescription: IExtensionDescription, notices: string[]): boolean {
if (!extensionDescription) {
notices.push(nls.localize('extensionDescription.empty', "Got empty extension description"));
return false;
}
if (typeof extensionDescription.publisher !== 'string') {
notices.push(nls.localize('extensionDescription.publisher', "property `{0}` is mandatory and must be of type `string`", 'publisher'));
return false;
}
if (typeof extensionDescription.name !== 'string') {
notices.push(nls.localize('extensionDescription.name', "property `{0}` is mandatory and must be of type `string`", 'name'));
return false;
}
if (typeof extensionDescription.version !== 'string') {
notices.push(nls.localize('extensionDescription.version', "property `{0}` is mandatory and must be of type `string`", 'version'));
return false;
}
if (!extensionDescription.engines) {
notices.push(nls.localize('extensionDescription.engines', "property `{0}` is mandatory and must be of type `object`", 'engines'));
return false;
}
if (typeof extensionDescription.engines.vscode !== 'string') {
notices.push(nls.localize('extensionDescription.engines.vscode', "property `{0}` is mandatory and must be of type `string`", 'engines.vscode'));
return false;
}
if (typeof extensionDescription.extensionDependencies !== 'undefined') {
if (!_isStringArray(extensionDescription.extensionDependencies)) {
notices.push(nls.localize('extensionDescription.extensionDependencies', "property `{0}` can be omitted or must be of type `string[]`", 'extensionDependencies'));
return false;
}
}
if (typeof extensionDescription.activationEvents !== 'undefined') {
if (!_isStringArray(extensionDescription.activationEvents)) {
notices.push(nls.localize('extensionDescription.activationEvents1', "property `{0}` can be omitted or must be of type `string[]`", 'activationEvents'));
return false;
}
if (typeof extensionDescription.main === 'undefined') {
notices.push(nls.localize('extensionDescription.activationEvents2', "properties `{0}` and `{1}` must both be specified or must both be omitted", 'activationEvents', 'main'));
return false;
}
}
if (typeof extensionDescription.main !== 'undefined') {
if (typeof extensionDescription.main !== 'string') {
notices.push(nls.localize('extensionDescription.main1', "property `{0}` can be omitted or must be of type `string`", 'main'));
return false;
} else {
let normalizedAbsolutePath = paths.normalize(paths.join(extensionFolderPath, extensionDescription.main));
if (normalizedAbsolutePath.indexOf(extensionFolderPath)) {
notices.push(nls.localize('extensionDescription.main2', "Expected `main` ({0}) to be included inside extension's folder ({1}). This might make the extension non-portable.", normalizedAbsolutePath, extensionFolderPath));
// not a failure case
}
}
if (typeof extensionDescription.activationEvents === 'undefined') {
notices.push(nls.localize('extensionDescription.main3', "properties `{0}` and `{1}` must both be specified or must both be omitted", 'activationEvents', 'main'));
return false;
}
}
return true;
}
export function isValidExtensionDescription(version: string, extensionFolderPath: string, extensionDescription: IExtensionDescription, notices: string[]): boolean {
if (!baseIsValidExtensionDescription(extensionFolderPath, extensionDescription, notices)) {