Browse Source

Render graph as HTML to allow opening it in browser directly

tags/v0.0.0-test.4
Philipp Oppermann 3 years ago
parent
commit
b266b82b7c
Failed to extract signature
5 changed files with 55 additions and 13 deletions
  1. +1
    -0
      Cargo.lock
  2. +1
    -0
      binaries/cli/Cargo.toml
  3. +17
    -0
      binaries/cli/src/graph/mermaid-template.html
  4. +9
    -5
      binaries/cli/src/graph/mod.rs
  5. +27
    -8
      binaries/cli/src/main.rs

+ 1
- 0
Cargo.lock View File

@@ -794,6 +794,7 @@ dependencies = [
"dora-core",
"eyre",
"serde_yaml 0.9.11",
"tempfile",
]

[[package]]


+ 1
- 0
binaries/cli/Cargo.toml View File

@@ -14,3 +14,4 @@ clap = { version = "3.2.20", features = ["derive"] }
eyre = "0.6.8"
dora-core = { path = "../../libraries/core" }
serde_yaml = "0.9.11"
tempfile = "3.3.0"

+ 17
- 0
binaries/cli/src/graph/mermaid-template.html View File

@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
</head>

<body>
<div class="mermaid">
____insert____
</div>
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>mermaid.initialize({ startOnLoad: true, securityLevel: 'loose' });
</script>
</body>

</html>

binaries/cli/src/graph.rs → binaries/cli/src/graph/mod.rs View File

@@ -1,12 +1,16 @@
use std::{
fs,
path::{Path, PathBuf},
};
use std::{fs, path::Path};

use dora_core::descriptor::Descriptor;
use eyre::Context;

pub fn visualize_as_mermaid(dataflow: PathBuf) -> eyre::Result<String> {
const MERMAID_TEMPLATE: &str = include_str!("mermaid-template.html");

pub fn visualize_as_html(dataflow: &Path) -> eyre::Result<String> {
let mermaid = visualize_as_mermaid(dataflow)?;
Ok(MERMAID_TEMPLATE.replacen("____insert____", &mermaid, 1))
}

pub fn visualize_as_mermaid(dataflow: &Path) -> eyre::Result<String> {
let descriptor = read_descriptor(&dataflow)
.with_context(|| format!("failed to read dataflow at `{}`", dataflow.display()))?;
let visualized = descriptor

+ 27
- 8
binaries/cli/src/main.rs View File

@@ -1,5 +1,7 @@
use clap::Parser;
use std::path::PathBuf;
use eyre::Context;
use std::{io::Write, path::PathBuf};
use tempfile::NamedTempFile;

mod build;
mod check;
@@ -20,6 +22,8 @@ enum Command {
},
Graph {
dataflow: PathBuf,
#[clap(long, action)]
mermaid: bool,
},
Build {
dataflow: PathBuf,
@@ -44,13 +48,28 @@ fn main() -> eyre::Result<()> {
dataflow,
runtime_path,
} => check::check(&dataflow, &runtime_path)?,
Command::Graph { dataflow } => {
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."
);
Command::Graph { dataflow, mermaid } => {
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();

println!(
"View graph by opening the following in your browser:\n file://{}",
path.display()
);

file.keep()?;
}
}
Command::Build { dataflow } => {
build::build(&dataflow)?;


Loading…
Cancel
Save