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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::collections::BTreeMap;
use std::ffi;
use std::iter;
use std::path;
use failure;
use globwalk;
use action;
use error;
pub trait ActionBuilder {
fn build(&self, target_dir: &path::Path) -> Result<Vec<Box<action::Action>>, failure::Error>;
}
#[derive(Default)]
pub struct Staging(BTreeMap<path::PathBuf, Vec<Box<ActionBuilder>>>);
impl ActionBuilder for Staging {
fn build(&self, target_dir: &path::Path) -> Result<Vec<Box<action::Action>>, failure::Error> {
let staging: Result<Vec<_>, _> = self.0
.iter()
.map(|(target, sources)| {
if target.is_absolute() {
bail!("target must be relative to the stage root: {:?}", target);
}
let target = target_dir.join(target);
let mut errors = error::Errors::new();
let sources = {
let sources = sources.into_iter().map(|s| s.build(&target));
let sources = error::ErrorPartition::new(sources, &mut errors);
let sources: Vec<_> = sources.collect();
sources
};
errors.ok(sources)
})
.collect();
let staging = staging?;
let staging: Vec<_> = staging
.into_iter()
.flat_map(|v| v.into_iter().flat_map(|v: Vec<_>| v.into_iter()))
.collect();
Ok(staging)
}
}
impl iter::FromIterator<(path::PathBuf, Vec<Box<ActionBuilder>>)> for Staging {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = (path::PathBuf, Vec<Box<ActionBuilder>>)>,
{
let staging = iter.into_iter().collect();
Self { 0: staging }
}
}
#[derive(Clone, Debug)]
pub struct Directory {
pub access: Vec<Access>,
}
impl ActionBuilder for Directory {
fn build(&self, target_dir: &path::Path) -> Result<Vec<Box<action::Action>>, failure::Error> {
let create: Box<action::Action> = Box::new(action::CreateDirectory::new(target_dir));
let mut actions = vec![create];
actions.extend(self.access.iter().cloned().map(|a| {
let a = action::Access::new(target_dir, a.op);
let a: Box<action::Action> = Box::new(a);
a
}));
Ok(actions)
}
}
#[derive(Clone, Debug)]
pub struct SourceFile {
pub path: path::PathBuf,
pub rename: Option<String>,
pub access: Vec<Access>,
pub symlink: Vec<String>,
}
impl ActionBuilder for SourceFile {
fn build(&self, target_dir: &path::Path) -> Result<Vec<Box<action::Action>>, failure::Error> {
let path = self.path.as_path();
if !path.is_absolute() {
bail!("SourceFile path must be absolute: {:?}", path);
}
let filename = self.rename
.as_ref()
.map(|n| ffi::OsStr::new(n))
.unwrap_or_else(|| path.file_name().unwrap_or_default());
let filename = path::Path::new(filename);
if filename.file_name() != Some(filename.as_os_str()) {
bail!(
"SourceFile rename must not change directories: {:?}",
filename
);
}
let copy_target = target_dir.join(filename);
let copy: Box<action::Action> = Box::new(action::CopyFile::new(©_target, path));
let mut actions = vec![copy];
actions.extend(self.access.iter().cloned().map(|a| {
let a = action::Access::new(target_dir, a.op);
let a: Box<action::Action> = Box::new(a);
a
}));
actions.extend(self.symlink.iter().map(|s| {
let s = path::Path::new(s);
let sym_target = target_dir.join(s);
let a: Box<action::Action> = Box::new(action::Symlink::new(sym_target, ©_target));
a
}));
Ok(actions)
}
}
#[derive(Clone, Debug)]
pub struct SourceFiles {
pub path: path::PathBuf,
pub pattern: Vec<String>,
pub follow_links: bool,
pub allow_empty: bool,
pub access: Vec<Access>,
}
impl ActionBuilder for SourceFiles {
fn build(&self, target_dir: &path::Path) -> Result<Vec<Box<action::Action>>, failure::Error> {
let mut actions: Vec<Box<action::Action>> = Vec::new();
let source_root = self.path.as_path();
if !source_root.is_absolute() {
bail!("SourceFiles path must be absolute: {:?}", source_root);
}
for entry in globwalk::GlobWalker::from_patterns(source_root, &self.pattern)?
.follow_links(self.follow_links)
{
let entry = entry?;
let source_file = entry.path();
if source_file.is_dir() {
continue;
}
let rel_source = source_file.strip_prefix(source_root)?;
let copy_target = target_dir.join(rel_source);
let copy: Box<action::Action> =
Box::new(action::CopyFile::new(©_target, source_file));
actions.push(copy);
actions.extend(self.access.iter().cloned().map(|a| {
let a = action::Access::new(target_dir, a.op);
let a: Box<action::Action> = Box::new(a);
a
}));
}
if actions.is_empty() {
if self.allow_empty {
info!(
"No files found under {:?} with patterns {:?}",
self.path, self.pattern
);
} else {
bail!(
"No files found under {:?} with patterns {:?}",
self.path,
self.pattern
);
}
}
Ok(actions)
}
}
#[derive(Clone, Debug)]
pub struct Symlink {
pub target: path::PathBuf,
pub rename: String,
pub access: Vec<Access>,
}
impl ActionBuilder for Symlink {
fn build(&self, target_dir: &path::Path) -> Result<Vec<Box<action::Action>>, failure::Error> {
let target = self.target.as_path();
let rename = path::Path::new(&self.rename);
if rename.file_name() != Some(rename.as_os_str()) {
bail!("Symlink rename must not change directories: {:?}", rename);
}
let staged = target_dir.join(rename);
let link: Box<action::Action> = Box::new(action::Symlink::new(&staged, target));
let mut actions = vec![link];
actions.extend(self.access.iter().cloned().map(|a| {
let a = action::Access::new(target, a.op);
let a: Box<action::Action> = Box::new(a);
a
}));
Ok(actions)
}
}
#[derive(Clone, Debug)]
pub struct Access {
pub op: String,
}