]> Cypherpunks.ru repositories - goredo.git/blob - dot.go
0ea2b4ae13fcffb9bd1fc858a329eadb216b8070
[goredo.git] / dot.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Dependency DOT graph generation
19
20 package main
21
22 import (
23         "errors"
24         "fmt"
25         "io"
26         "os"
27         "path"
28
29         "go.cypherpunks.ru/recfile"
30 )
31
32 type DotNodes struct {
33         from string
34         to   string
35 }
36
37 func dotWalker(data map[DotNodes]bool, tgt *Tgt) (map[DotNodes]bool, error) {
38         fdDep, err := os.Open(tgt.Dep())
39         if err != nil {
40                 return nil, ErrLine(err)
41         }
42         defer fdDep.Close()
43         var dep *Tgt
44         r := recfile.NewReader(fdDep)
45         for {
46                 m, err := r.NextMap()
47                 if err != nil {
48                         if errors.Is(err, io.EOF) {
49                                 break
50                         }
51                         return nil, ErrLine(err)
52                 }
53                 switch m["Type"] {
54                 case DepTypeIfcreate:
55                         data[DotNodes{tgt.rel, NewTgt(string(chunk)).rel}] = true
56                 case DepTypeIfchange:
57                         dep = NewTgt(path.Join(tgt.h, m["Target"]))
58                         if dep.a == tgt.a {
59                                 continue
60                         }
61                         data[DotNodes{tgt.rel, dep.rel}] = false
62                         if isSrc(dep) {
63                                 continue
64                         }
65                         data, err = dotWalker(data, dep)
66                         if err != nil {
67                                 return nil, err
68                         }
69                 }
70         }
71         return data, nil
72 }
73
74 func dotPrint(tgts []*Tgt) error {
75         data := map[DotNodes]bool{}
76         var err error
77         for _, tgt := range tgts {
78                 data, err = dotWalker(data, tgt)
79                 if err != nil {
80                         return err
81                 }
82         }
83         fmt.Println(`digraph d {
84         rankdir=LR
85         ranksep=2
86         splines=false // splines=ortho
87         node[shape=rectangle]`)
88         for nodes, nonexistent := range data {
89                 fmt.Printf("\n\t\"%s\" -> \"%s\"", nodes.from, nodes.to)
90                 if nonexistent {
91                         fmt.Print(" [style=dotted]")
92                 }
93         }
94         fmt.Println("\n}")
95         return nil
96 }