manager: Render graphviz tree and push it to git
This commit is contained in:
parent
3bd79f176e
commit
8b66d2551a
3 changed files with 81 additions and 1 deletions
69
techtree-manager/src/render.rs
Normal file
69
techtree-manager/src/render.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
use std::process::Command;
|
||||
|
||||
use anyhow::Context as _;
|
||||
|
||||
pub async fn render_and_publish(
|
||||
tree: &crate::tree::Tree,
|
||||
timestamp: &str,
|
||||
repo_auth_url: &url::Url,
|
||||
) -> anyhow::Result<()> {
|
||||
let render_repo = std::path::PathBuf::from("render-git");
|
||||
|
||||
if render_repo.is_dir() {
|
||||
log::info!("Found old {render_repo:?} repository, removing...");
|
||||
std::fs::remove_dir_all(&render_repo).context("Failed to remove stale render repository")?;
|
||||
}
|
||||
|
||||
std::fs::create_dir(&render_repo).context("Failed creating directory for rendered graph")?;
|
||||
|
||||
Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(&render_repo)
|
||||
.arg("init")
|
||||
.status()
|
||||
.context("Failed to initialize render repository")?;
|
||||
|
||||
let dot_file = render_repo.join("techtree.dot");
|
||||
let svg_file = render_repo.join("techtree.svg");
|
||||
|
||||
let dot_source = tree.to_dot();
|
||||
std::fs::write(&dot_file, dot_source.as_bytes())
|
||||
.context("Failed to write `dot` source file")?;
|
||||
|
||||
Command::new("dot")
|
||||
.args(["-T", "svg"])
|
||||
.arg("-o")
|
||||
.arg(&svg_file)
|
||||
.arg(&dot_file)
|
||||
.status()
|
||||
.context("Failed to generate svg graph from dot source")?;
|
||||
|
||||
Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(&render_repo)
|
||||
.arg("add")
|
||||
.arg(dot_file.file_name().unwrap())
|
||||
.arg(svg_file.file_name().unwrap())
|
||||
.status()
|
||||
.context("Failed to add generated graph files to git index")?;
|
||||
|
||||
Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(&render_repo)
|
||||
.arg("commit")
|
||||
.args(["-m", &format!("Updated techtree at {timestamp}")])
|
||||
.status()
|
||||
.context("Failed to add generated graph files to git index")?;
|
||||
|
||||
Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(&render_repo)
|
||||
.arg("push")
|
||||
.arg("--force")
|
||||
.arg(repo_auth_url.to_string())
|
||||
.arg("HEAD:refs/heads/render")
|
||||
.status()
|
||||
.context("Failed to push rendered graph to forgejo repository")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue