Skip to content

Commit

Permalink
Support defining external justfile locations through JUSTFILE_PATH en…
Browse files Browse the repository at this point in the history
…v var

This extends just so you can define multiple locations from which it tries
to import other justfiles. This allows, for example, to put shared justfiles
in a user defined global location once, rather than having to manage
duplicates in every repo they are used in or deal with symlinks.
  • Loading branch information
pprkut committed Jan 15, 2024
1 parent 22d462b commit 1ca493e
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::*;
use {
super::*,
std::env,
};

pub(crate) struct Compiler;

Expand All @@ -13,6 +16,8 @@ impl Compiler {
let mut srcs: HashMap<PathBuf, &str> = HashMap::new();
let mut loaded = Vec::new();

let import_path_key = "JUSTFILE_PATH";

let mut stack = Vec::new();
stack.push(Source::root(root));

Expand Down Expand Up @@ -78,24 +83,53 @@ impl Compiler {
optional,
path,
} => {
let import = current
.path
.parent()
.unwrap()
.join(Self::expand_tilde(&relative.cooked)?)
.lexiclean();

if import.is_file() {
if srcs.contains_key(&import) {
return Err(Error::CircularImport {
current: current.path,
import,
});
match env::var_os(import_path_key) {
Some(paths) => {
let mut import_found = false;
for path in env::split_paths(&paths) {
let import = path
.join(Self::expand_tilde(&relative.cooked)?)
.lexiclean();

if import.is_file() {
if srcs.contains_key(&import) {
return Err(Error::CircularImport {
current: current.path,
import,
});
}
*absolute = Some(import.clone());
stack.push(current.import(import));
import_found = true;
continue;
}
}

if !import_found && !*optional {
return Err(Error::MissingImportFile { path: *path });
}
}
None => {
let import = current
.path
.parent()
.unwrap()
.join(Self::expand_tilde(&relative.cooked)?)
.lexiclean();

if import.is_file() {
if srcs.contains_key(&import) {
return Err(Error::CircularImport {
current: current.path,
import,
});
}
*absolute = Some(import.clone());
stack.push(current.import(import));
} else if !*optional {
return Err(Error::MissingImportFile { path: *path });
}
}
*absolute = Some(import.clone());
stack.push(current.import(import));
} else if !*optional {
return Err(Error::MissingImportFile { path: *path });
}
}
_ => {}
Expand Down

0 comments on commit 1ca493e

Please sign in to comment.