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
|
# Given a path to an directory and a list of files in that directory, copies
# those files to a new temporary directory. Performs some small modifications
# in this process, including renaming single-file Shiny apps to "app.R" and
# stripping packrat and renv commands from .Rprofile. Returns the path to the
# temporary directory.
bundleAppDir <- function(
appDir,
appFiles,
appPrimaryDoc = NULL,
appMode = NULL,
verbose = FALSE
) {
logger <- verboseLogger(verbose)
logger("Creating bundle staging directory")
bundleDir <- dirCreate(tempfile())
defer(unlink(bundleDir))
logger("Copying files into bundle staging directory")
for (file in appFiles) {
logger("Copying", file)
from <- file.path(appDir, file)
to <- file.path(bundleDir, file)
if (!is.null(appMode) && appMode == "shiny") {
# When deploying a single-file Shiny application and we have been provided
# appPrimaryDoc (usually by RStudio), rename that file to `app.R` so it
# will be discovered and run by shiny::runApp(getwd()).
#
# Note: We do not expect to see writeManifest(appPrimaryDoc="notapp.R").
if (is.character(appPrimaryDoc) &&
tolower(tools::file_ext(appPrimaryDoc)) == "r" &&
file == appPrimaryDoc) {
to <- file.path(bundleDir, "app.R")
}
}
dirCreate(dirname(to))
file.copy(from, to, copy.date = TRUE)
# ensure .Rprofile doesn't call packrat/init.R or renv/activate.R
if (basename(to) == ".Rprofile") {
tweakRProfile(to)
}
}
bundleDir
}
tweakRProfile <- function(path) {
lines <- readLines(path)
packratLines <- grep('source("packrat/init.R")', lines, fixed = TRUE)
if (length(packratLines) > 0) {
lines[packratLines] <- paste0(
"# Packrat initialization disabled in published application\n",
'# source("packrat/init.R")'
)
}
renvLines <- grep('source("renv/activate.R")', lines, fixed = TRUE)
if (length(renvLines) > 0) {
lines[renvLines] <- paste0(
"# renv initialization disabled in published application\n",
'# source("renv/activate.R")'
)
}
if (length(renvLines) > 0 || length(packratLines) > 0) {
msg <- sprintf(
"# Modified by rsconnect package %s on %s",
packageVersion("rsconnect"),
Sys.time()
)
lines <- c(msg, lines)
}
writeLines(lines, path)
}
# Writes a tar.gz file located at bundlePath containing all files in bundleDir.
writeBundle <- function(bundleDir, bundlePath, verbose = FALSE) {
logger <- verboseLogger(verbose)
prevDir <- setwd(bundleDir)
defer(setwd(prevDir))
tarImplementation <- getTarImplementation()
logger("Using tar: ", tarImplementation)
if (tarImplementation == "internal") {
detectLongNames(bundleDir)
}
utils::tar(bundlePath, files = NULL, compression = "gzip", tar = tarImplementation)
}
getTarImplementation <- function() {
# Check the rsconnect.tar option first. If that is unset, check the
# RSCONNECT_TAR environment var. If neither are set, use "internal".
tarImplementation <- getOption("rsconnect.tar", default = NA)
if (is.na(tarImplementation) || !nzchar(tarImplementation)) {
tarImplementation <- Sys.getenv("RSCONNECT_TAR", unset = NA)
}
if (is.na(tarImplementation) || !nzchar(tarImplementation)) {
tarImplementation <- "internal"
}
return(tarImplementation)
}
isWindows <- function() {
Sys.info()[["sysname"]] == "Windows"
}
createAppManifest <- function(appDir,
appMetadata,
users = NULL,
pythonConfig = NULL,
retainPackratDirectory = TRUE,
image = NULL,
envManagement = NULL,
envManagementR = NULL,
envManagementPy = NULL,
verbose = FALSE,
quiet = FALSE) {
if (is.null(image)) {
imageEnv <- Sys.getenv("RSCONNECT_IMAGE", unset = NA)
if (!is.na(imageEnv) && nchar(imageEnv) > 0) {
image <- imageEnv
}
}
if (needsR(appMetadata)) {
extraPackages <- inferRPackageDependencies(appMetadata)
# provide package entries for all dependencies
packages <- bundlePackages(
bundleDir = appDir,
extraPackages = extraPackages,
verbose = verbose,
quiet = quiet
)
} else {
packages <- list()
}
needsPython <- appMetadata$documentsHavePython ||
"jupyter" %in% appMetadata$quartoInfo$engines ||
"reticulate" %in% names(packages)
if (needsPython && !is.null(pythonConfig)) {
python <- pythonConfig(appDir)
packageFile <- file.path(appDir, python$package_manager$package_file)
writeLines(python$package_manager$contents, packageFile)
python$package_manager$contents <- NULL
} else {
python <- NULL
}
if (!retainPackratDirectory) {
# Optionally remove the packrat directory when it will not be included in
# deployments, such as manifest-only deployments.
unlink(file.path(appDir, "packrat"), recursive = TRUE)
}
# build the list of files to checksum
files <- list.files(appDir, recursive = TRUE, all.files = TRUE,
full.names = FALSE)
# provide checksums for all files
filelist <- list()
for (file in files) {
filepath <- file.path(appDir, file)
checksum <- list(checksum = fileMD5(filepath))
filelist[[file]] <- I(checksum)
}
# create userlist
userlist <- list()
if (!is.null(users) && length(users) > 0) {
for (i in 1:nrow(users)) {
user <- users[i, "user"]
hash <- users[i, "hash"]
userinfo <- list()
userinfo$hash <- hash
userlist[[user]] <- userinfo
}
}
# create the manifest
manifest <- list()
manifest$version <- 1
manifest$locale <- getOption("rsconnect.locale", detectLocale())
manifest$platform <- paste(R.Version()$major, R.Version()$minor, sep = ".")
metadata <- list(appmode = appMetadata$appMode)
# emit appropriate primary document information
primaryDoc <- ifelse(is.null(appMetadata$appPrimaryDoc) ||
tolower(tools::file_ext(appMetadata$appPrimaryDoc)) == "r",
NA, appMetadata$appPrimaryDoc)
metadata$primary_rmd <- ifelse(appMetadata$appMode %in% c("rmd-shiny", "rmd-static", "quarto-shiny", "quarto-static"), primaryDoc, NA)
metadata$primary_html <- ifelse(appMetadata$appMode == "static", primaryDoc, NA)
# emit content category (plots, etc)
metadata$content_category <- ifelse(!is.null(appMetadata$contentCategory),
appMetadata$contentCategory, NA)
metadata$has_parameters <- appMetadata$hasParameters
# add metadata
manifest$metadata <- metadata
# handle shorthand arg to enable/disable both R and Python
if (!is.null(envManagement)) {
envManagementR <- envManagement
envManagementPy <- envManagement
}
# if envManagement is explicitly enabled/disabled,
# create an environment_management obj
envManagementInfo <- list()
if (!is.null(envManagementR)) {
envManagementInfo$r <- envManagementR
}
if (!is.null(envManagementPy)) {
envManagementInfo$python <- envManagementPy
}
# emit the environment field
if (!is.null(image) || length(envManagementInfo) > 0) {
manifest$environment <- list()
# if there is a target image, attach it to the environment
if (!is.null(image)) {
manifest$environment$image <- image
}
# if either environment_management.r or environment_management.python
# is provided, write the environment_management field
if (length(envManagementInfo) > 0) {
manifest$environment$environment_management <- envManagementInfo
}
}
# indicate whether this is a quarto app/doc
manifest$quarto <- appMetadata$quartoInfo
# if there is python info for reticulate or Quarto, attach it
if (!is.null(python)) {
manifest$python <- python
}
# if there are no packages set manifest$packages to NA (json null)
if (length(packages) > 0) {
manifest$packages <- I(packages)
} else {
manifest$packages <- NA
}
# if there are no files, set manifest$files to NA (json null)
if (length(files) > 0) {
manifest$files <- I(filelist)
} else {
manifest$files <- NA
}
# if there are no users set manifest$users to NA (json null)
if (length(users) > 0) {
manifest$users <- I(userlist)
} else {
manifest$users <- NA
}
manifest
}
|