@@ -43,10 +43,13 @@ type Task struct {
4343}
4444
4545type Executor struct {
46- Stdout io.Writer
47- Stdin io.Reader
48- Stderr io.Writer
49- Config * Config
46+ Stdout io.Writer
47+ Stdin io.Reader
48+ Stderr io.Writer
49+ Config * Config
50+ Prefix bool
51+ colorIndex int
52+ taskColors map [string ]string
5053}
5154
5255// sets the top-level env
@@ -90,6 +93,26 @@ func (t *Task) CompileEnv(env []string) ([]string, error) {
9093 return env , nil
9194}
9295
96+ func (exec * Executor ) colorForTask (task string ) string {
97+ if exec .taskColors == nil {
98+ exec .taskColors = make (map [string ]string )
99+ }
100+ if color , ok := exec .taskColors [task ]; ok {
101+ return color
102+ }
103+ color := prefixColors [exec .colorIndex % len (prefixColors )]
104+ exec .colorIndex ++
105+ exec .taskColors [task ] = color
106+ return color
107+ }
108+
109+ func (exec * Executor ) prefixedWriter (w io.Writer , prefix string , task string ) io.Writer {
110+ if exec .Prefix {
111+ return newPrefixWriter (w , prefix , exec .colorForTask (task ))
112+ }
113+ return w
114+ }
115+
93116func (exec * Executor ) RunTasks (config * Config , tasks * []string ) error {
94117 // top-level env
95118 env , err := config .CompileEnv ()
@@ -133,10 +156,18 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
133156 return err
134157 }
135158
159+ taskExec := & Executor {
160+ Stdout : exec .prefixedWriter (exec .Stdout , "::" + task + ":: " , task ),
161+ Stderr : exec .prefixedWriter (exec .Stderr , "::" + task + ":: " , task ),
162+ Stdin : exec .Stdin ,
163+ Config : exec .Config ,
164+ Prefix : exec .Prefix ,
165+ }
166+
136167 // if a task contains cmds, run them
137168 if len (taskConfig .Cmds ) > 0 {
138169 for _ , cmd := range taskConfig .Cmds {
139- err := exec .runCommand (cmd , taskConfig .Dir , env )
170+ err := taskExec .runCommand (cmd , taskConfig .Dir , env )
140171 if err != nil {
141172 fmt .Println (err .Error ())
142173 // if the cmd exited with an error, bail immediately
@@ -146,7 +177,7 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
146177 } else {
147178 // if there are no cmds then we intend to run a script with the name name as the task
148179 script := fmt .Sprintf ("%s/%s" , exec .Config .ScriptDir , task )
149- err := exec .runCommand (script , taskConfig .Dir , env )
180+ err := taskExec .runCommand (script , taskConfig .Dir , env )
150181 if err != nil {
151182 fmt .Println (err .Error ())
152183 // if the cmd exited with an error, bail immediately
0 commit comments