aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDoug Goldstein <cardoe@cardoe.com>2019-12-26 12:26:38 -0600
committerDoug Goldstein <cardoe@cardoe.com>2020-01-26 09:20:19 -0600
commit2649758e5431c75109f7e8564587a0f7a965b20f (patch)
tree217044458e8feb30f3628287edb696e86508cea2 /src
parentadd package name and version to metadata struct (diff)
downloadcargo-ebuild-2649758e5431c75109f7e8564587a0f7a965b20f.tar.gz
cargo-ebuild-2649758e5431c75109f7e8564587a0f7a965b20f.tar.bz2
cargo-ebuild-2649758e5431c75109f7e8564587a0f7a965b20f.zip
split metadata gathering and ebuild writing functions
Split the metadata gathering into its own function, separate from the ebuild writing function.
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs33
-rw-r--r--src/main.rs14
2 files changed, 29 insertions, 18 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 209f957..9e380ec 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -10,11 +10,11 @@
mod metadata;
-use anyhow::{format_err, Result};
+use anyhow::{format_err, Context, Result};
use std::collections::BTreeSet;
use std::fs::OpenOptions;
use std::io::Write;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use metadata::EbuildConfig;
@@ -27,7 +27,11 @@ fn parse_license<'a>(lic_str: &'a str) -> Vec<&'a str> {
.collect()
}
-pub fn run(verbose: u32, quiet: bool, manifest_path: Option<PathBuf>) -> Result<()> {
+pub fn gen_ebuild_data(
+ verbose: u32,
+ quiet: bool,
+ manifest_path: Option<PathBuf>,
+) -> Result<EbuildConfig> {
let mut cmd = cargo_metadata::MetadataCommand::new();
if let Some(path) = manifest_path {
@@ -73,20 +77,20 @@ pub fn run(verbose: u32, quiet: bool, manifest_path: Option<PathBuf>) -> Result<
// sort the crates
crates.sort();
- let root_pkg_name_ver = format!("{}-{}", root_pkg.name, root_pkg.version);
-
- let ebuild_data = EbuildConfig::from_package(root_pkg, crates, licenses);
-
- // build up the ebuild path
- let ebuild_path = PathBuf::from(format!("{}.ebuild", root_pkg_name_ver));
+ Ok(EbuildConfig::from_package(root_pkg, crates, licenses))
+}
+pub fn write_ebuild(ebuild_data: EbuildConfig, ebuild_path: impl AsRef<Path>) -> Result<()> {
// Open the file where we'll write the ebuild
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&ebuild_path)
- .expect("failed to create ebuild");
+ .context(format!(
+ "Unable to create {}",
+ ebuild_path.as_ref().display()
+ ))?;
// write the contents out
write!(
@@ -99,9 +103,8 @@ pub fn run(verbose: u32, quiet: bool, manifest_path: Option<PathBuf>) -> Result<
cargo_ebuild_ver = env!("CARGO_PKG_VERSION"),
this_year = 1900 + time::now().tm_year,
)
- .expect("unable to write ebuild to disk");
-
- println!("Wrote: {}", ebuild_path.display());
-
- Ok(())
+ .context(format!(
+ "Failed to write to {}",
+ ebuild_path.as_ref().display()
+ ))
}
diff --git a/src/main.rs b/src/main.rs
index e0d550b..bc3c520 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,7 +12,7 @@ extern crate cargo_ebuild;
extern crate structopt;
use anyhow::Result;
-use cargo_ebuild::run;
+use cargo_ebuild::{gen_ebuild_data, write_ebuild};
use std::path::PathBuf;
use structopt::clap::AppSettings;
use structopt::StructOpt;
@@ -48,6 +48,14 @@ enum Opt {
fn main() -> Result<()> {
let Opt::Ebuild(opt) = Opt::from_args();
- // run the actual code
- run(opt.verbose as u32, opt.quiet, opt.manifest_path)
+ // compute the data from the package that the build needs
+ let ebuild_data = gen_ebuild_data(opt.verbose as u32, opt.quiet, opt.manifest_path)?;
+
+ let ebuild_path = format!("{}-{}.ebuild", ebuild_data.name, ebuild_data.version);
+
+ write_ebuild(ebuild_data, &ebuild_path)?;
+
+ println!("Wrote: {}", ebuild_path);
+
+ Ok(())
}