Skip to content

Commit

Permalink
[ADF-5284] Restore original plugin functionality (#6348)
Browse files Browse the repository at this point in the history
* add $ignoreReferenceList to ExtensionConfig interface

* update schema

* change type

* load registered extensions

* filter out ignored extensions

* tests

* conditionally load plugins

* update tests
  • Loading branch information
pionnegru committed Nov 16, 2020
1 parent 4dfa9b6 commit 145e324
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/extensions/src/lib/config/extension.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ export interface ExtensionRef {

export interface ExtensionConfig extends ExtensionRef {
$references?: Array<string | ExtensionRef>;
$ignoreReferenceList?: Array<string>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,15 @@
"minItems": 0,
"uniqueItems": true
},
"$ignoreReferenceList": {
"description": "Plugins references to exclude",
"type": "array",
"items": {
"type": "string"
},
"minItems": 0,
"uniqueItems": true
},
"rules": {
"description": "List of rule definitions",
"type": "array",
Expand Down
121 changes: 121 additions & 0 deletions lib/extensions/src/lib/services/extension-loader.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { async, TestBed } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ExtensionConfig } from '../config/extension.config';
import { ExtensionLoaderService } from './extension-loader.service';
import { HttpClient } from '@angular/common/http';
import { of } from 'rxjs';

describe('ExtensionLoaderService', () => {
let extensionLoaderService: ExtensionLoaderService;
let httpClient: HttpClient;
let appExtensionsConfig: ExtensionConfig;
const pluginConfig1: ExtensionConfig = {
$id: 'test1',
$name: 'test.extension.1',
$version: '1.0.0',
$vendor: 'Alfresco',
$license: 'MIT',
$runtime: '2.6.1'
};
const pluginConfig2: ExtensionConfig = {
$id: 'test2',
$name: 'test.extension.2',
$version: '1.0.0',
$vendor: 'Alfresco',
$license: 'MIT',
$runtime: '2.6.1'
};
const pluginConfig3: ExtensionConfig = {
$id: 'test3',
$name: 'test.extension.3',
$version: '1.0.0',
$vendor: 'Alfresco',
$license: 'MIT',
$runtime: '2.6.1'
};

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
HttpClient,
ExtensionLoaderService
]
});
extensionLoaderService = TestBed.inject(ExtensionLoaderService);
httpClient = TestBed.inject(HttpClient);

appExtensionsConfig = {
$id: 'test',
$name: 'test.config',
$version: '1.0.0',
$vendor: 'Alfresco',
$license: 'MIT',
$runtime: '2.6.1',
$references: [],
$ignoreReferenceList: []
};

spyOn(httpClient, 'get').and.callFake((url: string) => {
if (url === 'assets/app.extensions.json') {
return of(appExtensionsConfig);
}

if (url === 'assets/plugins/test.extension.1.json') {
return of(pluginConfig1);
}

if (url === 'assets/plugins/test.extension.2.json') {
return of(pluginConfig2);
}

if (url === 'assets/plugins/test.extension.3.json') {
return of(pluginConfig3);
}

return of({});
});
});

it('should load default registered app extensions when no custom $references defined', async(() => {
extensionLoaderService.load('assets/app.extensions.json', 'assets/plugins', ['test.extension.1.json']).then((config: ExtensionConfig) => {
const pluginsReference = config.$references.map((entry: ExtensionConfig) => entry.$name);
expect(pluginsReference).toEqual(['test.extension.1']);
});
}));

it('should ignore default registered app extension if defined in $ignoreReferenceList', async(() => {
appExtensionsConfig.$ignoreReferenceList = ['test.extension.1.json'];

extensionLoaderService.load('assets/app.extensions.json', 'assets/plugins', ['test.extension.1.json']).then((config: ExtensionConfig) => {
const pluginsReference = config.$references.map((entry: ExtensionConfig) => entry.$name);
expect(pluginsReference).toEqual([]);
});
}));

