From 8c0e00cbb57d0127f3a03398885a6268a3d26915 Mon Sep 17 00:00:00 2001 From: Roman Nikitenko Date: Mon, 18 Nov 2019 09:26:01 +0200 Subject: [PATCH] Ability to add task subschema Signed-off-by: Roman Nikitenko --- packages/task/src/browser/index.ts | 1 + .../task/src/browser/task-schema-updater.ts | 39 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/task/src/browser/index.ts b/packages/task/src/browser/index.ts index 0f169b4b25014..cc0a729f5184a 100644 --- a/packages/task/src/browser/index.ts +++ b/packages/task/src/browser/index.ts @@ -19,3 +19,4 @@ export * from './task-contribution'; export * from './task-definition-registry'; export * from './task-problem-matcher-registry'; export * from './task-problem-pattern-registry'; +export * from './task-schema-updater'; diff --git a/packages/task/src/browser/task-schema-updater.ts b/packages/task/src/browser/task-schema-updater.ts index b17a030460769..28dd06f9e63f1 100644 --- a/packages/task/src/browser/task-schema-updater.ts +++ b/packages/task/src/browser/task-schema-updater.ts @@ -55,6 +55,9 @@ export class TaskSchemaUpdater { update(): void { const taskSchemaUri = new URI(taskSchemaId); + + taskConfigurationSchema.oneOf = [processTaskConfigurationSchema, ...customizedDetectedTasks, ...customSchemas]; + const schemaContent = this.getStrigifiedTaskSchema(); try { this.inmemoryResources.update(taskSchemaUri, schemaContent); @@ -67,6 +70,36 @@ export class TaskSchemaUpdater { } } + /** + * Adds given task schema to `taskConfigurationSchema` as `oneOf` subschema. + * Replaces existed subschema by given schema if the corrresponding `$id` properties are equal. + * + * Note: please provide `$id` property for subschema to have ability remove/replace it. + * @param schema subschema for adding to `taskConfigurationSchema` + */ + addSubschema(schema: IJSONSchema): void { + const schemaId = schema.$id; + if (schemaId) { + this.removeSubschema(schemaId); + } + + customSchemas.push(schema); + this.update(); + } + + /** + * Removes task subschema from `taskConfigurationSchema`. + * + * @param arg `$id` property of subschema + */ + removeSubschema(arg: string): void { + const index = customSchemas.findIndex(existed => !!existed.$id && existed.$id === arg); + if (index > -1) { + customSchemas.splice(index, 1); + this.update(); + } + } + /** Returns an array of task types that are registered, including the default types */ async getRegisteredTaskTypes(): Promise { const serverSupportedTypes = await this.taskServer.getRegisteredTaskTypes(); @@ -103,9 +136,6 @@ export class TaskSchemaUpdater { customizedDetectedTask.additionalProperties = true; customizedDetectedTasks.push(customizedDetectedTask); }); - - taskConfigurationSchema.oneOf!.length = 1; - taskConfigurationSchema.oneOf!.push(...customizedDetectedTasks); } /** Returns the task's JSON schema */ @@ -301,8 +331,9 @@ const processTaskConfigurationSchema: IJSONSchema = { }; const customizedDetectedTasks: IJSONSchema[] = []; +const customSchemas: IJSONSchema[] = []; const taskConfigurationSchema: IJSONSchema = { $id: taskSchemaId, - oneOf: [processTaskConfigurationSchema, ...customizedDetectedTasks] + oneOf: [processTaskConfigurationSchema, ...customizedDetectedTasks, ...customSchemas] };