From d48a2f3ccfe779b7631798d0d2e18e6e94ea4296 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Wed, 7 Mar 2018 22:54:37 -0300 Subject: [PATCH] Documentation changes for v2.0.0 release --- README.md | 473 +++++++++++++++++++++++++------------------ TASKFILE_VERSIONS.md | 91 +++++++++ 2 files changed, 366 insertions(+), 198 deletions(-) create mode 100644 TASKFILE_VERSIONS.md diff --git a/README.md b/README.md index 14b3260c..087a5ef1 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ # 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 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]. @@ -73,13 +77,16 @@ The example below allows compile a Go app and uses [Minify][minify] to concat and minify multiple CSS files into a single one. ```yml -build: - cmds: - - go build -v -i main.go +version: '2' -assets: - cmds: - - minify -o public/style.css src/css +tasks: + build: + cmds: + - go build -v -i main.go + + assets: + cmds: + - minify -o public/style.css src/css ``` Running the tasks is as simple as running: @@ -100,11 +107,14 @@ If you ommit a task name, "default" will be assumed. You can specify environment variables that are added when running a command: ```yml -build: - cmds: - - echo $hallo - env: - hallo: welt +version: '2' + +tasks: + build: + cmds: + - echo $hallo + env: + hallo: welt ``` ### OS specific task @@ -117,17 +127,21 @@ Example: Taskfile.yml: ```yml -build: - cmds: - - echo "default" +version: '2' + +tasks: + build: + cmds: + - echo "default" ``` Taskfile_linux.yml: ```yml -build: - cmds: - - echo "linux" +tasks: + build: + cmds: + - echo "linux" ``` Will print out `linux` and not default. @@ -143,11 +157,14 @@ located. But you can easily make the task run in another folder informing `dir`: ```yml -serve: - dir: public/www - cmds: - # run http server - - caddy +version: '2' + +tasks: + serve: + dir: public/www + cmds: + # run http server + - caddy ``` ### Task dependencies @@ -156,14 +173,17 @@ You may have tasks that depends on others. Just pointing them on `deps` will make them run automatically before running the parent task: ```yml -build: - deps: [assets] - cmds: - - go build -v -i main.go +version: '2' -assets: - cmds: - - minify -o public/style.css src/css +tasks: + build: + deps: [assets] + cmds: + - go build -v -i main.go + + assets: + cmds: + - minify -o public/style.css src/css ``` In the above example, `assets` will always run right before `build` if you run @@ -172,39 +192,45 @@ 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: ```yml -assets: - deps: [js, css] +version: '2' -js: - cmds: - - minify -o public/script.js src/js +tasks: + assets: + deps: [js, css] -css: - cmds: - - minify -o public/style.css src/css + js: + cmds: + - minify -o public/script.js src/js + + css: + cmds: + - minify -o public/style.css src/css ``` If there are more than one dependency, they always run in parallel for better 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 -default: - deps: - - task: echo_sth - vars: {TEXT: "before 1"} - - task: echo_sth - vars: {TEXT: "before 2"} - cmds: - - echo "after" +version: '2' -echo_sth: - cmds: - - echo {{.TEXT}} +tasks: + default: + deps: + - task: echo_sth + vars: {TEXT: "before 1"} + - task: echo_sth + vars: {TEXT: "before 2"} + cmds: + - echo "after" + + echo_sth: + cmds: + - echo {{.TEXT}} ``` - ### Calling another task When a task has many dependencies, they are executed concurrently. This will @@ -212,78 +238,74 @@ 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: ```yml -main-task: - cmds: - - task: task-to-be-called - - task: another-task - - echo "Both done" +version: '2' -task-to-be-called: - cmds: - - echo "Task to be called" +tasks: + main-task: + cmds: + - task: task-to-be-called + - task: another-task + - echo "Both done" -another-task: - cmds: - - echo "Another task" + task-to-be-called: + cmds: + - echo "Task to be called" + + another-task: + cmds: + - echo "Another task" ``` Overriding variables in the called task is as simple as informing `vars` attribute: ```yml -main-task: - cmds: - - task: write-file - vars: {FILE: "hello.txt", CONTENT: "Hello!"} - - task: write-file - vars: {FILE: "world.txt", CONTENT: "World!"} +version: '2' -write-file: - cmds: - - echo "{{.CONTENT}}" > {{.FILE}} +tasks: + main-task: + cmds: + - task: write-file + vars: {FILE: "hello.txt", CONTENT: "Hello!"} + - task: write-file + vars: {FILE: "world.txt", CONTENT: "World!"} + + write-file: + cmds: + - echo "{{.CONTENT}}" > {{.FILE}} ``` 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 If a task generates something, you can inform Task the source and generated files, so Task will prevent to run them if not necessary. ```yml -build: - deps: [js, css] - cmds: - - go build -v -i main.go +version: '2' -js: - cmds: - - minify -o public/script.js src/js - sources: - - src/js/**/*.js - generates: - - public/script.js +tasks: + build: + deps: [js, css] + cmds: + - go build -v -i main.go -css: - cmds: - - minify -o public/style.css src/css - sources: - - src/css/**/*.css - generates: - - public/style.css + js: + cmds: + - minify -o public/script.js src/js + sources: + - src/js/**/*.js + generates: + - public/script.js + + css: + cmds: + - minify -o public/style.css src/css + sources: + - src/css/**/*.css + generates: + - public/style.css ``` `sources` and `generates` can be files or file patterns. When both are given, @@ -298,14 +320,17 @@ 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. ```yml -build: - cmds: - - go build . - sources: - - ./*.go - generates: - - app{{exeExt}} - method: checksum +version: '2' + +tasks: + build: + cmds: + - go build . + sources: + - ./*.go + generates: + - app{{exeExt}} + method: checksum ``` > TIP: method `none` skips any validation and always run the task. @@ -314,16 +339,19 @@ 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: ```yml -generate-files: - cmds: - - mkdir directory - - touch directory/file1.txt - - touch directory/file2.txt - # test existence of files - status: - - test -d directory - - test -f directory/file1.txt - - test -f directory/file2.txt +version: '2' + +tasks: + generate-files: + cmds: + - mkdir directory + - touch directory/file1.txt + - touch directory/file2.txt + # test existence of files + status: + - test -d directory + - test -f directory/file1.txt + - test -f directory/file2.txt ``` You can use `--force` or `-f` if you want to force a task to run even when @@ -337,13 +365,14 @@ the tasks is not up-to-date. When doing interpolation of variables, Task will look for the below. 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. (See [Calling another task](#calling-another-task) above) -- Environment variables -- Variables declared locally in the task +- Variables declared in the `vars:` option in the `Taskfile` - Variables available in the `Taskvars.yml` file +- Environment variables -Example of overriding with environment variables: +Example of sending parameters with environment variables: ```bash $ TASK_VARIABLE=a-value task do-something @@ -361,11 +390,28 @@ $ task write-file FILE=file.txt "CONTENT=Hello, World!" print "MESSAGE=All done! Example of locally declared vars: ```yml -print-var: - cmds: - echo "{{.VAR}}" - vars: - VAR: Hello! +version: '2' + +tasks: + print-var: + cmds: + echo "{{.VAR}}" + vars: + 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: @@ -376,6 +422,30 @@ DEV_MODE: production 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 The below syntax (`sh:` prop in a variable) is considered a dynamic variable. @@ -383,30 +453,19 @@ 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. ```yml -build: - cmds: - - go build -ldflags="-X main.Version={{.GIT_COMMIT}}" main.go - vars: - GIT_COMMIT: - sh: git log -n 1 --format=%h +version: '2' + +tasks: + build: + cmds: + - go build -ldflags="-X main.Version={{.GIT_COMMIT}}" main.go + vars: + GIT_COMMIT: + sh: git log -n 1 --format=%h ``` 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 Task parse commands as [Go's template engine][gotemplate] before executing @@ -416,9 +475,12 @@ 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: ```yml -print-date: - cmds: - - echo {{now | date "2006-01-02"}} +version: '2' + +tasks: + print-date: + cmds: + - echo {{now | date "2006-01-02"}} ``` Task also adds the following functions: @@ -439,27 +501,27 @@ Task also adds the following functions: Example: ```yml -print-os: - cmds: - - echo '{{OS}} {{ARCH}}' - - 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 - - echo '{{fromSlash "path/to/file"}}' -enumerated-file: - vars: - CONTENT: | - foo - bar - cmds: - - | - cat << EOF > output.txt - {{range $i, $line := .CONTENT | splitLines -}} - {{printf "%3d" $i}}: {{$line}} - {{end}}EOF -``` +version: '2' -> NOTE: There are some deprecated function names still available: `ToSlash`, -`FromSlash` and `ExeExt`. These where changed for consistency with sprig lib. +tasks: + print-os: + cmds: + - echo '{{OS}} {{ARCH}}' + - 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 + - echo '{{fromSlash "path/to/file"}}' + enumerated-file: + vars: + CONTENT: | + foo + bar + cmds: + - | + cat << EOF > output.txt + {{range $i, $line := .CONTENT | splitLines -}} + {{printf "%3d" $i}}: {{$line}} + {{end}}EOF +``` ### Help @@ -467,23 +529,26 @@ Running `task --list` (or `task -l`) lists all tasks with a description. The following taskfile: ```yml -build: - desc: Build the go binary. - cmds: - - go build -v -i main.go +version: '2' -test: - desc: Run all the go tests. - cmds: - - go test -race ./... +tasks: + build: + desc: Build the go binary. + cmds: + - go build -v -i main.go -js: - cmds: - - minify -o public/script.js src/js + test: + desc: Run all the go tests. + cmds: + - go test -race ./... -css: - cmds: - - minify -o public/style.css src/css + js: + cmds: + - minify -o public/script.js src/js + + css: + cmds: + - minify -o public/style.css src/css ``` would print the following output: @@ -499,9 +564,12 @@ Silent mode disables echoing of commands before Task runs it. For the following Taskfile: ```yml -echo: - cmds: - - echo "Print something" +version: '2' + +tasks: + echo: + cmds: + - echo "Print something" ``` Normally this will be print: @@ -522,19 +590,25 @@ There's three ways to enable silent mode: * At command level: ```yml -echo: - cmds: - - cmd: echo "Print something" - silent: true +version: '2' + +tasks: + echo: + cmds: + - cmd: echo "Print something" + silent: true ``` * At task level: ```yml -echo: - cmds: - - echo "Print something" - silent: true +version: '2' + +tasks: + echo: + cmds: + - echo "Print something" + silent: true ``` * Or globally with `--silent` or `-s` flag @@ -542,9 +616,12 @@ echo: If you want to suppress stdout instead, just redirect a command to `/dev/null`: ```yml -echo: - cmds: - - echo "This will print nothing" > /dev/null +version: '2' + +tasks: + echo: + cmds: + - echo "This will print nothing" > /dev/null ``` ## Watch tasks (experimental) diff --git a/TASKFILE_VERSIONS.md b/TASKFILE_VERSIONS.md new file mode 100644 index 00000000..abea1bc8 --- /dev/null +++ b/TASKFILE_VERSIONS.md @@ -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}}" +```