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
//! This library provides the functions/structs/methods used by the main
//! binary. They are included
//! here in the hopes that they can be illuminating to users.

#![feature(type_ascription)]

extern crate case;
extern crate clap;
extern crate colored;
extern crate git2;
extern crate rustache;
#[macro_use]
extern crate serde_derive;
extern crate tempdir;
extern crate time;
extern crate toml;

use case::*;
use colored::*;
use rustache::{HashBuilder, VecBuilder};
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::path::PathBuf;
use toml::Value::Table;
use toml::de;

pub mod types;
pub mod repo;
pub mod includes;
pub mod render;

/// Given a filepath, read the .toml file there as containing the
/// directories/templates.
/// If no such file is found, read from global template directory in
/// `$HOME/.pi_templates/`.
pub fn read_toml_dir(template_path: &str, home: PathBuf) -> (types::Project, bool) {
    let (mut template_file, is_global_template) = if let Ok(f) = File::open(&template_path) {
        (f, false)
    } else if let Ok(f) = {
        let mut p = home;
        p.push(".pi_templates/");
        p.push(template_path);
        File::open(p)
    } {
        (f, true)
    } else {
        println!(
            "{}: File {:?} could not be opened. Check that it exists.",
            "Error".red(),
            template_path
        );
        std::process::exit(0x0f00);
    };
    let mut template = String::new();
    template_file
        .read_to_string(&mut template)
        .expect("Failed to read file"); // we can panic because we already errored if the file didn't exist.
    (read_toml_str(&template, template_path), is_global_template)
}

/// Read a string containing a toml file
pub fn read_toml_str(template: &str, template_path: &str) -> types::Project {
    if let Ok(t) = toml::from_str(template) {
        t
    } else if let Err(e) = toml::from_str(template): Result<String, de::Error> {
        println!("Error parsing {:?}: {}", template_path, e);
        std::process::exit(0x0f00);
    } else {
        std::process::exit(0x0f00);
    }
}

/// Given a `PathBuf`, read the .toml file there as a configuration file.
pub fn read_toml_config(config_path: &std::path::PathBuf) -> types::Config {
    let file = if let Ok(f) = File::open(&config_path) {
        Some(f)
    } else {
        println!(
            "{}: File {:?} could not be opened. Check that it exists.",
            "Warning".yellow(),
            config_path
        );
        None
    };
    let mut toml_str = String::new();
    let maybe_file = file.map(|mut x| x.read_to_string(&mut toml_str));
    if maybe_file.is_some() && maybe_file.unwrap().is_ok() {
        if let Ok(t) = toml::from_str(&toml_str) {
            t
        } else if let Err(e) = toml::from_str(&toml_str): Result<String, de::Error> {
            println!("Error parsing {:?}: {}", config_path, e);
            std::process::exit(0x0f00);
        } else {
            std::process::exit(0x0f00);
        }
    } else {
        eprintln!(
            "{}: No ~/.pi.toml found. Using defaults.",
            "Warning".yellow()
        );
        types::Config {
            version_control: None,
            author:          None,
            license:         None,
            user:            None,
        }
    }
}

