From 6160ee7198118db0faae1333a2fc48e2a6a8b2ca Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 26 Aug 2024 19:55:53 -0700 Subject: [PATCH 1/6] Refactor Subcommand::execute --- src/subcommand.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index cbb53d89fa..83cce35f6c 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -68,18 +68,16 @@ impl Subcommand { let compilation = Self::compile(config, loader, &search)?; let justfile = &compilation.justfile; - let ast = compilation.root_ast(); - let src = compilation.root_src(); match self { Choose { overrides, chooser } => { - Self::choose(config, justfile, &search, overrides, chooser.as_deref())?; + Self::choose(config, justfile, &search, overrides, chooser.as_deref())? } Command { overrides, .. } | Evaluate { overrides, .. } => { - justfile.run(config, &search, overrides, &[])?; + justfile.run(config, &search, overrides, &[])? } - Dump => Self::dump(config, ast, justfile)?, - Format => Self::format(config, &search, src, ast, justfile)?, + Dump => Self::dump(config, compilation)?, + Format => Self::format(config, &search, compilation)?, Groups => Self::groups(config, justfile), List { path } => Self::list(config, justfile, path)?, Show { path } => Self::show(config, justfile, path)?, @@ -303,14 +301,14 @@ impl Subcommand { Ok(()) } - fn dump(config: &Config, ast: &Ast, justfile: &Justfile) -> RunResult<'static> { + fn dump(config: &Config, compilation: Compilation) -> RunResult<'static> { match config.dump_format { DumpFormat::Json => { - serde_json::to_writer(io::stdout(), justfile) + serde_json::to_writer(io::stdout(), &compilation.justfile) .map_err(|serde_json_error| Error::DumpJson { serde_json_error })?; println!(); } - DumpFormat::Just => print!("{ast}"), + DumpFormat::Just => print!("{}", compilation.root_ast()), } Ok(()) } @@ -337,13 +335,11 @@ impl Subcommand { Ok(()) } - fn format( - config: &Config, - search: &Search, - src: &str, - ast: &Ast, - justfile: &Justfile, - ) -> RunResult<'static> { + fn format(config: &Config, search: &Search, compilation: Compilation) -> RunResult<'static> { + let justfile = &compilation.justfile; + let src = compilation.root_src(); + let ast = compilation.root_ast(); + config.require_unstable(justfile, UnstableFeature::FormatSubcommand)?; let formatted = ast.to_string(); From 1ed06417da944e516cf6d510bc0dadb0d91ad9f2 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 29 Aug 2024 04:37:50 -0700 Subject: [PATCH 2/6] needn't be public --- src/search.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/search.rs b/src/search.rs index 4d061e0f55..edd426be45 100644 --- a/src/search.rs +++ b/src/search.rs @@ -125,7 +125,7 @@ impl Search { } } - pub(crate) fn justfile(directory: &Path) -> SearchResult { + fn justfile(directory: &Path) -> SearchResult { for directory in directory.ancestors() { let mut candidates = BTreeSet::new(); From 29a12891e3aecc7015be78b9d24d8eff9c49bb75 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 30 Aug 2024 00:06:16 -0700 Subject: [PATCH 3/6] Clarify flow --- src/search.rs | 35 ++++++++++++++++------------------- src/subcommand.rs | 21 +++++++++++---------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/search.rs b/src/search.rs index edd426be45..bf671159d5 100644 --- a/src/search.rs +++ b/src/search.rs @@ -84,45 +84,42 @@ impl Search { }) } + /// Find a path for a newly-initialized default justfile pub(crate) fn init( search_config: &SearchConfig, invocation_directory: &Path, ) -> SearchResult { - match search_config { + let (working_directory, justfile) = match search_config { SearchConfig::FromInvocationDirectory => { let working_directory = Self::project_root(invocation_directory)?; let justfile = working_directory.join(DEFAULT_JUSTFILE_NAME); - Ok(Self { - justfile, - working_directory, - }) + (working_directory, justfile) } SearchConfig::FromSearchDirectory { search_directory } => { let search_directory = Self::clean(invocation_directory, search_directory); let working_directory = Self::project_root(&search_directory)?; let justfile = working_directory.join(DEFAULT_JUSTFILE_NAME); - Ok(Self { - justfile, - working_directory, - }) + (working_directory, justfile) } - SearchConfig::GlobalJustfile => Err(SearchError::GlobalJustfileInit), + SearchConfig::GlobalJustfile => return Err(SearchError::GlobalJustfileInit), SearchConfig::WithJustfile { justfile } => { let justfile = Self::clean(invocation_directory, justfile); let working_directory = Self::working_directory_from_justfile(&justfile)?; - Ok(Self { - justfile, - working_directory, - }) + (working_directory, justfile) } SearchConfig::WithJustfileAndWorkingDirectory { justfile, working_directory, - } => Ok(Self { - justfile: Self::clean(invocation_directory, justfile), - working_directory: Self::clean(invocation_directory, working_directory), - }), - } + } => ( + Self::clean(invocation_directory, working_directory), + Self::clean(invocation_directory, justfile), + ), + }; + + Ok(Self { + justfile, + working_directory, + }) } fn justfile(directory: &Path) -> SearchResult { diff --git a/src/subcommand.rs b/src/subcommand.rs index 83cce35f6c..40a6bc57d9 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -388,20 +388,21 @@ impl Subcommand { let search = Search::init(&config.search_config, &config.invocation_directory)?; if search.justfile.is_file() { - Err(Error::InitExists { + return Err(Error::InitExists { justfile: search.justfile, - }) - } else if let Err(io_error) = fs::write(&search.justfile, INIT_JUSTFILE) { - Err(Error::WriteJustfile { + }); + } + + if let Err(io_error) = fs::write(&search.justfile, INIT_JUSTFILE) { + return Err(Error::WriteJustfile { justfile: search.justfile, io_error, - }) - } else { - if config.verbosity.loud() { - eprintln!("Wrote justfile to `{}`", search.justfile.display()); - } - Ok(()) + }); + } + if config.verbosity.loud() { + eprintln!("Wrote justfile to `{}`", search.justfile.display()); } + Ok(()) } fn man() -> RunResult<'static> { From 887e0ee8c19a5d2c0354edda8317aef2bd56f851 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 30 Aug 2024 00:59:28 -0700 Subject: [PATCH 4/6] Edits --- src/search.rs | 4 ++++ src/subcommand.rs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/search.rs b/src/search.rs index bf671159d5..e609c69d8a 100644 --- a/src/search.rs +++ b/src/search.rs @@ -122,6 +122,8 @@ impl Search { }) } + /// Starting from a directory, search upwards in the directory tree until a file + /// whose name matches a `JUSTFILE_NAMES` entry is found fn justfile(directory: &Path) -> SearchResult { for directory in directory.ancestors() { let mut candidates = BTreeSet::new(); @@ -172,6 +174,8 @@ impl Search { clean.into_iter().collect() } + /// Starting from a subdirectory, attempt to find the root directory of a software project, as determined by the presence + /// of one of the version-control-system dotfiles specified in `PROJECT_ROOT_CHILDREN` fn project_root(directory: &Path) -> SearchResult { for directory in directory.ancestors() { let entries = fs::read_dir(directory).map_err(|io_error| SearchError::Io { diff --git a/src/subcommand.rs b/src/subcommand.rs index 40a6bc57d9..cc20dff8f7 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -71,10 +71,10 @@ impl Subcommand { match self { Choose { overrides, chooser } => { - Self::choose(config, justfile, &search, overrides, chooser.as_deref())? + Self::choose(config, justfile, &search, overrides, chooser.as_deref())?; } Command { overrides, .. } | Evaluate { overrides, .. } => { - justfile.run(config, &search, overrides, &[])? + justfile.run(config, &search, overrides, &[])?; } Dump => Self::dump(config, compilation)?, Format => Self::format(config, &search, compilation)?, From 2d8be1de179a41cc48bf79ef444361930a9f7236 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 31 Aug 2024 12:24:22 -0700 Subject: [PATCH 5/6] Tweak --- src/search.rs | 45 +++++++++++++++++++++++++-------------------- src/subcommand.rs | 2 ++ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/search.rs b/src/search.rs index e609c69d8a..8fa058dc4b 100644 --- a/src/search.rs +++ b/src/search.rs @@ -84,46 +84,50 @@ impl Search { }) } - /// Find a path for a newly-initialized default justfile + /// Get working directory and justfile path for newly-initialized justfile pub(crate) fn init( search_config: &SearchConfig, invocation_directory: &Path, ) -> SearchResult { - let (working_directory, justfile) = match search_config { + match search_config { SearchConfig::FromInvocationDirectory => { let working_directory = Self::project_root(invocation_directory)?; let justfile = working_directory.join(DEFAULT_JUSTFILE_NAME); - (working_directory, justfile) + Ok(Self { + justfile, + working_directory, + }) } SearchConfig::FromSearchDirectory { search_directory } => { let search_directory = Self::clean(invocation_directory, search_directory); let working_directory = Self::project_root(&search_directory)?; let justfile = working_directory.join(DEFAULT_JUSTFILE_NAME); - (working_directory, justfile) + Ok(Self { + justfile, + working_directory, + }) } - SearchConfig::GlobalJustfile => return Err(SearchError::GlobalJustfileInit), + SearchConfig::GlobalJustfile => Err(SearchError::GlobalJustfileInit), SearchConfig::WithJustfile { justfile } => { let justfile = Self::clean(invocation_directory, justfile); let working_directory = Self::working_directory_from_justfile(&justfile)?; - (working_directory, justfile) + Ok(Self { + justfile, + working_directory, + }) } SearchConfig::WithJustfileAndWorkingDirectory { justfile, working_directory, - } => ( - Self::clean(invocation_directory, working_directory), - Self::clean(invocation_directory, justfile), - ), - }; - - Ok(Self { - justfile, - working_directory, - }) + } => Ok(Self { + justfile: Self::clean(invocation_directory, working_directory), + working_directory: Self::clean(invocation_directory, justfile), + }), + } } - /// Starting from a directory, search upwards in the directory tree until a file - /// whose name matches a `JUSTFILE_NAMES` entry is found + /// Search upwards from `directory` for a file whose name matches one of + /// `JUSTFILE_NAMES` fn justfile(directory: &Path) -> SearchResult { for directory in directory.ancestors() { let mut candidates = BTreeSet::new(); @@ -174,8 +178,9 @@ impl Search { clean.into_iter().collect() } - /// Starting from a subdirectory, attempt to find the root directory of a software project, as determined by the presence - /// of one of the version-control-system dotfiles specified in `PROJECT_ROOT_CHILDREN` + /// Search upwards from `directory` for the root directory of a software + /// project, as determined by the presence of one of the version control + /// system directories given in `PROJECT_ROOT_CHILDREN` fn project_root(directory: &Path) -> SearchResult { for directory in directory.ancestors() { let entries = fs::read_dir(directory).map_err(|io_error| SearchError::Io { diff --git a/src/subcommand.rs b/src/subcommand.rs index cc20dff8f7..449d4a0920 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -399,9 +399,11 @@ impl Subcommand { io_error, }); } + if config.verbosity.loud() { eprintln!("Wrote justfile to `{}`", search.justfile.display()); } + Ok(()) } From 22c9332918ea48b1244236347bfcc164de05b06e Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 31 Aug 2024 12:25:24 -0700 Subject: [PATCH 6/6] Modify --- src/search.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/search.rs b/src/search.rs index 8fa058dc4b..01dcdacff9 100644 --- a/src/search.rs +++ b/src/search.rs @@ -120,8 +120,8 @@ impl Search { justfile, working_directory, } => Ok(Self { - justfile: Self::clean(invocation_directory, working_directory), - working_directory: Self::clean(invocation_directory, justfile), + justfile: Self::clean(invocation_directory, justfile), + working_directory: Self::clean(invocation_directory, working_directory), }), } }