Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Subcommand::run #2336

Merged
merged 11 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ impl Search {
paths
}

/// Find justfile given search configuration and invocation directory
pub(crate) fn find(
search_config: &SearchConfig,
invocation_directory: &Path,
) -> SearchResult<Self> {
match search_config {
SearchConfig::FromInvocationDirectory => Self::find_next(invocation_directory),
SearchConfig::FromInvocationDirectory => Self::find_in_directory(invocation_directory),
SearchConfig::FromSearchDirectory { search_directory } => {
let search_directory = Self::clean(invocation_directory, search_directory);
let justfile = Self::justfile(&search_directory)?;
Expand Down Expand Up @@ -75,7 +76,20 @@ impl Search {
}
}

pub(crate) fn find_next(starting_dir: &Path) -> SearchResult<Self> {
/// Find justfile starting from parent directory of current justfile
pub(crate) fn search_parent_directory(&self) -> SearchResult<Self> {
let parent = self
.justfile
.parent()
.and_then(|path| path.parent())
.ok_or_else(|| SearchError::JustfileHadNoParent {
path: self.justfile.clone(),
})?;
Self::find_in_directory(parent)
}

/// Find justfile starting in given directory searching upwards in directory tree
fn find_in_directory(starting_dir: &Path) -> SearchResult<Self> {
let justfile = Self::justfile(starting_dir)?;
let working_directory = Self::working_directory_from_justfile(&justfile)?;
Ok(Self {
Expand Down
119 changes: 39 additions & 80 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ impl Subcommand {
Completions { shell } => return Self::completions(*shell),
Init => return Self::init(config),
Man => return Self::man(),
Run {
arguments,
overrides,
} => return Self::run(config, loader, arguments, overrides),
_ => {}
}

Expand All @@ -70,6 +66,10 @@ impl Subcommand {
let justfile = &compilation.justfile;

match self {
Run {
arguments,
overrides,
} => Self::run(config, loader, search, compilation, arguments, overrides)?,
Choose { overrides, chooser } => {
Self::choose(config, justfile, &search, overrides, chooser.as_deref())?;
}
Expand All @@ -83,7 +83,7 @@ impl Subcommand {
Show { path } => Self::show(config, justfile, path)?,
Summary => Self::summary(config, justfile),
Variables => Self::variables(justfile),
Changelog | Completions { .. } | Edit | Init | Man | Run { .. } => unreachable!(),
Changelog | Completions { .. } | Edit | Init | Man => unreachable!(),
}

Ok(())
Expand All @@ -99,88 +99,47 @@ impl Subcommand {
fn run<'src>(
config: &Config,
loader: &'src Loader,
mut search: Search,
mut compilation: Compilation<'src>,
arguments: &[String],
overrides: &BTreeMap<String, String>,
) -> RunResult<'src> {
if matches!(
config.search_config,
SearchConfig::FromInvocationDirectory | SearchConfig::FromSearchDirectory { .. }
) {
let starting_path = match &config.search_config {
SearchConfig::FromInvocationDirectory => config.invocation_directory.clone(),
SearchConfig::FromSearchDirectory { search_directory } => config
.invocation_directory
.join(search_directory)
.lexiclean(),
_ => unreachable!(),
};

let mut path = starting_path.clone();

let mut unknown_recipes_errors = None;

loop {
let search = match Search::find_next(&path) {
Err(SearchError::NotFound) => match unknown_recipes_errors {
Some(err) => return Err(err),
None => return Err(SearchError::NotFound.into()),
},
Err(err) => return Err(err.into()),
Ok(search) => {
if config.verbosity.loquacious() && path != starting_path {
eprintln!(
"Trying {}",
starting_path
.strip_prefix(path)
.unwrap()
.components()
.map(|_| path::Component::ParentDir)
.collect::<PathBuf>()
.join(search.justfile.file_name().unwrap())
.display()
);
}
search
let starting_parent = search.justfile.parent().as_ref().unwrap().lexiclean();

loop {
let justfile = &compilation.justfile;
let fallback = justfile.settings.fallback
&& matches!(
config.search_config,
SearchConfig::FromInvocationDirectory | SearchConfig::FromSearchDirectory { .. }
);

let result = justfile.run(config, &search, overrides, arguments);

if fallback {
if let Err(err @ (Error::UnknownRecipe { .. } | Error::UnknownSubmodule { .. })) = result {
search = search.search_parent_directory().map_err(|_| err)?;

let new_parent = starting_parent
.strip_prefix(search.justfile.parent().unwrap())
.unwrap()
.components()
.map(|_| path::Component::ParentDir)
.collect::<PathBuf>()
.join(search.justfile.file_name().unwrap());

if config.verbosity.loquacious() {
eprintln!("Trying {}", new_parent.display());
}
};

match Self::run_inner(config, loader, arguments, overrides, &search) {
Err((err @ (Error::UnknownRecipe { .. } | Error::UnknownSubmodule { .. }), true)) => {
match search.justfile.parent().unwrap().parent() {
Some(parent) => {
unknown_recipes_errors.get_or_insert(err);
path = parent.into();
}
None => return Err(err),
}
}
result => return result.map_err(|(err, _fallback)| err),

compilation = Self::compile(config, loader, &search)?;

continue;
}
}
} else {
Self::run_inner(
config,
loader,
arguments,
overrides,
&Search::find(&config.search_config, &config.invocation_directory)?,
)
.map_err(|(err, _fallback)| err)
}
}

fn run_inner<'src>(
config: &Config,
loader: &'src Loader,
arguments: &[String],
overrides: &BTreeMap<String, String>,
search: &Search,
) -> Result<(), (Error<'src>, bool)> {
let compilation = Self::compile(config, loader, search).map_err(|err| (err, false))?;
let justfile = &compilation.justfile;
justfile
.run(config, search, overrides, arguments)
.map_err(|err| (err, justfile.settings.fallback))
return result;
}
}

fn compile<'src>(
Expand Down