-
Notifications
You must be signed in to change notification settings - Fork 198
/
copy.rs
80 lines (69 loc) · 2.69 KB
/
copy.rs
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
use crate::error::Result;
use std::fs;
use std::path::Path;
use regex::Regex;
/// Copies documentation from a crate's target directory to destination.
///
/// Target directory must have doc directory.
///
/// This function is designed to avoid file duplications.
pub fn copy_doc_dir<P: AsRef<Path>, Q: AsRef<Path>>(source: P, destination: Q) -> Result<()> {
let destination = destination.as_ref();
// Make sure destination directory exists
if !destination.exists() {
fs::create_dir_all(destination)?;
}
// Avoid copying common files
let dup_regex = Regex::new(
r"(\.lock|\.txt|\.woff|\.svg|\.css|main-.*\.css|main-.*\.js|normalize-.*\.js|rustdoc-.*\.css|storage-.*\.js|theme-.*\.js)$")
.unwrap();
for file in source.as_ref().read_dir()? {
let file = file?;
let destination_full_path = destination.join(file.file_name());
let metadata = file.metadata()?;
if metadata.is_dir() {
fs::create_dir_all(&destination_full_path)?;
copy_doc_dir(file.path(), destination_full_path)?
} else if dup_regex.is_match(&file.file_name().into_string().unwrap()[..]) {
continue;
} else {
fs::copy(&file.path(), &destination_full_path)?;
}
}
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use std::fs;
#[test]
fn test_copy_doc_dir() {
let source = tempfile::Builder::new()
.prefix("cratesfyi-src")
.tempdir()
.unwrap();
let destination = tempfile::Builder::new()
.prefix("cratesfyi-dst")
.tempdir()
.unwrap();
let doc = source.path().join("doc");
fs::create_dir(&doc).unwrap();
fs::create_dir(doc.join("inner")).unwrap();
fs::write(doc.join("index.html"), "<html>spooky</html>").unwrap();
fs::write(doc.join("index.txt"), "spooky").unwrap();
fs::write(doc.join("inner").join("index.html"), "<html>spooky</html>").unwrap();
fs::write(doc.join("inner").join("index.txt"), "spooky").unwrap();
fs::write(doc.join("inner").join("important.svg"), "<svg></svg>").unwrap();
// lets try to copy a src directory to tempdir
copy_doc_dir(source.path().join("doc"), destination.path()).unwrap();
assert!(destination.path().join("index.html").exists());
assert!(!destination.path().join("index.txt").exists());
assert!(destination.path().join("inner").join("index.html").exists());
assert!(!destination.path().join("inner").join("index.txt").exists());
assert!(!destination
.path()
.join("inner")
.join("important.svg")
.exists());
}
}