mirror of
https://github.com/go-task/task.git
synced 2024-12-12 10:45:49 +02:00
Move Variables and template documentation to the bottom, since they are more advanced than the other topics
This commit is contained in:
parent
17c3a67be6
commit
697e5466a5
156
README.md
156
README.md
@ -42,84 +42,6 @@ task assets build
|
||||
If Bash is available (Linux and Windows while on Git Bash), the commands will
|
||||
run in Bash, otherwise will fallback to `cmd` (on Windows).
|
||||
|
||||
### Variables
|
||||
|
||||
```yml
|
||||
build:
|
||||
deps: [setvar]
|
||||
cmds:
|
||||
- echo "{{.PREFIX}} {{.THEVAR}}"
|
||||
vars:
|
||||
PREFIX: "Path:"
|
||||
|
||||
setvar:
|
||||
cmds:
|
||||
- echo "{{.PATH}}"
|
||||
set: THEVAR
|
||||
```
|
||||
|
||||
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
|
||||
`Taskvars.yml` (or `Taskvars.toml` or `Taskvars.json`) as source of variables.
|
||||
|
||||
They are evaluated in the following order:
|
||||
|
||||
Task local variables are overwritten by variables found in `Taskvars` file.
|
||||
Variables found in `Taskvars` file 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
|
||||
build:
|
||||
deps: [setvar]
|
||||
cmds:
|
||||
- echo "{{.PREFIX}} '{{.THEVAR}}'"
|
||||
vars:
|
||||
PREFIX: "Result: "
|
||||
|
||||
setvar:
|
||||
cmds:
|
||||
- echo -n "a"
|
||||
- echo -n "{{.THEVAR}}b"
|
||||
- echo -n "{{.THEVAR}}c"
|
||||
set: THEVAR
|
||||
```
|
||||
|
||||
The result of a run of build would be:
|
||||
|
||||
```
|
||||
a
|
||||
ab
|
||||
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
|
||||
|
||||
By default, tasks will be executed in the directory where the Taskfile is
|
||||
@ -216,6 +138,84 @@ necessary to run the task. If not, it will just print
|
||||
You can use `--force` or `-f` if you want to force a task to run even when
|
||||
up-to-date.
|
||||
|
||||
### Variables
|
||||
|
||||
```yml
|
||||
build:
|
||||
deps: [setvar]
|
||||
cmds:
|
||||
- echo "{{.PREFIX}} {{.THEVAR}}"
|
||||
vars:
|
||||
PREFIX: "Path:"
|
||||
|
||||
setvar:
|
||||
cmds:
|
||||
- echo "{{.PATH}}"
|
||||
set: THEVAR
|
||||
```
|
||||
|
||||
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
|
||||
`Taskvars.yml` (or `Taskvars.toml` or `Taskvars.json`) as source of variables.
|
||||
|
||||
They are evaluated in the following order:
|
||||
|
||||
Task local variables are overwritten by variables found in `Taskvars` file.
|
||||
Variables found in `Taskvars` file 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
|
||||
build:
|
||||
deps: [setvar]
|
||||
cmds:
|
||||
- echo "{{.PREFIX}} '{{.THEVAR}}'"
|
||||
vars:
|
||||
PREFIX: "Result: "
|
||||
|
||||
setvar:
|
||||
cmds:
|
||||
- echo -n "a"
|
||||
- echo -n "{{.THEVAR}}b"
|
||||
- echo -n "{{.THEVAR}}c"
|
||||
set: THEVAR
|
||||
```
|
||||
|
||||
The result of a run of build would be:
|
||||
|
||||
```
|
||||
a
|
||||
ab
|
||||
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}}'
|
||||
```
|
||||
|
||||
[make]: https://www.gnu.org/software/make/
|
||||
[releases]: https://github.com/go-task/task/releases
|
||||
[golang]: https://golang.org/
|
||||
|
Loading…
Reference in New Issue
Block a user