aboutsummaryrefslogtreecommitdiff
path: root/src/legacy/dotgraph.cc
blob: 2c530999b551e77fa975ca6521ff3547a8df71e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "dotgraph.hh"
#include "store-api.hh"

#include <iostream>


using std::cout;

namespace nix {


static std::string dotQuote(std::string_view s)
{
    return "\"" + std::string(s) + "\"";
}


static const std::string & nextColour()
{
    static int n = 0;
    static std::vector<std::string> colours
        { "black", "red", "green", "blue"
        , "magenta", "burlywood" };
    return colours[n++ % colours.size()];
}


static std::string makeEdge(std::string_view src, std::string_view dst)
{
    return fmt("%1% -> %2% [color = %3%];\n",
        dotQuote(src), dotQuote(dst), dotQuote(nextColour()));
}


static std::string makeNode(std::string_view id, std::string_view label,
    std::string_view colour)
{
    return fmt("%1% [label = %2%, shape = box, "
        "style = filled, fillcolor = %3%];\n",
        dotQuote(id), dotQuote(label), dotQuote(colour));
}


void printDotGraph(ref<Store> store, StorePathSet && roots)
{
    StorePathSet workList(std::move(roots));
    StorePathSet doneSet;

    cout << "digraph G {\n";

    while (!workList.empty()) {
        auto path = std::move(workList.extract(workList.begin()).value());

        if (!doneSet.insert(path).second) continue;

        cout << makeNode(std::string(path.to_string()), path.name(), "#ff0000");

        for (auto & p : store->queryPathInfo(path)->references) {
            if (p != path) {
                workList.insert(p);
                cout << makeEdge(std::string(p.to_string()), std::string(path.to_string()));
            }
        }
    }

    cout << "}\n";
}


}