2020-02-12 22:54:25 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-05 17:36:34 +00:00
|
|
|
"os"
|
2020-02-12 22:54:25 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2020-02-12 23:28:27 +00:00
|
|
|
"strings"
|
2020-02-12 22:54:25 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Task struct {
|
2021-04-22 11:12:10 +00:00
|
|
|
Name string `yaml:"task"`
|
|
|
|
Watch []string `yaml:"watch"`
|
2020-02-12 22:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Group struct {
|
2024-06-05 17:36:34 +00:00
|
|
|
Name string `yaml:"group"`
|
|
|
|
Tasks []Task `yaml:"tasks"`
|
2020-02-12 22:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Deadlines []Group
|
|
|
|
)
|
|
|
|
|
2021-04-22 11:12:10 +00:00
|
|
|
func (d Deadlines) Tasks() []*Task {
|
|
|
|
var tasks []*Task
|
|
|
|
for _, g := range d {
|
|
|
|
for i := range g.Tasks {
|
|
|
|
tasks = append(tasks, &g.Tasks[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tasks
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:54:25 +00:00
|
|
|
func (d Deadlines) FindTask(name string) (*Group, *Task) {
|
|
|
|
for _, g := range d {
|
|
|
|
for _, t := range g.Tasks {
|
|
|
|
if t.Name == name {
|
|
|
|
return &g, &t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadDeadlines(filename string) (Deadlines, error) {
|
2024-06-05 17:36:34 +00:00
|
|
|
b, err := os.ReadFile(filename)
|
2020-02-12 22:54:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
var m struct {
|
|
|
|
Deadlines struct {
|
|
|
|
Schedule Deadlines `yaml:"schedule"`
|
|
|
|
} `yaml:"deadlines"`
|
2020-02-12 22:54:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
if err := yaml.Unmarshal(b, &m); err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading deadlines: %w", err)
|
2020-02-12 22:54:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 17:36:34 +00:00
|
|
|
d := m.Deadlines.Schedule
|
2020-02-12 22:54:25 +00:00
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findChangedTasks(d Deadlines, files []string) []string {
|
|
|
|
tasks := map[string]struct{}{}
|
|
|
|
|
|
|
|
for _, f := range files {
|
2020-02-12 23:28:27 +00:00
|
|
|
components := strings.Split(f, string(filepath.Separator))
|
|
|
|
if len(components) == 0 {
|
2020-02-12 22:54:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-12 23:28:27 +00:00
|
|
|
_, task := d.FindTask(components[0])
|
2021-04-22 11:12:10 +00:00
|
|
|
if task != nil {
|
|
|
|
tasks[task.Name] = struct{}{}
|
2020-02-12 22:54:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 11:12:10 +00:00
|
|
|
for _, task := range d.Tasks() {
|
|
|
|
for _, path := range task.Watch {
|
2022-04-15 14:58:09 +00:00
|
|
|
if strings.HasPrefix(f, path) {
|
2021-04-22 11:12:10 +00:00
|
|
|
tasks[task.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 22:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var l []string
|
|
|
|
for t := range tasks {
|
|
|
|
l = append(l, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(l)
|
|
|
|
return l
|
|
|
|
}
|