From ff6e193a0bc5f91ba6a9dba763cbf3704c1c8191 Mon Sep 17 00:00:00 2001 From: Thibault Ruby Date: Mon, 29 Jan 2024 10:37:51 +0100 Subject: [PATCH 1/4] chore: add option to display in json format --- src/main.rs | 31 ++++++++++++++++++++++++------- src/manifest.rs | 4 ++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8799830..eca3ac6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,13 @@ use ::clap::Parser; struct Args { #[arg(short, long, default_value_t = false, help = "Output a JSON report")] report: bool, + #[arg( + short, + long, + default_value_t = false, + help = "Display output in JSON format" + )] + json: bool, #[arg(short, long, default_value_t = false, help = "Verbose mode")] verbose: bool, } @@ -20,6 +27,7 @@ mod write_report; use env_logger::{Builder, Target}; use log::{error, info}; +use serde_json::{json, to_string_pretty}; use std::env; use std::io::Write; use std::string::String; @@ -49,13 +57,22 @@ fn main() { write_report::write_json_report(extraneous.clone(), implicit.clone()); } - info!("Extraneous dependencies"); - for dep in extraneous { - info!("{:?}", dep); - } - info!("Implicit dependencies"); - for dep in implicit { - info!("{:?}", dep); + if args.json { + let json_output = json!({ + "extraneous_dependencies": extraneous, + "implicit_dependencies": implicit, + }); + let json_to_print = to_string_pretty(&json_output).unwrap(); + info!("{}", json_to_print); + } else { + info!("Extraneous dependencies"); + for dep in extraneous { + info!("{:?}", dep); + } + info!("Implicit dependencies"); + for dep in implicit { + info!("{:?}", dep); + } } } Err(err) => error!("{:?}", err), diff --git a/src/manifest.rs b/src/manifest.rs index 9a6ae00..d6b8e10 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -1,4 +1,4 @@ -use log::info; +use log::debug; use std::collections::HashMap; use std::collections::HashSet; use std::env; @@ -41,7 +41,7 @@ fn find_closest_parent_manifest(path: &Path) -> Option<&Path> { pub fn read_manifest_dependencies(project_root: PathBuf) -> Result> { let manifest_path = project_root.join("package.json"); - info!("Found manifest path at {}", manifest_path.display()); + debug!("Found manifest path at {}", manifest_path.display()); let raw = fs::read_to_string(manifest_path).expect("Should have been able to read the manifest"); let manifest: Manifest = serde_json::from_str(&raw).expect("Cannot parse manifest"); From ff3e9c300bf31737ba9970ae872235eb737405be Mon Sep 17 00:00:00 2001 From: Thibault Ruby Date: Mon, 29 Jan 2024 12:18:30 +0100 Subject: [PATCH 2/4] chore: prettify output --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/main.rs | 11 +++++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 65a27cd..9b9fb40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,6 +65,15 @@ dependencies = [ "libc", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "anstream" version = "0.6.11" @@ -1140,6 +1149,7 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" name = "robespierre" version = "1.0.0-beta.5" dependencies = [ + "ansi_term", "anyhow", "clap", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 75d9d26..5ea3cd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ansi_term = "0.12.1" anyhow = "^1.0.79" clap = { version = "^4.4.18", features = ["derive"] } env_logger = "~0.11.0" diff --git a/src/main.rs b/src/main.rs index eca3ac6..4e085c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ use serde_json::{json, to_string_pretty}; use std::env; use std::io::Write; use std::string::String; +use ansi_term::Style; fn main() { let args = Args::parse(); @@ -65,13 +66,15 @@ fn main() { let json_to_print = to_string_pretty(&json_output).unwrap(); info!("{}", json_to_print); } else { - info!("Extraneous dependencies"); + info!("{}", Style::new().bold().paint("Extraneous dependencies:")); for dep in extraneous { - info!("{:?}", dep); + info!("├── {}", Style::new().underline().paint(dep)); } - info!("Implicit dependencies"); + info!("{}", Style::new().bold().paint("Implicit dependencies:")); for dep in implicit { - info!("{:?}", dep); + let details = actual_imports_map.get(dep).unwrap(); + info!("├── {}", Style::new().underline().paint(dep)); + info!("│ └── file://{}:{}", details.file, details.line); } } } From fe41ae664e1d6212a713210f21b2d1a9094707be Mon Sep 17 00:00:00 2001 From: Thibault Ruby Date: Mon, 29 Jan 2024 12:22:35 +0100 Subject: [PATCH 3/4] chore: fix fmt --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 4e085c5..77c3e6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,13 +25,13 @@ mod ast_browser; mod manifest; mod write_report; +use ansi_term::Style; use env_logger::{Builder, Target}; use log::{error, info}; use serde_json::{json, to_string_pretty}; use std::env; use std::io::Write; use std::string::String; -use ansi_term::Style; fn main() { let args = Args::parse(); From cf71bd69a05914a7d4c014c032edbb668b78ef75 Mon Sep 17 00:00:00 2001 From: Thibault Ruby Date: Mon, 29 Jan 2024 14:21:51 +0100 Subject: [PATCH 4/4] chore: small refacto --- src/main.rs | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 77c3e6d..9e7640c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use ::clap::Parser; +use std::collections::HashMap; #[derive(Parser)] #[command( author, @@ -25,6 +26,7 @@ mod ast_browser; mod manifest; mod write_report; +use crate::ast_browser::ImportStatement; use ansi_term::Style; use env_logger::{Builder, Target}; use log::{error, info}; @@ -59,29 +61,41 @@ fn main() { } if args.json { - let json_output = json!({ - "extraneous_dependencies": extraneous, - "implicit_dependencies": implicit, - }); - let json_to_print = to_string_pretty(&json_output).unwrap(); - info!("{}", json_to_print); + print_result_in_json_format(extraneous, implicit) } else { - info!("{}", Style::new().bold().paint("Extraneous dependencies:")); - for dep in extraneous { - info!("├── {}", Style::new().underline().paint(dep)); - } - info!("{}", Style::new().bold().paint("Implicit dependencies:")); - for dep in implicit { - let details = actual_imports_map.get(dep).unwrap(); - info!("├── {}", Style::new().underline().paint(dep)); - info!("│ └── file://{}:{}", details.file, details.line); - } + print_result(actual_imports_map, extraneous, implicit); } } Err(err) => error!("{:?}", err), } } +fn print_result_in_json_format(extraneous: Vec<&String>, implicit: Vec<&String>) { + let json_output = json!({ + "extraneous_dependencies": extraneous, + "implicit_dependencies": implicit, + }); + let json_to_print = to_string_pretty(&json_output).unwrap(); + info!("{}", json_to_print); +} + +fn print_result( + actual_imports_map: HashMap, + extraneous: Vec<&String>, + implicit: Vec<&String>, +) { + info!("{}", Style::new().bold().paint("Extraneous dependencies:")); + for dep in extraneous { + info!("├── {}", Style::new().underline().paint(dep)); + } + info!("{}", Style::new().bold().paint("Implicit dependencies:")); + for dep in implicit { + let details = actual_imports_map.get(dep).unwrap(); + info!("├── {}", Style::new().underline().paint(dep)); + info!("│ └── file://{}:{}", details.file, details.line); + } +} + fn configure_logging(args: &Args) { let mut logging_builder = Builder::from_default_env();