Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to remove code duplication and be more pipeliney #1991

Merged
merged 7 commits into from
Sep 22, 2015
13 changes: 11 additions & 2 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::slice;
use std::path::{Path, PathBuf};
use semver::Version;

use core::{Dependency, Manifest, PackageId, Registry, Target, Summary, Metadata};
use core::{Dependency, Manifest, PackageId, SourceId, Registry, Target, Summary, Metadata};
use ops;
use core::dependency::SerializedDependency;
use util::{CargoResult, graph};
use util::{CargoResult, graph, Config};
use rustc_serialize::{Encoder,Encodable};
use core::source::Source;

Expand Down Expand Up @@ -58,6 +59,14 @@ impl Package {
}
}

pub fn for_path(manifest_path: &Path, config: &Config) -> CargoResult<Package> {
let path = manifest_path.parent().unwrap();
let source_id = try!(SourceId::for_path(path));
let (pkg, _) = try!(ops::read_package(&manifest_path, &source_id,
config));
Ok(pkg)
}

pub fn dependencies(&self) -> &[Dependency] { self.manifest.dependencies() }
pub fn manifest(&self) -> &Manifest { &self.manifest }
pub fn manifest_path(&self) -> &Path { &self.manifest_path }
Expand Down
5 changes: 0 additions & 5 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ impl<'cfg> PackageRegistry<'cfg> {
Ok(())
}

pub fn preload(&mut self, id: &SourceId, source: Box<Source + 'cfg>) {
self.sources.insert(id, source);
self.source_ids.insert(id.clone(), (id.clone(), Kind::Locked));
}

pub fn add_sources(&mut self, ids: &[SourceId]) -> CargoResult<()> {
for id in ids.iter() {
try!(self.load(id, Kind::Locked));
Expand Down
8 changes: 2 additions & 6 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::fs;
use std::io::prelude::*;
use std::path::Path;

use core::{PackageSet, Profiles, Profile};
use core::{Package, PackageSet, Profiles, Profile};
use core::source::{Source, SourceMap};
use sources::PathSource;
use util::{CargoResult, human, ChainError, Config};
use ops::{self, Layout, Context, BuildConfig, Kind};

Expand All @@ -17,10 +16,7 @@ pub struct CleanOptions<'a> {

/// Cleans the project from build artifacts.
pub fn clean(manifest_path: &Path, opts: &CleanOptions) -> CargoResult<()> {
let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
opts.config));
try!(src.update());
let root = try!(src.root_package());
let root = try!(Package::for_path(manifest_path, opts.config));
let target_dir = opts.config.target_dir(&root);

// If we have a spec, then we need to delete some package,s otherwise, just
Expand Down
26 changes: 4 additions & 22 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;

use core::registry::PackageRegistry;
use core::{Source, SourceId, PackageSet, Package, Target, PackageId};
use core::{Source, SourceId, PackageSet, Package, Target};
use core::{Profile, TargetKind};
use core::resolver::Method;
use ops::{self, BuildOutput, ExecEngine};
use sources::{PathSource};
use util::config::{ConfigValue, Config};
use util::{CargoResult, internal, human, ChainError, profile};

Expand Down Expand Up @@ -87,22 +86,16 @@ pub fn compile<'a>(manifest_path: &Path,
-> CargoResult<ops::Compilation<'a>> {
debug!("compile; manifest-path={}", manifest_path.display());

let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(),
options.config));
try!(source.update());

// TODO: Move this into PathSource
let package = try!(source.root_package());
let package = try!(Package::for_path(manifest_path, options.config));
debug!("loaded package; package={}", package);

for key in package.manifest().warnings().iter() {
try!(options.config.shell().warn(key))
}
compile_pkg(&package, Some(Box::new(source)), options)
compile_pkg(&package, options)
}

pub fn compile_pkg<'a>(package: &Package,
source: Option<Box<Source + 'a>>,
options: &CompileOptions<'a>)
-> CargoResult<ops::Compilation<'a>> {
let CompileOptions { config, jobs, target, spec, features,
Expand All @@ -127,12 +120,6 @@ pub fn compile_pkg<'a>(package: &Package,

let (packages, resolve_with_overrides, sources) = {
let mut registry = PackageRegistry::new(config);
if let Some(source) = source {
registry.preload(package.package_id().source_id(), source);
} else {
try!(registry.add_sources(&[package.package_id().source_id()
.clone()]));
}

// First, resolve the package's *listed* dependencies, as well as
// downloading and updating all remotes and such.
Expand All @@ -155,12 +142,7 @@ pub fn compile_pkg<'a>(package: &Package,
try!(ops::resolve_with_previous(&mut registry, package, method,
Some(&resolve), None));

let req: Vec<PackageId> = resolved_with_overrides.iter().map(|r| {
r.clone()
}).collect();
let packages = try!(registry.get(&req).chain_error(|| {
human("Unable to get packages from source")
}));
let packages = try!(ops::get_resolved_packages(&resolved_with_overrides, &mut registry));

(packages, resolved_with_overrides, registry.move_sources())
};
Expand Down
9 changes: 2 additions & 7 deletions src/cargo/ops/cargo_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use std::fs;
use std::path::Path;
use std::process::Command;

use core::PackageIdSpec;
use core::source::Source;
use core::{Package, PackageIdSpec};
use ops;
use sources::PathSource;
use util::{CargoResult, human};

pub struct DocOptions<'a> {
Expand All @@ -16,10 +14,7 @@ pub struct DocOptions<'a> {

pub fn doc(manifest_path: &Path,
options: &DocOptions) -> CargoResult<()> {
let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(),
options.compile_opts.config));
try!(source.update());
let package = try!(source.root_package());
let package = try!(Package::for_path(manifest_path, options.compile_opts.config));

