/
utils-fs.R
102 lines (80 loc) · 2.43 KB
/
utils-fs.R
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
dir_copy_to <- function(pkg, from, to, overwrite = TRUE) {
stopifnot(length(to) == 1)
new_path <- function(path) {
path_abs(path_rel(path, start = from), start = to)
}
contents <- dir_ls(from, recurse = TRUE)
is_dir <- fs::is_dir(contents)
# First create directories
dir_create(to)
dirs <- contents[is_dir]
dir_create(new_path(dirs))
# Then copy files
file_copy_to(pkg, contents[!is_dir],
to_dir = to,
from_dir = from,
overwrite = overwrite
)
}
# Would be better to base on top of data structure that provides both
# files and root directory to use for printing
file_copy_to <- function(pkg,
from_paths,
to_dir = pkg$dst_path,
from_dir = path_common(from_paths),
overwrite = TRUE) {
if (length(from_paths) == 0) {
return()
}
from_rel <- path_rel(from_paths, from_dir)
to_paths <- path_abs(from_rel, to_dir)
# Ensure all the "to" directories exist
dirs_to_paths <- unique(fs::path_dir(to_paths))
dir_create(dirs_to_paths)
eq <- purrr::map2_lgl(from_paths, to_paths, file_equal)
if (any(!eq)) {
cat_line(
"Copying ", src_path(path_rel(from_paths[!eq], pkg$src_path)),
" to ", dst_path(path_rel(to_paths[!eq], pkg$dst_path))
)
}
file_copy(from_paths[!eq], to_paths[!eq], overwrite = overwrite)
}
out_of_date <- function(source, target) {
if (!file_exists(target))
return(TRUE)
if (!file_exists(source)) {
stop("'", source, "' does not exist", call. = FALSE)
}
file.info(source)$mtime > file.info(target)$mtime
}
# Path helpers ------------------------------------------------------------
path_abs <- function(path, start = ".") {
is_abs <- is_absolute_path(path)
path[is_abs] <- path_norm(path[is_abs])
path[!is_abs] <- fs::path_abs(path(start, path))
path_tidy(path)
}
path_first_existing <- function(...) {
paths <- path(...)
for (path in paths) {
if (file_exists(path))
return(path)
}
NULL
}
path_package_pkgdown <- function(..., package, bs_version = NULL) {
check_installed(package)
base <- system_file("pkgdown", package = package)
# If bs_version supplied, first try for versioned template
if (!is.null(bs_version)) {
path <- path(base, paste0("BS", bs_version), ...)
if (file_exists(path)) {
return(path)
}
}
path(base, ...)
}
path_pkgdown <- function(...) {
system_file(..., package = "pkgdown")
}