Skip to content

Commit

Permalink
GLobal justfile search locations
Browse files Browse the repository at this point in the history
  • Loading branch information
neunenak committed Jan 16, 2024
1 parent 5cc422e commit f94838f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
42 changes: 37 additions & 5 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use {super::*, std::path::Component};

const DEFAULT_JUSTFILE_NAME: &str = JUSTFILE_NAMES[0];
pub(crate) const JUSTFILE_NAMES: &[&str] = &["justfile", ".justfile"];
pub(crate) const JUSTFILE_NAMES: [&str; 2] = ["justfile", ".justfile"];
const DEFAULT_GLOBAL_JUSTFILE_NAME: &str = "global.just";
const PROJECT_ROOT_CHILDREN: &[&str] = &[".bzr", ".git", ".hg", ".svn", "_darcs"];

pub(crate) struct Search {
Expand All @@ -10,6 +11,27 @@ pub(crate) struct Search {
}

impl Search {
fn candidate_global_justfiles() -> Vec<PathBuf> {
// Just will search for a global justfile in `$XDG_CONFIG_HOME/just/global.just`,
// `$HOME/.justfile`, `$HOME/justfile`, in that order.
let mut global_candidate_paths = vec![];

if let Ok(config_var) = std::env::var("XDG_CONFIG_HOME") {
global_candidate_paths.push(
PathBuf::from(config_var)
.join("just")
.join(DEFAULT_GLOBAL_JUSTFILE_NAME),
);
}

if let Some(home_dir) = dirs::home_dir() {
global_candidate_paths.push(home_dir.join(JUSTFILE_NAMES[1]));
global_candidate_paths.push(home_dir.join(JUSTFILE_NAMES[0]));
}

global_candidate_paths
}

pub(crate) fn find(
search_config: &SearchConfig,
invocation_directory: &Path,
Expand All @@ -30,8 +52,14 @@ impl Search {
}
Global => {
let working_directory = Self::project_root(invocation_directory)?;
let home_dir = dirs::home_dir().ok_or(SearchError::MissingHomeDirectory)?;
let justfile = home_dir.join(DEFAULT_JUSTFILE_NAME);

let global_candidate_paths = Self::candidate_global_justfiles();
let justfile = global_candidate_paths
.iter()
.find(|path| path.try_exists().unwrap_or(false))
.cloned()
.ok_or(SearchError::MissingGlobalJustfile)?;

Self {
justfile,
working_directory,
Expand Down Expand Up @@ -96,8 +124,12 @@ impl Search {

Global => {
let working_directory = Self::project_root(invocation_directory)?;
let home_dir = dirs::home_dir().ok_or(SearchError::MissingHomeDirectory)?;
let justfile = home_dir.join(DEFAULT_JUSTFILE_NAME);

let global_candidate_paths = Self::candidate_global_justfiles();
let justfile = global_candidate_paths
.first()
.cloned()
.ok_or(SearchError::MissingGlobalJustfile)?;

Self {
justfile,
Expand Down
4 changes: 2 additions & 2 deletions src/search_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub(crate) enum SearchError {
#[snafu(display("Justfile path had no parent: {}", path.display()))]
JustfileHadNoParent { path: PathBuf },

#[snafu(display("Home directory not found"))]
MissingHomeDirectory,
#[snafu(display("Global justfile not found"))]
MissingGlobalJustfile,

#[snafu(display(
"Multiple candidate justfiles found in `{}`: {}",
Expand Down

0 comments on commit f94838f

Please sign in to comment.