let mut lib_names = HashSet::new();
let mut bin_names = HashSet::new();
Expand Down
22 changes: 10 additions & 12 deletions src/cargo/ops/cargo_fetch.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
use std::path::Path;

use core::registry::PackageRegistry;
use core::{Source, PackageId};
use core::{Package, PackageId, Resolve};
use ops;
use sources::PathSource;
use util::{CargoResult, Config, human, ChainError};

/// Executes `cargo fetch`.
pub fn fetch(manifest_path: &Path, config: &Config) -> CargoResult<()> {
let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(),
config));
try!(source.update());
let package = try!(source.root_package());

let package = try!(Package::for_path(manifest_path, config));
let mut registry = PackageRegistry::new(config);
registry.preload(package.package_id().source_id(), Box::new(source));
let resolve = try!(ops::resolve_pkg(&mut registry, &package));
let _ = get_resolved_packages(&resolve, &mut registry);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it should be wrapped in a try!, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooops yep!

Ok(())
}

pub fn get_resolved_packages(resolve: &Resolve, registry: &mut PackageRegistry)
-> CargoResult<Vec<Package>> {
let ids: Vec<PackageId> = resolve.iter().cloned().collect();
try!(registry.get(&ids).chain_error(|| {
human("unable to get packages from source")
}));
Ok(())
registry.get(&ids).chain_error(|| {
human("Unable to get packages from source")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this stay as lowercase? (conventionally our errors start with lowercase letters)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oops! Picked the wrong almost-duplicate :)

})
}
15 changes: 3 additions & 12 deletions src/cargo/ops/cargo_generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::path::Path;

use core::PackageId;
use core::registry::PackageRegistry;
use core::{Source, Resolve, SourceId};
use core::{Resolve, SourceId, Package};
use core::resolver::Method;
use ops;
use sources::{PathSource};
use util::config::{Config};
use util::{CargoResult, human};

Expand All @@ -19,12 +18,8 @@ pub struct UpdateOptions<'a> {

pub fn generate_lockfile(manifest_path: &Path, config: &Config)
-> CargoResult<()> {
let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(),
config));
try!(source.update());
let package = try!(source.root_package());
let package = try!(Package::for_path(manifest_path, config));
let mut registry = PackageRegistry::new(config);
registry.preload(package.package_id().source_id(), Box::new(source));
let resolve = try!(ops::resolve_with_previous(&mut registry, &package,
Method::Everything,
None, None));
Expand All @@ -34,10 +29,7 @@ pub fn generate_lockfile(manifest_path: &Path, config: &Config)

pub fn update_lockfile(manifest_path: &Path,
opts: &UpdateOptions) -> CargoResult<()> {
let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(),
opts.config));
try!(source.update());
let package = try!(source.root_package());
let package = try!(Package::for_path(manifest_path, opts.config));

