generate cool dependency graph

This commit is contained in:
2026-02-15 18:43:14 +01:00
parent 9dc1068034
commit 87a7eef39b
26 changed files with 321 additions and 22 deletions

View File

@@ -82,17 +82,28 @@ void install_package(const std::string &name) {
if (src_type == "tar") {
std::string archive_path = "/tmp/shrap/" + Util::basename(src_url);
Util::shell_command("./curl -L -o " + archive_path + " " + src_url);
std::string expected_hash = src.get_one("blake3").value;
std::string hash = Util::hash_file(archive_path);
if (expected_hash != hash) {
std::cerr << "Checksum check failed.\n";
std::cerr << "Expected: " << expected_hash << "\n";
std::cerr << "Got: " << hash << "\n";
std::exit(1);
bool needs_download = true;
// dont redownload if checksum matches
if (std::ifstream(archive_path)) {
std::string current_hash = Util::hash_file(archive_path);
if (current_hash == expected_hash) {
needs_download = false;
}
}
if (needs_download) {
Util::shell_command("./curl -L -o " + archive_path + " " + src_url);
std::string hash = Util::hash_file(archive_path);
if (expected_hash != hash) {
std::cerr << "Checksum check failed.\n";
std::cerr << "Expected: " << expected_hash << "\n";
std::cerr << "Got: " << hash << "\n";
std::exit(1);
}
}
Util::shell_command("tar xf " + archive_path + " -C /tmp/shrap/");
@@ -140,6 +151,19 @@ void install_package(const std::string &name) {
Util::shell_command("make " + make_flags + " -j " + jobs, src_path);
Util::shell_command("make install " + install_flags, src_path);
} else if (step_type == "cmake_ninja") {
std::string configure_flags;
try {
configure_flags = step.get_one("configure_flags").value;
} catch (std::out_of_range &) {
}
Util::shell_command("mkdir -p build", src_path);
Util::shell_command("cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr " +
configure_flags + " ..",
src_path + "/build");
Util::shell_command("ninja -j" + jobs, src_path + "/build");
Util::shell_command("ninja install", src_path + "/build");
} else if (step_type == "meson") {
std::string configure_flags;
try {
@@ -159,13 +183,30 @@ void install_package(const std::string &name) {
}
}
void generate_graph() {
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";
try {
for (const auto &dep : pkg.get("dependencies").children) {
out << " \"" << pkg_name << "\" -> \"" << dep.value << "\";\n";
}
} catch (std::out_of_range &) {
}
}
out << "}\n";
out.close();
Util::shell_command("dot -Tpng graph.dot > graph.png", "/tmp/shrap");
std::cout << "Graph saved to /tmp/shrap/graph.png\n";
}
int main(int argc, char **argv) {
auto args = std::span(argv, static_cast<size_t>(argc));
if (geteuid() != 0) {
std::cerr << "This program needs to be ran as root.\n";
return 1;
}
Util::shell_command("mkdir -p /tmp/shrap");
load_packages();
@@ -175,13 +216,16 @@ int main(int argc, char **argv) {
std::string arg = args[i];
if (arg == "-r") {
flag_raw = true;
} else if (arg == "-g") {
generate_graph();
return 0;
} else {
to_install.push_back(arg);
}
}
if (to_install.empty()) {
std::cerr << "Usage: " << args[0] << " [-r] package1 [package2 ...]\n";
std::cerr << "Usage: " << args[0] << " [-g] [-r] package1 [package2 ...]\n";
return 1;
}
@@ -189,9 +233,15 @@ int main(int argc, char **argv) {
to_install = resolve_dependencies(to_install);
}
if (geteuid() != 0) {
std::cerr << "This program needs to be ran as root.\n";
return 1;
}
std::cout << "\nFollowing packages will be installed:\n";
for (const std::string &pkg : to_install) {
std::cout << " - " << pkg << std::endl;
std::cout << " - " << pkg << " (" << packages[pkg].get_one("version").value
<< ")" << std::endl;
}
std::cin.get();