]> Cypherpunks.ru repositories - goredo.git/blob - dot.go
Redundant @documentencoding
[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, tgtOrig string) (map[DotNodes]bool, error) {
38         cwd, tgt := cwdAndTgt(tgtOrig)
39         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
40         fdDep, err := os.Open(depPath)
41         if err != nil {
42                 return nil, err
43         }
44         defer fdDep.Close()
45         var dep string
46         r := recfile.NewReader(fdDep)
47         for {
48                 m, err := r.NextMap()
49                 if err != nil {
50                         if errors.Is(err, io.EOF) {
51                                 break
52                         }
53                         return nil, err
54                 }
55                 switch m["Type"] {
56                 case DepTypeIfcreate:
57                         data[DotNodes{tgtOrig, cwdMustRel(cwd, m["Target"])}] = true
58                 case DepTypeIfchange:
59                         dep = m["Target"]
60                         if dep == tgt {
61                                 continue
62                         }
63                         data[DotNodes{tgtOrig, cwdMustRel(cwd, dep)}] = false
64                         if isSrc(cwd, dep) {
65                                 continue
66                         }
67                         data, err = dotWalker(data, cwdMustRel(cwd, dep))
68                         if err != nil {
69                                 return nil, err
70                         }
71                 }
72         }
73         return data, nil
74 }
75
76 func dotPrint(tgts []string) error {
77         data := map[DotNodes]bool{}
78         var err error
79         for _, tgt := range tgts {
80                 data, err = dotWalker(data, tgt)
81                 if err != nil {
82                         return err
83                 }
84         }
85         fmt.Println(`digraph d {
86         rankdir=LR
87         ranksep=2
88         splines=false // splines=ortho
89         node[shape=rectangle]`)
90         for nodes, nonexistent := range data {
91                 fmt.Printf("\n\t\"%s\" -> \"%s\"", nodes.from, nodes.to)
92                 if nonexistent {
93                         fmt.Print(" [style=dotted]")
94                 }
95         }
96         fmt.Println("\n}")
97         return nil
98 }