aboutsummaryrefslogtreecommitdiff
path: root/src/nix-store/dotgraph.cc
blob: 577cadceb313891f664475fbb9778e40ce35ea99 (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
71
#include "dotgraph.hh"
#include "util.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";
}


}