use crate::Result;
use fs_err as fs;
use fs_err::File;
use lazy_static::lazy_static;
use snafu::ResultExt;
use std::path::Path;
use crate::tasks::ProviderConfig;
use super::{DSFile, ProviderKind, Task, TaskProvider};
use crate::DSFILE;
pub const TEMPLATE: &str = r#"
tasks:
dev:
exec: echo dev
emoji: "๐น "
run:
exec: echo run
emoji: ๐ข
build:
exec: echo build
emoji: ๐ทโโ๏ธ
test:
exec: echo test
emoji: ๐ฆ
clean:
exec: echo clean
emoji: ๐งน
install:
exec: echo install
emoji: ๐ฆ
"#;
lazy_static! {
pub static ref CONFIG: ProviderConfig = serde_yaml::from_str(
r#"
matchers: ~
defaults: ~
"#
)
.unwrap();
}
#[derive(Default)]
pub struct Local {}
impl TaskProvider for Local {
fn parse(&self, path: &Path) -> Result<Vec<Task>> {
let file = path.join(DSFILE);
if !file.exists() {
return Ok(vec![]);
}
let mmfile: DSFile = serde_yaml::from_reader(File::open(&file).context(crate::IOSnafu)?)
.context(crate::SerializationYamlSnafu)?;
Ok(mmfile
.tasks
.iter()
.map(|(task, t)| Task {
provider: ProviderKind::Local,
task: task.clone(),
exec: t.exec.clone(),
emoji: if t.emoji.is_empty() {
"๐น ".to_string()
} else {
t.emoji.clone()
},
emoji_text: if t.emoji_text.is_empty() {
"[task]".to_string()
} else {
t.emoji_text.clone()
},
sh: t.sh,
details: t.details.clone(),
})
.collect::<Vec<_>>())
}
}
pub fn init_local(path: &Path) -> Result<String> {
fs::write(path.join(DSFILE), TEMPLATE).context(crate::IOSnafu)?;
Ok(DSFILE.to_string())
}