This repository was archived by the owner on Sep 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathvisualize.rs
More file actions
80 lines (76 loc) · 2.97 KB
/
visualize.rs
File metadata and controls
80 lines (76 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2023, stack-graphs authors.
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
// ------------------------------------------------------------------------------------------------
use clap::Args;
use clap::ValueHint;
use stack_graphs::serde::NoFilter;
use stack_graphs::stitching::Database;
use stack_graphs::stitching::ForwardPartialPathStitcher;
use stack_graphs::stitching::StitcherConfig;
use stack_graphs::storage::SQLiteReader;
use stack_graphs::NoCancellation;
use std::path::Path;
use std::path::PathBuf;
/// Visualize database
#[derive(Args)]
#[clap(after_help = r#"LIMITATIONS:
Visualizations will only work for very small stack graphs. This command is
useful for debugging minimal examples, but running it on any real-world code
will most likely result in HTML files that will not load in any browser.
"#)]
pub struct VisualizeArgs {
/// Source file or directory paths.
#[clap(
value_name = "SOURCE_PATH",
value_hint = ValueHint::AnyPath,
)]
pub source_paths: Vec<PathBuf>,
#[clap(
long,
short = 'o',
value_name = "OUTPUT_PATH",
value_hint = ValueHint::AnyPath,
default_value = "stack-graph.html",
)]
pub output: PathBuf,
}
impl VisualizeArgs {
pub fn run(self, db_path: &Path) -> anyhow::Result<()> {
let cancellation_flag = &NoCancellation;
let mut db = SQLiteReader::open(&db_path)?;
for source_path in &self.source_paths {
let source_path = source_path.canonicalize()?;
db.load_graphs_for_file_or_directory(&source_path, cancellation_flag)?;
}
let (graph, _, _) = db.get();
let starting_nodes = graph
.iter_nodes()
.filter(|n| graph[*n].is_reference())
.collect::<Vec<_>>();
let mut complete_paths_db = Database::new();
let stitcher_config = StitcherConfig::default()
// always detect similar paths, we don't know the language configurations for the data in the database
.with_detect_similar_paths(true);
ForwardPartialPathStitcher::find_all_complete_partial_paths(
&mut db,
starting_nodes,
stitcher_config,
cancellation_flag,
|g, ps, p| {
complete_paths_db.add_partial_path(g, ps, p.clone());
},
)?;
let (graph, partials, _) = db.get();
let html =
graph.to_html_string("stack-graph", partials, &mut complete_paths_db, &NoFilter)?;
if let Some(dir) = self.output.parent() {
std::fs::create_dir_all(dir)?;
}
std::fs::write(&self.output, html)?;
println!("Visualization at {}", self.output.display());
Ok(())
}
}