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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use rustc_serialize::Decodable;
use toml::{Parser, Value, Table, Decoder};
use dev_prefix::*;
use super::types::*;
use super::utils;
use core::link;
use core::locs;
lazy_static!{
pub static ref REPO_DIR: PathBuf = PathBuf::from(".art");
pub static ref SETTINGS_PATH: PathBuf = REPO_DIR.join("settings.toml");
}
pub fn parse_toml(toml: &str) -> Result<Table> {
let mut parser = Parser::new(toml);
match parser.parse() {
Some(table) => Ok(table),
None => {
let mut locs = String::new();
for e in &parser.errors {
let (line, col) = parser.to_linecol(e.lo);
write!(locs, "[{}:{}] {}, ", line, col, e.desc).unwrap();
}
Err(ErrorKind::TomlParse(locs).into())
}
}
}
macro_rules! get_attr {
($tbl: expr, $attr: expr, $default: expr, $ty: ident) => {
match $tbl.get($attr) {
Some(&Value::$ty(ref v)) => Some(v.clone()),
None => Some($default.clone()),
_ => None,
}
}
}
impl ProjectText {
pub fn load(&mut self, load_dir: &Path, loaded_dirs: &mut HashSet<PathBuf>) -> Result<()> {
loaded_dirs.insert(load_dir.to_path_buf());
let mut dirs_to_load: Vec<PathBuf> = Vec::new();
let dir_entries =
fs::read_dir(load_dir).chain_err(|| format!(
"could not get dir: {}", load_dir.display()))?;
for entry in dir_entries.filter_map(|e| e.ok()) {
let fpath = entry.path();
let ftype = entry.file_type()
.chain_err(|| format!("error reading type: {}", fpath.display()))?;
if ftype.is_dir() {
dirs_to_load.push(fpath.clone());
} else if ftype.is_file() {
let ext = match fpath.extension() {
None => continue,
Some(ext) => ext,
};
if ext != "toml" {
continue;
}
let mut text = String::new();
let mut fp = fs::File::open(&fpath).chain_err(|| format!(
"error opening: {}", fpath.display()))?;
fp.read_to_string(&mut text)
.chain_err(|| format!("Error loading path {}", fpath.display()))?;
self.files.insert(fpath.to_path_buf(), text);
}
}
for dir in dirs_to_load {
if !loaded_dirs.contains(dir.as_path()) {
self.load(dir.as_path(), loaded_dirs)?;
}
}
Ok(())
}
}
impl Project {
pub fn extend_text(&mut self, project_text: &ProjectText) -> Result<u64> {
let mut count = 0;
for (path, text) in &project_text.files {
count += load_toml(path, text, self)?;
}
Ok(count)
}
}
impl Settings {
pub fn from_table(tbl: &Table) -> Result<(RawSettings, Settings)> {
let value = Value::Table(tbl.clone());
let mut decoder = Decoder::new(value);
let raw = RawSettings::decode(&mut decoder).chain_err(|| "invalid settings")?;
if let Some(invalid) = decoder.toml {
return Err(ErrorKind::InvalidSettings(format!("{:?}", invalid)).into());
}
fn to_paths(paths: &Option<Vec<String>>) -> VecDeque<PathBuf> {
match *paths {
Some(ref p) => {
p.iter()
.map(|p| PathBuf::from(utils::convert_path_str(p)))
.collect()
}
None => VecDeque::new(),
}
}
let mut paths = to_paths(&raw.artifact_paths);
let artifact_paths: HashSet<PathBuf> = paths.drain(0..).collect();
let settings = Settings {
artifact_paths: artifact_paths,
code_paths: to_paths(&raw.code_paths),
exclude_code_paths: to_paths(&raw.exclude_code_paths),
additional_repos: to_paths(&raw.additional_repos),
};
Ok((raw, settings))
}
}
impl Artifact {
#[allow(should_implement_trait)]
pub fn from_str(toml: &str) -> Result<(ArtNameRc, Artifact)> {
let table = try!(parse_toml(toml));
if table.len() != 1 {
return Err(ErrorKind::Load("must contain a single table".to_string()).into());
}
let (name, value) = table.iter().next().unwrap();
let name = try!(ArtName::from_str(name));
let value = match *value {
Value::Table(ref t) => t,
_ => return Err(ErrorKind::Load("must contain a single table".to_string()).into()),
};
let artifact = try!(Artifact::from_table(&name, &Path::new("from_str"), value));
Ok((Arc::new(name), artifact))
}
fn from_table(name: &ArtName, path: &Path, tbl: &Table) -> Result<Artifact> {
let value = Value::Table(tbl.clone());
let mut decoder = Decoder::new(value);
let raw = match RawArtifact::decode(&mut decoder) {
Ok(v) => v,
Err(e) => {
return Err(ErrorKind::InvalidArtifact(name.to_string(), e.to_string()).into())
}
};
if let Some(invalid) = decoder.toml {
return Err(ErrorKind::InvalidArtifact(name.to_string(),
format!("invalid attrs: {}", invalid))
.into());
}
Ok(Artifact {
path: path.to_path_buf(),
text: raw.text.unwrap_or_default(),
partof: try!(ArtNames::from_str(&raw.partof.unwrap_or_default())),
loc: None,
parts: HashSet::new(),
completed: -1.0,
tested: -1.0,
})
}
}
pub fn load_toml(path: &Path, text: &str, project: &mut Project) -> Result<u64> {
let table = parse_toml(text)?;
let mut num_loaded: u64 = 0;
project.files.insert(path.to_path_buf());
for (name, value) in &table {
let aname = ArtName::from_str(name)?;
let art_tbl: &Table = match *value {
Value::Table(ref t) => t,
_ => {
let msg = format!("All top-level values must be a table: {}", name);
return Err(ErrorKind::Load(msg).into());
}
};
if let Some(overlap) = project.artifacts.get(&aname) {
let msg = format!("Overlapping key found <{}> other key at: {}",
name,
overlap.path.display());
return Err(ErrorKind::Load(msg).into());
}
let artifact = Artifact::from_table(&aname, path, art_tbl)?;
project.artifacts.insert(Arc::new(aname), artifact);
num_loaded += 1;
}
Ok(num_loaded)
}
pub fn resolve_settings_paths(repo: &Path, settings: &mut Settings) -> Result<()> {
let mut vars: HashMap<String, String> = HashMap::new();
let settings_path = repo.join(SETTINGS_PATH.as_path());
{
let cwd = repo.join(REPO_DIR.as_path());
let cwd_str = utils::get_path_str(&cwd)?;
vars.insert(CWD_VAR.to_string(), cwd_str.to_string());
vars.insert(REPO_VAR.to_string(), utils::get_path_str(repo)?.to_string());
}
{
let mut paths = HashSet::new();
for p in &settings.artifact_paths {
let p = utils::do_strfmt(utils::get_path_str(p)?, &vars, &settings_path)?;
let p = utils::canonicalize(Path::new(&p))?;
paths.insert(p);
}
settings.artifact_paths = paths;
}
{
let mut paths = VecDeque::new();
for p in &settings.code_paths {
let p = try!(utils::do_strfmt(utils::get_path_str(p)?, &vars, &settings_path));
let p = utils::canonicalize(Path::new(&p))?;
paths.push_back(p);
}
settings.code_paths = paths;
}
{
let mut paths = VecDeque::new();
for p in &settings.exclude_code_paths {
let p = try!(utils::do_strfmt(utils::get_path_str(p)?, &vars, &settings_path));
let p = match utils::canonicalize(Path::new(&p)) {
Ok(p) => p,
Err(_) => continue,
};
paths.push_back(p);
}
settings.exclude_code_paths = paths;
}
Ok(())
}
pub fn load_settings(repo: &Path) -> Result<Settings> {
let settings_path = repo.join(SETTINGS_PATH.as_path());
let mut text = String::new();
let mut f = fs::File::open(&settings_path).chain_err(|| format!(
"error opening settings: {}", settings_path.display()))?;
f.read_to_string(&mut text)
.chain_err(|| format!("error reading settings: {}", settings_path.display()))?;
let tbl = parse_toml(&text).chain_err(|| format!(
"error parsing settings: {}", settings_path.display()))?;
let (_, mut settings) = Settings::from_table(&tbl)?;
resolve_settings_paths(repo, &mut settings).chain_err(|| format!(
"error resolving paths: {}", settings_path.display()))?;
Ok(settings)
}
pub fn process_project(project: &mut Project) -> Result<()> {
let locs = locs::find_locs(&project.settings)?;
project.dne_locs = locs::attach_locs(&mut project.artifacts, locs);
link::do_links(&mut project.artifacts)?;
Ok(())
}
pub fn load_project(repo: &Path) -> Result<Project> {
let settings = load_settings(repo)?;
let mut project_text = ProjectText::default();
let mut loaded_dirs: HashSet<PathBuf> = HashSet::new();
loaded_dirs.insert(repo.join(REPO_DIR.as_path()));
for dir in &settings.artifact_paths {
if loaded_dirs.contains(dir) {
warn!("artifact_paths tried to load a directory twice: {}",
dir.display());
continue;
}
loaded_dirs.insert(dir.to_path_buf());
project_text.load(dir.as_path(), &mut loaded_dirs)?;
}
let mut project = Project::default();
project.settings = settings.clone();
project.extend_text(&project_text)?;
process_project(&mut project)?;
project.origin = repo.to_path_buf();
Ok(project)
}