Share artifact dependency queries across consumers
This commit is contained in:
@@ -1,124 +1,27 @@
|
||||
package artifacts
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactdeps"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
)
|
||||
|
||||
// AnalyzeTargetClosure returns targets and their prerequisites in work order.
|
||||
func AnalyzeTargetClosure(
|
||||
configured map[string]config.ScriptoriumArtifactConfig,
|
||||
targets []string,
|
||||
) ([]string, error) {
|
||||
closure := make(map[string]struct{}, len(targets))
|
||||
var visit func(string) error
|
||||
visit = func(key string) error {
|
||||
if _, seen := closure[key]; seen {
|
||||
return nil
|
||||
}
|
||||
artifactCfg, ok := configured[key]
|
||||
if !ok {
|
||||
return fmt.Errorf("selected artifact %q is not configured", key)
|
||||
}
|
||||
closure[key] = struct{}{}
|
||||
for _, dependency := range NormalizedDependencyKeys(artifactCfg.DependsOn) {
|
||||
if err := visit(dependency); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, target := range targets {
|
||||
if err := visit(target); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
order, err := OrderConfiguredArtifacts(configured)
|
||||
func AnalyzeTargetClosure(configured map[string]config.ScriptoriumArtifactConfig, targets []string) ([]string, error) {
|
||||
graph, err := config.BuildArtifactGraph(configured)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]string, 0, len(closure))
|
||||
for _, key := range order {
|
||||
if _, included := closure[key]; included {
|
||||
result = append(result, key)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type artifactReadyQueue []string
|
||||
|
||||
func (q artifactReadyQueue) Len() int { return len(q) }
|
||||
func (q artifactReadyQueue) Less(i, j int) bool { return q[i] < q[j] }
|
||||
func (q artifactReadyQueue) Swap(i, j int) { q[i], q[j] = q[j], q[i] }
|
||||
func (q *artifactReadyQueue) Push(value any) { *q = append(*q, value.(string)) }
|
||||
func (q *artifactReadyQueue) Pop() any {
|
||||
last := len(*q) - 1
|
||||
value := (*q)[last]
|
||||
(*q)[last] = ""
|
||||
*q = (*q)[:last]
|
||||
return value
|
||||
return graph.Prerequisites(targets, nil)
|
||||
}
|
||||
|
||||
// OrderConfiguredArtifacts returns configured keys in prerequisite-first order.
|
||||
func OrderConfiguredArtifacts(
|
||||
configured map[string]config.ScriptoriumArtifactConfig,
|
||||
) ([]string, error) {
|
||||
indegree := make(map[string]int, len(configured))
|
||||
edges := make(map[string][]string, len(configured))
|
||||
for key := range configured {
|
||||
indegree[key] = 0
|
||||
func OrderConfiguredArtifacts(configured map[string]config.ScriptoriumArtifactConfig) ([]string, error) {
|
||||
graph, err := config.BuildArtifactGraph(configured)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for key, artifactCfg := range configured {
|
||||
for _, dependency := range NormalizedDependencyKeys(artifactCfg.DependsOn) {
|
||||
if _, ok := configured[dependency]; !ok {
|
||||
return nil, fmt.Errorf("configured artifact %q depends on unknown artifact %q", key, dependency)
|
||||
}
|
||||
edges[dependency] = append(edges[dependency], key)
|
||||
indegree[key]++
|
||||
}
|
||||
}
|
||||
ready := make(artifactReadyQueue, 0, len(indegree))
|
||||
for key, degree := range indegree {
|
||||
if degree == 0 {
|
||||
ready = append(ready, key)
|
||||
}
|
||||
}
|
||||
heap.Init(&ready)
|
||||
order := make([]string, 0, len(indegree))
|
||||
for len(ready) > 0 {
|
||||
key := heap.Pop(&ready).(string)
|
||||
order = append(order, key)
|
||||
for _, dependent := range edges[key] {
|
||||
indegree[dependent]--
|
||||
if indegree[dependent] == 0 {
|
||||
heap.Push(&ready, dependent)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(order) != len(configured) {
|
||||
return nil, fmt.Errorf("pipeline.scriptorium.artifacts dependencies must not contain cycles")
|
||||
}
|
||||
return order, nil
|
||||
return graph.Order(), nil
|
||||
}
|
||||
|
||||
// NormalizedDependencyKeys deduplicates and sorts dependency keys.
|
||||
func NormalizedDependencyKeys(values []string) []string {
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
for _, value := range values {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed != "" {
|
||||
seen[trimmed] = struct{}{}
|
||||
}
|
||||
}
|
||||
result := make([]string, 0, len(seen))
|
||||
for value := range seen {
|
||||
result = append(result, value)
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result
|
||||
}
|
||||
func NormalizedDependencyKeys(values []string) []string { return artifactdeps.Normalize(values) }
|
||||
|
||||
Reference in New Issue
Block a user