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;
pub fn eject(dir: PathBuf) -> Result<()> {
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()))?;
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();
for line in content.lines() {
if line != ".perseus/" {
new_content_vec.push(line);
}
}
let new_content = new_content_vec.join("\n");
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()
))
}
}
pub fn has_ejected(dir: PathBuf) -> bool {
let ejected = dir.join(".perseus/.ejected");
ejected.exists()
}