let previous_resolve = match try!(ops::load_pkg_lockfile(&package)) {
Some(resolve) => resolve,
Expand Down Expand Up @@ -85,7 +77,6 @@ pub fn update_lockfile(manifest_path: &Path,
None => to_avoid.extend(previous_resolve.iter()),
}

registry.preload(package.package_id().source_id(), Box::new(source));
let resolve = try!(ops::resolve_with_previous(&mut registry,
&package,
Method::Everything,
Expand Down
5 changes: 2 additions & 3 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tar::Archive;
use flate2::{GzBuilder, Compression};
use flate2::read::GzDecoder;

use core::{Source, SourceId, Package, PackageId};
use core::{SourceId, Package, PackageId};
use sources::PathSource;
use util::{self, CargoResult, human, internal, ChainError, Config};
use ops;
Expand All @@ -29,7 +29,6 @@ pub fn package(manifest_path: &Path,
metadata: bool) -> CargoResult<Option<PathBuf>> {
let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
config));
try!(src.update());
let pkg = try!(src.root_package());

if metadata {
Expand Down Expand Up @@ -179,7 +178,7 @@ fn run_verify(config: &Config, pkg: &Package, tar: &Path)
let new_pkg = Package::new(new_manifest, &manifest_path);

// Now that we've rewritten all our path dependencies, compile it!
try!(ops::compile_pkg(&new_pkg, None, &ops::CompileOptions {
try!(ops::compile_pkg(&new_pkg, &ops::CompileOptions {
config: config,
jobs: None,
target: None,
Expand Down
8 changes: 2 additions & 6 deletions src/cargo/ops/cargo_pkgid.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use std::path::Path;

use ops;
use core::{Source, PackageIdSpec};
use sources::{PathSource};
use core::{PackageIdSpec, Package};
use util::{CargoResult, human, Config};

pub fn pkgid(manifest_path: &Path,
spec: Option<&str>,
config: &Config) -> CargoResult<PackageIdSpec> {
let mut source = try!(PathSource::for_path(&manifest_path.parent().unwrap(),
config));
try!(source.update());
let package = try!(source.root_package());
let package = try!(Package::for_path(manifest_path, config));

let lockfile = package.root().join("Cargo.lock");
let source_id = package.package_id().source_id();
Expand Down
8 changes: 2 additions & 6 deletions src/cargo/ops/cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ use std::path::Path;

use ops::{self, ExecEngine, CompileFilter};
use util::{self, CargoResult, human, process, ProcessError};
use core::source::Source;
use sources::PathSource;
use core::Package;

pub fn run(manifest_path: &Path,
options: &ops::CompileOptions,
args: &[String]) -> CargoResult<Option<ProcessError>> {
let config = options.config;
let mut src = try!(PathSource::for_path(&manifest_path.parent().unwrap(),
config));
try!(src.update());
let root = try!(src.root_package());
let root = try!(Package::for_path(manifest_path, config));

let mut bins = root.manifest().targets().iter().filter(|a| {
!a.is_lib() && !a.is_custom_build() && match options.filter {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use self::cargo_package::package;
pub use self::registry::{publish, registry_configuration, RegistryConfig};
pub use self::registry::{registry_login, search, http_proxy_exists, http_handle};
pub use self::registry::{modify_owners, yank, OwnersOptions};
pub use self::cargo_fetch::{fetch};
pub use self::cargo_fetch::{fetch, get_resolved_packages};
pub use self::cargo_pkgid::pkgid;
pub use self::resolve::{resolve_pkg, resolve_with_previous};

Expand Down
17 changes: 4 additions & 13 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use core::{Package, SourceId};
use core::dependency::Kind;
use core::manifest::ManifestMetadata;
use ops;
use sources::{PathSource, RegistrySource};
use sources::{RegistrySource};
use util::config;
use util::{CargoResult, human, ChainError, ToUrl};
use util::config::{Config, ConfigValue, Location};
Expand All @@ -31,10 +31,7 @@ pub fn publish(manifest_path: &Path,
token: Option<String>,
index: Option<String>,
verify: bool) -> CargoResult<()> {
let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
config));
try!(src.update());
let pkg = try!(src.root_package());
let pkg = try!(Package::for_path(&manifest_path, config));

let (mut registry, reg_id) = try!(registry(config, token, index));
try!(verify_dependencies(&pkg, &reg_id));
Expand Down Expand Up @@ -264,10 +261,7 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
Some(ref name) => name.clone(),
None => {
let manifest_path = try!(find_root_manifest_for_cwd(None));
let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
config));
try!(src.update());
let pkg = try!(src.root_package());
let pkg = try!(Package::for_path(&manifest_path, config));
pkg.name().to_string()
}
};
Expand Down Expand Up @@ -327,10 +321,7 @@ pub fn yank(config: &Config,
Some(name) => name,
None => {
let manifest_path = try!(find_root_manifest_for_cwd(None));
let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
config));
try!(src.update());
let pkg = try!(src.root_package());
let pkg = try!(Package::for_path(&manifest_path, config));
pkg.name().to_string()
}
};
Expand Down
3 changes: 3 additions & 0 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub fn resolve_with_previous<'a>(registry: &mut PackageRegistry,
to_avoid: Option<&HashSet<&'a PackageId>>)
-> CargoResult<Resolve> {

try!(registry.add_sources(&[package.package_id().source_id()
.clone()]));

// Here we place an artificial limitation that all non-registry sources
// cannot be locked at more than one revision. This means that if a git
// repository provides more than one package, they must all be updated in
Expand Down
6 changes: 2 additions & 4 deletions src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ impl<'cfg> PathSource<'cfg> {
}
}

pub fn root_package(&self) -> CargoResult<Package> {
pub fn root_package(&mut self) -> CargoResult<Package> {
trace!("root_package; source={:?}", self);

if !self.updated {
return Err(internal("source has not been updated"))
}
try!(self.update());

match self.packages.iter().find(|p| p.root() == &*self.path) {
Some(pkg) => Ok(pkg.clone()),
Expand Down
8 changes: 6 additions & 2 deletions tests/test_cargo_compile_path_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,12 @@ test!(error_message_for_missing_manifest {
assert_that(p.cargo_process("build"),
execs()
.with_status(101)
.with_stderr(&format!("Could not find `Cargo.toml` in `{}`\n",
p.root().join("src").join("bar").display())));
.with_stderr(&format!("\
Unable to update file://[..]

Caused by:
Could not find `Cargo.toml` in `{}`
", p.root().join("src").join("bar").display())));

});

Expand Down