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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//! `bard`, the Markdown-based songbook compiler.
//!
//! > ### <span style="font-variant: small-caps">**This is not a public API.** </span>
//! This library is an implementation detail of the `bard` CLI tool.
//! These APIs are internal and may break without notice.

#![allow(clippy::new_ret_no_self)]
#![allow(clippy::comparison_chain)]
#![allow(clippy::uninlined_format_args)]

use std::env;
use std::ffi::OsString;

use app::{App, InterruptFlag, MakeOpts, StdioOpts};
use clap::{CommandFactory as _, Parser as _};
use serde::Serialize;

pub mod app;
pub mod book;
pub mod default_project;
pub mod music;
pub mod parser;
pub mod prelude;
pub mod project;
pub mod render;
#[cfg(feature = "tectonic")]
pub mod tectonic_embed;
pub mod util;
pub mod util_cmd;
pub mod watch;

use crate::prelude::*;
use crate::project::{Project, Settings};
use crate::util_cmd::UtilCmd;
use crate::watch::Watch;

#[derive(Serialize, Clone, Debug)]
pub struct ProgramMeta {
    pub name: &'static str,
    pub version: &'static str,
    pub description: &'static str,
    pub homepage: &'static str,
    pub 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"),
};

#[derive(clap::Parser)]
#[command(
    version = env!("CARGO_PKG_VERSION"),
    about = "bard: A Markdown-based songbook compiler",
    help_expected = true,
    disable_version_flag = true,
)]
struct Cli {
    #[command(subcommand)]
    cmd: Option<Command>,

    /// Print program version in semver format
    #[arg(short = 'V', long, conflicts_with = "version_settings")]
    pub version: bool,
    /// Print project settings file version in semver format
    #[arg(long, conflicts_with = "version_ast")]
    pub version_settings: bool,
    /// Print project template AST version in semver format
    #[arg(long, conflicts_with = "version")]
    pub version_ast: bool,
}

impl Cli {
    fn print_version(&self) -> bool {
        if self.version {
            println!("{}", PROGRAM_META.version);
        }
        if self.version_settings {
            println!("{}", Settings::version());
        }
        if self.version_ast {
            println!("{}", book::version::current());
        }

        self.version || self.version_settings || self.version_ast
    }
}

#[derive(clap::Parser)]
enum Command {
    /// Initialize a new bard project skeleton in this directory
    Init {
        #[clap(flatten)]
        opts: StdioOpts,
    },
    /// Build the current project"
    Make {
        #[clap(flatten)]
        opts: MakeOpts,
    },
    /// Like make, but keep running and rebuild each time there's a change in project files
    Watch {
        #[clap(flatten)]
        opts: MakeOpts,
    },
    /// CLI utilities for postprocessing
    #[command(subcommand)]
    Util(UtilCmd),

    #[cfg(feature = "tectonic")]
    #[command(hide = true)]
    Tectonic(tectonic_embed::Tectonic),
}

impl Command {
    fn run(self, app: &App) -> Result<()> {
        use Command::*;

        match self {
            Init { .. } => bard_init(app),
            Make { .. } => bard_make(app),
            Watch { .. } => bard_watch(app),
            Util(cmd) => cmd.run(app),

            #[cfg(feature = "tectonic")]
            Tectonic(tectonic) => tectonic.run(app),
        }
    }
}

fn get_cwd() -> Result<PathBuf> {
    env::current_dir().context("Could not read current directory")
}

pub fn bard_init_at<P: AsRef<Path>>(app: &App, path: P) -> Result<()> {
    let path = path.as_ref();

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

pub fn bard_init(app: &App) -> Result<()> {
    let cwd = get_cwd()?;
    bard_init_at(app, cwd)
}

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

pub fn bard_make(app: &App) -> Result<()> {
    let cwd = get_cwd()?;

    bard_make_at(app, cwd)?;
    app.success("Done!");
    Ok(())
}

pub fn bard_watch_at<P: AsRef<Path>>(app: &App, path: P, mut watch: Watch) -> Result<()> {
    loop {
        let project = bard_make_at(app, &path)?;

        eprintln!();
        app.status("Watching", "for changes in the project ...");
        match watch.watch(&project, app.interrupt_flag())? {
            Some(paths) if paths.len() == 1 => {
                app.indent(format!("Change detected at {:?} ...", paths[0]))
            }
            Some(..) => app.indent("Change detected ..."),
            None => break,
        }
    }

    Ok(())
}

pub fn bard_watch(app: &App) -> Result<()> {
    let cwd = get_cwd()?;
    let watch = Watch::new()?;
    bard_watch_at(app, cwd, watch)
}

pub fn bard(args: &[OsString], interrupt: InterruptFlag) -> i32 {
    let cli = Cli::parse_from(args);
    if cli.print_version() {
        return 0;
    }

    let cmd = if let Some(cmd) = cli.cmd {
        cmd
    } else {
        let _ = Cli::command().print_help();
        return 0;
    };

    let app = match &cmd {
        Command::Init { opts } => App::new(&opts.clone().into(), interrupt),
        Command::Make { opts } => App::new(opts, interrupt),
        Command::Watch { opts } => App::new(opts, interrupt),
        Command::Util(_) => App::new(&Default::default(), interrupt),

        #[cfg(feature = "tectonic")]
        Command::Tectonic(_) => App::new_as_tectonic(interrupt),
    };

    if let Err(err) = cmd.run(&app) {
        app.error(err);
        1
    } else {
        0
    }
}