From f9a82d452e53ed8488019a09b765e2e1c30278a1 Mon Sep 17 00:00:00 2001 From: Steven Lougheed Date: Mon, 14 Feb 2022 09:44:12 -0500 Subject: [PATCH 1/3] Fixed path for loading cookiecutter.json Signed-off-by: slougheed --- .../src/actions/fetch/cookiecutter.ts | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts index c4d7f1e3e8..efc8b6215e 100644 --- a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts +++ b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts @@ -62,25 +62,34 @@ export class CookiecutterRunner { values: JsonObject; logStream: Writable; }): Promise { - const templateDir = path.join(workspacePath, 'template'); const intermediateDir = path.join(workspacePath, 'intermediate'); await fs.ensureDir(intermediateDir); const resultDir = path.join(workspacePath, 'result'); + const { + templateContentsDir, + templateDir, + imageName, + ...valuesForCookieCutterJson + } = values; // First lets grab the default cookiecutter.json file - const cookieCutterJson = await this.fetchTemplateCookieCutter(templateDir); + const cookieCutterJson = await this.fetchTemplateCookieCutter( + templateContentsDir as string, + ); - const { imageName, ...valuesForCookieCutterJson } = values; const cookieInfo = { ...cookieCutterJson, ...valuesForCookieCutterJson, }; - await fs.writeJSON(path.join(templateDir, 'cookiecutter.json'), cookieInfo); + await fs.writeJSON( + path.join(templateDir as string, 'cookiecutter.json'), + cookieInfo, + ); // Directories to bind on container const mountDirs = { - [templateDir]: '/input', + [templateDir as string]: '/input', [intermediateDir]: '/output', }; @@ -91,7 +100,13 @@ export class CookiecutterRunner { if (cookieCutterInstalled) { await runCommand({ command: 'cookiecutter', - args: ['--no-input', '-o', intermediateDir, templateDir, '--verbose'], + args: [ + '--no-input', + '-o', + intermediateDir, + templateDir as string, + '--verbose', + ], logStream, }); } else { @@ -233,6 +248,8 @@ export function createFetchCookiecutterAction(options: { _copy_without_render: ctx.input.copyWithoutRender, _extensions: ctx.input.extensions, imageName: ctx.input.imageName, + templateDir: templateDir, + templateContentsDir: templateContentsDir, }; // Will execute the template in ./template and put the result in ./result From 6c7a8796601cc467dff1791f90b215b1a7f57b3d Mon Sep 17 00:00:00 2001 From: slougheed Date: Wed, 2 Mar 2022 14:15:10 -0500 Subject: [PATCH 2/3] Cleanup based on code review and added changeset Signed-off-by: slougheed --- .changeset/tidy-hairs-sip.md | 5 +++ .../src/actions/fetch/cookiecutter.ts | 39 +++++++------------ 2 files changed, 20 insertions(+), 24 deletions(-) create mode 100644 .changeset/tidy-hairs-sip.md diff --git a/.changeset/tidy-hairs-sip.md b/.changeset/tidy-hairs-sip.md new file mode 100644 index 0000000000..512447236d --- /dev/null +++ b/.changeset/tidy-hairs-sip.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-cookiecutter': patch +--- + +Fixed bug where existing cookiecutter.json file is not used. diff --git a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts index efc8b6215e..01174e0f7a 100644 --- a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts +++ b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts @@ -57,39 +57,36 @@ export class CookiecutterRunner { workspacePath, values, logStream, + imageName, + templateDir, + templateContentsDir, }: { workspacePath: string; values: JsonObject; logStream: Writable; + imageName?: string; + templateDir: string; + templateContentsDir: string; }): Promise { const intermediateDir = path.join(workspacePath, 'intermediate'); await fs.ensureDir(intermediateDir); const resultDir = path.join(workspacePath, 'result'); - const { - templateContentsDir, - templateDir, - imageName, - ...valuesForCookieCutterJson - } = values; // First lets grab the default cookiecutter.json file const cookieCutterJson = await this.fetchTemplateCookieCutter( - templateContentsDir as string, + templateContentsDir, ); const cookieInfo = { ...cookieCutterJson, - ...valuesForCookieCutterJson, + ...values, }; - await fs.writeJSON( - path.join(templateDir as string, 'cookiecutter.json'), - cookieInfo, - ); + await fs.writeJSON(path.join(templateDir, 'cookiecutter.json'), cookieInfo); // Directories to bind on container const mountDirs = { - [templateDir as string]: '/input', + [templateDir]: '/input', [intermediateDir]: '/output', }; @@ -100,13 +97,7 @@ export class CookiecutterRunner { if (cookieCutterInstalled) { await runCommand({ command: 'cookiecutter', - args: [ - '--no-input', - '-o', - intermediateDir, - templateDir as string, - '--verbose', - ], + args: ['--no-input', '-o', intermediateDir, templateDir, '--verbose'], logStream, }); } else { @@ -247,16 +238,16 @@ export function createFetchCookiecutterAction(options: { ...ctx.input.values, _copy_without_render: ctx.input.copyWithoutRender, _extensions: ctx.input.extensions, - imageName: ctx.input.imageName, - templateDir: templateDir, - templateContentsDir: templateContentsDir, }; // Will execute the template in ./template and put the result in ./result await cookiecutter.run({ workspacePath: workDir, logStream: ctx.logStream, - values, + values: values, + imageName: ctx.input.imageName, + templateDir: templateDir, + templateContentsDir: templateContentsDir, }); // Finally move the template result into the task workspace From 7d533a7b5b7db11ba8ca1358a08c660cb1f86aa7 Mon Sep 17 00:00:00 2001 From: slougheed Date: Wed, 2 Mar 2022 14:25:20 -0500 Subject: [PATCH 3/3] Removed string casting of imageName Signed-off-by: slougheed --- .../src/actions/fetch/cookiecutter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts index 4e09c81311..7575627506 100644 --- a/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts +++ b/plugins/scaffolder-backend-module-cookiecutter/src/actions/fetch/cookiecutter.ts @@ -102,7 +102,7 @@ export class CookiecutterRunner { }); } else { await this.containerRunner.runContainer({ - imageName: (imageName as string) ?? 'spotify/backstage-cookiecutter', + imageName: imageName ?? 'spotify/backstage-cookiecutter', command: 'cookiecutter', args: ['--no-input', '-o', '/output', '/input', '--verbose'], mountDirs,