List included recipes in load order (#1745)

This commit is contained in:
Casey Rodarmor 2023-11-24 23:15:41 -08:00 committed by GitHub
parent 2cff91cee2
commit 92bae080ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 5 deletions

View File

@ -9,15 +9,17 @@ pub(crate) struct Analyzer<'src> {
impl<'src> Analyzer<'src> {
pub(crate) fn analyze(
loaded: Vec<PathBuf>,
paths: &HashMap<PathBuf, PathBuf>,
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
) -> CompileResult<'src, Justfile<'src>> {
Analyzer::default().justfile(paths, asts, root)
Analyzer::default().justfile(loaded, paths, asts, root)
}
fn justfile(
mut self,
loaded: Vec<PathBuf>,
paths: &HashMap<PathBuf, PathBuf>,
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
@ -101,6 +103,7 @@ impl<'src> Analyzer<'src> {
}),
aliases,
assignments: self.assignments,
loaded,
recipes,
settings,
warnings,

View File

@ -11,12 +11,14 @@ impl Compiler {
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
let mut srcs: HashMap<PathBuf, &str> = HashMap::new();
let mut loaded = Vec::new();
let mut stack: Vec<PathBuf> = Vec::new();
stack.push(root.into());
while let Some(current) = stack.pop() {
let (relative, src) = loader.load(root, &current)?;
loaded.push(relative.into());
let tokens = Lexer::lex(relative, src)?;
let mut ast = Parser::parse(&tokens)?;
@ -42,7 +44,7 @@ impl Compiler {
asts.insert(current.clone(), ast.clone());
}
let justfile = Analyzer::analyze(&paths, &asts, root)?;
let justfile = Analyzer::analyze(loaded, &paths, &asts, root)?;
Ok(Compilation {
asts,
@ -61,7 +63,7 @@ impl Compiler {
asts.insert(root.clone(), ast);
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
paths.insert(root.clone(), root.clone());
Analyzer::analyze(&paths, &asts, &root)
Analyzer::analyze(Vec::new(), &paths, &asts, &root)
}
}

View File

@ -6,6 +6,8 @@ pub(crate) struct Justfile<'src> {
pub(crate) assignments: Table<'src, Assignment<'src>>,
#[serde(rename = "first", serialize_with = "keyed::serialize_option")]
pub(crate) default: Option<Rc<Recipe<'src>>>,
#[serde(skip)]
pub(crate) loaded: Vec<PathBuf>,
pub(crate) recipes: Table<'src, Rc<Recipe<'src>>>,
pub(crate) settings: Settings<'src>,
pub(crate) warnings: Vec<Warning>,
@ -365,7 +367,16 @@ impl<'src> Justfile<'src> {
.collect::<Vec<&Recipe<Dependency>>>();
if source_order {
recipes.sort_by_key(|recipe| recipe.name.offset);
recipes.sort_by_key(|recipe| {
(
self
.loaded
.iter()
.position(|path| path == recipe.name.path)
.unwrap(),
recipe.name.offset,
)
});
}
recipes

View File

@ -3,6 +3,7 @@
clippy::enum_glob_use,
clippy::let_underscore_untyped,
clippy::needless_pass_by_value,
clippy::similar_names,
clippy::too_many_lines,
clippy::unnecessary_wraps,
clippy::wildcard_imports

View File

@ -68,7 +68,7 @@ pub(crate) fn analysis_error(
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
paths.insert("justfile".into(), "justfile".into());
match Analyzer::analyze(&paths, &asts, &root) {
match Analyzer::analyze(Vec::new(), &paths, &asts, &root) {
Ok(_) => panic!("Analysis unexpectedly succeeded"),
Err(have) => {
let want = CompileError {

View File

@ -120,3 +120,25 @@ fn include_recipes_are_not_default() {
.stderr("error: Justfile contains no default recipe.\n")
.run();
}
#[test]
fn listed_recipes_in_includes_are_in_load_order() {
Test::new()
.justfile(
"
!include ./include.justfile
foo:
",
)
.write("include.justfile", "bar:")
.args(["--list", "--unstable", "--unsorted"])
.test_round_trip(false)
.stdout(
"
Available recipes:
foo
bar
",
)
.run();
}