// BLANK returns nothing, so no more processing needs to be done
if filename == "BLANK" {
- return Ok(String::new());
+ return Ok(String::from(""));
};
// DEFAULT override for patterns defined higher in chain
metafile_to_string(&pattern)
}
-fn get_variable(_key: &str, _file: &MetaFile) -> Result<String> {
- todo!()
+fn get_variable(key: &str, file: &MetaFile) -> Result<String> {
+ let long_key = file.name()? + "." + key;
+ if let Some(val) = file.get_var(&long_key) {
+ Ok(val.clone())
+ } else if let Some(val) = file.get_var(key) {
+ Ok(val.clone())
+ } else if file.opts.undefined {
+ bail!("undefined variable: {}, {}", key, long_key)
+ } else {
+ Ok(String::new())
+ }
}
fn find_dest(path: &Path, opts: &Options) -> Result<PathBuf> {
pub fn get_opts() -> Result<Options> {
let opts = Options::try_from(Opts::parse())?;
- log!(
- opts,
- format!("cleaning build directory: {}", opts.build.display()),
- 1
- );
- if opts.clean && opts.build.exists() {
+ let exists = opts.build.exists();
+ if exists && opts.clean {
std::fs::remove_dir_all(&opts.build)?;
- }
-
- if !opts.build.exists() {
+ std::fs::create_dir(&opts.build)?;
+ } else if !exists {
std::fs::create_dir(&opts.build)?;
}
pub fn build_dir(opts: &Options) -> Result<()> {
let mut source = DirNode::build(opts.source.clone(), opts)?;
- let global_init = MetaFile::new(&opts);
+ let global_init = MetaFile::new(opts);
source.map(&global_init)?;
let str = fs::read_to_string(&path)?;
let mut metafile = parse_file(str, opts)?;
metafile.path = path;
- metafile.opts = opts;
Ok(metafile)
}
pub fn build(path: PathBuf, opts: &'a Options) -> Result<Self> {
assert!(path.is_dir() && path.exists());
- fs::create_dir(opts.build.join(path.strip_prefix(&opts.source)?))?;
+ let build_dir = opts.build.join(path.strip_prefix(&opts.source)?);
+ if !build_dir.exists() {
+ fs::create_dir(build_dir)?;
+ }
let files: Vec<MetaFile> = Vec::new();
let dirs: Vec<DirNode> = Vec::new();
Rule::var_sub => vec.push(source!(var(parse_sub(pair)))),
Rule::arr_sub => vec.push(source!(arr(parse_sub(pair)))),
Rule::pat_sub => vec.push(source!(pat(parse_sub(pair)))),
- Rule::char_seq => vec.push(source!(pair)),
+ Rule::char_seq => vec.push(source!(pair.as_str())),
// anything that isn't a substitution is a char_seq inside source
_ => unreachable!(),
}