PathSource - load packages in update fn

This commit is contained in:
Carl Lerche 2014-06-18 14:42:07 -07:00
parent c665938dcc
commit 21b7418a38
3 changed files with 34 additions and 18 deletions

View File

@ -6,7 +6,7 @@ extern crate hammer;
use hammer::FlagConfig; use hammer::FlagConfig;
use cargo::{execute_main_without_stdin,CLIResult,CLIError}; use cargo::{execute_main_without_stdin,CLIResult,CLIError};
use cargo::core::{Package,SourceId}; use cargo::core::{Package,Source,SourceId};
use cargo::sources::{PathSource}; use cargo::sources::{PathSource};
#[deriving(PartialEq,Clone,Decodable)] #[deriving(PartialEq,Clone,Decodable)]
@ -22,8 +22,11 @@ fn main() {
fn execute(options: Options) -> CLIResult<Option<Package>> { fn execute(options: Options) -> CLIResult<Option<Package>> {
let source_id = SourceId::for_path(&Path::new(options.manifest_path.as_slice())); 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() .get_root_package()
.map(|pkg| Some(pkg)) .map(|pkg| Some(pkg))
.map_err(|err| CLIError::new(err.get_desc(), Some(err.get_detail()), 1)) .map_err(|err| CLIError::new(err.get_desc(), Some(err.get_detail()), 1))

View File

@ -16,7 +16,7 @@
use std::os; use std::os;
use util::config::{ConfigValue}; use util::config::{ConfigValue};
use core::{SourceId,PackageSet,resolver}; use core::{Source,SourceId,PackageSet,resolver};
use core::registry::PackageRegistry; use core::registry::PackageRegistry;
use ops; use ops;
use sources::{PathSource}; use sources::{PathSource};
@ -25,8 +25,12 @@ use util::{CargoResult,Wrap,config,other_error};
pub fn compile(manifest_path: &Path) -> CargoResult<()> { pub fn compile(manifest_path: &Path) -> CargoResult<()> {
log!(4, "compile; manifest-path={}", manifest_path.display()); 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 // 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); debug!("loaded package; package={}", package);
let override_ids = try!(source_ids_from_config()); let override_ids = try!(source_ids_from_config());

View File

@ -6,7 +6,8 @@ use util::{CargoResult,simple_human};
pub struct PathSource { pub struct PathSource {
id: SourceId, id: SourceId,
path: Path, updated: bool,
packages: Vec<Package>
} }
/** /**
@ -24,26 +25,29 @@ impl PathSource {
log!(5, "new; id={}", id); log!(5, "new; id={}", id);
assert!(id.is_path(), "does not represent a path source; 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 { PathSource {
id: id.clone(), 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> { pub fn get_root_package(&self) -> CargoResult<Package> {
log!(5, "get_root_package; source={}", self); 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()), Some(pkg) => Ok(pkg.clone()),
None => Err(simple_human("no package found in source")) 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 { impl Show for PathSource {
@ -54,12 +58,19 @@ impl Show for PathSource {
impl Source for PathSource { impl Source for PathSource {
fn update(&mut self) -> CargoResult<()> { 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(()) Ok(())
} }
fn list(&self) -> CargoResult<Vec<Summary>> { fn list(&self) -> CargoResult<Vec<Summary>> {
let pkgs = try!(self.packages()); Ok(self.packages.iter()
Ok(pkgs.iter().map(|p| p.get_summary().clone()).collect()) .map(|p| p.get_summary().clone())
.collect())
} }
fn download(&self, _: &[PackageId]) -> CargoResult<()>{ fn download(&self, _: &[PackageId]) -> CargoResult<()>{
@ -70,9 +81,7 @@ impl Source for PathSource {
fn get(&self, ids: &[PackageId]) -> CargoResult<Vec<Package>> { fn get(&self, ids: &[PackageId]) -> CargoResult<Vec<Package>> {
log!(5, "getting packages; ids={}", ids); log!(5, "getting packages; ids={}", ids);
let pkgs = try!(self.packages()); Ok(self.packages.iter()
Ok(pkgs.iter()
.filter(|pkg| ids.iter().any(|id| pkg.get_package_id() == id)) .filter(|pkg| ids.iter().any(|id| pkg.get_package_id() == id))
.map(|pkg| pkg.clone()) .map(|pkg| pkg.clone())
.collect()) .collect())