artifact_app/user/
mod.rs

1/* Copyright (c) 2017 Garrett Berg, vitiral@gmail.com
2 *
3 * Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4 * http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5 * http://opensource.org/licenses/MIT>, at your option. This file may not be
6 * copied, modified, or distributed except according to those terms.
7 */
8//! This module defines the loading and saving of artifacts from user data
9
10use time;
11use dev_prefix::*;
12use types::*;
13use security;
14
15mod types;
16mod utils;
17mod save;
18mod link;
19mod locs;
20mod name;
21mod project;
22mod artifact;
23mod settings;
24
25#[cfg(test)]
26mod tests;
27
28// export for other modules to use
29pub use user::save::{PathDiff, ProjectText};
30
31// Exposed for testing only
32#[cfg(test)]
33pub use user::artifact::{load_text as load_project_text, load_toml};
34#[cfg(test)]
35pub use user::link::do_links;
36#[cfg(test)]
37pub use user::settings::{from_raw as settings_from_raw, from_text as settings_from_text};
38#[cfg(test)]
39pub use user::types::RawSettings;
40
41
42/// Process a raw project text file.
43///
44/// This can be called on a project which has not yet
45/// had it's links completed.
46///
47/// #SPC-project-process
48pub fn process_project(project: &mut Project) -> Result<()> {
49    let locs = locs::find_locs(&project.settings)?;
50    project.dne_locs = locs::attach_locs(&mut project.artifacts, locs)?;
51    link::do_links(&mut project.artifacts)?;
52    Ok(())
53}
54
55/// This method is for processing a raw project-text
56/// file into a full blown project.
57///
58/// This is mostly used for validation that nothing has
59/// changed in converting the project.
60///
61/// This method should be considered unstable.
62pub fn process_project_text(settings: Settings, project_text: &ProjectText) -> Result<Project> {
63    let mut project = Project::default();
64    project.settings = settings;
65    artifact::extend_text(&mut project, project_text)?;
66    process_project(&mut project)?;
67    project.origin = project_text.origin.clone();
68    Ok(project)
69}
70
71/// Load all items from the toml file in the repo
72///
73/// #SPC-project-load
74pub fn load_repo(repo: &Path) -> Result<Project> {
75    let start = time::get_time();
76    info!("Loading path: {}", repo.display());
77
78    let settings = settings::load_settings(repo)?;
79    security::validate_settings(repo, &settings)?;
80
81    let mut project_text = ProjectText::default();
82    let mut loaded_paths: HashSet<PathBuf> = HashSet::new();
83    loaded_paths.insert(repo.join(REPO_DIR.as_path()));
84    loaded_paths.extend(settings.exclude_artifact_paths.iter().cloned());
85
86    for path in &settings.artifact_paths {
87        if loaded_paths.contains(path) {
88            continue;
89        }
90        loaded_paths.insert(path.to_path_buf());
91        artifact::load_text(&mut project_text, path.as_path(), &mut loaded_paths)?;
92    }
93
94    let mut project = Project::default();
95    project.settings = settings.clone();
96    artifact::extend_text(&mut project, &project_text)?;
97
98    process_project(&mut project)?;
99    project.origin = repo.to_path_buf();
100
101    let total = time::get_time() - start;
102    info!(
103        "Done loading: {} artifacts loaded successfullly in {:.3} seconds",
104        project.artifacts.len(),
105        total.num_milliseconds() as f64 * 1e-3
106    );
107    Ok(project)
108}