]>
Commit | Line | Data |
---|---|---|
50f110a4 YK |
1 | use std::fmt; |
2 | use std::fmt::{Show,Formatter}; | |
61c95b75 | 3 | use core::{Package,PackageId,Summary}; |
21598204 | 4 | use core::source::Source; |
12f49111 | 5 | use ops; |
c57fef45 YKCL |
6 | use url; |
7 | use util::{CargoResult,simple_human,io_error,realpath}; | |
62bff631 | 8 | |
c57fef45 YKCL |
9 | /* |
10 | * TODO: Consider whether it may be more appropriate for a PathSource to only | |
11 | * take in a single path vs. a vec of paths. The pros / cons are unknown at | |
12 | * this point. | |
13 | */ | |
14 | pub struct PathSource { paths: Vec<Path> } | |
62bff631 C |
15 | |
16 | impl PathSource { | |
17 | pub fn new(paths: Vec<Path>) -> PathSource { | |
c57fef45 | 18 | log!(5, "new; paths={}", display(paths.as_slice())); |
62bff631 C |
19 | PathSource { paths: paths } |
20 | } | |
c57fef45 YKCL |
21 | |
22 | pub fn read_package(path: &Path) -> CargoResult<Package> { | |
23 | log!(5, "read_package; path={}", path.display()); | |
24 | ||
25 | // TODO: Use a realpath fn | |
26 | let dir = path.dir_path(); | |
27 | let namespace = try!(namespace(&dir)); | |
28 | ||
29 | ops::read_package(path, &namespace) | |
30 | } | |
62bff631 C |
31 | } |
32 | ||
50f110a4 YK |
33 | impl Show for PathSource { |
34 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { | |
cfeabbc2 | 35 | write!(f, "the paths source") |
50f110a4 YK |
36 | } |
37 | } | |
38 | ||
62bff631 C |
39 | impl Source for PathSource { |
40 | fn update(&self) -> CargoResult<()> { Ok(()) } | |
41 | ||
93730339 | 42 | fn list(&self) -> CargoResult<Vec<Summary>> { |
21598204 | 43 | Ok(self.paths.iter().filter_map(|path| { |
c57fef45 | 44 | match PathSource::read_package(&path.join("Cargo.toml")) { |
93730339 | 45 | Ok(ref pkg) => Some(pkg.get_summary().clone()), |
c040b891 | 46 | Err(e) => { |
c57fef45 | 47 | debug!("failed to read manifest; path={}; err={}", path.display(), e); |
c040b891 CL |
48 | None |
49 | } | |
21598204 YK |
50 | } |
51 | }).collect()) | |
62bff631 C |
52 | } |
53 | ||
61c95b75 | 54 | fn download(&self, _: &[PackageId]) -> CargoResult<()>{ |
62bff631 C |
55 | Ok(()) |
56 | } | |
57 | ||
c57fef45 YKCL |
58 | fn get(&self, ids: &[PackageId]) -> CargoResult<Vec<Package>> { |
59 | log!(5, "getting packages; ids={}", ids); | |
60 | ||
21598204 | 61 | Ok(self.paths.iter().filter_map(|path| { |
c57fef45 | 62 | match PathSource::read_package(&path.join("Cargo.toml")) { |
41b6f526 | 63 | Ok(pkg) => { |
c57fef45 YKCL |
64 | log!(5, "comparing; pkg={}", pkg); |
65 | ||
66 | if ids.iter().any(|pkg_id| pkg.get_package_id() == pkg_id) { | |
41b6f526 YK |
67 | Some(pkg) |
68 | } else { | |
69 | None | |
70 | } | |
71 | } | |
21598204 YK |
72 | Err(_) => None |
73 | } | |
74 | }).collect()) | |
62bff631 C |
75 | } |
76 | } | |
21598204 | 77 | |
c040b891 CL |
78 | fn display(paths: &[Path]) -> Vec<String> { |
79 | paths.iter().map(|p| p.display().to_str()).collect() | |
80 | } | |
c57fef45 YKCL |
81 | |
82 | fn namespace(path: &Path) -> CargoResult<url::Url> { | |
83 | let real = try!(realpath(path).map_err(io_error)); | |
84 | url::from_str(format!("file://{}", real.display()).as_slice()).map_err(|err| | |
85 | simple_human(err.as_slice())) | |
86 | } |