summaryrefslogtreecommitdiff
path: root/src/bin/subplot.rs
blob: ebbdec8590d516a99006404869b106349da60bf1 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! Subplot top level binary
//!

use anyhow::Result;

use chrono::{Local, TimeZone};
use structopt::StructOpt;
use subplot::{generate_test_program, resource, template_spec, DataFile, Document, Style};

use std::convert::TryFrom;
use std::ffi::OsString;
use std::fs::{self, write, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::{self, Command};
use std::time::UNIX_EPOCH;

mod cli;

#[derive(Debug, StructOpt)]
struct Toplevel {
    #[structopt(flatten)]
    resources: resource::ResourceOpts,

    #[structopt(flatten)]
    command: Cmd,
}

impl Toplevel {
    fn run(&self) -> Result<()> {
        self.command.run()
    }
}

#[derive(Debug, StructOpt)]
enum Cmd {
    Extract(Extract),
    Filter(Filter),
    Metadata(Metadata),
    Docgen(Docgen),
    Codegen(Codegen),
}

impl Cmd {
    fn run(&self) -> Result<()> {
        match self {
            Cmd::Extract(e) => e.run(),
            Cmd::Filter(f) => f.run(),
            Cmd::Metadata(m) => m.run(),
            Cmd::Docgen(d) => d.run(),
            Cmd::Codegen(c) => c.run(),
        }
    }
}

#[derive(Debug, StructOpt)]
/// Extract embedded files from a subplot document
///
/// If no embedded filenames are provided, this will
/// extract all embedded files.  if the output directory
/// is not specified then this will extract to the current directory.
struct Extract {
    /// Directory to write extracted files to
    #[structopt(
        name = "DIR",
        long = "directory",
        short = "d",
        parse(from_os_str),
        default_value = "."
    )]
    directory: PathBuf,

    /// Don't actually write the files out
    #[structopt(long)]
    dry_run: bool,

    /// Input subplot document filename
    #[structopt(parse(from_os_str))]
    filename: PathBuf,

    /// Names of embedded files to be extracted.
    embedded: Vec<String>,
}

impl Extract {
    fn run(&self) -> Result<()> {
        let doc = cli::load_document(&self.filename, Style::default())?;

        let files: Vec<&DataFile> = if self.embedded.is_empty() {
            doc.files()
                .iter()
                .map(Result::Ok)
                .collect::<Result<Vec<_>>>()
        } else {
            self.embedded
                .iter()
                .map(|filename| cli::extract_file(&doc, filename))
                .collect::<Result<Vec<_>>>()
        }?;

        for f in files {
            let outfile = self.directory.join(f.filename());
            if self.dry_run {
                println!("Would write out {}", outfile.display());
            } else {
                write(outfile, f.contents())?
            }
        }

        Ok(())
    }
}

#[derive(Debug, StructOpt)]
/// Filter a pandoc JSON document.
///
/// This filters a pandoc JSON document, applying Subplot's formatting rules and
/// image conversion support.
///
/// If input/output filename is provided, this operates on STDIN/STDOUT.
struct Filter {
    #[structopt(name = "INPUT", long = "input", short = "i", parse(from_os_str))]
    /// Input file (uses STDIN if omitted)
    input: Option<PathBuf>,

    #[structopt(name = "OUTPUT", long = "output", short = "o", parse(from_os_str))]
    /// Output file (uses STDOUT if omitted)
    output: Option<PathBuf>,

    #[structopt(name = "BASE", long = "base", short = "b", parse(from_os_str))]
    /// Base directory (defaults to dir of input if given, or '.' if using STDIN)
    base: Option<PathBuf>,
}

impl Filter {
    fn run(&self) -> Result<()> {
        let mut buffer = String::new();
        if let Some(filename) = &self.input {
            File::open(filename)?.read_to_string(&mut buffer)?;
        } else {
            std::io::stdin().read_to_string(&mut buffer)?;
        }
        let basedir = if let Some(path) = &self.base {
            path.as_path()
        } else if let Some(path) = &self.input {
            path.parent().unwrap_or_else(|| Path::new("."))
        } else {
            Path::new(".")
        };
        let style = Style::default();
        let mut doc = Document::from_json(basedir, vec![], &buffer, style)?;
        doc.typeset();
        let bytes = doc.ast()?.into_bytes();
        if let Some(filename) = &self.output {
            File::create(filename)?.write_all(&bytes)?;
        } else {
            std::io::stdout().write_all(&bytes)?;
        }
        Ok(())
    }
}

#[derive(Debug, StructOpt)]
/// Extract metadata about a document
///
/// Load and process a subplot document, extracting various metadata about the
/// document.  This can then render that either as a plain text report for humans,
/// or as a JSON object for further scripted processing.
struct Metadata {
    /// Form that you want the output to take
    #[structopt(short = "o", default_value = "plain", possible_values=&["plain", "json"])]
    output_format: cli::OutputFormat,

    /// Input subplot document filename
    #[structopt(parse(from_os_str))]
    filename: PathBuf,
}

