2017-02-27 21:44:08 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2017-07-16 21:15:29 +02:00
|
|
|
"context"
|
2017-09-16 16:44:13 +02:00
|
|
|
"fmt"
|
2017-02-27 21:44:08 +02:00
|
|
|
|
2018-11-05 01:23:35 +02:00
|
|
|
"github.com/go-task/task/v2/internal/execext"
|
|
|
|
"github.com/go-task/task/v2/internal/status"
|
|
|
|
"github.com/go-task/task/v2/internal/taskfile"
|
2017-02-27 21:44:08 +02:00
|
|
|
)
|
|
|
|
|
2017-12-27 01:43:52 +02:00
|
|
|
// Status returns an error if any the of given tasks is not up-to-date
|
2018-02-17 18:22:18 +02:00
|
|
|
func (e *Executor) Status(calls ...taskfile.Call) error {
|
2017-12-27 01:43:52 +02:00
|
|
|
for _, call := range calls {
|
2018-10-06 22:55:23 +02:00
|
|
|
t, err := e.CompiledTask(call)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-12-27 01:43:52 +02:00
|
|
|
}
|
2018-02-17 18:22:18 +02:00
|
|
|
isUpToDate, err := isTaskUpToDate(e.Context, t)
|
2017-12-27 01:43:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !isUpToDate {
|
|
|
|
return fmt.Errorf(`task: Task "%s" is not up-to-date`, t.Task)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool, error) {
|
2017-07-16 21:15:29 +02:00
|
|
|
if len(t.Status) > 0 {
|
2018-02-17 18:22:18 +02:00
|
|
|
return isTaskUpToDateStatus(ctx, t)
|
2017-07-16 21:15:29 +02:00
|
|
|
}
|
2017-09-16 16:44:13 +02:00
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
checker, err := getStatusChecker(t)
|
2017-09-16 16:44:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return checker.IsUpToDate()
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func statusOnError(t *taskfile.Task) error {
|
|
|
|
checker, err := getStatusChecker(t)
|
2017-09-16 16:44:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return checker.OnError()
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func getStatusChecker(t *taskfile.Task) (status.Checker, error) {
|
2017-09-16 16:44:13 +02:00
|
|
|
switch t.Method {
|
|
|
|
case "", "timestamp":
|
|
|
|
return &status.Timestamp{
|
|
|
|
Dir: t.Dir,
|
|
|
|
Sources: t.Sources,
|
|
|
|
Generates: t.Generates,
|
|
|
|
}, nil
|
|
|
|
case "checksum":
|
|
|
|
return &status.Checksum{
|
|
|
|
Dir: t.Dir,
|
|
|
|
Task: t.Task,
|
|
|
|
Sources: t.Sources,
|
|
|
|
}, nil
|
|
|
|
case "none":
|
|
|
|
return status.None{}, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(`task: invalid method "%s"`, t.Method)
|
|
|
|
}
|
2017-07-16 21:15:29 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func isTaskUpToDateStatus(ctx context.Context, t *taskfile.Task) (bool, error) {
|
2017-07-16 21:15:29 +02:00
|
|
|
for _, s := range t.Status {
|
2018-09-01 16:02:23 +02:00
|
|
|
err := execext.RunCommand(ctx, &execext.RunCommandOptions{
|
2017-07-16 21:15:29 +02:00
|
|
|
Command: s,
|
2017-07-31 00:45:01 +02:00
|
|
|
Dir: t.Dir,
|
2018-02-17 18:22:18 +02:00
|
|
|
Env: getEnviron(t),
|
2017-07-16 21:15:29 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|