This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
forked from project-stacker/stacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deps.go
75 lines (63 loc) · 1.79 KB
/
deps.go
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
package stacker
import (
"github.com/project-stacker/stacker/lib"
"github.com/project-stacker/stacker/types"
"sort"
)
// StackerDepsDAG processes the dependencies between different stacker recipes
type StackerFilesDAG struct {
dag lib.Graph
}
// NewStackerDepsDAG properly initializes a StackerDepsProcessor
func NewStackerFilesDAG(sfMap types.StackerFiles) (*StackerFilesDAG, error) {
dag := lib.NewDAG()
// The DAG.Sort() method uses Topological sort which for acyclical graphs
// will return an order dependent upon which node it starts. To ensure
// we build the same Graph, sort the list of input files so we get the
// same starting Node for DAG.Sort() resulting in a consistent build order.
keys := make([]string, 0, len(sfMap))
for k := range sfMap {
keys = append(keys, k)
}
sort.Strings(keys)
// Add vertices to dag
for _, path := range keys {
sf := sfMap[path]
// Add a vertex for every StackerFile object
err := dag.AddVertex(path, sf)
if err != nil {
return nil, err
}
}
// Update the dependencies in the dag
for path, sf := range sfMap {
prerequisites, err := sf.Prerequisites()
if err != nil {
return nil, err
}
for _, depPath := range prerequisites {
err := dag.AddDependencies(path, depPath)
if err != nil {
return nil, err
}
}
}
p := StackerFilesDAG{
dag: dag,
}
return &p, nil
}
func (d *StackerFilesDAG) GetStackerFile(path string) *types.Stackerfile {
value := d.dag.GetValue(path)
return value.(*types.Stackerfile)
}
// Sort provides a serial build order for the stacker files
func (d *StackerFilesDAG) Sort() []string {
var order []string
// Use dag.Sort() to ensure we always process targets in order of their dependencies
for _, i := range d.dag.Sort() {
path := i.Key.(string)
order = append(order, path)
}
return order
}