pub fn init_helper(
    home: PathBuf,
    project_dir: &str,
    decoded: types::Config,
    author: types::Author,
    name: &str,
    year: i32,
    current_date: &str,
    force: bool,
    parsed_toml: types::Project,
    is_global_project: bool,
) -> () {
    let project = if is_global_project {
        let mut p = home;
        p.push(".pi_templates/");
        p.push(project_dir);
        p.to_str().unwrap().to_string()
    } else {
        project_dir.to_string()
    };
    let parsed_dirs = parsed_toml.files;
    let parsed_config = parsed_toml.config;

    // set license if it's set
    let (license_contents, license_name) =
        // prefer project-specific license over global
        if let Some(l) = parsed_toml.license {
            match l.as_str() {
                "BSD3" => (Some(includes::BSD3), "BSD3"),
                "BSD" => (Some(includes::BSD), "BSD"),
                "MIT" => (Some(includes::MIT), "MIT"),
                "GPL3" => (Some(includes::GPL3), "GLP3"),
                "AllRightsReserved" => (Some(includes::BSD3), "AllRightsReserved"),
                _ => { println!("{}: requested license not found. Defaulting to AllRightsReserved","Warning".yellow()) 
                        ; (Some(includes::ALL_RIGHTS_RESERVED), "AllRightsReserved") }
            }
        }
        else if let Some(l) = decoded.license {
            match l.as_str() {
                "BSD3" => (Some(includes::BSD3), "BSD3"),
                "BSD" => (Some(includes::BSD), "BSD"),
                "MIT" => (Some(includes::MIT), "MIT"),
                "GPL3" => (Some(includes::GPL3), "GLP3"),
                "AllRightsReserved" => (Some(includes::BSD3), "AllRightsReserved"),
                _ => { println!("{}: requested license not found. Defaulting to AllRightsReserved","Warning".yellow()) 
                        ; (Some(includes::ALL_RIGHTS_RESERVED), "AllRightsReserved") }
            }
        }
        else {
            (None,"")
        };

    // set version
    let version = if let Some(config) = parsed_config.clone() {
        if let Some(v) = config.version {
            v
        } else {
            "0.1.0".to_string()
        }
    } else {
        eprintln!(
            "{}: no version info found, defaulting to '0.1.0'",
            "Warning".yellow()
        );
        "0.1.0".to_string()
    };

    // set github username to null if it's not provided
    let github_username = if let Some(uname) = author.github_username {
        uname
    } else {
        eprintln!(
            "{}: no github username found, defaulting to null",
            "Warning".yellow()
        );
        "".to_string()
    };

    // make user_keys into a vector; prepare to insert them into the `HashBuilder`
    let user_keys = if let Some(u) = parsed_toml.user {
        match u.toml {
            Table(t) => Some(t),
            _ => None,
        }
    } else {
        None
    };

    // make user_keys into a vector; prepare to insert them into the `HashBuilder`
    let user_keys_global = if let Some(u) = decoded.user {
        match u.toml {
            Table(t) => Some(t),
            _ => None,
        }
    } else {
        None
    };

    // Make a hash for inserting stuff into templates.
    let mut hash = HashBuilder::new();
    // project-specific
    if let Some(x) = user_keys {
        for (key, value) in &x {
            if let Some(a) = value.as_str() {
                hash = hash.insert(key, a);
            }
        }
    }
    // global
    if let Some(x) = user_keys_global {
        for (key, value) in &x {
            if let Some(a) = value.as_str() {
                hash = hash.insert(key, a);
            }
        }
    }
    // add the normal stuff
    hash = hash.insert("project", name)
        .insert("Project", name.to_capitalized())
        .insert("year", year)
        .insert("name", author.name)
        .insert("version", version)
        .insert("email", author.email)
        .insert("github_username", github_username)
        .insert("license", license_name)
        .insert("date", current_date);

    // check if the directory exists and exit, if we haven't forced an overwrite.
    if Path::new(name).exists() && !force {
        println!(
            "Path '{}' already exists. Rerun with -f or --force to overwrite.",
            name
        );
        std::process::exit(0x0f00);
    };

    // create directories
    let _ = fs::create_dir(name);
    if let Some(dirs_pre) = parsed_dirs.directories {
        render::render_dirs(dirs_pre, &hash, name);
    }

    // create a list of files contained in the project, and create those files.
    // TODO should include templates/scripts/etc.
    let files = if let Some(files_pre) = parsed_dirs.files {
        render::render_files(files_pre, &hash, name) // FIXME files need to have a newline insert in between them?
    } else {
        VecBuilder::new()
    };

    // create license if it was asked for
    if let Some(lic) = license_contents {
        render::render_file(lic, name, "LICENSE", &hash);
    }

    // render readme if requested
    if let Some(readme) = parsed_toml.with_readme {
        if readme {
            render::render_file(includes::README, name, "README.md", &hash);
        }
    }

    // Make a hash for inserting stuff into templates.
    hash = hash.insert("files", files);

    // render templates
    render::render_templates(&project, name, &hash, parsed_dirs.templates, false);

    // render scripts, i.e. files that should be executable.
    render::render_templates(&project, name, &hash, parsed_dirs.scripts, true);

    // initialize version control
    if let Some(config) = parsed_config {
        if let Some(vc) = config.version_control {
            match vc.as_str() {
                "git" => repo::git_init(name),
                "hg" | "mercurial" => repo::hg_init(name),
                "pijul" => repo::pijul_init(name),
                "darcs" => repo::darcs_init(name),
                _ => {
                    eprintln!(
                        "{}: version control {} is not yet supported. Supported version control tools are darcs, pijul, mercurial, and git.",
                        "Error".red(),
                        vc
                    );
                }
            }
        }
    } else if let Some(vc) = decoded.version_control {
        match vc.as_str() {
            "git" => repo::git_init(name),
            "hg" | "mercurial" => repo::hg_init(name),
            "pijul" => repo::pijul_init(name),
            "darcs" => repo::darcs_init(name),
            _ => {
                eprintln!(
                    "{}: version control {} is not yet supported. Supported version control tools are darcs, pijul, mercurial, and git.",
                    "Error".red(),
                    vc
                );
            }
        }
    }

    // Print that we're done
    println!("Finished initializing project in {}/", name);
}