@@ -77,16 +77,26 @@ configure_file <- function(
7777 rhs = " @" ,
7878 verbose = configure_verbose())
7979{
80+ # read source file
8081 contents <- readLines(source , warn = FALSE )
82+
83+ # replace defined variables
8184 enumerate(config , function (key , val ) {
8285 needle <- paste(lhs , key , rhs , sep = " " )
8386 replacement <- val
8487 contents <<- gsub(needle , replacement , contents , fixed = TRUE )
8588 })
8689
8790 ensure_directory(dirname(target ))
88- writeLines(contents , con = target )
8991
92+ # write configured file to target location
93+ # prefer unix newlines for Makevars
94+ mode <- if (target %in% " Makevars" ) " wb" else " w"
95+ conn <- file(target , open = mode )
96+ on.exit(close(conn ), add = TRUE )
97+ writeLines(contents , con = conn )
98+
99+ # copy over source permissions
90100 info <- file.info(source )
91101 Sys.chmod(target , mode = info $ mode )
92102
@@ -580,8 +590,9 @@ if (!interactive()) {
580590
581591 # switch working directory to the calling scripts's directory as set
582592 # by the shell, in case the R working directory was set to something else
583- basedir <- Sys.getenv(" PWD" , unset = " ." )
584- setwd(basedir )
593+ basedir <- Sys.getenv(" PWD" , unset = NA )
594+ if (! is.na(basedir ))
595+ setwd(basedir )
585596
586597 # report start of execution
587598 package <- Sys.getenv(" R_PACKAGE_NAME" , unset = " <unknown>" )
0 commit comments