proplate_core/
template.rs

1use std::{fmt::Display, path::PathBuf, process::exit};
2
3use proplate_errors::{ProplateError, ProplateErrorKind, TemplateErrorKind};
4use proplate_tui::logger::AsError;
5
6use self::config::TemplateConf;
7
8pub mod config;
9pub mod inquirer;
10pub mod interpolation;
11pub mod op;
12pub mod resolver;
13
14#[derive(Debug)]
15pub struct Template {
16  pub id: String,
17  /// Template path, which may be either the forked or local template path
18  pub base_path: PathBuf,
19  pub base_file_list: Vec<String>,
20  /// Github repo if he template is from github
21  pub fork_source: String,
22  pub conf: TemplateConf,
23}
24
25pub const META_CONF: &str = "meta.json";
26
27impl Display for Template {
28  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29    f.write_fmt(format_args!(
30      "TEMPLATE [{}], base_path: {:?}",
31      self.id, self.base_path
32    ))
33  }
34}
35
36impl Template {
37  pub fn build(
38    id: String,
39    base_path: PathBuf,
40    base_file_list: Vec<String>,
41    fork_source: String,
42  ) -> Template {
43    Template::validate_template_filebase(&base_file_list, fork_source.clone());
44    Template {
45      id,
46      base_path: base_path.clone(),
47      base_file_list,
48      fork_source,
49      conf: TemplateConf::new(&base_path),
50    }
51  }
52
53  /// Validates main files
54  /// Namely ensures that meta.json is present
55  fn validate_template_filebase(files: &Vec<String>, location: String) {
56    let mut violations = Vec::<String>::new();
57
58    if !files.contains(&META_CONF.to_string()) {
59      violations.push(String::from("No `meta_json` conf file"));
60    }
61
62    if !violations.is_empty() {
63      eprintln!(
64        "{}",
65        ProplateError::create(ProplateErrorKind::Template {
66          kind: TemplateErrorKind::NoConfig,
67          location
68        })
69        .with_ctx("template:validate")
70        .print_err()
71      );
72      exit(1);
73    }
74  }
75}