Skip to content

Commit

Permalink
main: automatically figure out if we're dealing with a crate or a gem
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasmus Thomsen committed Nov 10, 2018
1 parent a9345dc commit 7e6ea50
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -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.",
Expand All @@ -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())
}
}
24 changes: 17 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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!");
}
4 changes: 2 additions & 2 deletions src/tmplwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7e6ea50

Please sign in to comment.