From 37bc57f010498aa4fb4edd49f31d3e80415a1d62 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 26 Oct 2022 15:05:55 +0200 Subject: [PATCH] Create dataflow html file in current directory Chrome does not permit opening HTML files from the tmp directory. --- binaries/cli/src/graph/mod.rs | 52 ++++++++++++++++++++++++++++++++++- binaries/cli/src/main.rs | 28 ++----------------- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/binaries/cli/src/graph/mod.rs b/binaries/cli/src/graph/mod.rs index 26d4d4145..34ee73a32 100644 --- a/binaries/cli/src/graph/mod.rs +++ b/binaries/cli/src/graph/mod.rs @@ -1,10 +1,60 @@ -use std::{fs, path::Path}; +use std::{ + fs::{self, File}, + io::Write, + path::Path, +}; use dora_core::descriptor::Descriptor; use eyre::Context; const MERMAID_TEMPLATE: &str = include_str!("mermaid-template.html"); +pub(crate) fn create(dataflow: std::path::PathBuf, mermaid: bool, open: bool) -> eyre::Result<()> { + if mermaid { + let visualized = visualize_as_mermaid(&dataflow)?; + println!("{visualized}"); + println!( + "Paste the above output on https://mermaid.live/ or in a \ + ```mermaid code block on GitHub to display it." + ); + } else { + let html = visualize_as_html(&dataflow)?; + + let working_dir = std::env::current_dir().wrap_err("failed to get current working dir")?; + let graph_filename = match dataflow.file_stem().and_then(|n| n.to_str()) { + Some(name) => format!("{name}-graph"), + None => "graph".into(), + }; + let mut extra = 0; + let path = loop { + let adjusted_file_name = if extra == 0 { + format!("{graph_filename}.html") + } else { + format!("{graph_filename}.{extra}.html") + }; + let path = working_dir.join(&adjusted_file_name); + if path.exists() { + extra += 1; + } else { + break path; + } + }; + + let mut file = File::create(&path).context("failed to create graph HTML file")?; + file.write_all(html.as_bytes())?; + + println!( + "View graph by opening the following in your browser:\n file://{}", + path.display() + ); + + if open { + webbrowser::open(path.as_os_str().to_str().unwrap())?; + } + } + Ok(()) +} + pub fn visualize_as_html(dataflow: &Path) -> eyre::Result { let mermaid = visualize_as_mermaid(dataflow)?; Ok(MERMAID_TEMPLATE.replacen("____insert____", &mermaid, 1)) diff --git a/binaries/cli/src/main.rs b/binaries/cli/src/main.rs index 3a9bd4ff4..1726c55a8 100644 --- a/binaries/cli/src/main.rs +++ b/binaries/cli/src/main.rs @@ -4,8 +4,7 @@ use dora_core::topics::{ ZENOH_CONTROL_START, ZENOH_CONTROL_STOP, }; use eyre::{bail, eyre, Context}; -use std::{io::Write, ops::Deref, path::PathBuf, sync::Arc}; -use tempfile::NamedTempFile; +use std::{ops::Deref, path::PathBuf, sync::Arc}; use zenoh::{ prelude::{Receiver, Selector, SplitBuffer}, sync::ZFuture, @@ -99,30 +98,7 @@ fn main() -> eyre::Result<()> { mermaid, open, } => { - if mermaid { - let visualized = graph::visualize_as_mermaid(&dataflow)?; - println!("{visualized}"); - println!( - "Paste the above output on https://mermaid.live/ or in a \ - ```mermaid code block on GitHub to display it." - ); - } else { - let html = graph::visualize_as_html(&dataflow)?; - let mut file = NamedTempFile::new().context("failed to create temp file")?; - file.as_file_mut().write_all(html.as_bytes())?; - - let path = file.path().to_owned(); - file.keep()?; - - println!( - "View graph by opening the following in your browser:\n file://{}", - path.display() - ); - - if open { - webbrowser::open(path.as_os_str().to_str().unwrap())?; - } - } + graph::create(dataflow, mermaid, open)?; } Command::Build { dataflow } => { build::build(&dataflow)?;