From 3c3c30728bafd83ee4db2cedccdb7471864f3053 Mon Sep 17 00:00:00 2001 From: Mengnan Gong Date: Thu, 7 Jul 2022 08:52:27 +0800 Subject: [PATCH] Renders path templates when copyWithoutRender is used When putting templated paths in `copyWithoutRender` filter, like `{{project}}/something`, the path template won't get rendered and the files are copied to the exact `{{project}}/something` instead of `someproject/something`. Cookiecutter has fixed this issue, and the behaviour in scaffolder should be consistent with Cookiecutter. Signed-off-by: Mengnan Gong --- .changeset/five-falcons-destroy.md | 5 +++++ .../src/scaffolder/actions/builtin/fetch/template.test.ts | 4 ++-- .../src/scaffolder/actions/builtin/fetch/template.ts | 8 ++------ 3 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 .changeset/five-falcons-destroy.md diff --git a/.changeset/five-falcons-destroy.md b/.changeset/five-falcons-destroy.md new file mode 100644 index 0000000000000..e21d3618a8f60 --- /dev/null +++ b/.changeset/five-falcons-destroy.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Render the path templates for files that match `copyWithoutRender`, only the file content should be left untouched. diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts index 8f641fe2e2a0b..c2f24a868038e 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.test.ts @@ -300,10 +300,10 @@ describe('fetch:template', () => { await action.handler(context); }); - it('ignores template syntax in files matched in copyWithoutRender', async () => { + it('renders path template and ignores content template in files matched in copyWithoutRender', async () => { await expect( fs.readFile( - `${workspacePath}/target/.unprocessed/templated-content-\${{ values.name }}.txt`, + `${workspacePath}/target/.unprocessed/templated-content-test-project.txt`, 'utf-8', ), ).resolves.toEqual('${{ values.count }}'); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts index c6d6b86e7b48f..48cf70153ec60 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -193,22 +193,18 @@ export function createFetchTemplateAction(options: { }); for (const location of allEntriesInTemplate) { - let renderFilename: boolean; let renderContents: boolean; let localOutputPath = location; if (extension) { - renderFilename = true; renderContents = extname(localOutputPath) === extension; if (renderContents) { localOutputPath = localOutputPath.slice(0, -extension.length); } } else { - renderFilename = renderContents = !nonTemplatedEntries.has(location); - } - if (renderFilename) { - localOutputPath = renderTemplate(localOutputPath, context); + renderContents = !nonTemplatedEntries.has(location); } + localOutputPath = renderTemplate(localOutputPath, context); const outputPath = resolveSafeChildPath(outputDir, localOutputPath); // variables have been expanded to make an empty file name // this is due to a conditional like if values.my_condition then file-name.txt else empty string so skip