1
0
mirror of https://github.com/go-task/task.git synced 2025-03-19 21:17:46 +02:00

feat: added joinPath and relPath functions

This commit is contained in:
Pete Davison 2023-07-19 21:26:55 +00:00 committed by Andrey Nering
parent d447cc3f19
commit ca72f3c3a1
2 changed files with 33 additions and 7 deletions

View File

@ -883,17 +883,16 @@ you can use `requires`. This is useful when might not be clear to users which
variables are needed, or if you want clear message about what is required. Also variables are needed, or if you want clear message about what is required. Also
some tasks could have dangerous side effects if run with un-set variables. some tasks could have dangerous side effects if run with un-set variables.
Using `requires` you specify an array of strings in the `vars` sub-section Using `requires` you specify an array of strings in the `vars` sub-section under
under `requires`, these strings are variable names which are checked prior to `requires`, these strings are variable names which are checked prior to running
running the task. If any variables are un-set the the task will error and not the task. If any variables are un-set the the task will error and not run.
run.
Environmental variables are also checked. Environmental variables are also checked.
Syntax: Syntax:
```yaml ```yaml
requires: requires:
vars: [] # Array of strings vars: [] # Array of strings
``` ```
@ -914,7 +913,7 @@ tasks:
- 'docker build . -t {{.IMAGE_NAME}}:{{.IMAGE_TAG}}' - 'docker build . -t {{.IMAGE_NAME}}:{{.IMAGE_TAG}}'
# Make sure these variables are set before running # Make sure these variables are set before running
requires: requires:
vars: [IMAGE_NAME, IMAGE_TAG] vars: [IMAGE_NAME, IMAGE_TAG]
``` ```
@ -1042,6 +1041,26 @@ 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 you specify a source for `*.txt`, the loop will iterate over all files that
match that glob. 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:
```yaml
version: '3'
tasks:
default:
vars:
MY_DIR: /path/to/dir
dir: '{{.MY_DIR}}'
sources:
- foo.txt
- bar.txt
cmds:
- for: sources
cmd: cat {{ joinPath .MY_DIR .ITEM }}
```
### Looping over variables ### Looping over variables
To loop over the contents of a variable, you simply need to specify the variable To loop over the contents of a variable, you simply need to specify the variable

View File

@ -6,9 +6,10 @@ import (
"strings" "strings"
"text/template" "text/template"
sprig "github.com/go-task/slim-sprig"
"mvdan.cc/sh/v3/shell" "mvdan.cc/sh/v3/shell"
"mvdan.cc/sh/v3/syntax" "mvdan.cc/sh/v3/syntax"
sprig "github.com/go-task/slim-sprig"
) )
var templateFuncs template.FuncMap var templateFuncs template.FuncMap
@ -45,6 +46,12 @@ func init() {
}, },
// IsSH is deprecated. // IsSH is deprecated.
"IsSH": func() bool { return true }, "IsSH": func() bool { return true },
"joinPath": func(elem ...string) string {
return filepath.Join(elem...)
},
"relPath": func(basePath, targetPath string) (string, error) {
return filepath.Rel(basePath, targetPath)
},
} }
// Deprecated aliases for renamed functions. // Deprecated aliases for renamed functions.
taskFuncs["FromSlash"] = taskFuncs["fromSlash"] taskFuncs["FromSlash"] = taskFuncs["fromSlash"]