inst/gha/data-apps-deps-update.R

if (!requireNamespace("renv", quietly = TRUE)) install.packages("renv")
if (!requireNamespace("yaml", quietly = TRUE)) install.packages("yaml")
if (!requireNamespace("withr", quietly = TRUE)) install.packages("withr")
if (!requireNamespace("jsonlite", quietly = TRUE)) install.packages("jsonlite")

update_apps_deps <- function(repo_dir = ".") {
  withr::local_dir(repo_dir)

  revdeps <- renv::dependencies(
    "inst/apps",
    progress = TRUE
  )
  revdeps <-
    subset(
      revdeps,
      !(
        Package %in% c(base_packages())
      )
    )
  apps_deps <- sort(unique(revdeps$Package))

  revdeps$folder <- sub(".*/apps/(\\d\\d\\d-[^/]+)/.*", "\\1", revdeps$Source)

  apps_deps_map <- lapply(split(revdeps, revdeps$folder), function(revdeps_sub) {
    sort(unique(c(revdeps_sub$Package, "shiny"))) # always include `{shiny}`
  })

  apps_deps_txt <- utils::capture.output(dput(apps_deps))
  apps_deps_map_txt <- utils::capture.output(dput(apps_deps_map))

  # Pretty format
  apps_deps_map_txt <- paste0(apps_deps_map_txt, collapse = "\n")
  apps_deps_map_txt <- gsub("\n", "", apps_deps_map_txt)
  apps_deps_map_txt <- gsub("\\s+", " ", apps_deps_map_txt)
  apps_deps_map_txt <- gsub("(`", "(\n  `", apps_deps_map_txt, fixed = TRUE)
  apps_deps_map_txt <- gsub(", `", ",\n  `", apps_deps_map_txt, fixed = TRUE)
  apps_deps_map_txt <- gsub("))", ")\n)", apps_deps_map_txt, fixed = TRUE)
  apps_deps_map_txt <- gsub(" )", ")", apps_deps_map_txt, fixed = TRUE)


  cat(
    file = file.path(repo_dir, "R/data-apps-deps.R"),
    sep = "",
    "# Do not edit by hand!\n",
    "# This file is automatically generated by `./inst/gha/data-apps-deps-update.R`\n",
    # "apps_deps <- ", paste0(apps_deps_txt, collapse = "\n"), "\n",
    "apps_deps_map <- ", apps_deps_map_txt, "\n"
  )
}

base_packages <- function() {
  pkg_df <- as.data.frame(utils::installed.packages(), stringsAsFactors = FALSE)
  pkg_df$Package[pkg_df$Priority %in% c("base", "recommended")]
}

update_shinyverse <- function() {
  pkg_infos <- jsonlite::read_json("https://posit-dev-shinycoreci.r-universe.dev/api/packages/")

  pkgs <- vapply(pkg_infos, `[[`, character(1), "Package")
  urls <- vapply(pkg_infos, `[[`, character(1), "RemoteUrl")

  # Sort according to pkg name
  urls <- urls[order(pkgs)]
  pkgs <- pkgs[order(pkgs)]

  universe_txt <- '"https://posit-dev-shinycoreci.r-universe.dev"'
  pkgs_txt <- utils::capture.output(dput(pkgs))
  urls_txt <- utils::capture.output(dput(urls))
  cat(
    file = "R/data-shinyverse.R",
    sep = "",
    "# Do not edit by hand!\n",
    "# This file is automatically generated by `./inst/gha/data-apps-deps-update.R`\n",
    "# To add / update a package, make a PR here: https://github.com/posit-dev-shinycoreci/posit-dev-shinycoreci.r-universe.dev/blob/main/packages.json\n",
    "shinyverse_cran_url <- ", paste0(universe_txt, collapse = "\n"), "\n",
    "\n",
    "shinyverse_pkgs <- ", paste0(pkgs_txt, collapse = "\n"), "\n",
    "\n",
    "shinyverse_urls <- ", paste0(urls_txt, collapse = "\n"), "\n"
  )
}
rstudio/shinycoreci documentation built on April 11, 2025, 3:17 p.m.