Skip to content

Commit

Permalink
use manifest-path properly
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Dec 19, 2024
1 parent feafe83 commit 4d6d022
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
56 changes: 34 additions & 22 deletions components/clarinet-cli/src/frontend/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
#[clap(long = "manifest-path", short = 'm')]
pub manifest_path: Option<String>,
/// If specified, format only this file
#[clap(long = "file", short = 'f')]
pub file: Option<String>,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1243,32 +1242,45 @@ fn overwrite_formatted(file_path: &String, output: String) -> io::Result<()> {
Ok(())
}

fn get_source_with_path(code_path: Option<String>, file: Option<String>) -> 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<String>) -> Vec<String> {
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 => {
// TODO this should probably just panic or fail gracefully because
// if the manifest isn't specified or found at the default location
// we can't do much
vec![]
}
}
}
fn get_sources_to_format(
manifest_path: Option<String>,
file: Option<String>,
) -> Vec<(String, String)> {
let files: Vec<String> = 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
.into_iter()
.map(|file_path| {
let source = fs::read_to_string(&file_path)
.unwrap_or_else(|_| "// Failed to read file".to_string());
.unwrap_or_else(|_| "Failed to read file".to_string());
(file_path, source)
})
.collect()
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4d6d022

Please sign in to comment.