devstart 0.7.0

Make me: make me run dev tasks without thinking
Documentation
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<_>>())
    }
}

/// Initialize a local task provider config
///
/// # Errors
///
/// This function will return an error if IO fails
pub fn init_local(path: &Path) -> Result<String> {
    fs::write(path.join(DSFILE), TEMPLATE).context(crate::IOSnafu)?;
    Ok(DSFILE.to_string())
}