Skip to content

Commit

Permalink
feat: added joinPath and relPath functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 authored and andreynering committed Jul 25, 2023
1 parent d447cc3 commit ca72f3c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
31 changes: 25 additions & 6 deletions docs/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
under `requires`, these strings are variable names which are checked prior to
running the task. If any variables are un-set the the task will error and not
run.
Using `requires` you specify an array of strings in the `vars` sub-section under
`requires`, these strings are variable names which are checked prior to running
the task. If any variables are un-set the the task will error and not run.

Environmental variables are also checked.

Syntax:

```yaml
requires:
requires:
vars: [] # Array of strings
```

Expand All @@ -914,7 +913,7 @@ tasks:
- 'docker build . -t {{.IMAGE_NAME}}:{{.IMAGE_TAG}}'

# Make sure these variables are set before running
requires:
requires:
vars: [IMAGE_NAME, IMAGE_TAG]
```

Expand Down Expand Up @@ -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
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

To loop over the contents of a variable, you simply need to specify the variable
Expand Down
9 changes: 8 additions & 1 deletion internal/templater/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"strings"
"text/template"

sprig "github.com/go-task/slim-sprig"
"mvdan.cc/sh/v3/shell"
"mvdan.cc/sh/v3/syntax"

sprig "github.com/go-task/slim-sprig"
)

var templateFuncs template.FuncMap
Expand Down Expand Up @@ -45,6 +46,12 @@ func init() {
},
// IsSH is deprecated.
"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.
taskFuncs["FromSlash"] = taskFuncs["fromSlash"]
Expand Down

0 comments on commit ca72f3c

Please sign in to comment.