generated from coatless-devcontainer/r-pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdockerignore-instructions.R
155 lines (147 loc) · 4.63 KB
/
dockerignore-instructions.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#' Add patterns to a dockerignore object
#'
#' Adds one or more patterns to a `dockerignore` object, avoiding duplicates.
#'
#' @param dockerignore A `dockerignore` object
#' @param pattern Character vector of patterns to add
#'
#' @return
#' An updated `dockerignore` object with the new patterns added
#'
#' @examples
#' di <- dockerignore()
#'
#' # Add a single pattern
#' di <- di_add(di, ".git/")
#'
#' # Add multiple patterns
#' di <- di_add(di, c("*.log", "node_modules/", "*.tmp"))
#'
#' @details
#' Patterns follow the same syntax as `.gitignore` files:
#' * Lines starting with `#` are comments
#' * Blank lines are ignored
#' * Trailing slashes `/` specify directories
#' * Patterns with special characters like `*`, `?`, and `[]` use glob syntax
#' * Lines starting with `!` negate a pattern (include a file that would otherwise be ignored)
#'
#' @seealso
#' [di_remove()] for removing patterns &
#' [di_replace()] for replacing patterns
#'
#' @family dockerignore instruction functions
#' @export
di_add <- function(dockerignore, pattern) {
check_dockerignore(dockerignore)
# Handle vector input
for (p in pattern) {
# Add pattern if not already present
if (!p %in% dockerignore$patterns) {
dockerignore$patterns <- c(dockerignore$patterns, p)
}
}
dockerignore
}
#' Remove patterns from a `dockerignore` object
#'
#' Removes one or more patterns from a `dockerignore` object.
#'
#' @param dockerignore A `dockerignore` object
#' @param pattern Character vector of patterns to remove
#'
#' @return
#' An updated `dockerignore` object with the specified patterns removed
#'
#' @examples
#' # Create a dockerignore object and add some patterns
#' di <- dockerignore() |>
#' di_add(c(".git/", "*.log", "node_modules/"))
#'
#' # Remove a pattern
#' di <- di_remove(di, "*.log")
#'
#' @seealso
#' [di_add()] for adding patterns &
#' [di_replace()] for replacing patterns
#'
#' @family dockerignore instruction functions
#' @export
di_remove <- function(dockerignore, pattern) {
check_dockerignore(dockerignore)
# Remove patterns if present
dockerignore$patterns <- dockerignore$patterns[!dockerignore$patterns %in% pattern]
dockerignore
}
#' Replace patterns in a dockerignore object
#'
#' Replaces one or more patterns in a dockerignore object with new patterns.
#'
#' @param dockerignore A `dockerignore` object
#' @param old_pattern Pattern(s) to replace
#' @param new_pattern New pattern(s)
#'
#' @return
#' An updated `dockerignore` object with the specified patterns replaced
#'
#' @examples
#' # Create a dockerignore object and add some patterns
#' di <- dockerignore() |>
#' di_add(c("*.log", "*.tmp", "node_modules/"))
#'
#' # Replace a single pattern
#' di <- di_replace(di, "*.log", "logs/")
#'
#' # Replace multiple patterns with a single pattern
#' di <- di_replace(di, c("*.tmp", "node_modules/"), "temp/")
#'
#' # Replace patterns one-to-one
#' di <- di_replace(di,
#' c("*.log", "*.tmp"),
#' c("logs/*", "temp/*"))
#'
#' @details
#' This function allows you to replace patterns in a dockerignore object.
#' Three modes of operation are supported:
#'
#' 1. Replace a single pattern with a single pattern
#' 2. Replace multiple patterns with a single pattern
#' 3. Replace multiple patterns with corresponding new patterns (one-to-one)
#'
#' For the third mode, `old_pattern` and `new_pattern` must have the same length.
#'
#' @seealso
#' [di_add()] for adding patterns &
#' [di_remove()] for removing patterns
#'
#' @family dockerignore instruction functions
#' @export
di_replace <- function(dockerignore, old_pattern, new_pattern) {
check_dockerignore(dockerignore)
# Handle different input cases
if (length(old_pattern) == 1 && length(new_pattern) == 1) {
# Simple case: replace single pattern with single pattern
idx <- which(dockerignore$patterns == old_pattern)
if (length(idx) > 0) {
dockerignore$patterns[idx] <- new_pattern
}
} else if (length(old_pattern) > 1 && length(new_pattern) == 1) {
# Replace multiple patterns with a single pattern
for (op in old_pattern) {
idx <- which(dockerignore$patterns == op)
if (length(idx) > 0) {
dockerignore$patterns[idx] <- new_pattern
}
}
} else if (length(old_pattern) == length(new_pattern)) {
# Replace each old pattern with corresponding new pattern
for (i in seq_along(old_pattern)) {
idx <- which(dockerignore$patterns == old_pattern[i])
if (length(idx) > 0) {
dockerignore$patterns[idx] <- new_pattern[i]
}
}
} else {
cli::cli_abort("old_pattern and new_pattern must have the same length or new_pattern must be length 1")
}
dockerignore
}