Skip to content

Commit eeb6da5

Browse files
committed
add docker parse functionality through joyent/node-docker-file-parser
1 parent d5d498e commit eeb6da5

File tree

9 files changed

+672
-0
lines changed

9 files changed

+672
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.Rproj.user
2+
.Rhistory

DESCRIPTION

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ License: GPL-3
88
Encoding: UTF-8
99
LazyData: true
1010
RoxygenNote: 6.1.1
11+
Imports:
12+
R6,
13+
V8,
14+
dplyr,
15+
tibble,
16+
rlang,
17+
magrittr,
18+
vctrs

NAMESPACE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(Dockerfile)
4+
export(read_dockerfile)
5+
importFrom(R6,R6Class)
6+
importFrom(V8,v8)
7+
importFrom(dplyr,"%>%")
8+
importFrom(magrittr,"%$%")
9+
importFrom(rlang,"%||%")
10+
importFrom(tibble,as_tibble)
11+
importFrom(vctrs,"%0%")

R/dockerile.R

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#' A Dockerfile template
2+
#'
3+
#' @return A dockerfile template
4+
#'
5+
#' @section Methods:
6+
#' \describe{
7+
#' \item{\code{RUN}}{add a RUN command}
8+
#' \item{\code{ADD}}{add a ADD command}
9+
#' \item{\code{COPY}}{add a COPY command}
10+
#' \item{\code{WORKDIR}}{add a WORKDIR command}
11+
#' \item{\code{EXPOSE}}{add an EXPOSE command}
12+
#' \item{\code{VOLUME}}{add a VOLUME command}
13+
#' \item{\code{CMD}}{add a CMD command}
14+
#' \item{\code{LABEL}}{add a LABEL command}
15+
#' \item{\code{ENV}}{add a ENV command}
16+
#' \item{\code{ENTRYPOINT}}{add a ENTRYPOINT command}
17+
#' \item{\code{VOLUME}}{add a VOLUME command}
18+
#' \item{\code{USER}}{add a USER command}
19+
#' \item{\code{ARG}}{add an ARG command}
20+
#' \item{\code{ONBUILD}}{add a ONBUILD command}
21+
#' \item{\code{STOPSIGNAL}}{add a STOPSIGNAL command}
22+
#' \item{\code{HEALTHCHECK}}{add a HEALTHCHECK command}
23+
#' \item{\code{STOPSIGNAL}}{add a STOPSIGNAL command}
24+
#' \item{\code{SHELL}}{add a SHELL command}
25+
#' \item{\code{MAINTAINER}}{add a MAINTAINER command}
26+
#' \item{\code{custom}}{add a custom command}
27+
#' \item{\code{write}}{save the Dockerfile}
28+
#' \item{\code{switch_cmd}}{switch two command}
29+
#' \item{\code{remove_cmd}}{remove_cmd one or more command(s)}
30+
#' }
31+
#'
32+
#' @importFrom R6 R6Class
33+
#' @export
34+
#'
35+
#' @examples
36+
#' my_dock <- Dockerfile$new()
37+
38+
Dockerfile <- R6::R6Class("Dockerfile",
39+
public = list(
40+
Dockerfile = character(),
41+
## Either from a file, or from a character vector
42+
initialize = function(FROM = "rocker/r-base", AS = NULL){
43+
self$Dockerfile <- create_dockerfile(FROM, AS)
44+
},
45+
RUN = function(cmd){
46+
self$Dockerfile <- c(self$Dockerfile, add_run(cmd))
47+
},
48+
ADD = function(from, to, force = TRUE){
49+
self$Dockerfile <- c(self$Dockerfile,add_add(from, to, force))
50+
},
51+
COPY = function(from, to, force = TRUE){
52+
self$Dockerfile <- c(self$Dockerfile,add_copy(from, to, force))
53+
},
54+
WORKDIR = function(where){
55+
self$Dockerfile <- c(self$Dockerfile, add_workdir(where))
56+
},
57+
EXPOSE = function(port){
58+
self$Dockerfile <- c(self$Dockerfile, add_expose(port))
59+
},
60+
VOLUME = function(volume){
61+
self$Dockerfile <- c(self$Dockerfile, add_volume(volume))
62+
},
63+
CMD = function(cmd){
64+
self$Dockerfile <- c(self$Dockerfile, add_cmd(cmd))
65+
},
66+
LABEL = function(key, value){
67+
self$Dockerfile <- c(self$Dockerfile, add_label(key, value))
68+
},
69+
ENV = function(key, value){
70+
self$Dockerfile <- c(self$Dockerfile, add_env(key, value))
71+
},
72+
ENTRYPOINT = function(cmd){
73+
self$Dockerfile <- c(self$Dockerfile, add_entrypoint(cmd))
74+
},
75+
USER = function(user){
76+
self$Dockerfile <- c(self$Dockerfile, add_user(user))
77+
},
78+
ARG = function(arg, ahead = FALSE){
79+
if (ahead) {
80+
self$Dockerfile <- c(add_arg(arg), self$Dockerfile)
81+
} else {
82+
self$Dockerfile <- c(self$Dockerfile,add_arg(arg))
83+
}
84+
},
85+
ONBUILD = function(cmd){
86+
self$Dockerfile <- c(self$Dockerfile,add_onbuild(cmd))
87+
},
88+
STOPSIGNAL = function(signal){
89+
self$Dockerfile <- c(self$Dockerfile,add_stopsignal(signal))
90+
},
91+
HEALTHCHECK = function(check){
92+
self$Dockerfile <- c(self$Dockerfile,add_healthcheck(check))
93+
},
94+
SHELL = function(shell){
95+
self$Dockerfile <- c(self$Dockerfile,add_shell(shell))
96+
},
97+
MAINTAINER = function(name, email){
98+
self$Dockerfile <- c(self$Dockerfile,add_maintainer(name, email))
99+
},
100+
custom = function(base, cmd){
101+
self$Dockerfile <- c(self$Dockerfile, add_custom(base, cmd))
102+
},
103+
print = function(){
104+
cat(self$Dockerfile, sep = '\n')
105+
},
106+
write = function(as = "Dockerfile"){
107+
base::write(self$Dockerfile, file = as)
108+
},
109+
switch_cmd = function(a,b){
110+
self$Dockerfile <- switch_them(self$Dockerfile, a, b)
111+
},
112+
remove_cmd = function(where){
113+
self$Dockerfile <- remove_from(self$Dockerfile, where)
114+
},
115+
add_after = function(cmd, after){
116+
self$Dockerfile <- add_to(self$Dockerfile, cmd, after)
117+
}
118+
))

R/lumper.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#' \code{lumper} package
2+
#'
3+
#' Read and write Dockerfiles in R
4+
#'
5+
#' See the README on
6+
#' \href{https://github.com/svdwoude/lumper}{GitHub}
7+
#'
8+
#' @docType package
9+
#' @name lumper
10+
#' @importFrom dplyr %>%
11+
#' @importFrom magrittr %$%
12+
#' @importFrom rlang %||%
13+
#' @importFrom vctrs %0%
14+
NULL
15+
16+
17+
## quiets concerns of R CMD check re: the .'s that appear in pipelines
18+
if (getRversion() >= "2.15.1")
19+
utils::globalVariables(c(
20+
"."
21+
))
22+
23+
24+
#' parse Dockerfile
25+
#' @param Dockerfile string; content Dockerfile
26+
#' @importFrom tibble as_tibble
27+
#' @importFrom V8 v8
28+
#' @export
29+
read_dockerfile <- function(Dockerfile) {
30+
ct <- v8()
31+
ct$source(system.file("js/parser.js", package = "lumper"))
32+
ct$call("parse", Dockerfile) %>% as_tibble()
33+
}
34+
35+

0 commit comments

Comments
 (0)