From 21b7418a38d815bcc9f474b9c57fb8a430994d06 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 18 Jun 2014 14:42:07 -0700 Subject: [PATCH] PathSource - load packages in update fn --- src/bin/cargo-read-manifest.rs | 7 +++++-- src/cargo/ops/cargo_compile.rs | 8 ++++++-- src/cargo/sources/path.rs | 37 +++++++++++++++++++++------------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/bin/cargo-read-manifest.rs b/src/bin/cargo-read-manifest.rs index 3c3b8694d..4d97627ce 100644 --- a/src/bin/cargo-read-manifest.rs +++ b/src/bin/cargo-read-manifest.rs @@ -6,7 +6,7 @@ extern crate hammer; use hammer::FlagConfig; use cargo::{execute_main_without_stdin,CLIResult,CLIError}; -use cargo::core::{Package,SourceId}; +use cargo::core::{Package,Source,SourceId}; use cargo::sources::{PathSource}; #[deriving(PartialEq,Clone,Decodable)] @@ -22,8 +22,11 @@ fn main() { fn execute(options: Options) -> CLIResult> { let source_id = SourceId::for_path(&Path::new(options.manifest_path.as_slice())); + let mut source = PathSource::new(&source_id); - PathSource::new(&source_id) + try!(source.update().map_err(|err| CLIError::new(err.get_desc(), Some(err.get_detail()), 1))); + + source .get_root_package() .map(|pkg| Some(pkg)) .map_err(|err| CLIError::new(err.get_desc(), Some(err.get_detail()), 1)) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index fd94204fc..dafa492bd 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -16,7 +16,7 @@ use std::os; use util::config::{ConfigValue}; -use core::{SourceId,PackageSet,resolver}; +use core::{Source,SourceId,PackageSet,resolver}; use core::registry::PackageRegistry; use ops; use sources::{PathSource}; @@ -25,8 +25,12 @@ use util::{CargoResult,Wrap,config,other_error}; pub fn compile(manifest_path: &Path) -> CargoResult<()> { log!(4, "compile; manifest-path={}", manifest_path.display()); + let mut source = PathSource::new(&SourceId::for_path(&manifest_path.dir_path())); + + try!(source.update()); + // TODO: Move this into PathSource - let package = try!(PathSource::new(&SourceId::for_path(&manifest_path.dir_path())).get_root_package()); + let package = try!(source.get_root_package()); debug!("loaded package; package={}", package); let override_ids = try!(source_ids_from_config()); diff --git a/src/cargo/sources/path.rs b/src/cargo/sources/path.rs index 1322ce1e1..c8e9d6596 100644 --- a/src/cargo/sources/path.rs +++ b/src/cargo/sources/path.rs @@ -6,7 +6,8 @@ use util::{CargoResult,simple_human}; pub struct PathSource { id: SourceId, - path: Path, + updated: bool, + packages: Vec } /** @@ -24,26 +25,29 @@ impl PathSource { log!(5, "new; id={}", id); assert!(id.is_path(), "does not represent a path source; id={}", id); - let path = Path::new(id.get_url().path.as_slice()); - PathSource { id: id.clone(), - path: path + updated: false, + packages: Vec::new() } } + fn path(&self) -> Path { + Path::new(self.id.get_url().path.as_slice()) + } + pub fn get_root_package(&self) -> CargoResult { log!(5, "get_root_package; source={}", self); - match (try!(self.packages())).as_slice().head() { + if !self.updated { + return Err(simple_human("source has not been updated")) + } + + match self.packages.as_slice().head() { Some(pkg) => Ok(pkg.clone()), None => Err(simple_human("no package found in source")) } } - - fn packages(&self) -> CargoResult> { - ops::read_packages(&self.path, &self.id) - } } impl Show for PathSource { @@ -54,12 +58,19 @@ impl Show for PathSource { impl Source for PathSource { fn update(&mut self) -> CargoResult<()> { + if !self.updated { + let pkgs = try!(ops::read_packages(&self.path(), &self.id)); + self.packages.push_all_move(pkgs); + self.updated = true; + } + Ok(()) } fn list(&self) -> CargoResult> { - let pkgs = try!(self.packages()); - Ok(pkgs.iter().map(|p| p.get_summary().clone()).collect()) + Ok(self.packages.iter() + .map(|p| p.get_summary().clone()) + .collect()) } fn download(&self, _: &[PackageId]) -> CargoResult<()>{ @@ -70,9 +81,7 @@ impl Source for PathSource { fn get(&self, ids: &[PackageId]) -> CargoResult> { log!(5, "getting packages; ids={}", ids); - let pkgs = try!(self.packages()); - - Ok(pkgs.iter() + Ok(self.packages.iter() .filter(|pkg| ids.iter().any(|id| pkg.get_package_id() == id)) .map(|pkg| pkg.clone()) .collect())