]> Cypherpunks.ru repositories - goredo.git/blob - dot.go
e311c3044b64b89b5f87af1e909d3695616d586d
[goredo.git] / dot.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2024 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         "os"
25         "path"
26 )
27
28 type DotNodes struct {
29         from string
30         to   string
31 }
32
33 func dotWalker(data map[DotNodes]bool, tgt *Tgt) (map[DotNodes]bool, error) {
34         raw, err := os.ReadFile(tgt.dep)
35         if err != nil {
36                 return nil, ErrLine(err)
37         }
38         _, raw, err = depHeadParse(raw)
39         if err != nil {
40                 return nil, ErrLine(err)
41         }
42         var typ byte
43         var name string
44         var dep *Tgt
45         var chunk []byte
46         tgtH, _ := pathSplit(tgt.a)
47         for len(raw) > 0 {
48                 typ, chunk, raw, err = chunkRead(raw)
49                 if err != nil {
50                         return nil, ErrLine(err)
51                 }
52                 switch typ {
53                 case DepTypeIfcreate:
54                         data[DotNodes{tgt.rel, NewTgt(path.Join(tgtH, string(chunk))).rel}] = true
55                 case DepTypeIfchange, DepTypeIfchangeNonex:
56                         if typ == DepTypeIfchangeNonex {
57                                 name = string(chunk)
58                         } else {
59                                 name = string(chunk[InodeLen+HashLen:])
60                         }
61                         dep = NewTgt(path.Join(tgtH, name))
62                         if dep.a == tgt.a {
63                                 continue
64                         }
65                         data[DotNodes{tgt.rel, dep.rel}] = false
66                         if isSrc(dep) {
67                                 continue
68                         }
69                         data, err = dotWalker(data, dep)
70                         if err != nil {
71                                 return nil, err
72                         }
73                 }
74         }
75         return data, nil
76 }
77
78 func dotPrint(tgts []*Tgt) error {
79         data := map[DotNodes]bool{}
80         var err error
81         for _, tgt := range tgts {
82                 data, err = dotWalker(data, tgt)
83                 if err != nil {
84                         return err
85                 }
86         }
87         fmt.Println(`digraph d {
88         rankdir=LR
89         ranksep=2
90         splines=false // splines=ortho
91         node[shape=rectangle]`)
92         for nodes, nonexistent := range data {
93                 fmt.Printf("\n\t\"%s\" -> \"%s\"", nodes.from, nodes.to)
94                 if nonexistent {
95                         fmt.Print(" [style=dotted]")
96                 }
97         }
98         fmt.Println("\n}")
99         return nil
100 }