Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DESCRIPTION instead of dependencies.R #230

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Imports:
box,
cli,
config,
desc,
fs,
glue,
lintr (>= 2.0.0),
Expand Down
46 changes: 27 additions & 19 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,45 @@ handle_old_rprofile <- function() {
}
}

write_dependencies <- function() {
deps <- "rhino"
if (fs::file_exists("dependencies.R")) {
cli::cli_alert_info("Updating existing 'dependencies.R'.")
deps <- c(deps, renv::dependencies("dependencies.R")$Package)
} else if (fs::dir_exists("app")) {
cli::cli_alert_info("Generating 'dependencies.R' based on 'app' directory.")
deps <- c(deps, renv::dependencies("app")$Package)
write_description <- function() {
if (fs::file_exists("DESCRIPTION")) {
cli::cli_alert_info("Updating existing 'DESCRIPTION'.")
# Pass explicit `file` argument to read `DESCRIPTION` even if it lacks the `Package` field.
desc <- desc::description$new(file = "DESCRIPTION")
} else {
desc <- desc::description$new(text = "")
if (fs::dir_exists("app")) {
cli::cli_alert_info("Generating 'DESCRIPTION' based on 'app' directory.")
desc$set_deps(data.frame(
type = "Imports",
package = renv::dependencies("app")$Package,
version = "*"
))
}
}
deps <- sort(unique(deps))
deps <- purrr::map_chr(deps, function(name) glue::glue("library({name})"))
deps <- c(
"# This file allows packrat (used by rsconnect during deployment) to pick up dependencies.",
deps
)
writeLines(deps, "dependencies.R")
# The `Package` field must be set for packrat to read dependencies from `DESCRIPTION`.
if (!desc$has_fields("Package")) desc$set("Package", "app")
desc$set_dep("rhino")
desc$write(file = "DESCRIPTION")
}

init_renv <- function(rhino_version) {
handle_old_rprofile()
write_dependencies()
write_description()
copy_template("renv")
if (fs::file_exists("renv.lock")) {
renv::settings$snapshot.type("explicit")
renv::load()
renv::restore(prompt = FALSE, clean = TRUE)
renv::install(rhino_version)
renv::snapshot()
} else {
# With `restart = TRUE`, RStudio fails to create a project
# with an "Unable to establish connection with R session" message.
renv::init(restart = FALSE)
renv::init(
settings = list(snapshot.type = "explicit"),
# With `restart = TRUE`, RStudio fails to create a project
# with an "Unable to establish connection with R session" message.
restart = FALSE
)
}
cli::cli_alert_success("Initialized renv.")
}
Expand Down
3 changes: 0 additions & 3 deletions inst/templates/renv/dot.renvignore

This file was deleted.