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

an include feature for justfiles #1420

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 16 additions & 0 deletions examples/included.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file gets included.

# ANSI color escapes.
BLUE := '\e[34m'
BOLD := '\e[1m'
BROWN := '\e[34m'
CYAN := '\e[36m'
GRAY := '\e[37m'
GREEN := '\e[32m'
PURPLE := '\e[35m'
RED := '\e[31m'
RESET := '\e[0m'

# Report the animal with the best blep.
blep:
@printf "{{BOLD}}Cat{{RESET}} {{BLUE}}blep{{RESET}} is best."
16 changes: 16 additions & 0 deletions examples/parent.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@include "included.just"

# The recipes in this file rely on variables defined in the
# included file!

# Report the animal with the best sploot.
sploot:
@printf "{{BOLD}}Corgi{{RESET}} {{BLUE}}sploot{{RESET}} is best."

# Report the animal with the highest cuteness.
cute:
@printf "{{BOLD}}The baby sea otter{{RESET}} is, at 1.0 otters, the animal rated as most cute."

help:
@just -l

30 changes: 29 additions & 1 deletion src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ impl Loader {
path: path.to_owned(),
io_error,
})?;
Ok(self.arena.alloc(src))

// Parse include directives.
// Include directives are lines that look like:
// @include "relative/path/to/file"
let include_regexp = Regex::new(r#"^@include "([^"]+)"$"#).unwrap();

let parent = path.parent().map_or_else(|| Path::new(""), |p| p);

let mut buf = String::new();
let lines = src.lines();
for line in lines {
if let Some(captures) = include_regexp.captures(line) {
// safe because we don't get here without a match
let filename = captures.get(1).unwrap().as_str();
let mut include_path = PathBuf::from(&parent);
include_path.push(filename);

let contents = fs::read_to_string(&include_path).map_err(|io_error| Error::Load {
path: include_path.clone(),
io_error,
})?;
buf.push_str(&contents);
} else {
buf.push_str(line);
}
buf.push('\n');
}

Ok(self.arena.alloc(buf))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file currently adds functionality and requires manual validation.
It is missing a minimal automatic test, can you please add one?

}
}