impl Metadata {
    fn run(&self) -> Result<()> {
        let mut doc = cli::load_document(&self.filename, Style::default())?;
        let meta = cli::Metadata::try_from(&mut doc)?;
        match self.output_format {
            cli::OutputFormat::Plain => meta.write_out(),
            cli::OutputFormat::Json => println!("{}", serde_json::to_string_pretty(&meta)?),
        }
        Ok(())
    }
}

#[derive(Debug, StructOpt)]
/// Typeset subplot document
///
/// Process a subplot document and typeset it using Pandoc.
struct Docgen {
    // Input Subplot document
    #[structopt(parse(from_os_str))]
    input: PathBuf,

    // Output document filename
    #[structopt(name = "FILE", long = "--output", short = "-o", parse(from_os_str))]
    output: PathBuf,

    // Set date.
    #[structopt(name = "DATE", long = "--date")]
    date: Option<String>,
}

impl Docgen {
    fn run(&self) -> Result<()> {
        let mut style = Style::default();
        if self.output.extension() == Some(&OsString::from("pdf")) {
            style.typeset_links_as_notes();
        }
        let mut doc = cli::load_document(&self.input, style)?;
        doc.lint()?;
        if !doc.check_named_files_exist()? {
            eprintln!("Continuing despite warnings");
        }

        let mut pandoc = pandoc::new();
        // Metadata date from command line or file mtime. However, we
        // can't set it directly, since we don't want to override the date
        // in the actual document, if given, so we only set
        // user-provided-date. Our parsing code will use that if date is
        // not document metadata.
        let date = if let Some(date) = self.date.clone() {
            date
        } else if let Some(date) = doc.meta().date() {
            date.to_string()
        } else {
            Self::mtime_formatted(Self::mtime(&self.input)?)
        };
        pandoc.add_option(pandoc::PandocOption::Meta("date".to_string(), Some(date)));
        pandoc.add_option(pandoc::PandocOption::TableOfContents);
        pandoc.add_option(pandoc::PandocOption::Standalone);
        pandoc.add_option(pandoc::PandocOption::NumberSections);

        if Self::need_output(&mut doc, &self.output) {
            doc.typeset();
            pandoc.set_input_format(pandoc::InputFormat::Json, vec![]);
            pandoc.set_input(pandoc::InputKind::Pipe(doc.ast()?));
            pandoc.set_output(pandoc::OutputKind::File(self.output.clone()));
            pandoc.execute()?;
        }

        Ok(())
    }

    fn mtime(filename: &Path) -> Result<(u64, u32)> {
        let mtime = fs::metadata(filename)?.modified()?;
        let mtime = mtime.duration_since(UNIX_EPOCH)?;
        Ok((mtime.as_secs(), mtime.subsec_nanos()))
    }

    fn mtime_formatted(mtime: (u64, u32)) -> String {
        let secs: i64 = format!("{}", mtime.0).parse().unwrap_or(0);
        let dt = Local.timestamp(secs, mtime.1);
        dt.format("%Y-%m-%d %H:%M").to_string()
    }

    fn need_output(doc: &mut subplot::Document, output: &Path) -> bool {
        let output = match Self::mtime(output) {
            Err(_) => return true,
            Ok(ts) => ts,
        };

        for filename in doc.sources() {
            let source = match Self::mtime(&filename) {
                Err(_) => return true,
                Ok(ts) => ts,
            };
            if source >= output {
                return true;
            }
        }
        false
    }
}

#[derive(Debug, StructOpt)]
/// Generate test suites from Subplot documents
///
/// This reads a subplot document, extracts the scenarios, and writes out a test
/// program capable of running the scenarios in the subplot document.
struct Codegen {
    /// Input filename.
    #[structopt(parse(from_os_str))]
    filename: PathBuf,

    /// Write generated test program to this file.
    #[structopt(
        long,
        short,
        parse(from_os_str),
        help = "Writes generated test program to FILE"
    )]
    output: PathBuf,

    /// Run the generated test program after writing it?
    #[structopt(long, short, help = "Runs generated test program")]
    run: bool,
}

impl Codegen {
    fn run(&self) -> Result<()> {
        let mut doc = cli::load_document(&self.filename, Style::default())?;
        doc.lint()?;
        if !doc.check_named_files_exist()? {
            eprintln!("Unable to continue");
            std::process::exit(1);
        }

        let spec = template_spec(&doc)?;
        generate_test_program(&mut doc, &spec, &self.output)?;

        if self.run {
            let run = match spec.run() {
                None => {
                    eprintln!(
                        "Template {} does not specify how to run suites",
                        spec.template_filename().display()
                    );
                    std::process::exit(1);
                }
                Some(x) => x,
            };

            let status = Command::new(run).arg(&self.output).status()?;
            if !status.success() {
                eprintln!("Test suite failed!");
                std::process::exit(2);
            }
        }
        Ok(())
    }
}

fn main() {
    let args = Toplevel::from_args();
    args.resources.handle();
    match args.run() {
        Ok(_) => {}
        Err(e) => {
            eprintln!("Failure: {:?}", e);
            process::exit(1);
        }
    }
}