aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Goldstein <cardoe@cardoe.com>2019-12-23 20:09:11 -0600
committerDoug Goldstein <cardoe@cardoe.com>2020-01-26 09:20:10 -0600
commit92920325a7ba4d39a7443d10fec19a8a35ece40b (patch)
tree570503b62515258de8a31d4fc2d817e394d0473d
parentadd some more badges for crates.io (diff)
downloadcargo-ebuild-92920325a7ba4d39a7443d10fec19a8a35ece40b.tar.gz
cargo-ebuild-92920325a7ba4d39a7443d10fec19a8a35ece40b.tar.bz2
cargo-ebuild-92920325a7ba4d39a7443d10fec19a8a35ece40b.zip
switch to cargo_metadata for dep info
Switched from using Cargo to resolve the dependencies to using the cargo_metadata crate, which calls Cargo and parses the machine parseable data back.
-rw-r--r--Cargo.lock14
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs76
-rw-r--r--src/main.rs7
4 files changed, 44 insertions, 55 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 475d694..882e910 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -158,11 +158,24 @@ name = "cargo-ebuild"
version = "0.2.1-alpha.0"
dependencies = [
"cargo 0.37.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cargo_metadata 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
+name = "cargo_metadata"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "cc"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1202,6 +1215,7 @@ dependencies = [
"checksum bytesize 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "716960a18f978640f25101b5cbf1c6f6b0d3192fab36a2d98ca96f0ecbe41010"
"checksum c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7d64d04786e0f528460fc884753cf8dddcc466be308f6026f8e355c41a0e4101"
"checksum cargo 0.37.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e6924b75f0de3b87ffd13f291224a2eb0f92e961b287d900863fc586f8ecd833"
+"checksum cargo_metadata 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "46e3374c604fb39d1a2f35ed5e4a4e30e60d01fab49446e08f1b3e9a90aef202"
"checksum cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)" = "4fc9a35e1f4290eb9e5fc54ba6cf40671ed2a2514c3eeb2b2a908dda2ea5a1be"
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
diff --git a/Cargo.toml b/Cargo.toml
index ab7e8d1..828d38d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -30,5 +30,7 @@ maintenance = { status = "passively-maintained" }
[dependencies]
cargo = "^0.37"
+cargo_metadata = "^0.9"
+failure = "^0.1"
structopt = "^0.3"
time = "^0.1"
diff --git a/src/lib.rs b/src/lib.rs
index 2903814..3258dbc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -13,59 +13,41 @@ extern crate time;
mod metadata;
-use cargo::core::registry::PackageRegistry;
-use cargo::core::resolver::Method;
-use cargo::core::{Package, PackageSet, Resolve, Workspace};
-use cargo::ops;
+use cargo::core::Workspace;
use cargo::util::{important_paths, CargoResult};
use cargo::{CliResult, Config};
+use failure::format_err;
use std::fs::OpenOptions;
use std::io::Write;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use metadata::EbuildConfig;
/// Finds the root Cargo.toml of the workspace
-fn workspace(config: &Config) -> CargoResult<Workspace> {
- let root = important_paths::find_root_manifest_for_wd(config.cwd())?;
+fn workspace(config: &Config, manifest: impl AsRef<Path>) -> CargoResult<Workspace> {
+ let root = important_paths::find_root_manifest_for_wd(manifest.as_ref())?;
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(vec![package.package_id().source_id()])?;
- Ok(registry)
-}
+pub fn run(verbose: u32, quiet: bool, manifest_path: Option<PathBuf>) -> CliResult {
+ let mut cmd = cargo_metadata::MetadataCommand::new();
-/// Resolve the packages necessary for the workspace
-fn resolve<'a>(
- registry: &mut PackageRegistry<'a>,
- workspace: &Workspace<'a>,
-) -> 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 */
- &[],
- /* warn */
- true,
- )?;
+ if let Some(path) = manifest_path {
+ cmd.manifest_path(path);
+ }
- Ok((packages, resolve))
-}
+ let metadata = cmd
+ .exec()
+ .map_err(|e| format_err!("cargo metadata failed: {}", e))?;
+
+ let mut crates = Vec::with_capacity(metadata.packages.len());
+ for pkg in metadata.packages {
+ crates.push(format!("{}-{}\n", pkg.name, pkg.version));
+ }
+
+ // sort the crates
+ crates.sort();
-pub fn run(verbose: u32, quiet: bool) -> CliResult {
// create a default Cargo config
let mut config = Config::default()?;
@@ -87,23 +69,9 @@ pub fn run(verbose: u32, quiet: bool) -> CliResult {
)?;
// Load the workspace and current package
- let workspace = workspace(&config)?;
+ let workspace = workspace(&config, &metadata.workspace_root)?;
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();
-
let ebuild_data = EbuildConfig::from_package(package, crates);
// build up the ebuild path
diff --git a/src/main.rs b/src/main.rs
index 1f27e47..7999b8e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -14,6 +14,7 @@ extern crate structopt;
use cargo::util::CliError;
use cargo_ebuild::run;
+use std::path::PathBuf;
use std::process;
use structopt::clap::AppSettings;
use structopt::StructOpt;
@@ -26,6 +27,10 @@ struct Args {
/// Verbose mode (-v, -vv, -vvv, etc.)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
verbose: usize,
+
+ #[structopt(name = "PATH", long = "manifest-path", parse(from_os_str))]
+ /// Path to Cargo.toml.
+ manifest_path: Option<PathBuf>,
}
#[structopt(
@@ -46,7 +51,7 @@ fn main() {
let Opt::Ebuild(opt) = Opt::from_args();
// run the actual code
- if let Err(e) = run(opt.verbose as u32, opt.quiet) {
+ if let Err(e) = run(opt.verbose as u32, opt.quiet, opt.manifest_path) {
// break apart the error
let CliError {
error,