From 6fe4289ae32c3e40a5a034a9e7f622ede91583c9 Mon Sep 17 00:00:00 2001 From: "brady.ouren" Date: Wed, 18 Dec 2024 21:10:36 -0800 Subject: [PATCH] use manifest-path properly --- components/clarinet-cli/src/frontend/cli.rs | 63 ++++++++++++------- .../clarinet-format/src/formatter/mod.rs | 2 +- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/components/clarinet-cli/src/frontend/cli.rs b/components/clarinet-cli/src/frontend/cli.rs index 9b3ab7621..c73b19482 100644 --- a/components/clarinet-cli/src/frontend/cli.rs +++ b/components/clarinet-cli/src/frontend/cli.rs @@ -105,9 +105,8 @@ enum Command { #[derive(Parser, PartialEq, Clone, Debug)] struct Formatter { - /// Path to clarity files (defaults to ./contracts) - #[clap(long = "path", short = 'p')] - pub code_path: Option, + #[clap(long = "manifest-path", short = 'm')] + pub manifest_path: Option, /// If specified, format only this file #[clap(long = "file", short = 'f')] pub file: Option, @@ -1202,7 +1201,7 @@ pub fn main() { } }, Command::Formatter(cmd) => { - let sources = get_source_with_path(cmd.code_path, cmd.file); + let sources = get_sources_to_format(cmd.manifest_path, cmd.file); let mut settings = Settings::default(); if let Some(max_line_length) = cmd.max_line_length { @@ -1243,25 +1242,47 @@ fn overwrite_formatted(file_path: &String, output: String) -> io::Result<()> { Ok(()) } -fn get_source_with_path(code_path: Option, file: Option) -> Vec<(String, String)> { - // look for files at the default code path (./contracts/) if - // cmd.code_path is not specified OR if cmd.file is not specified - let path = code_path.unwrap_or_else(|| "./contracts/".to_string()); - - // Collect file paths and load source code +fn from_code_source(src: ClarityCodeSource) -> String { + match src { + ClarityCodeSource::ContractOnDisk(path_buf) => { + path_buf.as_path().to_str().unwrap().to_owned() + } + _ => panic!("invalid code source"), // TODO + } +} +// look for files at the default code path (./contracts/) if +// cmd.manifest_path is not specified OR if cmd.file is not specified +fn get_sources_from_manifest(manifest_path: Option) -> Vec { + let manifest = load_manifest_or_warn(manifest_path); + match manifest { + Some(manifest_path) => { + let contracts = manifest_path.contracts.values().cloned(); + contracts.map(|c| from_code_source(c.code_source)).collect() + } + None => { + println!("here"); + let path = "./contracts/".to_string(); + match fs::read_dir(&path) { + Ok(entries) => entries + .filter_map(Result::ok) + .filter(|entry| entry.path().is_file()) + .map(|entry| entry.path().to_string_lossy().into_owned()) + .collect(), + Err(message) => { + eprintln!("{}", format_err!(message)); + std::process::exit(1) + } + } + } + } +} +fn get_sources_to_format( + manifest_path: Option, + file: Option, +) -> Vec<(String, String)> { let files: Vec = match file { Some(file_name) => vec![format!("{}", file_name)], - None => match fs::read_dir(&path) { - Ok(entries) => entries - .filter_map(Result::ok) - .filter(|entry| entry.path().is_file()) - .map(|entry| entry.path().to_string_lossy().into_owned()) - .collect(), - Err(message) => { - eprintln!("{}", format_err!(message)); - std::process::exit(1) - } - }, + None => get_sources_from_manifest(manifest_path), }; // Map each file to its source code files diff --git a/components/clarinet-format/src/formatter/mod.rs b/components/clarinet-format/src/formatter/mod.rs index 0ce6340f6..e09324762 100644 --- a/components/clarinet-format/src/formatter/mod.rs +++ b/components/clarinet-format/src/formatter/mod.rs @@ -71,7 +71,7 @@ pub fn format_source_exprs( acc: &str, ) -> String { // print_pre_expr(expressions); - println!("exprs: {:?}", expressions); + // println!("exprs: {:?}", expressions); // println!("previous: {:?}", previous_expr); if let Some((expr, remaining)) = expressions.split_first() { let cur = display_pse(