1- # ' Add a line to a dockerfile at a specific position
1+ # ' Add a line to a ` dockerfile` at a specific position
22# '
3- # ' @param dockerfile A dockerfile object
4- # ' @param line Line to add
5- # ' @param after Position after which to add the line (default: end)
6- # ' @return Updated dockerfile object
3+ # ' Adds a raw line to a `dockerfile`` at a specified position. This is a lower-level
4+ # ' function typically used internally by higher-level functions.
5+ # '
6+ # ' @param dockerfile A `dockerfile` object
7+ # ' @param line Line to add (raw text)
8+ # ' @param after Position after which to add the line (default: end of file)
9+ # '
10+ # ' @return
11+ # ' An updated `dockerfile` object with the new line added
12+ # '
13+ # ' @examples
14+ # ' df <- dockerfile() |>
15+ # ' dfi_from("rocker/r-ver:4.4.0")
16+ # '
17+ # ' # Add a comment after the FROM instruction
18+ # ' df <- dfm_add_line(df, "# This is a comment", after = 1)
19+ # '
20+ # ' @details
21+ # ' Unlike the instruction-specific functions (`dfi_*`), this function adds
22+ # ' raw text without any formatting or validation. It's useful for adding
23+ # ' comments or custom instructions not covered by the built-in functions.
24+ # '
25+ # ' @seealso
26+ # ' [dfm_remove_line()] for removing a line &
27+ # ' [dfm_replace_line()] for replacing a line
28+ # '
29+ # ' @family dockerfile modification functions
730# ' @export
8- # ' @keywords internal
931dfm_add_line <- function (dockerfile , line , after = NULL ) {
1032 check_dockerfile(dockerfile )
1133
@@ -20,11 +42,30 @@ dfm_add_line <- function(dockerfile, line, after = NULL) {
2042 dockerfile
2143}
2244
23- # ' Remove a line from a dockerfile
45+ # ' Remove a line from a `dockerfile`
46+ # '
47+ # ' Removes a line at the specified position from a `dockerfile`.
48+ # '
49+ # ' @param dockerfile A `dockerfile` object
50+ # ' @param line_num Line number to remove
51+ # '
52+ # ' @return
53+ # ' An updated `dockerfile` object with the specified line removed
2454# '
25- # ' @param dockerfile A dockerfile object
26- # ' @param line_num Line number to remove
27- # ' @return Updated dockerfile object
55+ # ' @examples
56+ # ' df <- dockerfile() |>
57+ # ' dfi_from("rocker/r-ver:4.4.0") |>
58+ # ' dfi_run("apt-get update") |>
59+ # ' dfi_run("apt-get install -y libcurl4-openssl-dev")
60+ # '
61+ # ' # Remove the second RUN instruction (line 3)
62+ # ' df <- dfm_remove_line(df, 3)
63+ # '
64+ # ' @seealso
65+ # ' [dfm_add_line()] for adding a line &
66+ # ' [dfm_replace_line()] for replacing a line
67+ # '
68+ # ' @family dockerfile modification functions
2869# ' @export
2970dfm_remove_line <- function (dockerfile , line_num ) {
3071 check_dockerfile(dockerfile )
@@ -38,12 +79,31 @@ dfm_remove_line <- function(dockerfile, line_num) {
3879 dockerfile
3980}
4081
41- # ' Replace a line in a dockerfile
82+ # ' Replace a line in a `dockerfile`
83+ # '
84+ # ' Replaces a line at the specified position with new content.
85+ # '
86+ # ' @param dockerfile A `dockerfile` object
87+ # ' @param line_num Line number to replace
88+ # ' @param new_line New line content
4289# '
43- # ' @param dockerfile A dockerfile object
44- # ' @param line_num Line number to replace
45- # ' @param new_line New line content
46- # ' @return Updated dockerfile object
90+ # ' @return
91+ # ' An updated `dockerfile` object with the specified line replaced
92+ # '
93+ # ' @examples
94+ # ' df <- dockerfile() |>
95+ # ' dfi_from("rocker/r-ver:4.4.0") |>
96+ # ' dfi_run("apt-get update")
97+ # '
98+ # ' # Replace the RUN instruction with a more comprehensive one
99+ # ' df <- dfm_replace_line(df, 2,
100+ # ' "RUN apt-get update && apt-get install -y libcurl4-openssl-dev && apt-get clean")
101+ # '
102+ # ' @seealso
103+ # ' [dfm_add_line()] for adding a line &
104+ # ' [dfm_remove_line()] for removing a line
105+ # '
106+ # ' @family dockerfile modification functions
47107# ' @export
48108dfm_replace_line <- function (dockerfile , line_num , new_line ) {
49109 check_dockerfile(dockerfile )
@@ -57,12 +117,45 @@ dfm_replace_line <- function(dockerfile, line_num, new_line) {
57117 dockerfile
58118}
59119
60- # ' Move a line in a dockerfile
120+ # ' Move a line in a `dockerfile`
121+ # '
122+ # ' Moves a line from one position to another in a `dockerfile`.
61123# '
62- # ' @param dockerfile A dockerfile object
63- # ' @param from Source line number
64- # ' @param to Target position
65- # ' @return Updated dockerfile object
124+ # ' @param dockerfile A `dockerfile` object
125+ # ' @param from Source line number
126+ # ' @param to Target position
127+ # '
128+ # ' @return
129+ # ' An updated `dockerfile` object with the line moved to the new position
130+ # '
131+ # ' @examples
132+ # ' df <- dockerfile() |>
133+ # ' dfi_from("rocker/r-ver:4.4.0") |>
134+ # ' dfi_workdir("/app") |>
135+ # ' dfi_run("apt-get update") |>
136+ # ' dfi_copy(".", "/app/")
137+ # '
138+ # ' df
139+ # '
140+ # ' # Move the RUN instruction to be after COPY
141+ # ' df <- dfm_move_line(df, 3, 4)
142+ # ' df
143+ # '
144+ # ' @details
145+ # ' This function allows for reorganizing instructions in a **Dockerfile** by moving
146+ # ' lines to different positions. It's useful for correcting the order of
147+ # ' instructions without having to recreate the entire **Dockerfile**.
148+ # '
149+ # ' Note that moving certain instructions to incompatible positions can make
150+ # ' the **Dockerfile** invalid (e.g., moving a `FROM` instruction after a `RUN`).
151+ # ' Consider using [dfm_sort_by_instruction()] to follow Docker best practices.
152+ # '
153+ # ' @seealso
154+ # ' [dfm_add_line()] for adding a line,
155+ # ' [dfm_remove_line()] for removing a line, &
156+ # ' [dfm_sort_by_instruction()] for sorting instructions by type
157+ # '
158+ # ' @family dockerfile modification functions
66159# ' @export
67160dfm_move_line <- function (dockerfile , from , to ) {
68161 check_dockerfile(dockerfile )
@@ -104,10 +197,37 @@ dfm_move_line <- function(dockerfile, from, to) {
104197 dockerfile
105198}
106199
107- # ' Group similar instructions in a dockerfile
200+ # ' Group similar instructions in a `dockerfile`
201+ # '
202+ # ' Optimizes a `dockerfile` by grouping similar consecutive instructions
203+ # ' into single multi-command instructions where appropriate. This can reduce
204+ # ' the number of layers in the final Docker image.
205+ # '
206+ # ' @param dockerfile A `dockerfile` object
207+ # '
208+ # ' @return
209+ # ' A new `dockerfile` object with similar instructions grouped
210+ # '
211+ # ' @examples
212+ # ' df <- dockerfile() |>
213+ # ' dfi_from("rocker/r-ver:4.4.0") |>
214+ # ' dfi_run("apt-get update") |>
215+ # ' dfi_run("apt-get install -y curl") |>
216+ # ' dfi_run("apt-get clean")
217+ # '
218+ # ' # Group the three RUN instructions into one
219+ # ' df <- dfm_group_similar(df)
220+ # '
221+ # ' @details
222+ # ' This function primarily targets `RUN` instructions, combining them with `&&`
223+ # ' to create single multi-command instructions. This follows Docker best practices
224+ # ' by reducing the number of layers in the final image. Instructions like `FROM`,
225+ # ' `WORKDIR`, `USER`, `ENTRYPOINT`, and `CMD` are left as separate instructions.
108226# '
109- # ' @param dockerfile A dockerfile object
110- # ' @return Updated dockerfile object with similar instructions grouped
227+ # ' @seealso
228+ # ' [dfm_sort_by_instruction()] for sorting instructions by type
229+ # '
230+ # ' @family dockerfile modification functions
111231# ' @export
112232dfm_group_similar <- function (dockerfile ) {
113233 check_dockerfile(dockerfile )
@@ -189,11 +309,40 @@ dfm_group_similar <- function(dockerfile) {
189309 result
190310}
191311
192- # ' Sort instructions in a dockerfile by type
312+ # ' Sort instructions in a `dockerfile` by type
313+ # '
314+ # ' Reorders the instructions in a `dockerfile` according to Docker best practices
315+ # ' or a custom order specification.
316+ # '
317+ # ' @param dockerfile A `dockerfile` object
318+ # ' @param order Custom order for instructions (optional)
319+ # '
320+ # ' @return
321+ # ' A new `dockerfile` object with instructions sorted by type
322+ # '
323+ # ' @examples
324+ # ' df <- dockerfile() |>
325+ # ' dfi_cmd("R --no-save") |>
326+ # ' dfi_run("apt-get update") |>
327+ # ' dfi_from("rocker/r-ver:4.4.0")
328+ # '
329+ # ' # Sort according to best practices (FROM first, etc.)
330+ # ' df <- dfm_sort_by_instruction(df)
331+ # '
332+ # ' # Use a custom order
333+ # ' df <- dfm_sort_by_instruction(df,
334+ # ' order = c("FROM", "RUN", "WORKDIR", "CMD"))
335+ # '
336+ # ' @details
337+ # ' The default order follows Docker best practices, with instructions that change
338+ # ' less frequently appearing first (e.g., `FROM`, `ARG`, `LABEL`), and instructions
339+ # ' that change more frequently appearing later (e.g., `COPY`, `RUN`). This improves
340+ # ' caching and build performance.
341+ # '
342+ # ' @seealso
343+ # ' [dfm_group_similar()] for grouping similar instructions
193344# '
194- # ' @param dockerfile A dockerfile object
195- # ' @param order Custom order for instructions (optional)
196- # ' @return Updated dockerfile with sorted instructions
345+ # ' @family dockerfile modification functions
197346# ' @export
198347dfm_sort_by_instruction <- function (dockerfile , order = NULL ) {
199348 check_dockerfile(dockerfile )
0 commit comments