diff --git a/CHANGELOG.md b/CHANGELOG.md index bd558a8b..56859ef6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ - Wildcards can now [match multiple tasks](https://taskfile.dev/usage/#wildcard-arguments) (#2072, #2121 by @pd93). +- Added the ability to + [loop over the files specified by the `generates` keyword](https://taskfile.dev/usage/#looping-over-your-tasks-sources-or-generated-files). + This works the same way as looping over sources (#2151 by @sedyh). - Added the ability to resolve variables when defining an include variable (#2108, #2113 @pd93). - The default taskfile (output when using the `--init` flag) is now an embedded diff --git a/website/blog/2024-05-09-any-variables.mdx b/website/blog/2024-05-09-any-variables.mdx index 3c5f2607..755cac9e 100644 --- a/website/blog/2024-05-09-any-variables.mdx +++ b/website/blog/2024-05-09-any-variables.mdx @@ -31,13 +31,8 @@ some of the examples below for some inspiration. No more comparing strings to "true" or "false". Now you can use actual boolean values in your templates: - - - + + ```yaml version: 3 @@ -51,7 +46,7 @@ tasks: ``` - + ```yaml version: 3 @@ -64,7 +59,8 @@ tasks: - '{{if .BOOL}}echo foo{{end}}' # <-- No need to compare to "true" ``` - + + ### Arithmetic @@ -114,13 +110,8 @@ to specify the delimiter. However, we have now added support for looping over "collection-type" variables using the `for` keyword, so now you are able to loop over list variables directly: - - - + + ```yaml version: 3 @@ -137,7 +128,7 @@ tasks: ``` - + ```yaml version: 3 @@ -152,7 +143,8 @@ tasks: cmd: echo {{.ITEM}} ``` - + + ## What about maps? diff --git a/website/docs/usage.mdx b/website/docs/usage.mdx index 3def7114..001551ca 100644 --- a/website/docs/usage.mdx +++ b/website/docs/usage.mdx @@ -309,45 +309,38 @@ You can flatten the included Taskfile tasks into the main Taskfile by using the It means that the included Taskfile tasks will be available without the namespace. - + + - +```yaml +version: '3' - ```yaml - version: '3' +includes: + lib: + taskfile: ./Included.yml + flatten: true - includes: - lib: - taskfile: ./Included.yml - flatten: true +tasks: + greet: + cmds: + - echo "Greet" + - task: foo +``` - tasks: - greet: - cmds: - - echo "Greet" - - task: foo - ``` + + +```yaml +version: '3' - - - - ```yaml - version: '3' - - tasks: - foo: - cmds: - - echo "Foo" - ``` - - - +tasks: + foo: + cmds: + - echo "Foo" +``` + + If you run `task -a` it will print : @@ -368,43 +361,37 @@ Foo If multiple tasks have the same name, an error will be thrown: - + + - +```yaml +version: '3' +includes: + lib: + taskfile: ./Included.yml + flatten: true - ```yaml - version: '3' - includes: - lib: - taskfile: ./Included.yml - flatten: true +tasks: + greet: + cmds: + - echo "Greet" + - task: foo +``` - tasks: - greet: - cmds: - - echo "Greet" - - task: foo - ``` + + +```yaml +version: '3' - - +tasks: + greet: + cmds: + - echo "Foo" +``` - ```yaml - version: '3' - - tasks: - greet: - cmds: - - echo "Foo" - ``` - - - + + If you run `task -a` it will print: ```text @@ -420,35 +407,29 @@ You can do this by using the [`excludes` option](#exclude-tasks-from-being-inclu You can exclude tasks from being included by using the `excludes` option. This option takes the list of tasks to be excluded from this include. - + + - - ```yaml - version: '3' - includes: - included: - taskfile: ./Included.yml - excludes: [foo] - ``` +```yaml +version: '3' + includes: + included: + taskfile: ./Included.yml + excludes: [foo] +``` + + - - +```yaml +version: '3' - ```yaml - version: '3' +tasks: + foo: echo "Foo" + bar: echo "Bar" +``` - tasks: - foo: echo "Foo" - bar: echo "Bar" - ``` - - - + `task included:foo` will throw an error because the `foo` task is excluded but `task included:bar` will work and display `Bar`. @@ -1255,13 +1236,8 @@ a value from one task to another. However, the templating engine is only able to output strings. If you want to pass something other than a string to another task then you will need to use a reference (`ref`) instead. - - - + + ```yaml version: 3 @@ -1280,7 +1256,7 @@ tasks: ``` - + ```yaml version: 3 @@ -1299,7 +1275,8 @@ tasks: - 'echo {{index .FOO 0}}' # <-- FOO is still a map so the task outputs 'A' as expected ``` - + + This also works the same way when calling `deps` and when defining a variable and can be used in any combination: @@ -1441,9 +1418,13 @@ tasks: cmd: echo "{{.ITEM.OS}}/{{.ITEM.ARCH}}" ``` -### Looping over your task's sources +### Looping over your task's sources or generated files -You are also able to loop over the sources of your task: +You are also able to loop over the sources of your task or the files it +generates: + + + ```yaml version: '3' @@ -1458,14 +1439,37 @@ tasks: cmd: cat {{ .ITEM }} ``` -This will also work if you use globbing syntax in your sources. For example, if -you specify a source for `*.txt`, the loop will iterate over all files that -match that glob. + + -Source 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: + generates: + - foo.txt + - bar.txt + cmds: + - for: generates + cmd: cat {{ .ITEM }} +``` + + + + +This will also work if you use globbing syntax in `sources` or `generates`. For +example, if you specify a source for `*.txt`, the loop will iterate over all +files that match that glob. + +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' @@ -1483,31 +1487,8 @@ 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' @@ -1525,6 +1506,9 @@ tasks: cmd: cat {{joinPath .MY_DIR .ITEM}} ``` + + + ### Looping over variables To loop over the contents of a variable, you simply need to specify the variable