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

Documentation changes for v2.0.0 release

This commit is contained in:
Andrey Nering 2018-03-07 22:54:37 -03:00
parent c1ae36866e
commit d48a2f3ccf
2 changed files with 366 additions and 198 deletions

219
README.md
View File

@ -3,6 +3,10 @@
# Task - A task runner / simpler Make alternative written in Go # Task - A task runner / simpler Make alternative written in Go
> We recently released version 2.0.0 of Task. The Taskfile changed a bit.
Please, check the [Taskfile versions](TASKFILE_VERSIONS.md) document to see
what changed and how to upgrade.
Task is a simple tool that allows you to easily run development and build Task is a simple tool that allows you to easily run development and build
tasks. Task is written in Golang, but can be used to develop any language. tasks. Task is written in Golang, but can be used to develop any language.
It aims to be simpler and easier to use then [GNU Make][make]. It aims to be simpler and easier to use then [GNU Make][make].
@ -73,11 +77,14 @@ The example below allows compile a Go app and uses [Minify][minify] to concat
and minify multiple CSS files into a single one. and minify multiple CSS files into a single one.
```yml ```yml
build: version: '2'
tasks:
build:
cmds: cmds:
- go build -v -i main.go - go build -v -i main.go
assets: assets:
cmds: cmds:
- minify -o public/style.css src/css - minify -o public/style.css src/css
``` ```
@ -100,7 +107,10 @@ If you ommit a task name, "default" will be assumed.
You can specify environment variables that are added when running a command: You can specify environment variables that are added when running a command:
```yml ```yml
build: version: '2'
tasks:
build:
cmds: cmds:
- echo $hallo - echo $hallo
env: env:
@ -117,7 +127,10 @@ Example:
Taskfile.yml: Taskfile.yml:
```yml ```yml
build: version: '2'
tasks:
build:
cmds: cmds:
- echo "default" - echo "default"
``` ```
@ -125,7 +138,8 @@ build:
Taskfile_linux.yml: Taskfile_linux.yml:
```yml ```yml
build: tasks:
build:
cmds: cmds:
- echo "linux" - echo "linux"
``` ```
@ -143,7 +157,10 @@ located. But you can easily make the task run in another folder informing
`dir`: `dir`:
```yml ```yml
serve: version: '2'
tasks:
serve:
dir: public/www dir: public/www
cmds: cmds:
# run http server # run http server
@ -156,12 +173,15 @@ You may have tasks that depends on others. Just pointing them on `deps` will
make them run automatically before running the parent task: make them run automatically before running the parent task:
```yml ```yml
build: version: '2'
tasks:
build:
deps: [assets] deps: [assets]
cmds: cmds:
- go build -v -i main.go - go build -v -i main.go
assets: assets:
cmds: cmds:
- minify -o public/style.css src/css - minify -o public/style.css src/css
``` ```
@ -172,14 +192,17 @@ In the above example, `assets` will always run right before `build` if you run
A task can have only dependencies and no commands to group tasks together: A task can have only dependencies and no commands to group tasks together:
```yml ```yml
assets: version: '2'
tasks:
assets:
deps: [js, css] deps: [js, css]
js: js:
cmds: cmds:
- minify -o public/script.js src/js - minify -o public/script.js src/js
css: css:
cmds: cmds:
- minify -o public/style.css src/css - minify -o public/style.css src/css
``` ```
@ -187,10 +210,14 @@ css:
If there are more than one dependency, they always run in parallel for better If there are more than one dependency, they always run in parallel for better
performance. performance.
If you want to pass information to dependencies, you can do that the same manner as you would to [call another task](#calling-another-task): If you want to pass information to dependencies, you can do that the same
manner as you would to [call another task](#calling-another-task):
```yml ```yml
default: version: '2'
tasks:
default:
deps: deps:
- task: echo_sth - task: echo_sth
vars: {TEXT: "before 1"} vars: {TEXT: "before 1"}
@ -199,12 +226,11 @@ default:
cmds: cmds:
- echo "after" - echo "after"
echo_sth: echo_sth:
cmds: cmds:
- echo {{.TEXT}} - echo {{.TEXT}}
``` ```
### Calling another task ### Calling another task
When a task has many dependencies, they are executed concurrently. This will When a task has many dependencies, they are executed concurrently. This will
@ -212,17 +238,20 @@ often result in a faster build pipeline. But in some situations you may need
to call other tasks serially. In this case, just use the following syntax: to call other tasks serially. In this case, just use the following syntax:
```yml ```yml
main-task: version: '2'
tasks:
main-task:
cmds: cmds:
- task: task-to-be-called - task: task-to-be-called
- task: another-task - task: another-task
- echo "Both done" - echo "Both done"
task-to-be-called: task-to-be-called:
cmds: cmds:
- echo "Task to be called" - echo "Task to be called"
another-task: another-task:
cmds: cmds:
- echo "Another task" - echo "Another task"
``` ```
@ -231,45 +260,38 @@ Overriding variables in the called task is as simple as informing `vars`
attribute: attribute:
```yml ```yml
main-task: version: '2'
tasks:
main-task:
cmds: cmds:
- task: write-file - task: write-file
vars: {FILE: "hello.txt", CONTENT: "Hello!"} vars: {FILE: "hello.txt", CONTENT: "Hello!"}
- task: write-file - task: write-file
vars: {FILE: "world.txt", CONTENT: "World!"} vars: {FILE: "world.txt", CONTENT: "World!"}
write-file: write-file:
cmds: cmds:
- echo "{{.CONTENT}}" > {{.FILE}} - echo "{{.CONTENT}}" > {{.FILE}}
``` ```
The above syntax is also supported in `deps`. The above syntax is also supported in `deps`.
> NOTE: It's also possible to call a task without any param prefixing it
with `^`, but this syntax is deprecated:
```yml
a-task:
cmds:
- ^another-task
another-task:
cmds:
- echo "Another task"
```
### Prevent unnecessary work ### Prevent unnecessary work
If a task generates something, you can inform Task the source and generated If a task generates something, you can inform Task the source and generated
files, so Task will prevent to run them if not necessary. files, so Task will prevent to run them if not necessary.
```yml ```yml
build: version: '2'
tasks:
build:
deps: [js, css] deps: [js, css]
cmds: cmds:
- go build -v -i main.go - go build -v -i main.go
js: js:
cmds: cmds:
- minify -o public/script.js src/js - minify -o public/script.js src/js
sources: sources:
@ -277,7 +299,7 @@ js:
generates: generates:
- public/script.js - public/script.js
css: css:
cmds: cmds:
- minify -o public/style.css src/css - minify -o public/style.css src/css
sources: sources:
@ -298,7 +320,10 @@ You will probably want to ignore the `.task` folder in your `.gitignore` file
This feature is still experimental and can change until it's stable. This feature is still experimental and can change until it's stable.
```yml ```yml
build: version: '2'
tasks:
build:
cmds: cmds:
- go build . - go build .
sources: sources:
@ -314,7 +339,10 @@ Alternatively, you can inform a sequence of tests as `status`. If no error
is returned (exit status 0), the task is considered up-to-date: is returned (exit status 0), the task is considered up-to-date:
```yml ```yml
generate-files: version: '2'
tasks:
generate-files:
cmds: cmds:
- mkdir directory - mkdir directory
- touch directory/file1.txt - touch directory/file1.txt
@ -337,13 +365,14 @@ the tasks is not up-to-date.
When doing interpolation of variables, Task will look for the below. When doing interpolation of variables, Task will look for the below.
They are listed below in order of importance (e.g. most important first): They are listed below in order of importance (e.g. most important first):
- Variables declared locally in the task
- Variables given while calling a task from another. - Variables given while calling a task from another.
(See [Calling another task](#calling-another-task) above) (See [Calling another task](#calling-another-task) above)
- Environment variables - Variables declared in the `vars:` option in the `Taskfile`
- Variables declared locally in the task
- Variables available in the `Taskvars.yml` file - Variables available in the `Taskvars.yml` file
- Environment variables
Example of overriding with environment variables: Example of sending parameters with environment variables:
```bash ```bash
$ TASK_VARIABLE=a-value task do-something $ TASK_VARIABLE=a-value task do-something
@ -361,13 +390,30 @@ $ task write-file FILE=file.txt "CONTENT=Hello, World!" print "MESSAGE=All done!
Example of locally declared vars: Example of locally declared vars:
```yml ```yml
print-var: version: '2'
tasks:
print-var:
cmds: cmds:
echo "{{.VAR}}" echo "{{.VAR}}"
vars: vars:
VAR: Hello! VAR: Hello!
``` ```
Example of global vars in a `Taskfile.yml`:
```yml
version: '2'
vars:
GREETING: Hello from Taskfile!
tasks:
greet:
cmds:
- echo "{{.GREETING}}"
```
Example of `Taskvars.yml` file: Example of `Taskvars.yml` file:
```yml ```yml
@ -376,6 +422,30 @@ DEV_MODE: production
GIT_COMMIT: {sh: git log -n 1 --format=%h} GIT_COMMIT: {sh: git log -n 1 --format=%h}
``` ```
#### Variables expansion
Variables are expanded 2 times by default. You can change that by setting the
`expansions:` option. Change that will be necessary if you compose many
variables together:
```yml
version: '2'
expansions: 3
vars:
FOO: foo
BAR: bar
BAZ: baz
FOOBAR: "{{.FOO}}{{.BAR}}"
FOOBARBAZ: "{{.FOOBAR}}{{.BAZ}}"
tasks:
default:
cmds:
- echo "{{.FOOBARBAZ}}"
```
#### Dynamic variables #### Dynamic variables
The below syntax (`sh:` prop in a variable) is considered a dynamic variable. The below syntax (`sh:` prop in a variable) is considered a dynamic variable.
@ -383,7 +453,10 @@ The value will be treated as a command and the output assigned. If there is one
or more trailing newlines, the last newline will be trimmed. or more trailing newlines, the last newline will be trimmed.
```yml ```yml
build: version: '2'
tasks:
build:
cmds: cmds:
- go build -ldflags="-X main.Version={{.GIT_COMMIT}}" main.go - go build -ldflags="-X main.Version={{.GIT_COMMIT}}" main.go
vars: vars:
@ -393,20 +466,6 @@ build:
This works for all types of variables. This works for all types of variables.
> It's also possible to prefix the variable with `$` to have a dynamic
variable, but this is now considered deprecated:
```yml
# Taskvars.yml
# recommended
GIT_COMMIT:
sh: git log -n 1 --format=%h
# deprecated
GIT_COMMIT: $git log -n 1 --format=%h
```
### Go's template engine ### Go's template engine
Task parse commands as [Go's template engine][gotemplate] before executing Task parse commands as [Go's template engine][gotemplate] before executing
@ -416,7 +475,10 @@ All functions by the Go's [sprig lib](http://masterminds.github.io/sprig/)
are available. The following example gets the current date in a given format: are available. The following example gets the current date in a given format:
```yml ```yml
print-date: version: '2'
tasks:
print-date:
cmds: cmds:
- echo {{now | date "2006-01-02"}} - echo {{now | date "2006-01-02"}}
``` ```
@ -439,13 +501,16 @@ Task also adds the following functions:
Example: Example:
```yml ```yml
print-os: version: '2'
tasks:
print-os:
cmds: cmds:
- echo '{{OS}} {{ARCH}}' - echo '{{OS}} {{ARCH}}'
- echo '{{if eq OS "windows"}}windows-command{{else}}unix-command{{end}}' - echo '{{if eq OS "windows"}}windows-command{{else}}unix-command{{end}}'
# This will be path/to/file on Unix but path\to\file on Windows # This will be path/to/file on Unix but path\to\file on Windows
- echo '{{fromSlash "path/to/file"}}' - echo '{{fromSlash "path/to/file"}}'
enumerated-file: enumerated-file:
vars: vars:
CONTENT: | CONTENT: |
foo foo
@ -458,30 +523,30 @@ enumerated-file:
{{end}}EOF {{end}}EOF
``` ```
> NOTE: There are some deprecated function names still available: `ToSlash`,
`FromSlash` and `ExeExt`. These where changed for consistency with sprig lib.
### Help ### Help
Running `task --list` (or `task -l`) lists all tasks with a description. Running `task --list` (or `task -l`) lists all tasks with a description.
The following taskfile: The following taskfile:
```yml ```yml
build: version: '2'
tasks:
build:
desc: Build the go binary. desc: Build the go binary.
cmds: cmds:
- go build -v -i main.go - go build -v -i main.go
test: test:
desc: Run all the go tests. desc: Run all the go tests.
cmds: cmds:
- go test -race ./... - go test -race ./...
js: js:
cmds: cmds:
- minify -o public/script.js src/js - minify -o public/script.js src/js
css: css:
cmds: cmds:
- minify -o public/style.css src/css - minify -o public/style.css src/css
``` ```
@ -499,7 +564,10 @@ Silent mode disables echoing of commands before Task runs it.
For the following Taskfile: For the following Taskfile:
```yml ```yml
echo: version: '2'
tasks:
echo:
cmds: cmds:
- echo "Print something" - echo "Print something"
``` ```
@ -522,7 +590,10 @@ There's three ways to enable silent mode:
* At command level: * At command level:
```yml ```yml
echo: version: '2'
tasks:
echo:
cmds: cmds:
- cmd: echo "Print something" - cmd: echo "Print something"
silent: true silent: true
@ -531,7 +602,10 @@ echo:
* At task level: * At task level:
```yml ```yml
echo: version: '2'
tasks:
echo:
cmds: cmds:
- echo "Print something" - echo "Print something"
silent: true silent: true
@ -542,7 +616,10 @@ echo:
If you want to suppress stdout instead, just redirect a command to `/dev/null`: If you want to suppress stdout instead, just redirect a command to `/dev/null`:
```yml ```yml
echo: version: '2'
tasks:
echo:
cmds: cmds:
- echo "This will print nothing" > /dev/null - echo "This will print nothing" > /dev/null
``` ```

91
TASKFILE_VERSIONS.md Normal file
View File

@ -0,0 +1,91 @@
# Taskfile version
The Taskfile syntax and features changed with time. This document explains what
changed on each version and how to upgrade your Taskfile.
# What the Taskfile version mean
The Taskfile version follows the Task version. E.g. the change to Taskfile
version `2` means that Task `v2.0.0` should be release to support it.
The `version:` key on Taskfile accepts a semver string, so either `2`, `2.0` or
`2.0.0` is accepted. You you choose to use `2.0` Task will not enable future
`2.1` features, but if you choose to use `2`, than any `2.x.x` features will be
available, but not `3.0.0+`.
## Version 1
In the first version of the `Taskfile`, the `version:` key was not available,
because the tasks was in the root of the YAML document. Like this:
```yml
echo:
cmds:
- echo "Hello, World!"
```
The variable priority order was also different:
1. Call variables
2. Environment
3. Task variables
4. `Taskvars.yml` veriables
## Version 2.0
At version 2, we introduced the `version:` key, to allow us to envolve Task
with new features without breaking existing Taskfiles. The new syntax is as
follows:
```yml
version: '2'
tasks:
echo:
cmds:
- echo "Hello, World!"
```
Version 2 not allows you to write global variables directly in the Taskfile,
if you don't want to create a `Taskvars.yml`:
```yml
version: '2'
vars:
GREETING: Hello, World!
tasks:
greet:
cmds:
- echo "{{.GREETING}}"
```
The variable priority order changed to the following:
1. Task variables
2. Call variables
3. Taskfile variables
4. Taskvars file variables
5. Environment variables
A new global option was added to configure the number of variables expansions
(which default to 2):
```yml
version: '2'
expansions: 3
vars:
FOO: foo
BAR: bar
BAZ: baz
FOOBAR: "{{.FOO}}{{.BAR}}"
FOOBARBAZ: "{{.FOOBAR}}{{.BAZ}}"
tasks:
default:
cmds:
- echo "{{.FOOBARBAZ}}"
```