Skip to content

Commit

Permalink
List included recipes in load order (#1745)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 25, 2023
1 parent 2cff91c commit 92bae08
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -101,6 +103,7 @@ impl<'src> Analyzer<'src> {
}),
aliases,
assignments: self.assignments,
loaded,
recipes,
settings,
warnings,
Expand Down
6 changes: 4 additions & 2 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand All @@ -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,
Expand All @@ -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)
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions tests/includes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 92bae08

Please sign in to comment.