R/golem_hook.R

Defines functions golem_hook

Documented in golem_hook

# Generated by fusen: do not edit by hand

#' Golem Hook function
#'
#' @return Used for side effect
#' @inheritParams golem::project_hook
#' @export
#'
#' @examples
#' if (requireNamespace("golem") & interactive()) {
#'   golem::create_golem(file.path(tempdir(), "myapp"), project_hook = golem_hook)
#' }
golem_hook <- function(
  path,
  package_name,
  ...
) {
  unlink(file.path(path, "R/run_app.R"), TRUE, TRUE)
  unlink(file.path(path, "R/app_ui.R"), TRUE, TRUE)
  unlink(file.path(path, "R/app_server.R"), TRUE, TRUE)
  file.copy(
    system.file(
      "golem/run_app.R",
      package = "brochure"
    ),
    file.path(path, "R/run_app.R")
  )

  run_app <- readLines(
    file.path(path, "R/run_app.R")
  )
  run_app <- gsub(
    "REPLACEME",
    package_name,
    run_app
  )
  write(run_app, file.path(path, "R/run_app.R"))

  file.copy(
    system.file(
      "golem/mod_home.R",
      package = "brochure"
    ),
    file.path(path, "R/mod_home.R")
  )

  # The test structure is written here rather than left to
  # `golem::use_recommended_tests()`, which only builds it when `tests/` is
  # missing and would find this directory already there.
  dir.create(file.path(path, "tests/testthat"), recursive = TRUE, showWarnings = FALSE)
  file.copy(
    system.file(
      "golem/test-mod_home.R",
      package = "brochure"
    ),
    file.path(path, "tests/testthat/test-mod_home.R")
  )
  write(
    c(
      "library(testthat)",
      sprintf("library(%s)", package_name),
      "",
      sprintf('test_check("%s")', package_name)
    ),
    file.path(path, "tests/testthat.R")
  )

  # `golem::use_recommended_tests()` writes a file testing `app_ui()` and
  # `app_server()`, which this hook has just deleted -- and it errors rather
  # than overwrite one that is already there. The brochure version goes in now,
  # and the call in `dev/01_start.R` with it.
  file.copy(
    system.file(
      "golem/test-golem-recommended.R",
      package = "brochure"
    ),
    file.path(path, "tests/testthat/test-golem-recommended.R")
  )
  start_R <- readLines(file.path(path, "dev/01_start.R"))
  start_R[
    which(
      grepl(
        "golem::use_recommended_tests()",
        start_R,
        fixed = TRUE
      )
    )
  ] <- "# brochure::golem_hook() already wrote tests/testthat/test-golem-recommended.R"
  unlink(file.path(path, "dev/01_start.R"))
  write(start_R, file.path(path, "dev/01_start.R"))

  # The module template calls `page()`, so the app depends on brochure. Written
  # by hand rather than with `usethis::use_package()`: brochure does not depend
  # on usethis, and this hook runs before anything is installed.
  description <- readLines(file.path(path, "DESCRIPTION"))
  description <- append(
    description,
    "    brochure,",
    after = grep("^Imports:", description)
  )
  description <- c(
    description,
    "Suggests: ",
    "    testthat (>= 3.0.0)",
    "Config/testthat/edition: 3"
  )
  write(description, file.path(path, "DESCRIPTION"))

  dev_R <- readLines(
    file.path(path, "dev/02_dev.R")
  )
  dev_R[
    which(
      grepl(
        "name_of_module1",
        dev_R
      )
    )
  ] <- 'golem::add_module(name = "name_of_module1", with_test = TRUE, module_template = brochure::new_page) # Name of the module'

  dev_R[
    which(
      grepl(
        "name_of_module2",
        dev_R
      )
    )
  ] <- 'golem::add_module(name = "name_of_module2", with_test = TRUE, module_template = brochure::new_page) # Name of the module'
  unlink(file.path(path, "dev/02_dev.R"))
  write(dev_R, file.path(path, "dev/02_dev.R"))
}

Try the brochure package in your browser

Any scripts or data that you put into this service are public.

brochure documentation built on Oct. 14, 2026, 5:08 p.m.