1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use glob::Pattern;
use shellexpand;
use walkdir::{DirEntry, WalkDir, WalkDirIterator};
use cache::{Cache, CacheContent};
use config::{Config, InitialStr};
use vcs;
use util;
use super::repository::Repository;
#[derive(Default, Serialize, Deserialize)]
struct CacheData {
repositories: Vec<Repository>,
}
impl<'de> CacheContent<'de> for CacheData {
fn name() -> &'static str {
"cache"
}
}
#[derive(Serialize, Deserialize)]
struct ConfigData {
root: Option<String>,
includes: Option<Vec<String>>,
excludes: Option<Vec<String>>,
}
impl ConfigData {
pub fn root_dir(&self) -> Option<PathBuf> {
self.root
.as_ref()
.and_then(|root| util::make_path_buf(root).ok())
}
pub fn include_dirs(&self) -> Vec<PathBuf> {
self.includes
.as_ref()
.map(Vec::as_slice)
.unwrap_or(&[])
.into_iter()
.filter_map(|root| util::make_path_buf(&root).ok())
.collect()
}
pub fn exclude_patterns(&self) -> Vec<Pattern> {
self.excludes
.as_ref()
.map(Vec::as_slice)
.unwrap_or(&[])
.into_iter()
.filter_map(|ex| {
shellexpand::full(&ex)
.ok()
.map(|ex| ex.replace(r"\", "/"))
.and_then(|ex| Pattern::new(&ex).ok())
})
.collect()
}
}
impl InitialStr for ConfigData {
#[inline]
fn initial_str() -> &'static str {
include_str!("config.toml")
}
}
pub struct Workspace<'a> {
cache: Cache<CacheData>,
config: Config<ConfigData>,
root: Option<&'a Path>,
}
impl<'a> Workspace<'a> {
pub fn new(root: Option<&'a Path>) -> ::Result<Workspace<'a>> {
let cache = Cache::load()?;
let config = Config::load()?;
Ok(Workspace {
cache: cache,
config: config,
root: root,
})
}
pub fn root_dir(&self) -> Option<Cow<Path>> {
self.root
.map(Into::into)
.or_else(|| self.config.root_dir().map(Into::into))
}
pub fn repositories(&self) -> Option<&[Repository]> {
self.cache
.get_opt()
.map(|cache| cache.repositories.as_slice())
}
pub fn scan_repositories_default(&mut self, verbose: bool, depth: Option<usize>) -> ::Result<()> {
for root in self.config.include_dirs() {
self.scan_repositories(root, verbose, depth)?;
}
Ok(())
}
pub fn scan_repositories<P: AsRef<Path>>(&mut self, root: P, verbose: bool, depth: Option<usize>) -> ::Result<()> {
for path in collect_repositories(root, depth, self.config.exclude_patterns()) {
let repo = Repository::from_path(path)?;
self.add_repository(repo, verbose);
}
Ok(())
}
pub fn add_repository(&mut self, repo: Repository, verbose: bool) {
let ref mut repos = self.cache.get_mut().repositories;
if let Some(r) = repos.iter_mut().find(|r| r.is_same_local(&repo)) {
if verbose {
println!("Overwrite existed entry: {}", repo.path_string());
}
*r = repo;
return;
}
if verbose {
println!("Add new entry: {}", repo.path_string());
}
repos.push(repo);
}
pub fn drop_invalid_repositories(&mut self, verbose: bool) {
let mut new_repo = Vec::new();
for repo in &self.cache.get_mut().repositories {
let repo = match repo.clone().refresh() {
Some(r) => r,
None => continue,
};
if self.config
.exclude_patterns()
.into_iter()
.all(|ex| !ex.matches(&repo.path_string()))
{
new_repo.push(repo.clone());
} else {
if verbose {
println!("Dropped: {}", repo.path_string());
}
}
}
self.cache.get_mut().repositories = new_repo;
}
pub fn sort_repositories(&mut self) {
self.cache
.get_mut()
.repositories
.sort_by(|a, b| a.name().cmp(b.name()));
}
pub fn save_cache(&mut self) -> ::Result<()> {
self.cache.dump()?;
Ok(())
}
}
fn collect_repositories<P>(root: P, depth: Option<usize>, excludes: Vec<Pattern>) -> Vec<PathBuf>
where
P: AsRef<Path>,
{
let filter = {
let root = root.as_ref();
move |entry: &DirEntry| {
if entry.path() == root {
return true;
}
!entry
.path()
.parent()
.map(|path| vcs::detect_from_path(&path).is_some())
.unwrap_or(false)
&& entry
.path()
.canonicalize()
.ok()
.map(|path| {
let path = path.to_str().unwrap().trim_left_matches(r"\\?\");
excludes.iter().all(|ex| !ex.matches(path))
})
.unwrap_or(false)
}
};
let mut walkdir = WalkDir::new(root.as_ref()).follow_links(true);
if let Some(depth) = depth {
walkdir = walkdir.max_depth(depth);
}
walkdir
.into_iter()
.filter_entry(filter)
.filter_map(Result::ok)
.filter(|entry| vcs::detect_from_path(entry.path()).is_some())
.map(|entry| entry.path().into())
.collect()
}