]> Cypherpunks.ru repositories - goredo.git/blob - dot.go
Do not use deprecated os.SEEK_*
[goredo.git] / dot.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2021 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         "fmt"
24         "io"
25         "os"
26         "path"
27
28         "go.cypherpunks.ru/recfile"
29 )
30
31 type DotNodes struct {
32         from string
33         to   string
34 }
35
36 func dotWalker(data map[DotNodes]bool, tgtOrig string) (map[DotNodes]bool, error) {
37         cwd, tgt := cwdAndTgt(tgtOrig)
38         depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
39         fdDep, err := os.Open(depPath)
40         if err != nil {
41                 return nil, err
42         }
43         defer fdDep.Close()
44         var dep string
45         r := recfile.NewReader(fdDep)
46         for {
47                 m, err := r.NextMap()
48                 if err != nil {
49                         if err == io.EOF {
50                                 break
51                         }
52                         return nil, err
53                 }
54                 switch m["Type"] {
55                 case DepTypeIfcreate:
56                         data[DotNodes{tgtOrig, cwdMustRel(cwd, m["Target"])}] = true
57                 case DepTypeIfchange:
58                         dep = m["Target"]
59                         data[DotNodes{tgtOrig, cwdMustRel(cwd, dep)}] = false
60                         if isSrc(cwd, dep) || dep == tgt {
61                                 continue
62                         }
63                         data, err = dotWalker(data, cwdMustRel(cwd, dep))
64                         if err != nil {
65                                 return nil, err
66                         }
67                 }
68         }
69         return data, nil
70 }
71
72 func dotPrint(tgts []string) error {
73         data := map[DotNodes]bool{}
74         var err error
75         for _, tgt := range tgts {
76                 data, err = dotWalker(data, tgt)
77                 if err != nil {
78                         return err
79                 }
80         }
81         fmt.Println(`digraph d {
82         rankdir=LR
83         ranksep=2
84         splines=false // splines=ortho
85         node[shape=rectangle]`)
86         for nodes, nonexistent := range data {
87                 fmt.Printf("\n\t\"%s\" -> \"%s\"", nodes.from, nodes.to)
88                 if nonexistent {
89                         fmt.Print(" [style=dotted]")
90                 }
91         }
92         fmt.Println("\n}")
93         return nil
94 }