PathSource - load packages in update fn
authorCarl Lerche <me@carllerche.com>
Wed, 18 Jun 2014 21:42:07 +0000 (14:42 -0700)
committerCarl Lerche <me@carllerche.com>
Wed, 18 Jun 2014 21:42:07 +0000 (14:42 -0700)
src/bin/cargo-read-manifest.rs
src/cargo/ops/cargo_compile.rs
src/cargo/sources/path.rs

index 3c3b8694d0cb8088b2921db8c32e00134396af41..4d97627ce7af1a907400114e03103be8108eddf7 100644 (file)
@@ -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<Option<Package>> {
     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))
index fd94204fc76841af9f45dbb650168564dee437eb..dafa492bd4b56826579b2c09e024a789a7abdaca 100644 (file)
@@ -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());
index 1322ce1e12f4b982cd2f4bc2b834accf5bb815de..c8e9d6596c06b8dacda9cfb434599b3358a4df88 100644 (file)
@@ -6,7 +6,8 @@ use util::{CargoResult,simple_human};
 
 pub struct PathSource {
     id: SourceId,
-    path: Path,
+    updated: bool,
+    packages: Vec<Package>
 }
 
 /**
@@ -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<Package> {
         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<Vec<Package>> {
-        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<Vec<Summary>> {
-        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<Vec<Package>> {
         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())