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
use crate::errors::*;
use std::fs;
use std::path::PathBuf;

/// Ejects the user from the Perseus CLi harness by exposing the internal subcrates to them. All this does is remove `.perseus/` from
/// the user's `.gitignore` and add a file `.ejected` to `.perseus/`.
pub fn eject(dir: PathBuf) -> Result<()> {
    // Create a file declaring ejection so `clean` throws errors (we don't want the user to accidentally delete everything)
    let ejected = dir.join(".perseus/.ejected");
    fs::write(
        &ejected,
        "This file signals to Perseus that you've ejected. Do NOT delete it!",
    )
    .map_err(|err| ErrorKind::GitignoreEjectUpdateFailed(err.to_string()))?;
    // Now remove `.perseus/` from the user's `.gitignore`
    let gitignore = dir.join(".gitignore");
    if gitignore.exists() {
        let content = fs::read_to_string(&gitignore)
            .map_err(|err| ErrorKind::GitignoreEjectUpdateFailed(err.to_string()))?;
        let mut new_content_vec = Vec::new();
        // Remove the line pertaining to Perseus
        // We only target the one that's exactly the same as what's automatically injected, anything else can be done manually
        for line in content.lines() {
            if line != ".perseus/" {
                new_content_vec.push(line);
            }
        }
        let new_content = new_content_vec.join("\n");
        // Make sure we've actually changed something
        if content == new_content {
            bail!(ErrorKind::GitignoreEjectUpdateFailed(
                "line `.perseus/` to remove not found".to_string()
            ))
        }
        fs::write(&gitignore, new_content)
            .map_err(|err| ErrorKind::GitignoreEjectUpdateFailed(err.to_string()))?;

        Ok(())
    } else {
        bail!(ErrorKind::GitignoreEjectUpdateFailed(
            "file not found".to_string()
        ))
    }
}

/// Checks if the user has ejected or not. If they have, commands like `clean` should fail unless `--force` is provided.
pub fn has_ejected(dir: PathBuf) -> bool {
    let ejected = dir.join(".perseus/.ejected");
    ejected.exists()
}