From 56b61c2bc05c7d1d932c85632737f7eba2bc2690 Mon Sep 17 00:00:00 2001 From: Mike Lewis Date: Wed, 7 Jul 2021 11:26:00 +0100 Subject: [PATCH] scaffolder: prefix input values in fetch:template templates Signed-off-by: Mike Lewis --- .../software-templates/builtin-actions.md | 6 +++--- .../actions/builtin/fetch/template.test.ts | 19 +++++++++++-------- .../actions/builtin/fetch/template.ts | 11 ++++++----- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/docs/features/software-templates/builtin-actions.md b/docs/features/software-templates/builtin-actions.md index a165a0a274..5cd6b005d9 100644 --- a/docs/features/software-templates/builtin-actions.md +++ b/docs/features/software-templates/builtin-actions.md @@ -41,10 +41,10 @@ verbose template variables expressions. `fetch:cookiecutter` to `fetch:template`. 2. Update variable syntax in file names and content. `fetch:cookiecutter` expects variables to be enclosed in `{{` `}}` and prefixed with - `cookiecutter.`, while `fetch:template` doesn't require prefixing and expects - variables to be enclosed in `${{` `}}`. For example, a reference to variable + `cookiecutter.`, while `fetch:template` expects variables to be enclosed in + `${{` `}}` and prefixed with `values.`. For example, a reference to variable `myInputVariable` would need to be migrated from - `{{ cookiecutter.myInputVariable }}` to `${{ myInputVariable }}`. + `{{ cookiecutter.myInputVariable }}` to `${{ values.myInputVariable }}`. 3. Replace uses of `jsonify` with `dump`. The `jsonify` filter is built in to `cookiecutter`, and is not available by default when using `fetch:template`. The `dump` filter is equivalent, so an expression like 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 56fffe0189..79cc5be524 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 @@ -120,13 +120,14 @@ describe('fetch:template', () => { mockFetchContents.mockImplementation(({ outputPath }) => { mockFs({ [outputPath]: { - 'empty-dir-${{ count }}': {}, + 'empty-dir-${{ values.count }}': {}, 'static.txt': 'static content', - '${{ name }}.txt': 'static content', + '${{ values.name }}.txt': 'static content', subdir: { - 'templated-content.txt': '${{ name }}: ${{ count }}', + 'templated-content.txt': + '${{ values.name }}: ${{ values.count }}', }, - '.${{ name }}': '${{ itemList | dump }}', + '.${{ values.name }}': '${{ values.itemList | dump }}', 'a-binary-file.png': aBinaryFile, }, }); @@ -202,10 +203,12 @@ describe('fetch:template', () => { mockFs({ [outputPath]: { processed: { - 'templated-content-${{ name }}.txt': '${{ count }}', + 'templated-content-${{ values.name }}.txt': + '${{ values.count }}', }, '.unprocessed': { - 'templated-content-${{ name }}.txt': '${{ count }}', + 'templated-content-${{ values.name }}.txt': + '${{ values.count }}', }, }, }); @@ -219,10 +222,10 @@ describe('fetch:template', () => { it('ignores template syntax in files matched in copyWithoutRender', async () => { await expect( fs.readFile( - `${workspacePath}/target/.unprocessed/templated-content-\${{ name }}.txt`, + `${workspacePath}/target/.unprocessed/templated-content-\${{ values.name }}.txt`, 'utf-8', ), - ).resolves.toEqual('${{ count }}'); + ).resolves.toEqual('${{ values.count }}'); }); it('processes files not matched in copyWithoutRender', async () => { 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 ac7051fe69..4fd505f9b0 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/template.ts @@ -180,9 +180,10 @@ export function createFetchTemplateAction(options: { // `cookiecutter.`. To replicate this, we wrap our parameters // in an object with a `cookiecutter` property when compat // mode is enabled. - const parameters = ctx.input.cookiecutterCompat - ? { cookiecutter: ctx.input.values } - : ctx.input.values; + const { cookiecutterCompat, values } = ctx.input; + const context = { + [cookiecutterCompat ? 'cookiecutter' : 'values']: values, + }; ctx.logger.info( `Processing ${allEntriesInTemplate.length} template files/directories with input values`, @@ -196,7 +197,7 @@ export function createFetchTemplateAction(options: { outputDir, shouldCopyWithoutRender ? location - : templater.renderString(location, parameters), + : templater.renderString(location, context), ); if (shouldCopyWithoutRender) { @@ -227,7 +228,7 @@ export function createFetchTemplateAction(options: { outputPath, shouldCopyWithoutRender ? inputFileContents - : templater.renderString(inputFileContents, parameters), + : templater.renderString(inputFileContents, context), ); } }