-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_ignorepattern.R
62 lines (55 loc) · 1.92 KB
/
write_ignorepattern.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
# write_to_rbuildignore --------------------------------------------------------
#' Write to .Rbuildignore
#'
#' @param ignore_pattern pattern that should be added to .Rbuildignore
#' @param comment optional comment to write to file before the ignore pattern
#' (default: NULL)
#' @return .Rbuildignore created/updated with pattern
#' @export
#' @importFrom fs file_exists
#'
write_to_rbuildignore <- function(ignore_pattern, comment = NULL)
{
write_ignorepattern_to_file(ignore_pattern, comment, file = ".Rbuildignore")
}
# write_to_gitignore -----------------------------------------------------------
#' Write to .gitignore
#'
#' @param ignore_pattern pattern that should be added to .Rbuildignore
#' @param comment optional comment to write to file before the ignore pattern
#' #' (default: NULL)
#' @return .gitignore created/updated with pattern
#' @export
#' @importFrom fs file_exists
#'
write_to_gitignore <- function(ignore_pattern, comment = NULL)
{
write_ignorepattern_to_file(ignore_pattern, comment, file = ".gitignore")
}
#' Write ignore pattern to file
#'
#' @param ignore_pattern pattern that should be added to .Rbuildignore
#' @param comment optional comment to write to file before the ignore pattern
#' (default: NULL)
#' @param file path to file where to write
#' @keywords internal
#' @noRd
#' @importFrom fs file_exists
#'
write_ignorepattern_to_file <- function(ignore_pattern, comment = NULL, file)
{
# If there is no file, write the pattern, prepended by the comment (if any)
# to the file and return
if (! fs::file_exists(file)) {
writeLines(c(comment, ignore_pattern), file)
return()
}
# File exists, read the file
patterns <- readLines(file)
# Return if the ignore pattern is already contained
if (any(grepl(ignore_pattern, patterns, fixed = TRUE))) {
return()
}
# Add comment (if any) and pattern to the existing patterns in the file
writeLines(c(patterns, comment, ignore_pattern), file)
}