highlight dependencies on graph

This commit is contained in:
2026-02-15 18:58:17 +01:00
parent 87a7eef39b
commit 9fde568bee
4 changed files with 24 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
#include "parser.h"
#include "util.h"
#include <algorithm>
#include <filesystem>
#include <functional>
#include <span>
@@ -9,6 +10,7 @@
std::unordered_map<std::string, Expr> packages;
bool flag_raw = false;
bool flag_graph = false;
void load_packages() {
for (const auto &e : std::filesystem::directory_iterator("./packages/")) {
@@ -87,7 +89,7 @@ void install_package(const std::string &name) {
bool needs_download = true;
// dont redownload if checksum matches
if (std::ifstream(archive_path)) {
if (std::filesystem::exists(archive_path)) {
std::string current_hash = Util::hash_file(archive_path);
if (current_hash == expected_hash) {
needs_download = false;
@@ -183,14 +185,19 @@ void install_package(const std::string &name) {
}
}
void generate_graph() {
void generate_graph(const std::vector<std::string> &to_highlight) {
std::ofstream out("/tmp/shrap/graph.dot");
out << "digraph dependencies {\n";
out << " rankdir=LR;\n";
for (const auto &[pkg_name, pkg] : packages) {
out << " \"" << pkg_name << "\";\n";
if (std::find(to_highlight.begin(), to_highlight.end(), pkg_name) !=
to_highlight.end()) {
out << " \"" << pkg_name << "\" [style=filled, fillcolor=yellow];\n";
} else {
out << " \"" << pkg_name << "\";\n";
}
try {
for (const auto &dep : pkg.get("dependencies").children) {
out << " \"" << pkg_name << "\" -> \"" << dep.value << "\";\n";
@@ -217,22 +224,26 @@ int main(int argc, char **argv) {
if (arg == "-r") {
flag_raw = true;
} else if (arg == "-g") {
generate_graph();
return 0;
flag_graph = true;
} else {
to_install.push_back(arg);
}
}
if (!flag_raw) {
to_install = resolve_dependencies(to_install);
}
if (flag_graph) {
generate_graph(to_install);
return 0;
}
if (to_install.empty()) {
std::cerr << "Usage: " << args[0] << " [-g] [-r] package1 [package2 ...]\n";
return 1;
}
if (!flag_raw) {
to_install = resolve_dependencies(to_install);
}
if (geteuid() != 0) {
std::cerr << "This program needs to be ran as root.\n";
return 1;