origen/commands/
save_ref.rs

1use std::path::Path;
2
3use crate::commands::_prelude::*;
4use origen_metal::framework::reference_files;
5
6pub const BASE_CMD: &'static str = "save_ref";
7
8gen_core_cmd_funcs!(
9    BASE_CMD,
10    "Save a reference version of the given file, this will be automatically checked for differences the next time it is generated",
11    { |cmd: App| {
12        cmd
13            .arg(
14                Arg::new("files")
15                    .help("The name of the file(s) to be saved")
16                    .action(AppendArgs)
17                    .value_name("FILES")
18                    .num_args(1..)
19                    .required_unless_present_any(["new", "changed"]),
20            )
21            .arg(
22                Arg::new("new")
23                    .long("new")
24                    .required(false)
25                    .action(SetArgTrue)
26                    .help("Update all NEW file references from the last generate run"),
27            )
28            .arg(
29                Arg::new("changed")
30                    .long("changed")
31                    .required(false)
32                    .action(SetArgTrue)
33                    .help("Update all CHANGED file references from the last generate run"),
34            )
35    }}
36);
37
38pub fn run(matches: &clap::ArgMatches) -> Result<()> {
39    let new = matches.contains_id("new");
40    let changed = matches.contains_id("changed");
41    let files = matches.get_many::<String>("files");
42
43    if new {
44        if let Err(e) = reference_files::apply_all_new_refs() {
45            bail!("Something went wrong saving the NEW references - {}", e);
46        }
47    }
48
49    if changed {
50        if let Err(e) = reference_files::apply_all_changed_refs() {
51            bail!(
52                "Something went wrong updating the CHANGED references - {}",
53                e
54            );
55        }
56    }
57
58    if let Some(files) = files {
59        for key in files {
60            if let Err(e) = reference_files::apply_ref(Path::new(key)) {
61                bail!("Could not save '{}' - {}", key, e);
62            }
63        }
64    }
65    Ok(())
66}