This repository was archived by the owner on Jul 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.go
More file actions
190 lines (163 loc) · 4.07 KB
/
runner.go
File metadata and controls
190 lines (163 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2023 The Happy Authors
package taskrunner
import (
"bufio"
"context"
"os"
"slices"
"strings"
"sync"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/google/uuid"
)
const (
progressTaskSteps float64 = 100.0
)
type Runner struct {
tasks []Task
mu sync.Mutex
model *model
program *tea.Program
capturedLines []string
captureMu sync.Mutex
failedTasks []TaskID
ctx context.Context
}
func New() *Runner {
// Initialize model
s := spinner.New(
spinner.WithSpinner(spinner.Points),
)
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#ffed56"))
p := progress.New(
progress.WithScaledGradient("#ff9800", "#4caf50"),
progress.WithWidth(50),
progress.WithFillCharacters('▰', '▱'),
progress.WithSpringOptions(120.0, 1.0),
progress.WithoutPercentage(),
)
return &Runner{
tasks: make([]Task, 0),
capturedLines: make([]string, 0),
model: &model{
spinner: s,
progress: p,
},
}
}
// AddTask adds a task to runner.
func (tr *Runner) AddTask(task Task) TaskID {
tr.mu.Lock()
defer tr.mu.Unlock()
tr.tasks = append(tr.tasks, task)
tr.model.totalTasks++
if len(task.name) > tr.model.longestTaskNameLength {
tr.model.longestTaskNameLength = len(task.name)
}
return task.id
}
// Add adds a task without any dependencies.
func (tr *Runner) Add(name string, action Action) TaskID {
task := NewTask(name, action)
tr.AddTask(task)
return task.id
}
// AddD adds a task with a dependency on other task.
func (tr *Runner) AddD(dep TaskID, name string, action Action) TaskID {
task := NewTask(name, action).DependsOn(dep)
tr.AddTask(task)
return task.id
}
func (tr *Runner) Run() error {
ctx, done := context.WithCancel(context.Background())
tr.ctx = ctx
defer done()
// Create pipe to capture stdout
pr, pw, err := os.Pipe()
if err != nil {
return err
}
original := os.Stdout
// Start bubbletea program
tr.model.progressTotalSteps = float64(tr.model.totalTasks) * progressTaskSteps
tr.program = tea.NewProgram(tr.model, tea.WithFPS(120))
// Start output capture goroutine
os.Stdout = pw
defer func() {
// Restore stdout and clean up
os.Stdout = original
pw.Close()
pr.Close()
}()
go tr.captureOutput(pr)
// Start task execution goroutine
go tr.executeTasks(original)
// Run the program
_, err = tr.program.Run()
return err
}
func (tr *Runner) captureOutput(pr *os.File) {
scanner := bufio.NewScanner(pr)
for {
select {
case <-tr.ctx.Done():
return
default:
if !scanner.Scan() {
return
}
output := strings.TrimSpace(scanner.Text())
if output != "" {
for line := range strings.SplitSeq(output, "\n") {
if strings.TrimSpace(line) != "" {
tr.program.Send(OutputMsg(strings.TrimSpace(line)))
}
}
}
}
}
}
func (tr *Runner) executeTasks(out *os.File) {
for _, task := range tr.tasks {
e := newExecutor(task.name, tr.program, out)
if task.id == uuid.Nil {
res := Failure("invalid task id")
res.name = task.name
tr.program.Send(res)
continue
}
tr.program.Send(SetStatusMsg(task.name))
if task.dependsOn != uuid.Nil && slices.Contains(tr.failedTasks, task.dependsOn) {
res := Skip("skip").WithDesc("dependency not satisified")
res.id = task.id
res.name = task.name
tr.failedTasks = append(tr.failedTasks, task.id)
tr.program.Send(res)
continue
}
res := task.action(e)
res.id = task.id
res.name = task.name
if res.state == FAILURE || res.state == SKIPPED {
tr.failedTasks = append(tr.failedTasks, task.id)
tr.program.Send(res)
continue
}
sstate, failedTasks := e.runSubtasks(tr.failedTasks)
tr.failedTasks = append(tr.failedTasks, failedTasks...)
if sstate == FAILURE || sstate == SKIPPED {
tr.failedTasks = append(tr.failedTasks, task.id)
}
if sstate != SUCCESS {
res.state = sstate
}
tr.program.Send(res)
}
// Send all tasks complete message
tr.program.Send(allTasksCompleteMsg{})
}