1
0
mirror of https://github.com/go-task/task.git synced 2024-12-04 10:24:45 +02:00

Always expode .TIMESTAMP and .STATUS when using status:

This commit is contained in:
Andrey Nering 2019-09-14 18:04:41 -03:00
parent 1a28e5e0d4
commit 14d7f04a81
4 changed files with 48 additions and 66 deletions

View File

@ -56,19 +56,9 @@ func (e *Executor) getStatusChecker(t *taskfile.Task) (status.Checker, error) {
}
switch method {
case "timestamp":
return &status.Timestamp{
Dir: t.Dir,
Sources: t.Sources,
Generates: t.Generates,
}, nil
return e.timestampChecker(t), nil
case "checksum":
return &status.Checksum{
Dir: t.Dir,
Task: t.Task,
Sources: t.Sources,
Generates: t.Generates,
Dry: e.Dry,
}, nil
return e.checksumChecker(t), nil
case "none":
return status.None{}, nil
default:
@ -76,6 +66,24 @@ func (e *Executor) getStatusChecker(t *taskfile.Task) (status.Checker, error) {
}
}
func (e *Executor) timestampChecker(t *taskfile.Task) status.Checker {
return &status.Timestamp{
Dir: t.Dir,
Sources: t.Sources,
Generates: t.Generates,
}
}
func (e *Executor) checksumChecker(t *taskfile.Task) status.Checker {
return &status.Checksum{
Dir: t.Dir,
Task: t.Task,
Sources: t.Sources,
Generates: t.Generates,
Dry: e.Dry,
}
}
func (e *Executor) isTaskUpToDateStatus(ctx context.Context, t *taskfile.Task) (bool, error) {
for _, s := range t.Status {
err := execext.RunCommand(ctx, &execext.RunCommandOptions{

View File

@ -370,48 +370,31 @@ func TestStatusChecksum(t *testing.T) {
}
func TestStatusVariables(t *testing.T) {
t.Run("Checksum", func(t *testing.T) {
const dir = "testdata/status_vars"
const dir = "testdata/status_vars"
_ = os.RemoveAll(filepath.Join(dir, ".task"))
_ = os.Remove(filepath.Join(dir, "generated.txt"))
_ = os.RemoveAll(filepath.Join(dir, ".task"))
_ = os.Remove(filepath.Join(dir, "generated.txt"))
var buff bytes.Buffer
e := task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
Silent: false,
Verbose: true,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build-with-checksum"}))
assert.Contains(t, buff.String(), "d41d8cd98f00b204e9800998ecf8427e")
})
var buff bytes.Buffer
e := task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
Silent: false,
Verbose: true,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build"}))
t.Run("Timestamp", func(t *testing.T) {
const dir = "testdata/status_vars"
assert.Contains(t, buff.String(), "d41d8cd98f00b204e9800998ecf8427e")
_ = os.Remove(filepath.Join(dir, "generated.txt"))
inf, err := os.Stat(filepath.Join(dir, "source.txt"))
assert.NoError(t, err)
ts := fmt.Sprintf("%d", inf.ModTime().Unix())
tf := fmt.Sprintf("%s", inf.ModTime())
var buff bytes.Buffer
e := task.Executor{
Dir: dir,
Stdout: &buff,
Stderr: &buff,
Silent: false,
Verbose: true,
}
assert.NoError(t, e.Setup())
assert.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "build-with-timestamp"}))
inf, err := os.Stat(filepath.Join(dir, "source.txt"))
assert.NoError(t, err)
ts := fmt.Sprintf("%d", inf.ModTime().Unix())
tf := fmt.Sprintf("%s", inf.ModTime())
assert.Contains(t, buff.String(), ts)
assert.Contains(t, buff.String(), tf)
})
assert.Contains(t, buff.String(), ts)
assert.Contains(t, buff.String(), tf)
}
func TestInit(t *testing.T) {

View File

@ -1,17 +1,10 @@
version: '3'
tasks:
build-with-checksum:
build:
sources:
- ./source.txt
method: checksum
status:
- echo "{{.CHECKSUM}}"
build-with-timestamp:
sources:
- ./source.txt
method: timestamp
status:
- echo '{{.TIMESTAMP.Unix}}'
- echo '{{.TIMESTAMP}}'

View File

@ -5,6 +5,7 @@ import (
"strings"
"github.com/go-task/task/v2/internal/execext"
"github.com/go-task/task/v2/internal/status"
"github.com/go-task/task/v2/internal/taskfile"
"github.com/go-task/task/v2/internal/templater"
)
@ -96,17 +97,14 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
}
if len(origTask.Status) > 0 {
checker, err := e.getStatusChecker(&new)
if err != nil {
return nil, err
for _, checker := range []status.Checker{e.timestampChecker(&new), e.checksumChecker(&new)} {
value, err := checker.Value()
if err != nil {
return nil, err
}
vars[strings.ToUpper(checker.Kind())] = taskfile.Var{Live: value}
}
value, err := checker.Value()
if err != nil {
return nil, err
}
vars[strings.ToUpper(checker.Kind())] = taskfile.Var{Live: value}
// Adding new variables, requires us to refresh the templaters
// cache of the the values manually
r.ResetCache()