]> Cypherpunks.ru repositories - goredo.git/blob - dot.go
Download link for 2.6.2 release
[goredo.git] / dot.go
1 // goredo -- djb's redo implementation on pure Go
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 // Dependency DOT graph generation
17
18 package main
19
20 import (
21         "fmt"
22         "os"
23         "path"
24 )
25
26 type DotNodes struct {
27         from string
28         to   string
29 }
30
31 func dotWalker(data map[DotNodes]bool, tgt *Tgt) (map[DotNodes]bool, error) {
32         raw, err := os.ReadFile(tgt.dep)
33         if err != nil {
34                 return nil, ErrLine(err)
35         }
36         _, raw, err = depHeadParse(raw)
37         if err != nil {
38                 return nil, ErrLine(err)
39         }
40         var typ byte
41         var name string
42         var dep *Tgt
43         var chunk []byte
44         tgtH, _ := pathSplit(tgt.a)
45         for len(raw) > 0 {
46                 typ, chunk, raw, err = chunkRead(raw)
47                 if err != nil {
48                         return nil, ErrLine(err)
49                 }
50                 switch typ {
51                 case DepTypeIfcreate:
52                         data[DotNodes{tgt.rel, NewTgt(path.Join(tgtH, string(chunk))).rel}] = true
53                 case DepTypeIfchange, DepTypeIfchangeNonex:
54                         if typ == DepTypeIfchangeNonex {
55                                 name = string(chunk)
56                         } else {
57                                 name = string(chunk[InodeLen+HashLen:])
58                         }
59                         dep = NewTgt(path.Join(tgtH, name))
60                         if dep.a == tgt.a {
61                                 continue
62                         }
63                         data[DotNodes{tgt.rel, dep.rel}] = false
64                         if isSrc(dep) {
65                                 continue
66                         }
67                         data, err = dotWalker(data, dep)
68                         if err != nil {
69                                 return nil, err
70                         }
71                 }
72         }
73         return data, nil
74 }
75
76 func dotPrint(tgts []*Tgt) 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 }