subcomponent 0.1.0

A components orchestrator
/*
 * Copyright (c) 2016-2017 Jean Guyomarc'h
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

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> {
        /* Create the subcomponent directory */
        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));

        /* Write the components.sub file in the subcomponent directory */
        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,
    }
}