2017-02-27 16:44:08 -03:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2017-07-16 16:15:29 -03:00
|
|
|
"context"
|
2017-09-16 11:44:13 -03:00
|
|
|
"fmt"
|
2017-02-27 16:44:08 -03:00
|
|
|
|
2017-10-15 17:58:21 -02:00
|
|
|
"github.com/go-task/task/internal/execext"
|
|
|
|
"github.com/go-task/task/internal/status"
|
2018-02-17 14:22:18 -02:00
|
|
|
"github.com/go-task/task/internal/taskfile"
|
2017-02-27 16:44:08 -03:00
|
|
|
)
|
|
|
|
|
2017-12-26 21:43:52 -02:00
|
|
|
// Status returns an error if any the of given tasks is not up-to-date
|
2018-02-17 14:22:18 -02:00
|
|
|
func (e *Executor) Status(calls ...taskfile.Call) error {
|
2017-12-26 21:43:52 -02:00
|
|
|
for _, call := range calls {
|
2017-12-29 18:27:32 -02:00
|
|
|
t, ok := e.Taskfile.Tasks[call.Task]
|
2017-12-26 21:43:52 -02:00
|
|
|
if !ok {
|
|
|
|
return &taskNotFoundError{taskName: call.Task}
|
|
|
|
}
|
2018-02-17 14:22:18 -02:00
|
|
|
isUpToDate, err := isTaskUpToDate(e.Context, t)
|
2017-12-26 21: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 14:22:18 -02:00
|
|
|
func isTaskUpToDate(ctx context.Context, t *taskfile.Task) (bool, error) {
|
2017-07-16 16:15:29 -03:00
|
|
|
if len(t.Status) > 0 {
|
2018-02-17 14:22:18 -02:00
|
|
|
return isTaskUpToDateStatus(ctx, t)
|
2017-07-16 16:15:29 -03:00
|
|
|
}
|
2017-09-16 11:44:13 -03:00
|
|
|
|
2018-02-17 14:22:18 -02:00
|
|
|
checker, err := getStatusChecker(t)
|
2017-09-16 11:44:13 -03:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return checker.IsUpToDate()
|
|
|
|
}
|
|
|
|
|
2018-02-17 14:22:18 -02:00
|
|
|
func statusOnError(t *taskfile.Task) error {
|
|
|
|
checker, err := getStatusChecker(t)
|
2017-09-16 11:44:13 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return checker.OnError()
|
|
|
|
}
|
|
|
|
|
2018-02-17 14:22:18 -02:00
|
|
|
func getStatusChecker(t *taskfile.Task) (status.Checker, error) {
|
2017-09-16 11:44:13 -03: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 16:15:29 -03:00
|
|
|
}
|
|
|
|
|
2018-02-17 14:22:18 -02:00
|
|
|
func isTaskUpToDateStatus(ctx context.Context, t *taskfile.Task) (bool, error) {
|
2017-07-16 16:15:29 -03:00
|
|
|
for _, s := range t.Status {
|
|
|
|
err := execext.RunCommand(&execext.RunCommandOptions{
|
|
|
|
Context: ctx,
|
|
|
|
Command: s,
|
2017-07-30 19:45:01 -03:00
|
|
|
Dir: t.Dir,
|
2018-02-17 14:22:18 -02:00
|
|
|
Env: getEnviron(t),
|
2017-07-16 16:15:29 -03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|