mirror of
https://github.com/go-task/task.git
synced 2025-07-17 01:43:07 +02:00
Possibility to call another task
This commit is contained in:
22
README.md
22
README.md
@ -141,6 +141,28 @@ task2:
|
|||||||
|
|
||||||
Will stop at the moment the dependencies of `task2` are executed.
|
Will stop at the moment the dependencies of `task2` are executed.
|
||||||
|
|
||||||
|
### Calling a task from another task
|
||||||
|
|
||||||
|
When a task has many dependencies, they are executed concurrently. This will
|
||||||
|
often result in a faster build pipeline. But in some situations you may need
|
||||||
|
to call other tasks serially. For this just prefix a command with `^`:
|
||||||
|
|
||||||
|
```yml
|
||||||
|
a-task:
|
||||||
|
cmds:
|
||||||
|
- ^another-task
|
||||||
|
- ^even-another-task
|
||||||
|
- echo "Both done"
|
||||||
|
|
||||||
|
another-task:
|
||||||
|
cmds:
|
||||||
|
- ...
|
||||||
|
|
||||||
|
even-another-task:
|
||||||
|
cmds:
|
||||||
|
- ...
|
||||||
|
```
|
||||||
|
|
||||||
### Prevent task from running when not necessary
|
### Prevent task from running when not necessary
|
||||||
|
|
||||||
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
|
||||||
|
22
task.go
22
task.go
@ -74,13 +74,17 @@ func Run() {
|
|||||||
|
|
||||||
// RunTask runs a task by its name
|
// RunTask runs a task by its name
|
||||||
func RunTask(name string) error {
|
func RunTask(name string) error {
|
||||||
mu.Lock()
|
if strings.HasPrefix(name, "^") {
|
||||||
if _, found := runnedTasks[name]; found {
|
name = strings.TrimPrefix(name, "^")
|
||||||
|
} else {
|
||||||
|
mu.Lock()
|
||||||
|
if _, found := runnedTasks[name]; found {
|
||||||
|
mu.Unlock()
|
||||||
|
return &cyclicDepError{name}
|
||||||
|
}
|
||||||
|
runnedTasks[name] = struct{}{}
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
return &cyclicDepError{name}
|
|
||||||
}
|
}
|
||||||
runnedTasks[name] = struct{}{}
|
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
t, ok := Tasks[name]
|
t, ok := Tasks[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -174,6 +178,14 @@ func (t *Task) runCommand(i int) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(c, "^") {
|
||||||
|
if err = RunTask(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
dir, err := ReplaceVariables(t.Dir, vars)
|
dir, err := ReplaceVariables(t.Dir, vars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
27
task_test.go
27
task_test.go
@ -85,3 +85,30 @@ func TestVars(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTaskCall(t *testing.T) {
|
||||||
|
const dir = "testdata/task_call"
|
||||||
|
|
||||||
|
files := []string{
|
||||||
|
"foo.txt",
|
||||||
|
"bar.txt",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
_ = os.Remove(filepath.Join(dir, f))
|
||||||
|
}
|
||||||
|
|
||||||
|
c := exec.Command("task")
|
||||||
|
c.Dir = dir
|
||||||
|
|
||||||
|
if err := c.Run(); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
1
testdata/task_call/.gitignore
vendored
Normal file
1
testdata/task_call/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.txt
|
20
testdata/task_call/Taskfile.yml
vendored
Normal file
20
testdata/task_call/Taskfile.yml
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
default:
|
||||||
|
cmds:
|
||||||
|
- ^set-foo
|
||||||
|
- ^print
|
||||||
|
- ^set-bar
|
||||||
|
- ^print
|
||||||
|
|
||||||
|
print:
|
||||||
|
cmds:
|
||||||
|
- echo text > {{.FILE}}
|
||||||
|
|
||||||
|
set-foo:
|
||||||
|
set: FILE
|
||||||
|
cmds:
|
||||||
|
- echo foo.txt
|
||||||
|
|
||||||
|
set-bar:
|
||||||
|
set: FILE
|
||||||
|
cmds:
|
||||||
|
- echo bar.txt
|
Reference in New Issue
Block a user