mirror of
https://github.com/go-task/task.git
synced 2025-03-19 21:17:46 +02:00
README: document use of Go's template engine
This commit is contained in:
parent
91d5fa5fe6
commit
41df7e19b4
45
README.md
45
README.md
@ -48,37 +48,41 @@ run in Bash, otherwise will fallback to `cmd` (on Windows).
|
|||||||
build:
|
build:
|
||||||
deps: [setvar]
|
deps: [setvar]
|
||||||
cmds:
|
cmds:
|
||||||
- echo "{{PREFIX}} {{THEVAR}}"
|
- echo "{{.PREFIX}} {{.THEVAR}}"
|
||||||
vars:
|
vars:
|
||||||
PREFIX: "Path:"
|
PREFIX: "Path:"
|
||||||
|
|
||||||
setvar:
|
setvar:
|
||||||
cmds:
|
cmds:
|
||||||
- echo "{{PATH}}"
|
- echo "{{.PATH}}"
|
||||||
set: THEVAR
|
set: THEVAR
|
||||||
```
|
```
|
||||||
|
|
||||||
The above sample saves the path into a new variable which is then again echoed.
|
The above sample saves the path into a new variable which is then again echoed.
|
||||||
|
|
||||||
You can use environment variables, task level variables and a file called `Variables` as source of variables.
|
You can use environment variables, task level variables and a file called
|
||||||
|
`Variables` as source of variables.
|
||||||
|
|
||||||
They are evaluated in the following order:
|
They are evaluated in the following order:
|
||||||
|
|
||||||
Task local variables are overwritten by variables found in `Variables`. Variables found in `Variables` are overwritten with variables from the environment. The output of the last command is stored in the environment. So you can do something like this:
|
Task local variables are overwritten by variables found in `Variables`.
|
||||||
|
Variables found in `Variables` are overwritten with variables from the
|
||||||
|
environment. The output of the last command is stored in the environment. So
|
||||||
|
you can do something like this:
|
||||||
|
|
||||||
```yml
|
```yml
|
||||||
build:
|
build:
|
||||||
deps: [setvar]
|
deps: [setvar]
|
||||||
cmds:
|
cmds:
|
||||||
- echo "{{PREFIX}} '{{THEVAR}}'"
|
- echo "{{.PREFIX}} '{{.THEVAR}}'"
|
||||||
vars:
|
vars:
|
||||||
PREFIX: "Result: "
|
PREFIX: "Result: "
|
||||||
|
|
||||||
setvar:
|
setvar:
|
||||||
cmds:
|
cmds:
|
||||||
- echo -n "a"
|
- echo -n "a"
|
||||||
- echo -n "{{THEVAR}}b"
|
- echo -n "{{.THEVAR}}b"
|
||||||
- echo -n "{{THEVAR}}c"
|
- echo -n "{{.THEVAR}}c"
|
||||||
set: THEVAR
|
set: THEVAR
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -91,6 +95,32 @@ abc
|
|||||||
Result: 'abc'
|
Result: 'abc'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Go's template engine
|
||||||
|
|
||||||
|
Task parse commands as [Go's template engine][gotemplate] before executing
|
||||||
|
them. Variables are acessible trought dot syntax (`.VARNAME`). The following
|
||||||
|
functions are available:
|
||||||
|
|
||||||
|
- `OS`: return operating system. Possible values are "windows", "linux",
|
||||||
|
"darwin" (macOS) and "freebsd".
|
||||||
|
- `ARCH`: return the architecture Task was compiled to: "386", "amd64", "arm"
|
||||||
|
or "s390x".
|
||||||
|
- `IsSH`: on unix system this should always return `true`. On Windows, will
|
||||||
|
return `true` if `sh` command was found (Git Bash). In this case commands
|
||||||
|
will run on `sh`. Otherwise, this function will return `false` meaning
|
||||||
|
commands will run on `cmd`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```yml
|
||||||
|
printos:
|
||||||
|
cmds:
|
||||||
|
- echo '{{OS}} {{ARCH}}'
|
||||||
|
- "echo '{{if eq OS \"windows\"}}windows-command{{else}}unix-command{{end}}'"
|
||||||
|
- echo 'Is SH? {{IsSH}}'
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
### Running task in another dir
|
### Running task in another dir
|
||||||
|
|
||||||
By default, tasks will be executed in the directory where the Taskfile is
|
By default, tasks will be executed in the directory where the Taskfile is
|
||||||
@ -190,3 +220,4 @@ up-to-date.
|
|||||||
[make]: https://www.gnu.org/software/make/
|
[make]: https://www.gnu.org/software/make/
|
||||||
[releases]: https://github.com/go-task/task/releases
|
[releases]: https://github.com/go-task/task/releases
|
||||||
[golang]: https://golang.org/
|
[golang]: https://golang.org/
|
||||||
|
[gotemplate]: https://golang.org/pkg/text/template/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user