Skip to content

Commit

Permalink
Allow to use extension value in addition to passing JSON file name (#…
Browse files Browse the repository at this point in the history
…9192)

* [AAE-18136] Allow to use extension value

* Apply suggestions from code review

Co-authored-by: Robert Duda <[email protected]>

---------

Co-authored-by: Robert Duda <[email protected]>
  • Loading branch information
BSekula and DudaRobert authored Dec 23, 2023
1 parent e9c769b commit a1b4c54
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 8 deletions.
66 changes: 66 additions & 0 deletions lib/extensions/src/lib/services/extension-loader.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,70 @@ describe('ExtensionLoaderService', () => {
done();
});
});

it('should load extensions from passed extension value', (done) => {
appExtensionsConfig.$references = ['test.extension.1.json'];

extensionLoaderService.load(
'assets/app.extensions.json',
'assets/plugins',
undefined,
[{
$id: 'extension-value-id',
$license: 'license',
$name: 'name',
$version:'version',
$vendor: 'vendor'
}]).then((config: ExtensionConfig) => {
const hasExtensionValue = config.$references.some((entry: ExtensionConfig) => entry.$id === 'extension-value-id');
expect(hasExtensionValue).toBe(true);
done();
});
});

it('should load extensions if only extension value was passed', (done) => {
extensionLoaderService.load(
'assets/app.extensions.json',
'assets/plugins',
undefined,
[{
$id: 'extension-value-id',
$license: 'license',
$name: 'name',
$version:'version',
$vendor: 'vendor'
}]).then((config: ExtensionConfig) => {
const hasExtensionValue = config.$references.some((entry: ExtensionConfig) => entry.$id === 'extension-value-id');
expect(hasExtensionValue).toBe(true);
done();
});
});

it('should load extensions with multiple extension values', (done) => {
appExtensionsConfig.$references = ['test.extension.1.json'];

extensionLoaderService.load(
'assets/app.extensions.json',
'assets/plugins',
undefined,
[{
$id: 'extension-value-id-1',
$license: 'license',
$name: 'name',
$version:'version',
$vendor: 'vendor'
},{
$id: 'extension-value-id-2',
$license: 'license',
$name: 'name',
$version:'version',
$vendor: 'vendor'
}]).then((config: ExtensionConfig) => {
const hasFirstExtensionValue = config.$references.some((entry: ExtensionConfig) => entry.$id === 'extension-value-id-1');
expect(hasFirstExtensionValue).toBe(true);
const hasSecondExtensionValue = config.$references.some((entry: ExtensionConfig) => entry.$id === 'extension-value-id-2');
expect(hasSecondExtensionValue).toBe(true);
done();
});
});
});
21 changes: 17 additions & 4 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,12 @@ export class ExtensionLoaderService {
constructor(private http: HttpClient) {
}

load(configPath: string, pluginsPath: string, extensions?: string[]): Promise<ExtensionConfig> {
load(
configPath: string,
pluginsPath: string,
extensions?: string[],
extensionValues?: ExtensionConfig[]
): Promise<ExtensionConfig> {
return new Promise<any>((resolve) => {
this.loadConfig(configPath, 0).then((result) => {
if (result) {
Expand All @@ -48,17 +53,25 @@ export class ExtensionLoaderService {
config.$references = this.filterIgnoredExtensions(config.$references, config.$ignoreReferenceList);
}

if (config.$references && config.$references.length > 0) {
const plugins = config.$references.map((name, idx) =>
if (config.$references?.length > 0 || extensionValues) {
const plugins = (config.$references ?? []).map((name, idx) =>
this.loadConfig(`${pluginsPath}/${name}`, idx)
);

Promise.all(plugins).then((results) => {
const configs = results
let configs = results
.filter((entry) => entry)
.sort(sortByOrder)
.map((entry) => entry.config);

if (extensionValues) {
configs = [
...configs,
...extensionValues
];
}


if (configs.length > 0) {
config = mergeObjects(config, ...configs);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/extensions/src/lib/services/extension.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('ExtensionService', () => {
loader = new ExtensionLoaderService(null);
componentRegister = new ComponentRegisterService();
ruleService = new RuleService(loader);
service = new ExtensionService(loader, componentRegister, ruleService, []);
service = new ExtensionService(loader, componentRegister, ruleService, [], []);
});

it('should load and setup a config', async () => {
Expand All @@ -53,7 +53,7 @@ describe('ExtensionService', () => {
await service.load();

expect(loader.load).toHaveBeenCalled();
expect(loader.load).toHaveBeenCalledWith('assets/app.extensions.json', 'assets/plugins', []);
expect(loader.load).toHaveBeenCalledWith('assets/app.extensions.json', 'assets/plugins', [], []);
expect(service.setup).toHaveBeenCalledWith(blankConfig);
});

Expand Down
26 changes: 24 additions & 2 deletions lib/extensions/src/lib/services/extension.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export const EXTENSION_JSONS = new InjectionToken<string[][]>('extension-jsons',
factory: extensionJsonsFactory
});

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

// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
/**
* Provides the extension json values for the angular modules
Expand All @@ -57,6 +62,20 @@ export function provideExtensionConfig(jsons: string[]) {
};
};

/**
* Provides the extension json raw values for the angular modules
*
* @param extensionConfigValue config value
* @returns a provider section
*/
export function provideExtensionConfigValues(extensionConfigValue: ExtensionConfig[]) {
return {
provide: EXTENSION_JSON_VALUES,
useValue: extensionConfigValue,
multi: true
};
};

@Injectable({
providedIn: 'root'
})
Expand All @@ -78,7 +97,8 @@ export class ExtensionService {
protected loader: ExtensionLoaderService,
protected componentRegister: ComponentRegisterService,
protected ruleService: RuleService,
@Inject(EXTENSION_JSONS) protected extensionJsons: string[]
@Inject(EXTENSION_JSONS) protected extensionJsons: string[],
@Inject(EXTENSION_JSON_VALUES) protected extensionJsonValues: ExtensionConfig[]
) {
this.setup$ = this.onSetup$.asObservable();
}
Expand All @@ -92,8 +112,10 @@ export class ExtensionService {
const config = await this.loader.load(
this.configPath,
this.pluginsPath,
this.extensionJsons.flat()
this.extensionJsons.flat(),
this.extensionJsonValues.flat()
);

this.setup(config);
return config;
}
Expand Down

0 comments on commit a1b4c54

Please sign in to comment.