diff options
author | Doug Goldstein <cardoe@cardoe.com> | 2018-06-29 10:42:01 -0500 |
---|---|---|
committer | Doug Goldstein <cardoe@cardoe.com> | 2018-06-29 10:42:01 -0500 |
commit | c1e69a50a2f0ec20a52f4e9d4e71aacbd22a9774 (patch) | |
tree | 722817817e2d4f11ce2814c4140dc5d808ff5d76 | |
parent | Merge pull request #13 from cardoe/travis-updates (diff) | |
download | cargo-ebuild-c1e69a50a2f0ec20a52f4e9d4e71aacbd22a9774.tar.gz cargo-ebuild-c1e69a50a2f0ec20a52f4e9d4e71aacbd22a9774.tar.bz2 cargo-ebuild-c1e69a50a2f0ec20a52f4e9d4e71aacbd22a9774.zip |
separate functionality into a lib
To allow for testing the core functionality needs to be in a lib due to
the way testing works with Rust and Cargo. The binary is now just the
argument parser and then calling into the actual code in the lib.
-rw-r--r-- | Cargo.lock | 20 | ||||
-rw-r--r-- | Cargo.toml | 8 | ||||
-rw-r--r-- | src/lib.rs | 155 | ||||
-rw-r--r-- | src/main.rs | 138 |
4 files changed, 176 insertions, 145 deletions
@@ -1,13 +1,3 @@ -[root] -name = "cargo-ebuild" -version = "0.1.6-pre" -dependencies = [ - "cargo 0.21.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "advapi32-sys" version = "0.2.0" @@ -114,6 +104,16 @@ dependencies = [ ] [[package]] +name = "cargo-ebuild" +version = "0.1.6-pre" +dependencies = [ + "cargo 0.21.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "cc" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -13,6 +13,14 @@ description = """ Generates an ebuild for a package using the in-tree eclasses. """ +[[bin]] +name = "cargo-ebuild" +path = "src/main.rs" + +[lib] +name = "cargo_ebuild" +path = "src/lib.rs" + [badges] travis-ci = { repository = "cardoe/cargo-ebuild" } diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f6da6bf --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,155 @@ +/* + * Copyright 2016-2018 Doug Goldstein <cardoe@cardoe.com> + * + * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or + * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license + * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your + * option. This file may not be copied, modified, or distributed + * except according to those terms. + */ + +extern crate cargo; +#[macro_use] +extern crate serde_derive; +extern crate time; + +use cargo::core::registry::PackageRegistry; +use cargo::core::resolver::Method; +use cargo::core::{Package, PackageSet, Resolve, Workspace}; +use cargo::ops; +use cargo::util::{important_paths, CargoResult, CargoResultExt}; +use cargo::{CliResult, Config}; +use std::fs::OpenOptions; +use std::io::Write; +use std::path::PathBuf; + +/// Finds the root Cargo.toml of the workspace +fn workspace(config: &Config, manifest_path: Option<String>) -> CargoResult<Workspace> { + let root = important_paths::find_root_manifest_for_wd(manifest_path, config.cwd())?; + Workspace::new(&root, config) +} + +/// Generates a package registry by using the Cargo.lock or creating one as necessary +fn registry<'a>(config: &'a Config, package: &Package) -> CargoResult<PackageRegistry<'a>> { + let mut registry = PackageRegistry::new(config)?; + registry.add_sources(&[package.package_id().source_id().clone()])?; + Ok(registry) +} + +/// Resolve the packages necessary for the workspace +fn resolve<'a>( + registry: &mut PackageRegistry, + workspace: &'a Workspace, +) -> CargoResult<(PackageSet<'a>, Resolve)> { + // resolve our dependencies + let (packages, resolve) = ops::resolve_ws(workspace)?; + + // resolve with all features set so we ensure we get all of the depends downloaded + let resolve = ops::resolve_with_previous( + registry, + workspace, + /* resolve it all */ + Method::Everything, + /* previous */ + Some(&resolve), + /* don't avoid any */ + None, + /* specs */ + &[], + )?; + + Ok((packages, resolve)) +} + +#[derive(Deserialize)] +pub struct Options { + flag_verbose: u32, + flag_quiet: Option<bool>, +} + +pub fn real_main(options: Options, config: &Config) -> CliResult { + config.configure( + options.flag_verbose, + options.flag_quiet, + /* color */ + &None, + /* frozen */ + false, + /* locked */ + false, + )?; + + // Load the workspace and current package + let workspace = workspace(config, None)?; + let package = workspace.current()?; + + // Resolve all dependencies (generate or use Cargo.lock as necessary) + let mut registry = registry(config, &package)?; + let resolve = resolve(&mut registry, &workspace)?; + + // build the crates the package needs + let mut crates = resolve + .1 + .iter() + .map(|pkg| format!("{}-{}\n", pkg.name(), pkg.version())) + .collect::<Vec<String>>(); + + // sort the crates + crates.sort(); + + // root package metadata + let metadata = package.manifest().metadata(); + + // package description + let desc = metadata + .description + .as_ref() + .cloned() + .unwrap_or_else(|| String::from(package.name())); + + // package homepage + let homepage = metadata.homepage.as_ref().cloned().unwrap_or( + metadata + .repository + .as_ref() + .cloned() + .unwrap_or_else(|| String::from("")), + ); + + let license = metadata + .license + .as_ref() + .cloned() + .unwrap_or_else(|| String::from("unknown license")); + + // build up the ebuild path + let ebuild_path = PathBuf::from(format!("{}-{}.ebuild", package.name(), package.version())); + + // Open the file where we'll write the ebuild + let mut file = try!( + OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(&ebuild_path) + .chain_err(|| "failed to create ebuild") + ); + + // write the contents out + try!( + write!( + file, + include_str!("ebuild.template"), + description = desc.trim(), + homepage = homepage.trim(), + license = license.trim(), + crates = crates.join(""), + cargo_ebuild_ver = env!("CARGO_PKG_VERSION"), + this_year = 1900 + time::now().tm_year, + ).chain_err(|| "unable to write ebuild to disk") + ); + + println!("Wrote: {}", ebuild_path.display()); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index ef9b4ad..9626f8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,62 +9,11 @@ */ extern crate cargo; -#[macro_use] -extern crate serde_derive; -extern crate time; +extern crate cargo_ebuild; -use cargo::{Config, CliResult}; -use cargo::core::{Package, PackageSet, Resolve, Workspace}; -use cargo::core::registry::PackageRegistry; -use cargo::core::resolver::Method; -use cargo::ops; -use cargo::util::{important_paths, CargoResult, CargoResultExt}; +use cargo::Config; +use cargo_ebuild::real_main; use std::env; -use std::fs::OpenOptions; -use std::io::Write; -use std::path::PathBuf; - -/// Finds the root Cargo.toml of the workspace -fn workspace(config: &Config, manifest_path: Option<String>) -> CargoResult<Workspace> { - let root = important_paths::find_root_manifest_for_wd(manifest_path, config.cwd())?; - Workspace::new(&root, config) -} - -/// Generates a package registry by using the Cargo.lock or creating one as necessary -fn registry<'a>(config: &'a Config, package: &Package) -> CargoResult<PackageRegistry<'a>> { - let mut registry = PackageRegistry::new(config)?; - registry - .add_sources(&[package.package_id().source_id().clone()])?; - Ok(registry) -} - -/// Resolve the packages necessary for the workspace -fn resolve<'a>(registry: &mut PackageRegistry, - workspace: &'a Workspace) - -> CargoResult<(PackageSet<'a>, Resolve)> { - // resolve our dependencies - let (packages, resolve) = ops::resolve_ws(workspace)?; - - // resolve with all features set so we ensure we get all of the depends downloaded - let resolve = ops::resolve_with_previous(registry, - workspace, - /* resolve it all */ - Method::Everything, - /* previous */ - Some(&resolve), - /* don't avoid any */ - None, - /* specs */ - &[])?; - - Ok((packages, resolve)) -} - -#[derive(Deserialize)] -struct Options { - flag_verbose: u32, - flag_quiet: Option<bool>, -} const USAGE: &'static str = r#" Create an ebuild for a project @@ -86,84 +35,3 @@ fn main() { cargo::exit_with_error(e, &mut *config.shell()); } } - -fn real_main(options: Options, config: &Config) -> CliResult { - config - .configure(options.flag_verbose, - options.flag_quiet, - /* color */ - &None, - /* frozen */ - false, - /* locked */ - false)?; - - // Load the workspace and current package - let workspace = workspace(config, None)?; - let package = workspace.current()?; - - // Resolve all dependencies (generate or use Cargo.lock as necessary) - let mut registry = registry(config, &package)?; - let resolve = resolve(&mut registry, &workspace)?; - - // build the crates the package needs - let mut crates = resolve - .1 - .iter() - .map(|pkg| format!("{}-{}\n", pkg.name(), pkg.version())) - .collect::<Vec<String>>(); - - // sort the crates - crates.sort(); - - // root package metadata - let metadata = package.manifest().metadata(); - - // package description - let desc = metadata - .description - .as_ref() - .cloned() - .unwrap_or_else(|| String::from(package.name())); - - // package homepage - let homepage = - metadata.homepage.as_ref().cloned().unwrap_or(metadata - .repository - .as_ref() - .cloned() - .unwrap_or_else(|| String::from(""))); - - let license = metadata - .license - .as_ref() - .cloned() - .unwrap_or_else(|| String::from("unknown license")); - - // build up the ebuild path - let ebuild_path = PathBuf::from(format!("{}-{}.ebuild", package.name(), package.version())); - - // Open the file where we'll write the ebuild - let mut file = try!(OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(&ebuild_path) - .chain_err(|| "failed to create ebuild")); - - // write the contents out - try!(write!(file, - include_str!("ebuild.template"), - description = desc.trim(), - homepage = homepage.trim(), - license = license.trim(), - crates = crates.join(""), - cargo_ebuild_ver = env!("CARGO_PKG_VERSION"), - this_year = 1900 + time::now().tm_year, - ).chain_err(|| "unable to write ebuild to disk")); - - println!("Wrote: {}", ebuild_path.display()); - - - Ok(()) -} |