use {
crate::*,
std::path::{
Path,
PathBuf,
},
};
pub struct Module {
pub name: String,
pub root: PathBuf,
pub config: Option<Sourced<Config>>,
pub src_path: PathBuf,
}
impl Module {
pub fn load<S: Into<String>>(
name: S,
root: &Path,
) -> DdResult<Self> {
let config = Config::in_dir(root)?;
let src_path = root.join("src");
let module = Self {
name: name.into(),
config,
root: root.to_owned(),
src_path,
};
Ok(module)
}
pub fn is_main(&self) -> bool {
self.name.is_empty()
}
fn list_static_entries_in(
&self,
subdir: &str,
ext_filter: Option<&str>,
entries: &mut Vec<StaticEntry>,
) -> DdResult<()> {
let static_src = self.src_path.join(subdir);
let prefix = format!("{}/", subdir);
StaticEntry::list_in(&static_src, &prefix, ext_filter, entries)
}
pub fn list_js(
&self,
entries: &mut Vec<StaticEntry>,
) -> DdResult<()> {
self.list_static_entries_in("js", Some(".js"), entries)
}
pub fn list_css(
&self,
entries: &mut Vec<StaticEntry>,
) -> DdResult<()> {
self.list_static_entries_in("css", Some(".css"), entries)
}
pub fn add_watch_targets(
&self,
targets: &mut Vec<WatchTarget>,
) {
if let Some(config) = &self.config {
let config_path = config.src();
targets.push(WatchTarget::new_file(config_path));
}
if self.src_path.exists() {
targets.push(WatchTarget::new_dir(&self.src_path));
}
}
fn copy_static_into(
&self,
dir: &str,
build_root: &Path, ) -> DdResult<()> {
let static_src = self.src_path.join(dir);
if !static_src.exists() {
return Ok(());
}
let static_dst = build_root.join(dir); copy_normal_recursive(&static_src, &static_dst)?;
Ok(())
}
pub fn copy_all_statics_into(
&self,
build_root: &Path, ) -> DdResult<()> {
self.copy_static_into("js", build_root)?;
self.copy_static_into("css", build_root)?;
self.copy_static_into("img", build_root)?;
Ok(())
}
}