@@ -14,13 +14,28 @@ use ignore::gitignore::GitignoreBuilder;
14
14
use log:: { trace, warn} ;
15
15
use walkdir:: WalkDir ;
16
16
17
+ /// A source represents one or multiple packages gathering from a given root
18
+ /// path on the filesystem.
19
+ ///
20
+ /// It's the cornerstone of every other source --- other implementations
21
+ /// eventually need to call `PathSource` to read local packages somewhere on
22
+ /// the filesystem.
23
+ ///
24
+ /// It also provides convenient methods like [`PathSource::list_files`] to
25
+ /// list all files in a package, given its ability to walk the filesystem.
17
26
pub struct PathSource < ' cfg > {
27
+ /// The unique identifier of this source.
18
28
source_id : SourceId ,
29
+ /// The root path of this source.
19
30
path : PathBuf ,
31
+ /// Whether this source has updated all package informations it may contain.
20
32
updated : bool ,
33
+ /// Packages that this sources has discovered.
21
34
packages : Vec < Package > ,
22
- config : & ' cfg Config ,
35
+ /// Whether this source should discover nested packages recursively.
36
+ /// See [`PathSource::new_recursive`] for more.
23
37
recursive : bool ,
38
+ config : & ' cfg Config ,
24
39
}
25
40
26
41
impl < ' cfg > PathSource < ' cfg > {
@@ -41,9 +56,9 @@ impl<'cfg> PathSource<'cfg> {
41
56
42
57
/// Creates a new source which is walked recursively to discover packages.
43
58
///
44
- /// This is similar to the ` new` method except that instead of requiring a
45
- /// valid package to be present at `root` the folder is walked entirely to
46
- /// crawl for packages.
59
+ /// This is similar to the [`PathSource:: new`] method except that instead
60
+ /// of requiring a valid package to be present at `root` the folder is
61
+ /// walked entirely to crawl for packages.
47
62
///
48
63
/// Note that this should be used with care and likely shouldn't be chosen
49
64
/// by default!
@@ -54,6 +69,8 @@ impl<'cfg> PathSource<'cfg> {
54
69
}
55
70
}
56
71
72
+ /// Preloads a package for this source. The source is assumed that it has
73
+ /// yet loaded any other packages.
57
74
pub fn preload_with ( & mut self , pkg : Package ) {
58
75
assert ! ( !self . updated) ;
59
76
assert ! ( !self . recursive) ;
@@ -62,6 +79,7 @@ impl<'cfg> PathSource<'cfg> {
62
79
self . packages . push ( pkg) ;
63
80
}
64
81
82
+ /// Gets the package on the root path.
65
83
pub fn root_package ( & mut self ) -> CargoResult < Package > {
66
84
trace ! ( "root_package; source={:?}" , self ) ;
67
85
@@ -76,6 +94,8 @@ impl<'cfg> PathSource<'cfg> {
76
94
}
77
95
}
78
96
97
+ /// Returns the packages discovered by this source. It may walk the
98
+ /// the filesystem if package informations haven't yet updated.
79
99
pub fn read_packages ( & self ) -> CargoResult < Vec < Package > > {
80
100
if self . updated {
81
101
Ok ( self . packages . clone ( ) )
@@ -96,7 +116,8 @@ impl<'cfg> PathSource<'cfg> {
96
116
///
97
117
/// The basic assumption of this method is that all files in the directory
98
118
/// are relevant for building this package, but it also contains logic to
99
- /// use other methods like .gitignore to filter the list of files.
119
+ /// use other methods like `.gitignore`, `package.include`, or
120
+ /// `package.exclude` to filter the list of files.
100
121
pub fn list_files ( & self , pkg : & Package ) -> CargoResult < Vec < PathBuf > > {
101
122
self . _list_files ( pkg) . with_context ( || {
102
123
format ! (
@@ -106,6 +127,7 @@ impl<'cfg> PathSource<'cfg> {
106
127
} )
107
128
}
108
129
130
+ /// See [`PathSource::list_files`].
109
131
fn _list_files ( & self , pkg : & Package ) -> CargoResult < Vec < PathBuf > > {
110
132
let root = pkg. root ( ) ;
111
133
let no_include_option = pkg. manifest ( ) . include ( ) . is_empty ( ) ;
@@ -218,6 +240,11 @@ impl<'cfg> PathSource<'cfg> {
218
240
Ok ( None )
219
241
}
220
242
243
+ /// Lists files relevant to building this package inside this source by
244
+ /// consulting both Git index (tracked) or status (untracked) under
245
+ /// a given Git repository.
246
+ ///
247
+ /// This looks into Git submodules as well.
221
248
fn list_files_git (
222
249
& self ,
223
250
pkg : & Package ,
@@ -373,6 +400,11 @@ impl<'cfg> PathSource<'cfg> {
373
400
}
374
401
}
375
402
403
+ /// Lists files relevant to building this package inside this source by
404
+ /// walking the filesystem from the package root path.
405
+ ///
406
+ /// This is a fallback for [`PathSource::list_files_git`] when the package
407
+ /// is not tracked under a Git repository.
376
408
fn list_files_walk (
377
409
& self ,
378
410
pkg : & Package ,
@@ -383,6 +415,7 @@ impl<'cfg> PathSource<'cfg> {
383
415
Ok ( ret)
384
416
}
385
417
418
+ /// Helper recursive function for [`PathSource::list_files_walk`].
386
419
fn walk (
387
420
& self ,
388
421
path : & Path ,
@@ -448,6 +481,7 @@ impl<'cfg> PathSource<'cfg> {
448
481
Ok ( ( ) )
449
482
}
450
483
484
+ /// Gets the last modified file in a package.
451
485
pub fn last_modified_file ( & self , pkg : & Package ) -> CargoResult < ( FileTime , PathBuf ) > {
452
486
if !self . updated {
453
487
return Err ( internal ( format ! (
@@ -479,10 +513,12 @@ impl<'cfg> PathSource<'cfg> {
479
513
Ok ( ( max, max_path) )
480
514
}
481
515
516
+ /// Returns the root path of this source.
482
517
pub fn path ( & self ) -> & Path {
483
518
& self . path
484
519
}
485
520
521
+ /// Discovers packages inside this source if it hasn't yet done.
486
522
pub fn update ( & mut self ) -> CargoResult < ( ) > {
487
523
if !self . updated {
488
524
let packages = self . read_packages ( ) ?;
0 commit comments