aboutsummaryrefslogtreecommitdiff
path: root/src/legacy/dotgraph.cc
diff options
context:
space:
mode:
authorRebecca Turner <rbt@sent.as>2024-09-10 13:13:19 -0700
committerRebecca Turner <rbt@sent.as>2024-10-01 16:08:58 -0700
commitb63d4a0c622fa556695e7666b9b3bde920904920 (patch)
tree48f4df7f13c1908f943f45be0c6f5c9c56252d6a /src/legacy/dotgraph.cc
parent775292766025380d04004e42fefbdb8ca40b3fa3 (diff)
Remove static initializers for `RegisterLegacyCommand`
This moves the "legacy"/"nix2" commands under a new `src/legacy/` directory, instead of being scattered around in a bunch of different directories. A new `liblegacy` build target is defined, and the `nix` binary is linked against it. Then, `RegisterLegacyCommand` is replaced with `LegacyCommand::add` calls in functions like `registerNixCollectGarbage()`. These registration functions are called explicitly in `src/nix/main.cc`. See: https://git.lix.systems/lix-project/lix/issues/359 Change-Id: Id450ffc3f793374907599cfcc121863b792aac1a
Diffstat (limited to 'src/legacy/dotgraph.cc')
-rw-r--r--src/legacy/dotgraph.cc70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/legacy/dotgraph.cc b/src/legacy/dotgraph.cc
new file mode 100644
index 000000000..2c530999b
--- /dev/null
+++ b/src/legacy/dotgraph.cc
@@ -0,0 +1,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";
+}
+
+
+}