Skip to content

Commit 235ffa2

Browse files
Add command to visualize graph
1 parent f615964 commit 235ffa2

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

tree-sitter-stack-graphs/src/cli.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub mod query;
7070
pub mod status;
7171
pub mod test;
7272
mod util;
73+
pub mod visualize;
7374

7475
pub mod path_loading {
7576
use std::path::PathBuf;
@@ -87,6 +88,7 @@ pub mod path_loading {
8788
use crate::cli::r#match::MatchArgs;
8889
use crate::cli::status::StatusArgs;
8990
use crate::cli::test::TestArgs;
91+
use crate::cli::visualize::VisualizeArgs;
9092

9193
use super::database::DatabaseArgs;
9294

@@ -102,6 +104,7 @@ pub mod path_loading {
102104
Query(Query),
103105
Status(Status),
104106
Test(Test),
107+
Visualize(Visualize),
105108
}
106109

107110
impl Subcommands {
@@ -117,6 +120,7 @@ pub mod path_loading {
117120
Self::Query(cmd) => cmd.run(default_db_path),
118121
Self::Status(cmd) => cmd.run(default_db_path),
119122
Self::Test(cmd) => cmd.run(),
123+
Self::Visualize(cmd) => cmd.run(default_db_path),
120124
}
121125
}
122126
}
@@ -269,6 +273,22 @@ pub mod path_loading {
269273
self.test_args.run(loader)
270274
}
271275
}
276+
277+
/// Visualize command
278+
#[derive(clap::Parser)]
279+
pub struct Visualize {
280+
#[clap(flatten)]
281+
db_args: DatabaseArgs,
282+
#[clap(flatten)]
283+
visualize_args: VisualizeArgs,
284+
}
285+
286+
impl Visualize {
287+
pub fn run(self, default_db_path: PathBuf) -> anyhow::Result<()> {
288+
let db_path = self.db_args.get_or(default_db_path);
289+
self.visualize_args.run(&db_path)
290+
}
291+
}
272292
}
273293

274294
pub mod provided_languages {
@@ -287,6 +307,7 @@ pub mod provided_languages {
287307
use crate::cli::r#match::MatchArgs;
288308
use crate::cli::status::StatusArgs;
289309
use crate::cli::test::TestArgs;
310+
use crate::cli::visualize::VisualizeArgs;
290311
use crate::loader::LanguageConfiguration;
291312

292313
use super::database::DatabaseArgs;
@@ -303,6 +324,7 @@ pub mod provided_languages {
303324
Query(Query),
304325
Status(Status),
305326
Test(Test),
327+
Visualize(Visualize),
306328
}
307329

308330
impl Subcommands {
@@ -322,6 +344,7 @@ pub mod provided_languages {
322344
Self::Query(cmd) => cmd.run(default_db_path),
323345
Self::Status(cmd) => cmd.run(default_db_path),
324346
Self::Test(cmd) => cmd.run(configurations),
347+
Self::Visualize(cmd) => cmd.run(default_db_path),
325348
}
326349
}
327350
}
@@ -482,4 +505,20 @@ pub mod provided_languages {
482505
self.test_args.run(loader)
483506
}
484507
}
508+
509+
/// Visualize command
510+
#[derive(clap::Parser)]
511+
pub struct Visualize {
512+
#[clap(flatten)]
513+
db_args: DatabaseArgs,
514+
#[clap(flatten)]
515+
visualize_args: VisualizeArgs,
516+
}
517+
518+
impl Visualize {
519+
pub fn run(self, default_db_path: PathBuf) -> anyhow::Result<()> {
520+
let db_path = self.db_args.get_or(default_db_path);
521+
self.visualize_args.run(&db_path)
522+
}
523+
}
485524
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// -*- coding: utf-8 -*-
2+
// ------------------------------------------------------------------------------------------------
3+
// Copyright © 2023, stack-graphs authors.
4+
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
5+
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
6+
// ------------------------------------------------------------------------------------------------
7+
8+
use clap::Args;
9+
use clap::ValueHint;
10+
use stack_graphs::serde::NoFilter;
11+
use stack_graphs::storage::SQLiteReader;
12+
use stack_graphs::NoCancellation;
13+
use std::path::Path;
14+
use std::path::PathBuf;
15+
16+
/// Visualize database
17+
#[derive(Args)]
18+
pub struct VisualizeArgs {
19+
/// Source file or directory paths.
20+
#[clap(
21+
value_name = "SOURCE_PATH",
22+
value_hint = ValueHint::AnyPath,
23+
)]
24+
pub source_paths: Vec<PathBuf>,
25+
26+
#[clap(
27+
long,
28+
short = 'o',
29+
value_name = "OUTPUT_PATH",
30+
value_hint = ValueHint::AnyPath,
31+
default_value = "stack-graph.html",
32+
)]
33+
pub output: PathBuf,
34+
}
35+
36+
impl VisualizeArgs {
37+
pub fn run(self, db_path: &Path) -> anyhow::Result<()> {
38+
let cancellation_flag = &NoCancellation;
39+
let mut db = SQLiteReader::open(&db_path)?;
40+
for source_path in &self.source_paths {
41+
let source_path = source_path.canonicalize()?;
42+
db.load_graph_for_file_or_directory(&source_path, cancellation_flag)?;
43+
}
44+
let (graph, partials, db) = db.get();
45+
let html = graph.to_html_string("stack-graph", partials, db, &NoFilter)?;
46+
if let Some(dir) = self.output.parent() {
47+
std::fs::create_dir_all(dir)?;
48+
}
49+
std::fs::write(&self.output, html)?;
50+
println!("Visualization at {}", self.output.display());
51+
Ok(())
52+
}
53+
}

0 commit comments

Comments
 (0)