Skip to content

Commit

Permalink
fix: render option vars before magic vars
Browse files Browse the repository at this point in the history
Brought forward the compile of variables from options before magic
variables are processed.
  • Loading branch information
ismtabo committed Aug 19, 2023
1 parent 1aacdaa commit 123d8bb
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/

# Go workspace file
go.work
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
golangci-lint 1.53.3
task 3.28.0
semver 3.4.0
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions Taskfile.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
)
16 changes: 8 additions & 8 deletions magic/magic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions magic/magic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"),
Expand Down
14 changes: 14 additions & 0 deletions scripts/Taskfile.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 123d8bb

Please sign in to comment.