From 74bc8d965581ddafaf455e5da78fe757ab003c61 Mon Sep 17 00:00:00 2001 From: C J Silverio Date: Wed, 31 Aug 2022 17:06:49 -0700 Subject: [PATCH 1/3] Proof of concept: an include feature for justfiles. (#1) * Proof of concept: an include feature for justfiles. A fast proof that it's straightforward to do a no-fancy-parsing include feature. This design takes all lines that look like this: `@include "relative/path/to/file"` and replaces them with the contents of the pointed-to file. The resulting buffer is passed along to be parsed and executed as before. That's it: no attempt to look at the file or do anything with the contents. This is pure concatenation. * Satisfy clippy. Co-Authored-By: kevin@seaplane.io --- examples/included.just | 16 ++++++++++++++++ examples/parent.just | 16 ++++++++++++++++ src/loader.rs | 30 +++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 examples/included.just create mode 100644 examples/parent.just diff --git a/examples/included.just b/examples/included.just new file mode 100644 index 0000000000..9d48d89369 --- /dev/null +++ b/examples/included.just @@ -0,0 +1,16 @@ +# This file gets included. + +# ANSI color escapes. +BOLD := '\e[1m' +RESET := '\e[0m' +RED := '\e[31m' +GREEN := '\e[32m' +BROWN := '\e[34m' +BLUE := '\e[34m' +PURPLE := '\e[35m' +CYAN := '\e[36m' +GRAY := '\e[37m' + +# Report the animal with the best blep. +blep: + @printf "{{BOLD}}Cat{{RESET}} {{BLUE}}blep{{RESET}} is best." diff --git a/examples/parent.just b/examples/parent.just new file mode 100644 index 0000000000..2cc7059e5c --- /dev/null +++ b/examples/parent.just @@ -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 + diff --git a/src/loader.rs b/src/loader.rs index 95fa7c34dd..b3fd5b27cd 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -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)) } } From 0225f8b5939013b81f03e2839f3d0c51c0e8e15f Mon Sep 17 00:00:00 2001 From: Kevin K Date: Sat, 26 Nov 2022 08:47:47 -0500 Subject: [PATCH 2/3] sort example variables Co-authored-by: Mihai Galos --- examples/included.just | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/included.just b/examples/included.just index 9d48d89369..f182e40785 100644 --- a/examples/included.just +++ b/examples/included.just @@ -1,15 +1,15 @@ # This file gets included. # ANSI color escapes. +BLUE := '\e[34m' BOLD := '\e[1m' -RESET := '\e[0m' -RED := '\e[31m' -GREEN := '\e[32m' BROWN := '\e[34m' -BLUE := '\e[34m' -PURPLE := '\e[35m' 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: From 54b47fbd8264b12e2dcde824ea4f6e9b6ac726c0 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Sat, 26 Nov 2022 08:56:29 -0500 Subject: [PATCH 3/3] change '#include' -> '@include' --- examples/parent.just | 2 +- src/loader.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/parent.just b/examples/parent.just index 2cc7059e5c..2288bf6444 100644 --- a/examples/parent.just +++ b/examples/parent.just @@ -1,4 +1,4 @@ -#include "included.just" +@include "included.just" # The recipes in this file rely on variables defined in the # included file! diff --git a/src/loader.rs b/src/loader.rs index b3fd5b27cd..add4dd9c06 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -19,8 +19,8 @@ impl Loader { // Parse include directives. // Include directives are lines that look like: - // #include "relative/path/to/file" - let include_regexp = Regex::new(r#"^#include "([^"]+)"$"#).unwrap(); + // @include "relative/path/to/file" + let include_regexp = Regex::new(r#"^@include "([^"]+)"$"#).unwrap(); let parent = path.parent().map_or_else(|| Path::new(""), |p| p);