mirror of
https://github.com/go-task/task.git
synced 2025-07-17 01:43:07 +02:00
Add CHANGELOG, documentation and small improvements to #626
This commit is contained in:
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- It's now possible to schedule cleanup commands to run once a task finishes
|
||||||
|
with the `defer:` keyword
|
||||||
|
([Documentation](https://taskfile.dev/#/usage?id=doing-task-cleanup-with-defer), [#475](https://github.com/go-task/task/issues/475), [#626](https://github.com/go-task/task/pull/626/files)).
|
||||||
- Remove long deprecated and undocumented `$` variable prefix and `^` command
|
- Remove long deprecated and undocumented `$` variable prefix and `^` command
|
||||||
prefix
|
prefix
|
||||||
([#642](https://github.com/go-task/task/issues/642), [#644](https://github.com/go-task/task/issues/644), [#645](https://github.com/go-task/task/pull/645)).
|
([#642](https://github.com/go-task/task/issues/642), [#644](https://github.com/go-task/task/issues/644), [#645](https://github.com/go-task/task/pull/645)).
|
||||||
|
@ -608,6 +608,45 @@ tasks:
|
|||||||
- yarn {{.CLI_ARGS}}
|
- yarn {{.CLI_ARGS}}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Doing task cleanup with `defer`
|
||||||
|
|
||||||
|
With the `defer` keyword, it's possible to schedule cleanup to be run once
|
||||||
|
the task finishes. The difference with just putting it as the last command is
|
||||||
|
that this command will run even when the task fails.
|
||||||
|
|
||||||
|
In the example below `rm -rf tmpdir/` will run even if the third command fails:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
default:
|
||||||
|
cmds:
|
||||||
|
- mkdir -p tmpdir/
|
||||||
|
- defer: rm -rf tmpdir/
|
||||||
|
- echo 'Do work on tmpdir/'
|
||||||
|
```
|
||||||
|
|
||||||
|
If you want to move the cleanup command into another task, that's possible as
|
||||||
|
well:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
default:
|
||||||
|
cmds:
|
||||||
|
- mkdir -p tmpdir/
|
||||||
|
- defer: { task: cleanup }
|
||||||
|
- echo 'Do work on tmpdir/'
|
||||||
|
|
||||||
|
cleanup: rm -rf tmpdir/
|
||||||
|
```
|
||||||
|
|
||||||
|
> NOTE: Due to the nature of how the
|
||||||
|
[Go's own `defer` work](https://go.dev/tour/flowcontrol/13), the deferred
|
||||||
|
commands are executed in the reverse order if you schedule multiple of them.
|
||||||
|
|
||||||
## 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
|
||||||
|
4
task.go
4
task.go
@ -8,7 +8,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-task/task/v3/internal/compiler"
|
"github.com/go-task/task/v3/internal/compiler"
|
||||||
compilerv2 "github.com/go-task/task/v3/internal/compiler/v2"
|
compilerv2 "github.com/go-task/task/v3/internal/compiler/v2"
|
||||||
@ -402,8 +401,9 @@ func (e *Executor) runDeps(ctx context.Context, t *taskfile.Task) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Executor) runDeferred(t *taskfile.Task, call taskfile.Call, i int) {
|
func (e *Executor) runDeferred(t *taskfile.Task, call taskfile.Call, i int) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err := e.runCommand(ctx, t, call, i); err != nil {
|
if err := e.runCommand(ctx, t, call, i); err != nil {
|
||||||
e.Logger.VerboseErrf(logger.Yellow, `task: ignored error in deferred cmd: %s`, err.Error())
|
e.Logger.VerboseErrf(logger.Yellow, `task: ignored error in deferred cmd: %s`, err.Error())
|
||||||
}
|
}
|
||||||
|
@ -43,10 +43,7 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var deferredCall struct {
|
var deferredCall struct {
|
||||||
Defer struct {
|
Defer Call
|
||||||
Task string
|
|
||||||
Vars *Vars
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if err := unmarshal(&deferredCall); err == nil && deferredCall.Defer.Task != "" {
|
if err := unmarshal(&deferredCall); err == nil && deferredCall.Defer.Task != "" {
|
||||||
c.Defer = true
|
c.Defer = true
|
||||||
|
Reference in New Issue
Block a user