-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathconfig.R
615 lines (499 loc) · 16.7 KB
/
config.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# configure-database.R -------------------------------------------------------
#' Retrieve the Global Configuration Database
#'
#' Retrieve the global configuration database.
#' `db` is a helper alias for the database
#' returned by `configure_database()`.
#'
#' @export
configure_database <- local({
database <- new.env(parent = emptyenv())
class(database) <- "configure_database"
function() database
})
#' @export
print.configure_database <- function(x, ...) {
str.configure_database(x, ...)
}
#' @export
str.configure_database <- function(object, ...) {
writeLines("<configure database>")
objects <- mget(ls(envir = object, all.names = TRUE), object)
output <- utils::capture.output(utils::str(objects, ...))
writeLines(output[-1])
invisible(output)
}
#' 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. [define()] is provided as a shorter alias for the
#' same function.
#'
#' @param ... A set of named arguments, mapping configuration names to values.
#'
#' @export
configure_define <- function(...) {
envir <- configure_database()
list2env(list(...), envir = envir)
}
#' @rdname configure_define
#' @export
define <- configure_define
#' @rdname configure_database
#' @export
db <- configure_database()
# utils.R --------------------------------------------------------------------
#' Configure a File
#'
#' Configure a file, replacing (by default) 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 lhs The left-hand side marker; defaults to `@`.
#' @param rhs The right-hand side marker; defaults to `@`.
#' @param verbose Boolean; report files as they are configured?
#'
#' @family configure
#'
#' @export
configure_file <- function(
source,
target = sub("[.]in$", "", source),
config = configure_database(),
lhs = "@",
rhs = "@",
verbose = configure_verbose())
{
# read source file
contents <- readLines(source, warn = FALSE)
# replace defined variables
enumerate(config, function(key, val) {
needle <- paste(lhs, key, rhs, sep = "")
replacement <- val
contents <<- gsub(needle, replacement, contents, fixed = TRUE)
})
ensure_directory(dirname(target))
# write configured file to target location
# prefer unix newlines for Makevars
mode <- if (basename(target) %in% "Makevars") "wb" else "w"
conn <- file(target, open = mode)
on.exit(close(conn), add = TRUE)
writeLines(contents, con = conn)
# copy over source permissions
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) {
if (!isTRUE(getOption("configure.auto", default = TRUE)))
return(invisible(FALSE))
if (isTRUE(getOption("configure.common", default = TRUE)))
configure_common(type = type)
if (isTRUE(getOption("configure.platform", default = TRUE)))
configure_platform(type = type)
}
configure_common <- function(type) {
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)
}
configure_platform <- function(type) {
sysname <- tolower(Sys.info()[["sysname"]])
subdirs <- sysname
if (sysname != "windows")
subdirs <- c("unix", subdirs)
dirs <- c("R", "src")
for (dir in dirs) {
# list files (take care to remove directories)
sources <- Filter(
function(file) identical(file.info(file)$isdir, FALSE),
list.files(file.path(dir, subdirs), full.names = TRUE)
)
# configure all discovered sources
for (source in sources) {
target <- file.path(dir, basename(source))
switch(type,
configure = configure_file(source, target),
cleanup = remove_file(target))
}
}
}
#' Execute R CMD config
#'
#' Read information about how \R is configured as through `R CMD config`.
#'
#' @param ... The names of potential configuration values.
#' @param simplify Boolean; simplify in the case where a single value was
#' requested?
#'
#' @export
r_cmd_config <- function(..., simplify = 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)
}
# loop through requested values and call R CMD config
values <- unlist(list(...), recursive = TRUE)
config <- lapply(values, function(value) {
# execute it
stdout <- tempfile("r-cmd-config-", fileext = ".txt")
on.exit(unlink(stdout), add = TRUE)
status <- system2(R, c("CMD", "config", value), stdout = stdout)
# report failures as NULL (distinct from empty string)
if (status)
return(NULL)
readLines(stdout)
})
names(config) <- values
if (simplify && length(config) == 1)
return(config[[1]])
config
}
#' 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) {
# R CMD config --all only available since R 3.4.0
if (getRversion() < "3.4.0") {
fmt <- "'R CMD config --all' not available in R version '%s'"
stop(sprintf(fmt, getRversion()))
}
# execute action
stdout <- tempfile("r-cmd-config-", fileext = ".txt")
on.exit(unlink(stdout), add = TRUE)
status <- system2(R, c("CMD", "config", "--all"), stdout = stdout)
if (status)
stop("failed to execute 'R CMD config --all'")
# read and parse output
output <- readLines(stdout, warn = FALSE)
config <- parse_key_value(output)
} else {
# loop through requested values and call R CMD config
config <- lapply(values, function(value) {
# execute it
stdout <- tempfile("r-cmd-config-", fileext = ".txt")
on.exit(unlink(stdout), add = TRUE)
status <- system2(R, c("CMD", "config", value), stdout = stdout)
# report failures as NULL (distinct from empty string)
if (status)
return(NULL)
readLines(stdout)
})
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 = section_header(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")
}
# write placeholders for 'configure.R', 'cleanup.R' if none exist
ensure_directory("tools/config")
configure <- "tools/config/configure.R"
if (!file.exists("tools/config/configure.R")) {
text <- c(
"# Prepare your package for installation here.",
"# Use 'define()' to define configuration variables.",
"# Use 'configure_file()' to substitute configuration values.",
"",
""
)
writeLines(text, con = configure)
}
cleanup <- "tools/config/cleanup.R"
if (!file.exists("tools/config/cleanup.R")) {
text <- c(
"# Clean up files generated during configuration here.",
"# Use 'remove_file()' to remove files generated during configuration.",
"",
""
)
writeLines(text, con = cleanup)
}
# notify the user what we did
message("* Copied 'configure{.win}' and 'cleanup{.win}'.")
message("* Updated 'tools/config.R'.")
# open 'configure.R', 'cleanup.R' for editing if in RStudio
rstudio <-
!is.na(Sys.getenv("RSTUDIO", unset = NA)) &&
requireNamespace("rstudioapi", quietly = TRUE)
if (rstudio) {
rstudioapi::navigateToFile("tools/config/configure.R", 5, 1)
rstudioapi::navigateToFile("tools/config/cleanup.R", 4, 1)
} else {
message("* Use 'tools/config/configure.R' for package configuration.")
message("* Use 'tools/config/cleanup.R' for package cleanup.")
}
}
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(dir, 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))
return(TRUE)
name <- if (info$isdir) "directory" else "file"
unlink(path, recursive = isTRUE(info$isdir))
if (file.exists(path)) {
fmt <- "failed to remove %s '%s' (insufficient permissions?)"
stop(sprintf(fmt, name, path))
}
if (verbose) {
fmt <- "*** removed %s '%s'"
message(sprintf(fmt, name, 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())
}
named <- function(object, nm) {
names(object) <- nm
object
}
parse_key_value <- function(
text,
separator = "=",
trim = TRUE)
{
# find the separator
index <- regexpr(separator, text, fixed = TRUE)
# split into parts
keys <- substring(text, 1, index - 1)
vals <- substring(text, index + 1)
# trim if requested
if (trim) {
keys <- trim_whitespace(keys)
vals <- trim_whitespace(vals)
}
# put together into R list
named(as.list(vals), keys)
}
move_directory <- function(source, target) {
# ensure we're trying to move a directory
info <- file.info(source)
if (is.na(info$isdir)) {
fmt <- "no directory exists at path '%s'"
stop(sprintf(fmt, source), call. = FALSE)
}
if (!info$isdir) {
fmt <- "'%s' exists but is not a directory"
stop(sprintf(fmt, source), call. = FALSE)
}
# good to go -- let's move it
unlink(target, recursive = TRUE)
file.rename(source, target)
unlink(source, recursive = TRUE)
}
section_header <- function(
label,
prefix = "#",
suffix = "-",
length = 78L)
{
# figure out length of full header
n <- length - nchar(label) - nchar(prefix) - 2L
n[n < 0] <- 0
# generate '-' suffixes
tail <- vapply(n, function(i) {
paste(rep(suffix, i), collapse = "")
}, character(1))
# join it all together
paste(prefix, label, tail)
}
# run.R ----------------------------------------------------------------------
if (!interactive()) {
# extract path to install script
args <- commandArgs(TRUE)
type <- args[[1]]
# preserve working directory
owd <- getwd()
on.exit(setwd(owd), add = TRUE)
# switch working directory to the calling scripts's directory as set
# by the shell, in case the R working directory was set to something else
basedir <- Sys.getenv("PWD", unset = NA)
if (!is.na(basedir))
setwd(basedir)
# 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))
}