use failure::ResultExt;
use mkdirp::mkdirp;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
static BIN_DEPLOY_PS1: &str =
include_str!("../templates/bin/before_deploy.ps1");
static BIN_DEPLOY_SH: &str = include_str!("../templates/bin/before_deploy.sh");
static BIN_INSTALL: &str = include_str!("../templates/bin/install.sh");
static BIN_SCRIPT: &str = include_str!("../templates/bin/script.sh");
static BIN_TRAVIS: &str = include_str!("../templates/bin/travis.yml");
static LIB_DEPLOY_PS1: &str =
include_str!("../templates/lib/before_deploy.ps1");
static LIB_DEPLOY_SH: &str = include_str!("../templates/lib/before_deploy.sh");
static LIB_INSTALL: &str = include_str!("../templates/lib/install.sh");
static LIB_SCRIPT: &str = include_str!("../templates/lib/script.sh");
static LIB_TRAVIS: &str = include_str!("../templates/lib/travis.yml");
pub struct Templates {
name: String,
dir: PathBuf,
token: String,
lib: bool,
}
impl Templates {
#[deprecated(since = "0.5.0", note = "Use `gen_bin` or `gen_lib` instead")]
pub fn new(dir: PathBuf, name: String, token: String) -> ::Result<Self> {
Self::gen_bin(dir, name, token)
}
pub fn gen_bin(dir: PathBuf, name: String, token: String) -> ::Result<Self> {
Self::scaffold(false, dir, name, token)
}
pub fn gen_lib(dir: PathBuf, name: String, token: String) -> ::Result<Self> {
Self::scaffold(true, dir, name, token)
}
#[inline]
fn scaffold(
lib: bool,
dir: PathBuf,
name: String,
token: String,
) -> ::Result<Self> {
let scripts_dir = dir.join("scripts");
mkdirp(&scripts_dir).context(::ErrorKind::Other)?;
Ok(Self {
lib,
name,
dir,
token,
})
}
pub fn write_all(&self) -> ::Result<()> {
let scripts_dir = &self.dir.join("scripts");
if self.lib {
self.write(&scripts_dir, "before_deploy.ps1", LIB_DEPLOY_PS1)?;
self.write(&scripts_dir, "before_deploy.sh", LIB_DEPLOY_SH)?;
self.write(&scripts_dir, "install.sh", LIB_INSTALL)?;
self.write(&scripts_dir, "script.sh", LIB_SCRIPT)?;
self.write(&self.dir, ".travis.yml", LIB_TRAVIS)?; } else {
self.write(&scripts_dir, "before_deploy.ps1", BIN_DEPLOY_PS1)?;
self.write(&scripts_dir, "before_deploy.sh", BIN_DEPLOY_SH)?;
self.write(&scripts_dir, "install.sh", BIN_INSTALL)?;
self.write(&scripts_dir, "script.sh", BIN_SCRIPT)?;
self.write(&self.dir, ".travis.yml", BIN_TRAVIS)?; }
Ok(())
}
fn write(
&self,
issue_dir: &PathBuf,
file_name: &str,
template: &str,
) -> ::Result<()> {
let dir = issue_dir.join(file_name);
let mut file = File::create(dir).context(::ErrorKind::Other)?;
let template = str::replace(&template, "{{PKG_NAME}}", &self.name);
let template = str::replace(&template, "{{TOKEN}}", &self.token);
file
.write_all(template.as_bytes())
.context(::ErrorKind::Other)?;
Ok(())
}
}