Open aureamunoz opened 8 months ago
As the template includes a step to enhance the skeleton of the project generated by code.quarkus.io, we should revisit the if condition which is currently based on the extension io.quarkus:quarkus-hibernate-orm-rest-data-panache
if: ${{ 'io.quarkus:quarkus-hibernate-orm-rest-data-panache' in parameters.extensions }}
action: fetch:template
input:
url: skeletons/quarkus-orm-rest/
replace: true
Could we check if the quarkus version is <= 3.9 and if the extension contains the word orm-rest-data-panache
?
@aureamunoz
Could this syntax work if: ${{ 'io.quarkus:quarkus-hibernate-orm-rest-data-panache' in parameters.extensions and (parameters.quarkusVersion | split(':') | last | float) <= '3.9' }}
?
Could this syntax work
No luck => 2024-04-04T10:36:55.463Z scaffolder error Failed to parse template string: ${{ 'io.quarkus:quarkus-hibernate-orm-rest-data-panache' in parameters.extensions and (parameters.quarkusVersion | split(':') | last | float) <= '3.9' }} with error (unknown path) Error: filter not found: split type=plugin
I found how :-)
Such additional filters should be added to the scaffolder plugin
...
const actions = [
createArgoCdResources( env.config, env.logger ),
...builtInActions,
createQuarkusApp(),
cloneQuarkusQuickstart()
];
return await createRouter({
actions,
logger: env.logger,
config: env.config,
database: env.database,
reader: env.reader,
catalogClient,
identity: env.identity,
permissions: env.permissions,
additionalTemplateFilters: {
message: input => {
return `This is a much better string than "${input}", don't you think?`;
}
}
});
and within the template
- id: publish
name: Publishing to Code Source Repository - Github
action: publish:github
input:
allowedHosts: ['github.com']
description: This is ${{ parameters.component_id | message }}
Such a nice feature is not documented at all !!!
Cool! Waou, this is not easy to understand from my side.
Backstage template & scaffolding uses nunjucks under the hood to render the template.
It support the nunjucks syntax as described here: https://mozilla.github.io/nunjucks/templating.html#logic for files which are processed when we call the action fetch:template
=> example: what we do when we generate the java files, etc)
Unfortunately, nunjucks dont support a split function, this is what I discovered when I was using such a syntax could work parameters.quarkusVersion | split(':') | last | float
Hopefully, backstage allows to add your own filters
using the parameter additionalTemplateFilters
available on the function createRouter
Important: This option is not documented at all and not defined part of the schema and then non visible when we list the actions !
This test case should help you to understand a bit more: https://github.com/backstage/backstage/blob/e3c38284bfa723ea2956995e1cf07f02f4cf3665/plugins/scaffolder-backend/src/lib/templating/SecureTemplater.test.ts#L106-L150
Question: Should we design the filter as such to get a float object usign some nunjucks supported words + our own filter that we can compare to 3.9
if: ${{ 'io.quarkus:quarkus-hibernate-orm-rest-data-panache' in parameters.extensions
and
(parameters.quarkusVersion | split(':') | last | float) <= '3.9' }}
OR should we aggregate the operations split(':') | last | float
into one
if: ${{ 'io.quarkus:quarkus-hibernate-orm-rest-data-panache' in parameters.extensions
and
(parameters.quarkusVersion | keyToFloat ) <= '3.9' }}
Note: keyToFloat could be renamed to something else toFloat
, getVersionAsFloat
or be extractKeyVersion | float
etc
WDYT ? @aureamunoz @iocanel
I don't have a very strong opinion on this. I would say that adding it to an existing filter if there is one that fits our use case, seems to me to be quicker to do
We could test easily such additional filters =>
import {SecureTemplater} from './SecureTemplater';
function extractVersionFromKey(
streamKey: string,
): string {
if (!streamKey) {
throw new Error(`StreamKey to be processed cannot be empty`);
}
let streamKeyArr = streamKey.split(":")
if (streamKeyArr.length < 2) {
throw new Error(`The streamKey is not formatted as: io.quarkus.platform:\<version\>`);
} else {
return streamKeyArr[1]
}
}
describe('QuarkusStreamKey', () => {
it('should get an error as stremKey is not well formatted', async () => {
const renderWith = await SecureTemplater.loadRenderer({
templateFilters: {
extractVersionFromKey: (streamKey) => extractVersionFromKey(streamKey as string)
},
});
let ctx = {inputValue: 'io.quarkus.platform-3.9'};
expect(() => renderWith('${{ inputValue | extractVersionFromKey }}', ctx),).toThrow(
/Error: The streamKey is not formatted as: io.quarkus.platform:<version>/,
);
});
});
The following project https://github.com/ch007m/ch007m-quarkus-app generated via backstage Quarkus plugin contains duplicated entities and resources. It comes from the recently generator code added for hibernate-orm-rest-data extension in Quarkus.
Duplicated code can be found in
src/main/java/io/quarkus/MyEntity.java
andsrc/main/java/io/quarkus/demo/MyEntity.java
src/main/java/io/quarkus/MyEntityResource.java
andsrc/main/java/io/quarkus/demo/MyEntityResource.java
The following error is launched when the application is started with
mvn quarkus:dev
command:cc @iocanel @cmoulliard