mirror of
https://github.com/go-task/task.git
synced 2026-07-07 14:09:11 +00:00
feat: support for loops with generates (#2151)
This commit is contained in:
@@ -199,6 +199,9 @@ it is defined as a string, you can give it any of the following values:
|
||||
- `sources` - Will run the command for each source file defined on the task.
|
||||
(Glob patterns will be resolved, so `*.go` will run for every Go file that
|
||||
matches).
|
||||
- `generates` - Will run the command for each file defined in the task's generates
|
||||
list. (Glob patterns will be resolved, so `*.txt` will run for every text file
|
||||
that matches).
|
||||
|
||||
If it is defined as a list of strings, the command will be run for each value.
|
||||
|
||||
|
||||
@@ -1483,6 +1483,48 @@ tasks:
|
||||
cmd: cat {{joinPath .MY_DIR .ITEM}}
|
||||
```
|
||||
|
||||
### Looping over your task's generates
|
||||
|
||||
Similar to sources, you can also loop over the generates of your task:
|
||||
|
||||
```yaml
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
default:
|
||||
generates:
|
||||
- foo.txt
|
||||
- bar.txt
|
||||
cmds:
|
||||
- for: generates
|
||||
cmd: cat {{ .ITEM }}
|
||||
```
|
||||
|
||||
This will also work if you use globbing syntax in your generates. For example, if
|
||||
you specify a generate for `*.txt`, the loop will iterate over all files that
|
||||
match that glob.
|
||||
|
||||
Generate paths will always be returned as paths relative to the task directory. If
|
||||
you need to convert this to an absolute path, you can use the built-in
|
||||
`joinPath` function. There are some [special variables](/reference/templating/#special-variables)
|
||||
that you may find useful for this.
|
||||
|
||||
```yaml
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
default:
|
||||
vars:
|
||||
MY_DIR: /path/to/dir
|
||||
dir: '{{.MY_DIR}}'
|
||||
generates:
|
||||
- foo.txt
|
||||
- bar.txt
|
||||
cmds:
|
||||
- for: generates
|
||||
cmd: cat {{joinPath .MY_DIR .ITEM}}
|
||||
```
|
||||
|
||||
### Looping over variables
|
||||
|
||||
To loop over the contents of a variable, you simply need to specify the variable
|
||||
|
||||
@@ -475,7 +475,7 @@
|
||||
"for_attribute": {
|
||||
"description": "The task attribute to iterate over",
|
||||
"type": "string",
|
||||
"enum": ["sources"]
|
||||
"enum": ["sources", "generates"]
|
||||
},
|
||||
"for_var": {
|
||||
"description": "Which variables to iterate over. The variable will be split using any whitespace character by default. This can be changed by using the `split` attribute.",
|
||||
|
||||
Reference in New Issue
Block a user