Skip to content
Permalink
Browse files

main: automatically figure out if we're dealing with a crate or a gem

  • Loading branch information...
Cogitri committed Nov 10, 2018
1 parent a9345dc commit 7e6ea50f2451393550a4f49bb096c2c1610826cb
Showing with 54 additions and 12 deletions.
  1. +2 −3 src/cli.yml
  2. +27 −0 src/helpers.rs
  3. +17 −7 src/main.rs
  4. +2 −2 src/tmplwriter.rs
  5. +6 −0 src/types.rs
@@ -15,13 +15,12 @@ args:
short: t
long: tmpltype
value_name: crate/gem
help: Sets what kind of template we want to generate
required: true
help: Explicitly sets what kind of template we want to generate
- verbose:
short: v
long: verbose
help: Be more verbose
- INPUT:
- PKGNAME:
help: Sets for which package the template should be generated
required: true
index: 1
@@ -1,3 +1,5 @@
use types::*;

pub fn missing_field_s(field_name: &str) -> String {
error!(
"Couldn't determine field '{}'! Please add it to the template yourself.",
@@ -15,3 +17,28 @@ pub fn missing_field_v(field_name: &str) -> Vec<String> {

vec![String::from("")]
}

pub fn figure_out_provider(
tmpl_type: Option<PkgType>,
pkg_name: &String,
) -> Result<PkgType, String> {
if tmpl_type.is_none() {
if crates_io_api::SyncClient::new()
.get_crate(&pkg_name)
.is_ok()
{
debug!("Determined the target package to be a crate");
Ok(PkgType::Crate)
} else if rubygems_api::SyncClient::new()
.gem_info(&pkg_name)
.is_ok()
{
debug!("Determined the target package to be a ruby gem");
Ok(PkgType::Gem)
} else {
Err("Unable to determine what type of the target package, please specify it via the '-t' parameter!".to_string())
}
} else {
Ok(tmpl_type.unwrap())
}
}
@@ -33,16 +33,24 @@ mod types;
use clap::App;
use crates::*;
use gems::*;
use helpers::figure_out_provider;
use tmplwriter::*;
use types::PkgType;

// Print the help script if invoked without arguments or with `--help`/`-h`
pub fn help_string() -> (String, String, bool, bool, bool) {
pub fn help_string() -> (String, Option<PkgType>, bool, bool, bool) {
let help_yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(help_yaml).get_matches();

let tmpl_type = String::from(matches.value_of("tmpltype").unwrap());
let tmpl_type = if matches.value_of("tmpltype").unwrap_or_default() == "crate" {
Some(PkgType::Crate)
} else if matches.value_of("tmpltype").unwrap_or_default() == "gem" {
Some(PkgType::Gem)
} else {
None
};

let crate_name = String::from(matches.value_of("INPUT").unwrap());
let crate_name = String::from(matches.value_of("PKGNAME").unwrap());

let force_overwrite = matches.is_present("force");

@@ -73,16 +81,18 @@ fn main() {
env_logger::init();
}

let pkg_type = figure_out_provider(tmpl_type, &pkg_name).unwrap();

info!(
"Generating template for package {} of type {}",
pkg_name, tmpl_type
"Generating template for package {} of type {:?}",
&pkg_name, pkg_type
);

let pkg_info = if tmpl_type == "crate" {
let pkg_info = if pkg_type == PkgType::Crate {
crate_info(&pkg_name).expect("Failed to get the crate's info")
} else {
gem_info(&pkg_name).expect("Failed to get the gem's info")
};

write_template(&pkg_info, force_overwrite, tmpl_type).expect("Failed to write the template!");
write_template(&pkg_info, force_overwrite, pkg_type).expect("Failed to write the template!");
}
@@ -9,7 +9,7 @@ use types::*;
pub fn write_template(
pkg_info: &PkgInfo,
force_overwrite: bool,
tmpl_type: String,
tmpl_type: PkgType,
) -> Result<(), Error> {
let template_in = include_str!("template.in");

@@ -36,7 +36,7 @@ pub fn write_template(
.replace("@homepage@", &pkg_info.homepage)
.replace("@maintainer@", &maintainer);

if tmpl_type == "gem" {
if tmpl_type == PkgType::Gem {
let dependencies = &pkg_info.dependencies.as_ref().unwrap();

let mut makedepends = String::new();
@@ -8,6 +8,12 @@ pub enum Error {
Gem(rubygems_api::Error),
}

#[derive(Debug, PartialEq)]
pub enum PkgType {
Crate,
Gem,
}

impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::File(e)

0 comments on commit 7e6ea50

Please sign in to comment.
You can’t perform that action at this time.