-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathconfig.R
More file actions
353 lines (295 loc) · 9.37 KB
/
config.R
File metadata and controls
353 lines (295 loc) · 9.37 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# configure-database.R ----
#' Retrieve the Global Configuration Database
#'
#' Retrieve the global configuration database (as an \R environment).
#'
#' @family configure-db
#'
#' @export
configure_database <- local({
database <- new.env(parent = emptyenv())
function() database
})
#' Define Variables for the Configuration Database
#'
#' Define variables to be used as part of the default configuration database.
#' These will be used by [configure_file()] when no configuration database
#' is explicitly supplied.
#'
#' @param ... A set of named arguments, mapping configuration names to values.
#'
#' @family configure-db
#'
#' @export
configure_define <- function(...) {
envir <- configure_database()
list2env(list(...), envir = envir)
}
#' @export
define <- configure_define
# utils.R ----
#' Configure a File
#'
#' Configure a file, replacing any instances of `@`-delimited variables, e.g.
#' `@VAR@`, with the value of the variable called `VAR` in the associated
#' `config` environment.
#'
#' @param source The file to be configured.
#' @param target The file to be generated.
#' @param config The configuration database.
#' @param verbose Boolean; report files as they are configured?
#'
#' @family configure
#'
#' @export
configure_file <- function(
source,
target = sub("[.]in$", "", source),
config = configure_database(),
verbose = configure_verbose())
{
contents <- readLines(source, warn = FALSE)
enumerate(config, function(key, val) {
needle <- paste("@", key, "@", sep = "")
replacement <- val
contents <<- gsub(needle, replacement, contents)
})
ensure_directory(dirname(target))
writeLines(contents, con = target)
info <- file.info(source)
Sys.chmod(target, mode = info$mode)
if (isTRUE(verbose)) {
fmt <- "** configured file: '%s' => '%s'"
message(sprintf(fmt, source, target))
}
}
#' Configure Files in a Directory
#'
#' This companion function to [configure_file()] can be used to
#' configure all `.in` files within a directory.
#'
#' @param path The path to a directory in which files should be configured.
#' @param config The configuration database to be used.
#' @param verbose Boolean; report files as they are configured?
#'
#' @family configure
#'
#' @export
configure_directory <- function(
path = ".",
config = configure_database(),
verbose = configure_verbose())
{
files <- list.files(
path = path,
pattern = "[.]in$",
full.names = TRUE)
lapply(files, configure_file, config = config, verbose = verbose)
}
configure_auto <- function(type) {
configure_common(type = type)
}
configure_common <- function(type) {
if (!isTRUE(getOption("configure.common", default = TRUE)))
return(invisible(FALSE))
sources <- list.files(
path = c("R", "src"),
pattern = "[.]in$",
full.names = TRUE
)
sources <- sub("[.]/", "", sources)
if (type == "configure") {
lapply(sources, configure_file)
} else if (type == "cleanup") {
targets <- sub("[.]in$", "", sources)
lapply(targets, remove_file)
}
invisible(TRUE)
}
#' Read R Configuration for a Package
#'
#' Read the \R configuration, as through `R CMD config`.
#'
#' @param ... The \R configuration values to read (as a character vector).
#' If empty, all values are read as through `R CMD config --all`).
#' @param package The path to the \R package's sources.
#' @param envir The environment in which the configuration information should
#' be assigned. By default, the [configure_database()] is populated with the
#' requested values.
#' @param verbose Boolean; notify the user as \R configuration is read?
#'
#' @export
read_r_config <- function(
...,
package = Sys.getenv("R_PACKAGE_DIR", unset = "."),
envir = configure_database(),
verbose = configure_verbose())
{
# move to requested directory
owd <- setwd(package)
on.exit(setwd(owd), add = TRUE)
R <- file.path(R.home("bin"), "R")
# suppress cygwin path warnings for windows
if (Sys.info()[["sysname"]] == "Windows") {
CYGWIN <- Sys.getenv("CYGWIN")
Sys.setenv(CYGWIN = "nodosfilewarning")
on.exit(Sys.setenv(CYGWIN = CYGWIN), add = TRUE)
}
values <- unlist(list(...), recursive = TRUE)
if (length(values) == 0) {
if (verbose)
message("** executing 'R CMD config --all'")
output <- system2(R, c("CMD", "config", "--all"), stdout = TRUE)
equalsIndex <- regexpr("=", output, fixed = TRUE)
keys <- trim_whitespace(substring(output, 1, equalsIndex - 1))
config <- as.list(trim_whitespace(substring(output, equalsIndex + 1)))
names(config) <- keys
} else {
if (verbose)
message("** executing 'R CMD config'")
config <- lapply(values, function(value) {
system2(R, c("CMD", "config", value), stdout = TRUE)
})
names(config) <- values
}
if (is.null(envir))
return(config)
list2env(config, envir = envir)
}
#' Concatenate the Contents of a Set of Files
#'
#' Given a set of files, concatenate their contents into
#' a single file.
#'
#' @param sources An \R list of files
#' @param target The file to use for generation.
#' @param headers Headers to be used for each file copied.
#' @param preamble Text to be included at the beginning of the document.
#' @param postamble Text to be included at the end of the document.
#' @param verbose Boolean; inform the user when the requested file is created?
#'
#' @export
concatenate_files <- function(
sources,
target,
headers = sprintf("# %s ----", basename(sources)),
preamble = NULL,
postamble = NULL,
verbose = configure_verbose())
{
pieces <- vapply(seq_along(sources), function(i) {
source <- sources[[i]]
header <- headers[[i]]
contents <- trim_whitespace(read_file(source))
paste(header, contents, "", sep = "\n\n")
}, character(1))
all <- c(preamble, pieces, postamble)
ensure_directory(dirname(target))
writeLines(all, con = target)
if (verbose) {
fmt <- "** created file '%s'"
message(sprintf(fmt, target))
}
TRUE
}
#' Add Configure Infrastructure to an R Package
#'
#' Add the infrastructure needed to configure an R package.
#'
#' @param package The path to the top-level directory of an \R package.
#' @export
use_configure <- function(package = ".") {
# preserve working directory
owd <- getwd()
on.exit(setwd(owd), add = TRUE)
# find resources
package <- normalizePath(package, winslash = "/")
resources <- system.file("resources", package = "configure")
# copy into temporary directory
dir <- tempfile("configure-")
on.exit(unlink(dir, recursive = TRUE), add = TRUE)
dir.create(dir)
file.copy(resources, dir, recursive = TRUE)
# rename resources directory
setwd(dir)
file.rename(basename(resources), basename(package))
# now, copy these files back into the target directory
file.copy(basename(package), dirname(package), recursive = TRUE)
# ensure DESCRIPTION contains 'Biarch: TRUE' for Windows
setwd(package)
DESCRIPTION <- read_file("DESCRIPTION")
if (!grepl("(?:^|\n)Biarch:", DESCRIPTION)) {
DESCRIPTION <- paste(DESCRIPTION, "Biarch: TRUE", sep = "\n")
DESCRIPTION <- gsub("\n{2,}", "\n", DESCRIPTION)
cat(DESCRIPTION, file = "DESCRIPTION", sep = "\n")
}
}
ensure_directory <- function(dir) {
info <- file.info(dir)
# no file exists at this location; try to make it
if (is.na(info$isdir)) {
dir.create(info$isdir, recursive = TRUE, showWarnings = FALSE)
if (!file.exists(dir))
stop("failed to create directory '", dir, "'")
return(TRUE)
}
# a directory already exists
if (isTRUE(info$isdir))
return(TRUE)
# a file exists, but it's not a directory
stop("file already exists at path '", dir, "'")
}
enumerate <- function(x, f, ...) {
nms <- if (is.environment(x)) ls(envir = x) else names(x)
lapply(nms, function(nm) {
f(nm, x[[nm]], ...)
})
}
read_file <- function(path) {
paste(readLines(path, warn = FALSE), collapse = "\n")
}
remove_file <- function(
path,
verbose = configure_verbose())
{
info <- file.info(path)
if (!is.na(info$isdir)) {
unlink(path, recursive = isTRUE(info$isdir))
if (verbose) {
fmt <- "** removed file '%s'"
message(sprintf(fmt, path))
}
}
TRUE
}
source_file <- function(
path,
envir = parent.frame())
{
contents <- read_file(path)
invisible(eval(parse(text = contents), envir = envir))
}
trim_whitespace <- function(x) {
gsub("^[[:space:]]*|[[:space:]]*$", "", x)
}
configure_verbose <- function() {
getOption("configure.verbose", !interactive())
}
# run.R ----
local({
# extract path to install script
args <- commandArgs(TRUE)
type <- args[[1]]
# report start of execution
package <- Sys.getenv("R_PACKAGE_NAME", unset = "<unknown>")
fmt <- "* preparing to %s package '%s' ..."
message(sprintf(fmt, type, package))
# execute the requested script
path <- sprintf("tools/config/%s.R", type)
if (file.exists(path)) source_file(path)
# perform automatic configuration
configure_auto(type = type)
# report end of execution
fmt <- "* finished %s for package '%s'"
message(sprintf(fmt, type, package))
})