/* goredo -- djb's redo implementation on pure Go Copyright (C) 2020-2023 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ // Dependency DOT graph generation package main import ( "errors" "fmt" "io" "os" "path" "go.cypherpunks.ru/recfile" ) type DotNodes struct { from string to string } func dotWalker(data map[DotNodes]bool, tgt *Tgt) (map[DotNodes]bool, error) { fdDep, err := os.Open(tgt.Dep()) if err != nil { return nil, ErrLine(err) } defer fdDep.Close() var dep *Tgt r := recfile.NewReader(fdDep) for { m, err := r.NextMap() if err != nil { if errors.Is(err, io.EOF) { break } return nil, ErrLine(err) } switch m["Type"] { case DepTypeIfcreate: data[DotNodes{tgt.String(), NewTgt(m["Target"]).String()}] = true case DepTypeIfchange: dep = NewTgt(path.Join(tgt.h, m["Target"])) if dep.a == tgt.a { continue } data[DotNodes{tgt.String(), dep.String()}] = false if isSrc(dep) { continue } data, err = dotWalker(data, dep) if err != nil { return nil, err } } } return data, nil } func dotPrint(tgts []*Tgt) error { data := map[DotNodes]bool{} var err error for _, tgt := range tgts { data, err = dotWalker(data, tgt) if err != nil { return err } } fmt.Println(`digraph d { rankdir=LR ranksep=2 splines=false // splines=ortho node[shape=rectangle]`) for nodes, nonexistent := range data { fmt.Printf("\n\t\"%s\" -> \"%s\"", nodes.from, nodes.to) if nonexistent { fmt.Print(" [style=dotted]") } } fmt.Println("\n}") return nil }