Skip to content

Commit 2e1ab3e

Browse files
committed
refactor run
1 parent f222b02 commit 2e1ab3e

File tree

2 files changed

+62
-81
lines changed

2 files changed

+62
-81
lines changed

src/search.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ impl Search {
3434
paths
3535
}
3636

37+
/// Attempt to find a justfile given the search configuration and invocation directory
3738
pub(crate) fn find(
3839
search_config: &SearchConfig,
3940
invocation_directory: &Path,
4041
) -> SearchResult<Self> {
4142
match search_config {
42-
SearchConfig::FromInvocationDirectory => Self::find_next(invocation_directory),
43+
SearchConfig::FromInvocationDirectory => Self::find_in_directory(invocation_directory),
4344
SearchConfig::FromSearchDirectory { search_directory } => {
4445
let search_directory = Self::clean(invocation_directory, search_directory);
4546
let justfile = Self::justfile(&search_directory)?;
@@ -75,7 +76,20 @@ impl Search {
7576
}
7677
}
7778

78-
pub(crate) fn find_next(starting_dir: &Path) -> SearchResult<Self> {
79+
/// Look for a justfile starting from the parent directory to the current justfile
80+
pub(crate) fn search_parent_directory(&self) -> SearchResult<Self> {
81+
let parent_dir = self
82+
.justfile
83+
.parent()
84+
.and_then(|p| p.parent())
85+
.ok_or_else(|| SearchError::JustfileHadNoParent {
86+
path: self.justfile.clone(),
87+
})?;
88+
Self::find_in_directory(parent_dir)
89+
}
90+
91+
/// Find a justfile starting in the given directory and searching upwards in the directory tree
92+
fn find_in_directory(starting_dir: &Path) -> SearchResult<Self> {
7993
let justfile = Self::justfile(starting_dir)?;
8094
let working_directory = Self::working_directory_from_justfile(&justfile)?;
8195
Ok(Self {

src/subcommand.rs

+46-79
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ impl Subcommand {
5353
Completions { shell } => return Self::completions(*shell),
5454
Init => return Self::init(config),
5555
Man => return Self::man(),
56-
Run {
57-
arguments,
58-
overrides,
59-
} => return Self::run(config, loader, arguments, overrides),
6056
_ => {}
6157
}
6258

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

7268
match self {
69+
Run {
70+
arguments,
71+
overrides,
72+
} => Self::run(config, loader, search, arguments, overrides)?,
7373
Choose { overrides, chooser } => {
7474
Self::choose(config, justfile, &search, overrides, chooser.as_deref())?;
7575
}
@@ -83,7 +83,7 @@ impl Subcommand {
8383
Show { path } => Self::show(config, justfile, path)?,
8484
Summary => Self::summary(config, justfile),
8585
Variables => Self::variables(justfile),
86-
Changelog | Completions { .. } | Edit | Init | Man | Run { .. } => unreachable!(),
86+
Changelog | Completions { .. } | Edit | Init | Man => unreachable!(),
8787
}
8888

8989
Ok(())
@@ -99,90 +99,57 @@ impl Subcommand {
9999
fn run<'src>(
100100
config: &Config,
101101
loader: &'src Loader,
102+
initial_search: Search,
102103
arguments: &[String],
103104
overrides: &BTreeMap<String, String>,
104105
) -> RunResult<'src> {
105-
if matches!(
106-
config.search_config,
107-
SearchConfig::FromInvocationDirectory | SearchConfig::FromSearchDirectory { .. }
108-
) {
109-
let starting_path = match &config.search_config {
110-
SearchConfig::FromInvocationDirectory => config.invocation_directory.clone(),
111-
SearchConfig::FromSearchDirectory { search_directory } => config
112-
.invocation_directory
113-
.join(search_directory)
114-
.lexiclean(),
115-
_ => unreachable!(),
116-
};
117-
118-
let mut path = starting_path.clone();
119-
120-
let mut unknown_recipes_errors = None;
121-
122-
loop {
123-
let search = match Search::find_next(&path) {
124-
Err(SearchError::NotFound) => match unknown_recipes_errors {
125-
Some(err) => return Err(err),
126-
None => return Err(SearchError::NotFound.into()),
127-
},
128-
Err(err) => return Err(err.into()),
129-
Ok(search) => {
130-
if config.verbosity.loquacious() && path != starting_path {
131-
eprintln!(
132-
"Trying {}",
133-
starting_path
134-
.strip_prefix(path)
135-
.unwrap()
136-
.components()
137-
.map(|_| path::Component::ParentDir)
138-
.collect::<PathBuf>()
139-
.join(search.justfile.file_name().unwrap())
140-
.display()
141-
);
142-
}
143-
search
144-
}
145-
};
146-
147-
match Self::run_inner(config, loader, arguments, overrides, &search) {
148-
Err((err @ (Error::UnknownRecipe { .. } | Error::UnknownSubmodule { .. }), true)) => {
149-
match search.justfile.parent().unwrap().parent() {
150-
Some(parent) => {
151-
unknown_recipes_errors.get_or_insert(err);
152-
path = parent.into();
153-
}
154-
None => return Err(err),
106+
let starting_path = initial_search
107+
.justfile
108+
.parent()
109+
.as_ref()
110+
.unwrap()
111+
.lexiclean();
112+
113+
let mut search = initial_search;
114+
115+
loop {
116+
let compilation = Self::compile(config, loader, &search)?;
117+
let justfile = &compilation.justfile;
118+
let fallback = justfile.settings.fallback
119+
&& matches!(
120+
config.search_config,
121+
SearchConfig::FromInvocationDirectory | SearchConfig::FromSearchDirectory { .. }
122+
);
123+
let run_result = justfile.run(config, &search, overrides, arguments);
124+
125+
match run_result {
126+
Err(err @ (Error::UnknownRecipe { .. } | Error::UnknownSubmodule { .. })) if fallback => {
127+
let new_search = match search.search_parent_directory() {
128+
Ok(s) => s,
129+
Err(_e) => {
130+
return Err(err);
155131
}
132+
};
133+
let p = new_search.justfile.parent().unwrap();
134+
let new_path = starting_path
135+
.strip_prefix(p)
136+
.unwrap()
137+
.components()
138+
.map(|_| path::Component::ParentDir)
139+
.collect::<PathBuf>()
140+
.join(new_search.justfile.file_name().unwrap());
141+
142+
search = new_search;
143+
144+
if config.verbosity.loquacious() {
145+
eprintln!("Trying {}", new_path.display());
156146
}
157-
result => return result.map_err(|(err, _fallback)| err),
158147
}
148+
result => return result,
159149
}
160-
} else {
161-
Self::run_inner(
162-
config,
163-
loader,
164-
arguments,
165-
overrides,
166-
&Search::find(&config.search_config, &config.invocation_directory)?,
167-
)
168-
.map_err(|(err, _fallback)| err)
169150
}
170151
}
171152

172-
fn run_inner<'src>(
173-
config: &Config,
174-
loader: &'src Loader,
175-
arguments: &[String],
176-
overrides: &BTreeMap<String, String>,
177-
search: &Search,
178-
) -> Result<(), (Error<'src>, bool)> {
179-
let compilation = Self::compile(config, loader, search).map_err(|err| (err, false))?;
180-
let justfile = &compilation.justfile;
181-
justfile
182-
.run(config, search, overrides, arguments)
183-
.map_err(|err| (err, justfile.settings.fallback))
184-
}
185-
186153
fn compile<'src>(
187154
config: &Config,
188155
loader: &'src Loader,

0 commit comments

Comments
 (0)