1
0
mirror of https://github.com/go-task/task.git synced 2025-06-15 00:15:10 +02:00

feat: added basic merge template function (#1438)

This commit is contained in:
Pete Davison
2024-01-11 14:29:14 +00:00
committed by GitHub
parent d2522a6d9d
commit f6a24fe925
3 changed files with 21 additions and 8 deletions

View File

@ -646,9 +646,9 @@ tasks:
compare the checksum of the source files to determine if it's necessary to run compare the checksum of the source files to determine if it's necessary to run
the task. If not, it will just print a message like `Task "js" is up to date`. the task. If not, it will just print a message like `Task "js" is up to date`.
`exclude:` can also be used to exclude files from fingerprinting. `exclude:` can also be used to exclude files from fingerprinting. Sources are
Sources are evaluated in order, so `exclude:` must come after the positive evaluated in order, so `exclude:` must come after the positive glob it is
glob it is negating. negating.
```yaml ```yaml
version: '3' version: '3'
@ -1291,6 +1291,9 @@ Task also adds the following functions:
- `relPath`: Converts an absolute path (second argument) into a relative path, - `relPath`: Converts an absolute path (second argument) into a relative path,
based on a base path (first argument). The same as Go's based on a base path (first argument). The same as Go's
[filepath.Rel](https://pkg.go.dev/path/filepath#Rel). [filepath.Rel](https://pkg.go.dev/path/filepath#Rel).
- `merge`: Creates a new map that is a copy of the first map with the keys of
the second map merged into it. If there are duplicate keys, the value of the
second map is used.
- `spew`: Returns the Go representation of a specific variable. Useful for - `spew`: Returns the Go representation of a specific variable. Useful for
debugging. Uses the [davecgh/go-spew](https://github.com/davecgh/go-spew) debugging. Uses the [davecgh/go-spew](https://github.com/davecgh/go-spew)
package. package.
@ -1489,8 +1492,8 @@ task: "This is a dangerous command... Do you want to continue?" [y/N]
``` ```
Warning prompts are called before executing a task. If a prompt is denied Task Warning prompts are called before executing a task. If a prompt is denied Task
will exit with [exit code](/api#exit-codes) 205. If approved, Task will exit with [exit code](/api#exit-codes) 205. If approved, Task will continue
will continue as normal. as normal.
```bash ```bash
❯ task example ❯ task example
@ -1844,7 +1847,7 @@ tasks:
sources: sources:
- '**/*.go' - '**/*.go'
cmds: cmds:
- go build # ... - go build # ...
``` ```
:::info :::info

View File

@ -6,11 +6,10 @@ import (
"strings" "strings"
"text/template" "text/template"
"github.com/davecgh/go-spew/spew"
"mvdan.cc/sh/v3/shell" "mvdan.cc/sh/v3/shell"
"mvdan.cc/sh/v3/syntax" "mvdan.cc/sh/v3/syntax"
"github.com/davecgh/go-spew/spew"
sprig "github.com/go-task/slim-sprig/v3" sprig "github.com/go-task/slim-sprig/v3"
) )
@ -54,6 +53,16 @@ func init() {
"relPath": func(basePath, targetPath string) (string, error) { "relPath": func(basePath, targetPath string) (string, error) {
return filepath.Rel(basePath, targetPath) return filepath.Rel(basePath, targetPath)
}, },
"merge": func(a, b map[string]any) map[string]any {
m := make(map[string]any, len(a)+len(b))
for k, v := range a {
m[k] = v
}
for k, v := range b {
m[k] = v
}
return m
},
"spew": func(v any) string { "spew": func(v any) string {
return spew.Sdump(v) return spew.Sdump(v)
}, },

View File

@ -7,6 +7,7 @@ tasks:
- task: bool - task: bool
- task: int - task: int
- task: string-array - task: string-array
- task: map
- task: for-string - task: for-string
- task: for-int - task: for-int
- task: for-map - task: for-map