github.com/beauknowssoftware/makehcl@v0.0.0-20200322000747-1b9bb1e1c008/internal/graph/constructImportGraph.go (about) 1 package graph 2 3 import ( 4 "sort" 5 6 "github.com/beauknowssoftware/makehcl/internal/parse2" 7 "github.com/emicklei/dot" 8 ) 9 10 func constructImportGraph(d *parse2.Definition) *Graph { 11 g := dot.NewGraph(dot.Directed) 12 13 nodeMap := make(map[string]dot.Node) 14 15 filenames := make([]string, 0, len(d.Files)) 16 for _, f := range d.Files { 17 filenames = append(filenames, f.Name) 18 } 19 20 sort.Strings(filenames) 21 22 for _, name := range filenames { 23 n := g.Node(name) 24 25 f := d.Files[name] 26 if !f.HasContents() { 27 n.Attr("color", "red") 28 } 29 30 nodeMap[name] = n 31 } 32 33 for _, name := range filenames { 34 n1 := nodeMap[name] 35 36 f := d.Files[name] 37 imports := make([]string, 0, len(f.ImportBlocks)) 38 39 for _, imp := range f.ImportBlocks { 40 if imp.File == nil { 41 continue 42 } 43 44 imports = append(imports, imp.File.Value) 45 } 46 47 sort.Strings(imports) 48 49 for _, imp := range imports { 50 n2 := nodeMap[imp] 51 g.Edge(n1, n2) 52 } 53 } 54 55 return g 56 }