it('should load only extensions defined by $references', async(() => {
appExtensionsConfig.$references = ['test.extension.1.json'];

extensionLoaderService.load('assets/app.extensions.json', 'assets/plugins', ['test.extension.2.json, test.extension.3.json']).then((config: ExtensionConfig) => {
const pluginsReference = config.$references.map((entry: ExtensionConfig) => entry.$name);
expect(pluginsReference).toEqual(['test.extension.1']);
});
}));
});
20 changes: 15 additions & 5 deletions lib/extensions/src/lib/services/extension-loader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ExtensionLoaderService {
constructor(private http: HttpClient) {
}

load(configPath: string, pluginsPath: string, extensions?: ExtensionConfig[]): Promise<ExtensionConfig> {
load(configPath: string, pluginsPath: string, extensions?: string[]): Promise<ExtensionConfig> {
return new Promise<any>((resolve) => {
this.loadConfig(configPath, 0).then((result) => {
if (result) {
Expand All @@ -42,6 +42,12 @@ export class ExtensionLoaderService {
config = JSON.parse(override);
}

if (!config.$references || !config.$references.length) {
config.$references = this.filterIgnoredExtensions(extensions || [], config.$ignoreReferenceList);
} else {
config.$references = this.filterIgnoredExtensions(config.$references, config.$ignoreReferenceList);
}

if (config.$references && config.$references.length > 0) {
const plugins = config.$references.map((name, idx) =>
this.loadConfig(`${pluginsPath}/${name}`, idx)
Expand All @@ -53,10 +59,6 @@ export class ExtensionLoaderService {
.sort(sortByOrder)
.map((entry) => entry.config);

if (extensions && extensions.length > 0) {
configs.push(...extensions);
}

if (configs.length > 0) {
config = mergeObjects(config, ...configs);
}
Expand Down Expand Up @@ -166,4 +168,12 @@ export class ExtensionLoaderService {
}
return action;
}

private filterIgnoredExtensions(extensions: Array<string | ExtensionRef>, ignoreReferenceList: string[]): Array<string | ExtensionRef> {
if (!ignoreReferenceList || !ignoreReferenceList.length) {
return extensions;
}

return extensions.map((file: string) => file.match('(?!.*\/).+')[0]).filter((fileName: string) => !ignoreReferenceList.includes(fileName));
}
}
8 changes: 4 additions & 4 deletions lib/extensions/src/lib/services/extension.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { Injectable, Type, InjectionToken, Inject } from '@angular/core';
import { RuleEvaluator, RuleRef, RuleContext } from '../config/rule.extensions';
import { ExtensionConfig, ExtensionRef } from '../config/extension.config';
import { ExtensionConfig } from '../config/extension.config';
import { ExtensionLoaderService } from './extension-loader.service';
import { RouteRef } from '../config/routing.extensions';
import { ActionRef } from '../config/action.extensions';
Expand All @@ -30,12 +30,12 @@ export function extensionJsonsFactory() {
return [];
}

export const EXTENSION_JSONS = new InjectionToken<ExtensionRef[][]>('extension-jsons', {
export const EXTENSION_JSONS = new InjectionToken<string[][]>('extension-jsons', {
providedIn: 'root',
factory: extensionJsonsFactory
});

export function provideExtensionConfig(jsons: ExtensionRef[]) {
export function provideExtensionConfig(jsons: string[]) {
return {
provide: EXTENSION_JSONS,
useValue: jsons,
Expand All @@ -62,7 +62,7 @@ export class ExtensionService {
protected loader: ExtensionLoaderService,
protected componentRegister: ComponentRegisterService,
protected ruleService: RuleService,
@Inject(EXTENSION_JSONS) protected extensionJsons: ExtensionRef[]
@Inject(EXTENSION_JSONS) protected extensionJsons: string[]
) {
}

Expand Down

0 comments on commit 145e324

Please sign in to comment.