From 123d8bb3a7576dc201c95dc768551ff5f58fe996 Mon Sep 17 00:00:00 2001 From: Ismael Taboada Rodero Date: Sat, 19 Aug 2023 10:05:29 +0200 Subject: [PATCH] fix: render option vars before magic vars Brought forward the compile of variables from options before magic variables are processed. --- .gitignore | 2 +- .tool-versions | 1 + CHANGELOG.md | 6 ++++++ Taskfile.dist.yaml | 18 ++++++++++++++++++ go.mod | 4 ++-- magic/magic.go | 16 ++++++++-------- magic/magic_test.go | 34 ++++++++++++++++++++++++++++++++++ scripts/Taskfile.dist.yaml | 14 ++++++++++++++ 8 files changed, 84 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index bfc7a0b..6f8daf9 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,7 @@ *.out # Dependency directories (remove the comment below to include it) -# vendor/ +vendor/ # Go workspace file go.work diff --git a/.tool-versions b/.tool-versions index 2ca6d4e..dc5a82f 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,3 @@ golangci-lint 1.53.3 task 3.28.0 +semver 3.4.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index 755da02..b61291e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Security +## [1.0.5] - (2023-08-19) + +### Fixed + +* fix: render options variables before magic variables + ## [1.0.4] - (2023-08-07) ### Fixed diff --git a/Taskfile.dist.yaml b/Taskfile.dist.yaml index b140a22..0b2fdb3 100644 --- a/Taskfile.dist.yaml +++ b/Taskfile.dist.yaml @@ -112,3 +112,21 @@ tasks: - VERSION - CHANGELOG - TARGET_DIR + hotfix: + desc: Create hotfix branch + cmds: + - git switch -c hotfix/{{.PATCH_VERSION}} + vars: + VERSION: + sh: git describe --tags --match "*.*.*" --candidates 1 | sed -e 's/^v//' + PATCH_VERSION: + sh: semver bump patch {{.VERSION}} + fix: + desc: Create fix branch + cmds: + - git switch main + - git pull + - git switch -c fix/{{.PATCH_VERSION}} + requires: + vars: + - PATCH_VERSION diff --git a/go.mod b/go.mod index 5dd2a3e..8cf21b5 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.20 require ( github.com/go-playground/validator/v10 v10.14.1 + github.com/go-task/slim-sprig v2.20.0+incompatible github.com/gookit/goutil v0.6.11 github.com/lithammer/dedent v1.1.0 github.com/stretchr/testify v1.8.4 @@ -15,7 +16,6 @@ require ( github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-task/slim-sprig v2.20.0+incompatible // indirect github.com/google/uuid v1.3.0 // indirect github.com/huandu/xstrings v1.4.0 // indirect github.com/imdario/mergo v0.3.16 // indirect @@ -42,5 +42,5 @@ require ( github.com/samber/lo v1.38.1 github.com/spf13/cobra v1.7.0 gopkg.in/yaml.v2 v2.4.0 - gopkg.in/yaml.v3 v3.0.1 // indirect + gopkg.in/yaml.v3 v3.0.1 ) diff --git a/magic/magic.go b/magic/magic.go index 6d152c6..21a21b5 100644 --- a/magic/magic.go +++ b/magic/magic.go @@ -33,14 +33,6 @@ func NewMagic(version, name string, vars []variable.Variable, casts []cast.Cast) func (m *magic) Render(ctx context.Context, opts MagicRenderOptions) ([]file.File, error) { files := []file.File{} - for _, v := range m.vars { - value, err := v.Value(ctx) - if err != nil { - // TODO: Wrap error - return nil, err - } - ctx = ctx.WithVariable(v.Name(), value) - } if opts.Variables != nil { for _, v := range opts.Variables { value, err := v.Value(ctx) @@ -51,6 +43,14 @@ func (m *magic) Render(ctx context.Context, opts MagicRenderOptions) ([]file.Fil ctx = ctx.WithVariable(v.Name(), value) } } + for _, v := range m.vars { + value, err := v.Value(ctx) + if err != nil { + // TODO: Wrap error + return nil, err + } + ctx = ctx.WithVariable(v.Name(), value) + } for _, c := range m.casts { result, err := c.Compile(ctx) if err != nil { diff --git a/magic/magic_test.go b/magic/magic_test.go index 6956098..b65b67b 100644 --- a/magic/magic_test.go +++ b/magic/magic_test.go @@ -61,6 +61,40 @@ func TestMagic_Render(t *testing.T) { }, files) }) + t.Run("should render a magic with variables and casts and render options", func(t *testing.T) { + m := magic.NewMagic("1.0.0", "test", []variable.Variable{}, []cast.Cast{ + cast.NewBaseCast(source.NewTemplateSource("{{ .name }}"), template.NewTemplatedPath("test"), variable.Variables{}), + }) + files, err := m.Render(context.New(), magic.MagicRenderOptions{ + Variables: variable.Variables{ + variable.NewLiteralVariable("name", "Jane Doe"), + }, + }) + + assert.NoError(t, err) + assert.Equal(t, []file.File{ + file.NewTextFile("test", "Jane Doe"), + }, files) + }) + + t.Run("should render option variables before magic variables", func(t *testing.T) { + m := magic.NewMagic("1.0.0", "test", []variable.Variable{ + variable.NewTemplateVariable("name", "{{ .name }}"), + }, []cast.Cast{ + cast.NewBaseCast(source.NewTemplateSource("{{ .name }}"), template.NewTemplatedPath("test"), variable.Variables{}), + }) + files, err := m.Render(context.New(), magic.MagicRenderOptions{ + Variables: variable.Variables{ + variable.NewLiteralVariable("name", "Jane Doe"), + }, + }) + + assert.NoError(t, err) + assert.Equal(t, []file.File{ + file.NewTextFile("test", "Jane Doe"), + }, files) + }) + t.Run("should return an error if a variable fails to render", func(t *testing.T) { m := magic.NewMagic("1.0.0", "test", []variable.Variable{ variable.NewTemplateVariable("age", "{{ .name }"), diff --git a/scripts/Taskfile.dist.yaml b/scripts/Taskfile.dist.yaml index f2b4472..4641def 100644 --- a/scripts/Taskfile.dist.yaml +++ b/scripts/Taskfile.dist.yaml @@ -11,6 +11,13 @@ tasks: changelog:get: silent: true desc: Get changelog for version + summary: | + Get changelog for version + Variables: + - VERSION: Version to get changelog for + - CHANGELOG: (optional) Path to changelog file + Example: + - task changelog:get VERSION=1.0.0 CHANGELOG=CHANGELOG.md cmds: - pnpm run --silent changelog get {{.VERSION}} --changelog {{.CHANGELOG}} --quiet requires: @@ -20,6 +27,13 @@ tasks: SILENT: true changelog:create: desc: Create changelog for version + summary: | + Create changelog for version + Variables: + - VERSION: Version to create changelog for + - CHANGELOG: (optional) Path to changelog file + Example: + - task changelog:create VERSION=1.0.0 CHANGELOG=CHANGELOG.md cmds: - pnpm --silent run changelog create {{.VERSION}} {{.DATE}} --changelog {{.CHANGELOG}} --quiet requires: