|
| 1 | +use std::fs::{File, create_dir_all}; |
| 2 | +use std::io::prelude::*; |
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | + |
| 5 | +use toml::{self, Value as TomlValue}; |
| 6 | +use tera::{Tera, Context}; |
| 7 | +use walkdir::WalkDir; |
| 8 | + |
| 9 | +use prompt::{ask_string, ask_bool}; |
| 10 | + |
| 11 | + |
| 12 | +// TODO: error handling |
| 13 | +pub fn read_file(p: &Path) -> String { |
| 14 | + let mut f = File::open(p).expect("file not found"); |
| 15 | + |
| 16 | + let mut contents = String::new(); |
| 17 | + f.read_to_string(&mut contents) |
| 18 | + .expect("something went wrong reading the file"); |
| 19 | + |
| 20 | + contents |
| 21 | +} |
| 22 | + |
| 23 | +pub fn write_file(p: &Path, contents: &str) { |
| 24 | + let mut f = File::create(p).expect("Unable to create file"); |
| 25 | + f.write_all(contents.as_bytes()).expect("Unable to write data"); |
| 26 | +} |
| 27 | + |
| 28 | +pub fn create_directory(path: &Path) { |
| 29 | + if !path.exists() { |
| 30 | + create_dir_all(path).unwrap(); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Debug, PartialEq)] |
| 35 | +pub struct Template { |
| 36 | + path: PathBuf, |
| 37 | + name: String, |
| 38 | +} |
| 39 | + |
| 40 | +impl Template { |
| 41 | + pub fn from_cli(path: &str) -> Template { |
| 42 | + // TODO: templates can be from git as well, not only local |
| 43 | + let path = Path::new(path); |
| 44 | + let name = path.file_name().unwrap().to_string_lossy(); |
| 45 | + |
| 46 | + Template { |
| 47 | + path: path.to_path_buf(), |
| 48 | + name: name.to_string(), |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + pub fn download_from_vcs(&self) { |
| 53 | + // TODO |
| 54 | + } |
| 55 | + |
| 56 | + fn ask_questions(&self, conf: TomlValue) -> Context { |
| 57 | + let table = conf.as_table().unwrap(); |
| 58 | + let mut context = Context::new(); |
| 59 | + |
| 60 | + for (key, data) in table { |
| 61 | + let question = data["question"].as_str().unwrap(); |
| 62 | + if let Some(b) = data["default"].as_bool() { |
| 63 | + let res = ask_bool(question, b); |
| 64 | + context.add(key, &res); |
| 65 | + |
| 66 | + } else { |
| 67 | + let res = ask_string(question, data["default"].as_str().unwrap()); |
| 68 | + context.add(key, &res); |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + context |
| 73 | + } |
| 74 | + |
| 75 | + // TODO: error handling |
| 76 | + pub fn generate(&self, output_dir: PathBuf) { |
| 77 | + // Get the variables from the user first |
| 78 | + let conf_path = self.path.join("template.toml"); |
| 79 | + if !conf_path.exists() { |
| 80 | + panic!("template.toml missing") |
| 81 | + } |
| 82 | + let conf: TomlValue = toml::from_str(&read_file(&conf_path)).unwrap(); |
| 83 | + let context = self.ask_questions(conf); |
| 84 | + |
| 85 | + if !output_dir.exists() { |
| 86 | + create_directory(&output_dir); |
| 87 | + } |
| 88 | + |
| 89 | + // And now generate the files (in current directory for now) |
| 90 | + for entry in WalkDir::new(&self.path).into_iter().filter_map(|e| e.ok()) { |
| 91 | + // Skip root folder and the template.toml |
| 92 | + if entry.path() == self.path || entry.path() == conf_path { |
| 93 | + continue; |
| 94 | + } |
| 95 | + let path = entry.path().strip_prefix(&self.path).unwrap(); |
| 96 | + // TODO: only render tpl if {{..}} |
| 97 | + let tpl = Tera::one_off(&format!("{}", path.display()), &context, false).unwrap(); |
| 98 | + let real_path = Path::new(&tpl); |
| 99 | + |
| 100 | + if entry.path().is_dir() { |
| 101 | + create_directory(&output_dir.join(real_path)); |
| 102 | + } else { |
| 103 | + let contents = Tera::one_off(&read_file(&entry.path()), &context, false).unwrap(); |
| 104 | + write_file(&output_dir.join(real_path), &contents); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments