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
use crate::errors::*;
use std::fs;
use std::path::PathBuf;
pub fn eject(dir: PathBuf) -> Result<(), EjectionError> {
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| EjectionError::GitignoreUpdateFailed { source: err })?;
let gitignore = dir.join(".gitignore");
if gitignore.exists() {
let content = fs::read_to_string(&gitignore)
.map_err(|err| EjectionError::GitignoreUpdateFailed { source: err })?;
let mut new_content_vec = Vec::new();
let mut have_changed = false;
for line in content.lines() {
if line != ".perseus/" {
new_content_vec.push(line);
} else {
have_changed = true;
}
}
let new_content = new_content_vec.join("\n");
if !have_changed {
return Err(EjectionError::GitignoreLineNotPresent);
}
fs::write(&gitignore, new_content)
.map_err(|err| EjectionError::GitignoreUpdateFailed { source: err })?;
Ok(())
} else {
Err(EjectionError::GitignoreUpdateFailed {
source: std::io::Error::from(std::io::ErrorKind::NotFound),
})
}
}
pub fn has_ejected(dir: PathBuf) -> bool {
let ejected = dir.join(".perseus/.ejected");
ejected.exists()
}