extern crate getopts;
extern crate std;
use std::io::Write;
use cmd;
use common;
use compiler;
const PROLOG: &'static str =
"Usage:
subcomponent template [options]
";
const TEMPLATE: &'static [u8] =
b"/*
* Subcomponent Template File
*/
subcomponents {
example {
name: \"Example: vim plugin for subcomponent\";
path: \"example-vim-subcomponent\";
fetch {
git {
url: \"https://github.com/subcomponent/vim-subcomponent\";
branch: \"master\";
}
}
}
}
";
pub struct CmdTemplate {
force: bool,
}
impl cmd::Cmd for CmdTemplate {
fn help(&self, opts: &getopts::Options) {
println!("{}", opts.usage(PROLOG));
}
fn getopts_set(&self, opts: &mut getopts::Options) {
opts.optflag("", "force", "Overwrite existing files");
}
fn getopts_matches_use(&mut self, matches: &getopts::Matches) {
if matches.opt_present("force") {
self.force = true;
}
}
#[allow(unused)]
fn run(&self, parser_arg: &Option<compiler::parser::Parser>) -> Result<(), cmd::Error> {
let path = std::path::Path::new(common::sub_dir_get());
if self.force && path.exists() {
try!(std::fs::remove_dir_all(path));
}
try!(std::fs::create_dir(&path));
let file = std::path::Path::new(common::sub_file_get());
let filename = path.join(file);
let mut template = try!(std::fs::File::create(filename));
try!(template.write_all(TEMPLATE));
Ok(())
}
}
pub fn new() -> CmdTemplate {
CmdTemplate {
force: false,
}
}