1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use std::env;
use std::ffi::OsString;
use std::path::{Path, PathBuf};

use clap::{App, SubCommand, AppSettings};
use serde::Serialize;

pub mod project;
pub mod default_project;
pub mod music;
pub mod parser;
pub mod book;
pub mod render;
pub mod watch;
pub mod cli;
pub mod util;
pub mod error;

use crate::project::Project;
use crate::watch::{Watch, WatchEvent};
use crate::error::*;

#[derive(Serialize, Clone, Debug)]
pub struct ProgramMeta {
    name: &'static str,
    version: &'static str,
    description: &'static str,
    homepage: &'static str,
    authors: &'static str,
}

pub const PROGRAM_META: ProgramMeta = ProgramMeta {
    name: env!("CARGO_PKG_NAME"),
    version: env!("CARGO_PKG_VERSION"),
    description: env!("CARGO_PKG_DESCRIPTION"),
    homepage: env!("CARGO_PKG_HOMEPAGE"),
    authors: env!("CARGO_PKG_AUTHORS"),
};

fn get_cwd() -> Result<PathBuf> {
    let cwd = env::current_dir().context("Could not read current directory")?;
    ensure!(
        cwd.as_path().to_str().is_some(),
        format!("Path is not valid unicode: '{}'", cwd.display())
    );
    Ok(cwd)
}

pub fn bard_init() -> Result<()> {
    let cwd = get_cwd()?;

    cli::status("Initialize", &format!("new project at {}", cwd.display()));
    Project::init(&cwd).context("Could not initialize a new project")?;
    cli::success("Done!");
    Ok(())
}

pub fn bard_make_at(path: &Path) -> Result<Project> {
    Project::new(path)
        .and_then(|project| {
            project.render()?;
            Ok(project)
        })
        .context("Could not make project")
}

pub fn bard_make() -> Result<Project> {
    let cwd = get_cwd()?;

    let project = bard_make_at(&cwd)?;
    cli::success("Done!");
    Ok(project)
}

pub fn bard_watch() -> Result<()> {
    let cwd = get_cwd()?;
    let (mut watch, cancellation) = Watch::new()?;

    let _ = ctrlc::set_handler(move || {
        cancellation.cancel();
    });

    loop {
        let project = bard_make_at(&cwd)?;

        eprintln!("");
        cli::status("Watching", "for changes in the project ...");
        match watch.watch(&project)? {
            WatchEvent::Path(path) => {
                cli::status(
                    "",
                    &format!("Modification detected at '{}' ...", path.display()),
                );
            }
            WatchEvent::Pathless => cli::status("", "Modification detected ..."),
            WatchEvent::Cancel => break,
        }
    }

    Ok(())
}

pub fn bard(args: &[OsString]) -> Result<()> {
    let args = App::new("bard")
        .version("0.3")
        .author("Vojtech Kral <vojtech@kral.hk>")
        .about("bard: Songbook compiler")
        .setting(AppSettings::VersionlessSubcommands)
        .setting(AppSettings::ArgRequiredElseHelp)
        .subcommand(
            SubCommand::with_name("init")
                .about("Initialize and empty project with default settings"),
        )
        .subcommand(
            SubCommand::with_name("make")
                .about("Process the current project and generate output files"),
        )
        .subcommand(SubCommand::with_name("watch").about(
            "Watch the current project and its input files for changes, and re-make the project \
             each time there's a change",
        ))
        .get_matches_from(args.iter());

    if let Some(_args) = args.subcommand_matches("init") {
        bard_init()?;
    } else if let Some(_args) = args.subcommand_matches("make") {
        bard_make()?;
    } else if let Some(_args) = args.subcommand_matches("watch") {
        bard_watch()?;
    }

    Ok(())
}