From b5c3132edbee5e80292c7e3a976bd1bd73d0c420 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Mon, 5 Apr 2021 11:48:45 -0600 Subject: implement function `dir` --- src/makefile/functions.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/makefile') diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index 6815ac4..4ead48f 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -37,6 +37,10 @@ pub fn expand_call( text::sort(macros, &args[0]) } + "dir" => { + assert_eq!(args.len(), 1); + file_name::dir(macros, &args[0]) + } "notdir" => { assert_eq!(args.len(), 1); file_name::notdir(macros, &args[0]) @@ -190,6 +194,20 @@ mod file_name { use super::*; + pub fn dir(macros: &MacroSet, words: &TokenString) -> Result { + let words = macros.expand(words)?; + let words = words + .split_whitespace() + .map(|word| { + Path::new(word) + .parent() + .and_then(Path::to_str) + .unwrap_or("./") + }) + .collect::>(); + Ok(words.join(" ")) + } + pub fn notdir(macros: &MacroSet, words: &TokenString) -> Result { let words = macros.expand(words)?; let words = words -- cgit v1.2.3