Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/tsk/tsk.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Options struct {
pure bool
taskFile string
tasks []string
time bool
which bool
}

Expand All @@ -50,6 +51,7 @@ func main() {
flag.BoolVar(&opts.prefix, "prefix", false, "prefix task output with the task name")
flag.BoolVarP(&opts.pure, "pure", "", false, "don't inherit the parent env")
flag.StringVarP(&opts.taskFile, "file", "f", "", "taskfile to use")
flag.BoolVar(&opts.time, "time", false, "show task duration after completion")
flag.BoolVar(&opts.which, "which", false, "print the path to the found tasks.toml, or an error")
flag.BoolVarP(&help, "help", "h", false, "")
flag.Parse()
Expand Down Expand Up @@ -94,6 +96,7 @@ func main() {
Stderr: os.Stderr,
Config: cfg,
Prefix: opts.prefix,
Time: opts.time,
}

if opts.listTasks {
Expand Down
21 changes: 21 additions & 0 deletions internal/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"regexp"
"strings"
"sync"
"time"

output "github.com/notnmeyer/tsk/internal/outputformat"

Expand Down Expand Up @@ -48,6 +49,7 @@ type Executor struct {
Stderr io.Writer
Config *Config
Prefix bool
Time bool
colorIndex int
taskColors map[string]string
}
Expand Down Expand Up @@ -131,6 +133,8 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
taskConfig.Dir = config.TaskFileDir
}

taskStart := time.Now()

if len(taskConfig.Deps) > 0 {
for _, depGroup := range taskConfig.Deps {
var wg sync.WaitGroup
Expand Down Expand Up @@ -162,6 +166,7 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
Stdin: exec.Stdin,
Config: exec.Config,
Prefix: exec.Prefix,
Time: exec.Time,
}

// if a task contains cmds, run them
Expand All @@ -184,6 +189,10 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
os.Exit(1)
}
}

if exec.Time && exec.Stderr != nil {
fmt.Fprintf(exec.Stderr, "::%s:: duration %s\n", task, formatDuration(time.Since(taskStart)))
}
}
return nil
}
Expand Down Expand Up @@ -314,6 +323,18 @@ func (exec *Executor) VerifyTasks(tasks []string) error {
return nil
}

func formatDuration(d time.Duration) string {
if d < time.Second {
return fmt.Sprintf("%dms", d.Milliseconds())
}
if d < time.Minute {
return fmt.Sprintf("%.2fs", d.Seconds())
}
m := int(d.Minutes())
s := d.Seconds() - float64(m*60)
return fmt.Sprintf("%dm%.2fs", m, s)
}

func filterTasks(tasks *map[string]Task, regex *regexp.Regexp) map[string]Task {
filtered := make(map[string]Task)
for k, v := range *tasks {
Expand Down
Loading