Compare commits

...

9 Commits

Author SHA1 Message Date
Andrey Nering
58275b4b33 v3.33.1 2023-12-21 20:10:41 -03:00
Andrey Nering
862237a931 chore(readme): fix typo on link 2023-12-21 20:07:55 -03:00
Pete Davison
9d81608337 chore: changelog for #1437 2023-12-21 16:07:44 +00:00
Pete Davison
39a4b4d413 fix: variable propagation (#1437) 2023-12-21 16:04:45 +00:00
Pete Davison
21ceb05080 chore: changelog 2023-12-21 15:51:34 +00:00
Pete Davison
b592648d55 feat: support looping over map variables (#1436)
* feat: support looping over map variables

* feat: add .KEY variable
2023-12-21 15:43:56 +00:00
Pete Davison
658b6012a6 revert: docs back to .md files until prettier supports mdx 2023-12-21 15:20:14 +00:00
Pete Davison
311cdf00ab docs: add information about loops 2023-12-21 11:09:34 +00:00
Pete Davison
453538b405 chore: update any_variables doc to mdx 2023-12-21 02:26:53 +00:00
29 changed files with 148 additions and 19 deletions

View File

@@ -1,9 +1,17 @@
# Changelog
## v3.33.1 - 2023-12-21
- Added support for looping over map variables with the
[Any Variables experiment](https://taskfile.dev/experiments/any_variables)
enabled (#1435, #1437 by @pd93).
- Fixed a bug where dynamic variables were causing errors during fast
compilation (#1435, #1437 by @pd93)
## v3.33.0 - 2023-12-20
- Added
[Any Variables experiment](https://taskfile.dev/experiments/any_variables)
[Any Variables experiment](https://taskfile.dev/experiments/any-variables)
(#1415, #1421 by @pd93).
- Updated Docusaurus to v3 (#1432 by @pd93).
- Added `aliases` to `--json` flag output (#1430, #1431 by @pd93).

View File

@@ -14,7 +14,7 @@ import (
const (
changelogSource = "CHANGELOG.md"
changelogTarget = "docs/docs/changelog.mdx"
changelogTarget = "docs/docs/changelog.md"
)
var (

View File

@@ -5,10 +5,18 @@ sidebar_position: 14
# Changelog
## v3.33.1 - 2023-12-21
- Added support for looping over map variables with the
[Any Variables experiment](https://taskfile.dev/experiments/any_variables)
enabled (#1435, #1437 by @pd93).
- Fixed a bug where dynamic variables were causing errors during fast
compilation (#1435, #1437 by @pd93)
## v3.33.0 - 2023-12-20
- Added
[Any Variables experiment](https://taskfile.dev/experiments/any_variables)
[Any Variables experiment](https://taskfile.dev/experiments/any-variables)
(#1415, #1421 by @pd93).
- Updated Docusaurus to v3 (#1432 by @pd93).
- Added `aliases` to `--json` flag output (#1430, #1431 by @pd93).

View File

@@ -19,9 +19,8 @@ the website homepage and on the GitHub repository README. Make contact with
## GitHub Sponsors
The preferred way to donate to the maintainers is via GitHub Sponsors. Just use
the following links to do your donation.
We suggest a 50/50 split to each maintainer of the total amount you plan to
donate to the project.
the following links to do your donation. We suggest a 50/50 split to each
maintainer of the total amount you plan to donate to the project.
- [@andreynering](https://github.com/sponsors/andreynering)
- [@pd93](https://github.com/sponsors/pd93)

View File

@@ -4,7 +4,7 @@ slug: /experiments/any-variables/
# Any Variables
- Issue: [#1415](https://github.com/go-task/task/issues/1415)
- Issue: #1415
- Environment variable: `TASK_X_ANY_VARIABLES=1`
- Breaks:
- Dynamically defined variables (using the `sh` keyword)
@@ -49,7 +49,7 @@ tasks:
- 'echo {{add .INT .FLOAT}}'
```
Loops:
Ranging:
```yaml
version: 3
@@ -62,7 +62,81 @@ tasks:
- 'echo {{range .ARRAY}}{{.}}{{end}}'
```
etc.
There are many more templating functions which can be used with the new types of
variables. For a full list, see the [slim-sprig][slim-sprig] documentation.
## Looping over variables
Previously, you would have to use a delimiter separated string to loop over an
arbitrary list of items in a variable and split them by using the `split` subkey
to specify the delimiter:
```yaml
version: 3
tasks:
foo:
vars:
LIST: 'foo,bar,baz'
cmds:
- for:
var: LIST
split: ','
cmd: echo {{.ITEM}}
```
Because this experiment adds support for "collection-type" variables, the `for`
keyword has been updated to support looping over arrays directly:
```yaml
version: 3
tasks:
foo:
vars:
LIST: [foo, bar, baz]
cmds:
- for:
var: LIST
cmd: echo {{.ITEM}}
```
This also works for maps. When looping over a map we also make an additional
`{{.KEY}}` variable availabe that holds the string value of the map key.
Remember that maps are unordered, so the order in which the items are looped
over is random:
```yaml
version: 3
tasks:
foo:
vars:
MAP:
KEY_1:
SUBKEY: sub_value_1
KEY_2:
SUBKEY: sub_value_2
KEY_3:
SUBKEY: sub_value_3
cmds:
- for:
var: MAP
cmd: echo {{.KEY}} {{.ITEM.SUBKEY}}
```
String splitting is still supported and remember that for simple cases, you have
always been able to loop over an array without using variables at all:
```yaml
version: 3
tasks:
foo:
cmds:
- for: [foo, bar, baz]
cmd: echo {{.ITEM}}
```
## Migration
@@ -103,3 +177,7 @@ task:
If your current Taskfile contains a string variable that begins with a `$`, you
will now need to escape the `$` with a backslash (`\`) to stop Task from
executing it as a command.
<!-- prettier-ignore-start -->
[slim-sprig]: https://go-task.github.io/slim-sprig/
<!-- prettier-ignore-end -->

View File

@@ -68,17 +68,23 @@ func (c *CompilerV3) getVariables(t *taskfile.Task, call *taskfile.Call, evaluat
}
newVar.Sh = tr.Replace(v.Sh)
newVar.Dir = v.Dir
if err := tr.Err(); err != nil {
return err
}
// If the variable should not be evaluated, but is nil, set it to an empty string
// This stops empty interface errors when using the templater to replace values later
if !evaluateShVars && newVar.Value == nil {
result.Set(k, taskfile.Var{Value: ""})
return nil
}
// If the variable should not be evaluated and it is set, we can set it and return
if !evaluateShVars {
result.Set(k, taskfile.Var{Value: newVar.Value})
return nil
}
// Now we can check for errors since we've handled all the cases when we don't want to evaluate
if err := tr.Err(); err != nil {
return err
}
// If the variable is not dynamic, we can set it and return
if !evaluateShVars || newVar.Value != nil || newVar.Sh == "" {
if newVar.Value != nil || newVar.Sh == "" {
result.Set(k, taskfile.Var{Value: newVar.Value})
return nil
}

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{
"name": "@go-task/cli",
"version": "3.33.0",
"version": "3.33.1",
"lockfileVersion": 2,
"requires": true,
"packages": {

View File

@@ -1,6 +1,6 @@
{
"name": "@go-task/cli",
"version": "3.33.0",
"version": "3.33.1",
"description": "A task runner / simpler Make alternative written in Go",
"scripts": {
"postinstall": "go-npm install",

View File

@@ -9,6 +9,8 @@ tasks:
- task: string-array
- task: for-string
- task: for-int
- task: for-map
- task: for-multi-layer-map
dynamic:
vars:
@@ -78,3 +80,27 @@ tasks:
var: LIST
cmd: echo {{add .ITEM 100}}
for-map:
vars:
MAP:
KEY_1: value_1
KEY_2: value_2
KEY_3: value_3
cmds:
- for:
var: MAP
cmd: echo {{.KEY}} {{.ITEM}}
for-multi-layer-map:
vars:
MAP:
KEY_1:
SUBKEY: sub_value_1
KEY_2:
SUBKEY: sub_value_2
KEY_3:
SUBKEY: sub_value_3
cmds:
- for:
var: MAP
cmd: echo {{.KEY}} {{.ITEM.SUBKEY}}

View File

@@ -133,6 +133,7 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
continue
}
if cmd.For != nil {
var keys []string
var list []any
// Get the list from the explicit for list
if cmd.For.List != nil && len(cmd.For.List) > 0 {
@@ -170,9 +171,9 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
case []any:
list = value
case map[string]any:
return &taskfile.Task{}, errors.TaskfileInvalidError{
URI: origTask.Location.Taskfile,
Err: errors.New("sh is not supported with the 'Any Variables' experiment enabled.\nSee https://taskfile.dev/experiments/any-variables for more information."),
for k, v := range value {
keys = append(keys, k)
list = append(list, v)
}
default:
return nil, errors.TaskfileInvalidError{
@@ -191,10 +192,13 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
as = "ITEM"
}
// Create a new command for each item in the list
for _, loopValue := range list {
for i, loopValue := range list {
extra := map[string]any{
as: loopValue,
}
if len(keys) > 0 {
extra["KEY"] = keys[i]
}
new.Cmds = append(new.Cmds, &taskfile.Cmd{
Cmd: r.ReplaceWithExtra(cmd.Cmd, extra),
Task: r.ReplaceWithExtra(cmd.Task, extra),