mirror of
https://github.com/go-task/task.git
synced 2026-05-18 13:15:41 +02:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
58275b4b33 | ||
|
|
862237a931 | ||
|
|
9d81608337 | ||
|
|
39a4b4d413 | ||
|
|
21ceb05080 | ||
|
|
b592648d55 | ||
|
|
658b6012a6 | ||
|
|
311cdf00ab | ||
|
|
453538b405 |
10
CHANGELOG.md
10
CHANGELOG.md
@@ -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).
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
|
||||
const (
|
||||
changelogSource = "CHANGELOG.md"
|
||||
changelogTarget = "docs/docs/changelog.mdx"
|
||||
changelogTarget = "docs/docs/changelog.md"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -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).
|
||||
@@ -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)
|
||||
@@ -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 -->
|
||||
@@ -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
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@go-task/cli",
|
||||
"version": "3.33.0",
|
||||
"version": "3.33.1",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
|
||||
@@ -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",
|
||||
|
||||
26
testdata/vars/any/Taskfile.yml
vendored
26
testdata/vars/any/Taskfile.yml
vendored
@@ -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}}
|
||||
|
||||
12
variables.go
12
variables.go
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user