aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDoug Goldstein <cardoe@cardoe.com>2016-08-01 08:33:25 -0500
committerDoug Goldstein <cardoe@cardoe.com>2016-08-01 08:33:25 -0500
commitb4450c59b23b846260d93c8c47264b4cc0be0d91 (patch)
tree72dbffa5124b91c31a37fcc788b27a161842d01c /src
parentinitial version (diff)
downloadcargo-ebuild-b4450c59b23b846260d93c8c47264b4cc0be0d91.tar.gz
cargo-ebuild-b4450c59b23b846260d93c8c47264b4cc0be0d91.tar.bz2
cargo-ebuild-b4450c59b23b846260d93c8c47264b4cc0be0d91.zip
generate initial ebuild
Generates an initial ebuild for use with the in-tree eclasses to build up Cargo based packages on Gentoo.
Diffstat (limited to 'src')
-rw-r--r--src/ebuild.template20
-rw-r--r--src/main.rs70
2 files changed, 85 insertions, 5 deletions
diff --git a/src/ebuild.template b/src/ebuild.template
new file mode 100644
index 0000000..fa57587
--- /dev/null
+++ b/src/ebuild.template
@@ -0,0 +1,20 @@
+# Auto-Generated by cargo-ebuild 0.1.0
+
+EAPI=6
+
+CRATES="
+{{ crates }}"
+
+inherit cargo
+
+DESCRIPTION="{{ description }}"
+HOMEPAGE="{{ homepage }}"
+SRC_URI="$(cargo_crate_uris ${CRATES})"
+RESTRICT="mirror"
+LICENSE=""
+SLOT="0"
+KEYWORDS="~amd64"
+IUSE=""
+
+DEPEND=""
+RDEPEND=""
diff --git a/src/main.rs b/src/main.rs
index 4048a9c..dba36ee 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,11 +1,17 @@
extern crate cargo;
+extern crate rustache;
extern crate rustc_serialize;
-use cargo::{Config, CliResult};
+use cargo::{Config, CliError, CliResult};
use cargo::core::Package;
use cargo::core::registry::PackageRegistry;
use cargo::ops;
use cargo::util::important_paths;
+use rustache::HashBuilder;
+use std::error::Error;
+use std::fs::OpenOptions;
+use std::io;
+use std::path::PathBuf;
#[derive(RustcDecodable)]
struct Options {
@@ -41,11 +47,65 @@ fn real_main(options: Options, config: &Config) -> CliResult<Option<()>> {
try!(registry.add_sources(&[package.package_id().source_id().clone()]));
let resolve = try!(ops::resolve_pkg(&mut registry, &package, config));
- println!("CRATES=\"");
- for x in resolve.iter() {
- println!("{}-{}", x.name(), x.version());
+ // build the crates the package needs
+ let mut crates = Vec::<String>::new();
+ for pkg in resolve.iter() {
+ crates.push(format!("{}-{}\n", pkg.name(), pkg.version()));
}
- println!("\"");
+
+ // root package metadata
+ let metadata = package.manifest().metadata();
+
+ // package description
+ let desc = metadata.description
+ .as_ref()
+ .map(|d| d.clone())
+ .unwrap_or(String::from(package.name()));
+
+ // package homepage
+ let homepage = metadata.homepage
+ .as_ref()
+ .map(|h| h.clone())
+ .unwrap_or(
+ metadata.repository
+ .as_ref()
+ .map(|h| h.clone())
+ .unwrap_or(String::from(""))
+ );
+
+ // build up the ebuild path
+ let ebuild_path = PathBuf::from(format!("{}-{}.ebuild", package.name(), package.version()));
+
+ // build up the varibles for the template
+ let data = HashBuilder::new()
+ .insert_string("description", desc.trim())
+ .insert_string("homepage", homepage.trim())
+ .insert_string("crates", crates.join(""));
+
+ // load the ebuild template
+ let template = include_str!("ebuild.template");
+
+ // generate the ebuild using Rustache to process the template
+ let mut templ = try!(rustache::render_text(template, data).map_err(|_| {
+ CliError::new("unable to generate ebuild: {}", 1)
+ }));
+
+ // Open the file where we'll write the ebuild
+ let mut file = try!(OpenOptions::new()
+ .write(true)
+ .create(true)
+ .open(&ebuild_path)
+ .map_err(|err| {
+ CliError::new(&format!("failed to create ebuild: {}", err.description()), 1)
+ }));
+
+ // write the contents out
+ try!(io::copy(&mut templ, &mut file).map_err(|err| {
+ CliError::new(&format!("unable to write ebuild to disk: {}", err.description()), 1)
+ }));
+
+ println!("Wrote: {}", ebuild_path.display());
+
Ok(None)
}