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
use std::fs;
use std::path::Path;

use storage::CachedBackend;
use super::{CliError, LalResult, Lockfile, Manifest};

fn clean_input() {
    let input = Path::new("./INPUT");
    if input.is_dir() {
        fs::remove_dir_all(&input).unwrap();
    }
}

/// Fetch all dependencies from `manifest.json`
///
/// This will read, and HTTP GET all the dependencies at the specified versions.
/// If the `core` bool is set, then `devDependencies` are not installed.
pub fn fetch<T: CachedBackend + ?Sized>(
    manifest: &Manifest,
    backend: &T,
    core: bool,
    env: &str,
) -> LalResult<()> {
    debug!("Installing dependencies{}",
           if !core { " and devDependencies" } else { "" });

    // create the joined hashmap of dependencies and possibly devdependencies
    let mut deps = manifest.dependencies.clone();
    if !core {
        for (k, v) in &manifest.devDependencies {
            deps.insert(k.clone(), *v);
        }
    }
    let mut extraneous = vec![]; // stuff we should remove

    // figure out what we have already
    let lf = Lockfile::default()
        .populate_from_input()
        .map_err(|e| {
            // Guide users a bit if they did something dumb - see #77
            warn!("Populating INPUT data failed - your INPUT may be corrupt");
            warn!("This can happen if you CTRL-C during `lal fetch`");
            warn!("Try to `rm -rf INPUT` and `lal fetch` again.");
            e
        })?;
    // filter out what we already have (being careful to examine env)
    for (name, d) in lf.dependencies {
        // if d.name at d.version in d.environment matches something in deps
        if let Some(&cand) = deps.get(&name) {
            // version found in manifest
            // ignore non-integer versions (stashed things must be overwritten)
            if let Ok(n) = d.version.parse::<u32>() {
                if n == cand && d.environment == env {
                    info!("Reuse {} {} {}", env, name, n);
                    deps.remove(&name);
                }
            }
        } else {
            extraneous.push(name.clone());
        }
    }

    let mut err = None;
    for (k, v) in deps {
        info!("Fetch {} {} {}", env, k, v);

        // first kill the folders we actually need to fetch:
        let cmponent_dir = Path::new("./INPUT").join(&k);
        if cmponent_dir.is_dir() {
            // Don't think this can fail, but we are dealing with NFS
            fs::remove_dir_all(&cmponent_dir)
                .map_err(|e| {
                    warn!("Failed to remove INPUT/{} - {}", k, e);
                    warn!("Please clean out your INPUT folder yourself to avoid corruption");
                    e
                })?;
        }

        let _ = backend.unpack_published_component(&k, Some(v), Some(env)).map_err(|e| {
            warn!("Failed to completely install {} ({})", k, e);
            // likely symlinks inside tarball that are being dodgy
            // this is why we clean_input
            err = Some(e);
        });
    }

    // remove extraneous deps
    for name in extraneous {
        info!("Remove {}", name);
        let pth = Path::new("./INPUT").join(&name);
        if pth.is_dir() {
            fs::remove_dir_all(&pth)?;
        }
    }

    if err.is_some() {
        warn!("Cleaning potentially broken INPUT");
        clean_input(); // don't want to risk having users in corrupted states
        return Err(CliError::InstallFailure);
    }
    Ok(())
}