From 7f300043f64af215fc050812a456682cc8c32e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Tue, 10 Sep 2024 11:41:21 +0200 Subject: [PATCH] feat: use_poisson_pkgdown() --- DESCRIPTION | 4 +++ NAMESPACE | 1 + R/use-poisson-pkgdown.R | 59 ++++++++++++++++++++++++++++++++++++++ README.md | 31 ++++++-------------- man/use_poisson_pkgdown.Rd | 21 ++++++++++++++ 5 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 R/use-poisson-pkgdown.R create mode 100644 man/use_poisson_pkgdown.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 4ad4179..6ef6507 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,3 +21,7 @@ Suggests: knitr, rmarkdown VignetteBuilder: knitr +Imports: + cli, + desc, + yaml diff --git a/NAMESPACE b/NAMESPACE index 318d198..56811ac 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,4 @@ # Generated by roxygen2: do not edit by hand export(ruler) +export(use_poisson_pkgdown) diff --git a/R/use-poisson-pkgdown.R b/R/use-poisson-pkgdown.R new file mode 100644 index 0000000..9318e35 --- /dev/null +++ b/R/use-poisson-pkgdown.R @@ -0,0 +1,59 @@ +#' Set up poissontemplate usage +#' +#' This function +#' * registers poissontemplate usage in the pkgdown config; +#' * explains what the next steps are. +#' +#' @param pkg Path to package +#' +#' @return None +#' @export +#' +use_poisson_pkgdown <- function(pkg = getwd()) { + cat(paste(cli::symbol$info, sprintf("Working in %s\n", pkg))) + + current_needs <- desc::desc_get("Config/Needs/website") + needs <- if (!is.na(current_needs)) { + toString(c(as.character(current_needs), "poissonconsulting/poissontemplate")) + } else { + "poissonconsulting/poissontemplate" + } + desc::desc_set("Config/Needs/website" = needs) + cat(paste(cli::symbol$tick, "Registered GHA dependency on poissontemplate.\n")) + + config_path <- find_pkgdown_config(pkg) + if (is.null(config_path)) { + meta <- list( + template = list(package = "poissontemplate") + ) + yaml::write_yaml(meta, file.path(pkg, "_pkgdown.yml")) + } else { + config <- yaml::read_yaml(config_path) + config$template$package <- "poissontemplate" + config$template$params$bootswatch <- NULL + config$template$includes$before_title <- NULL + if (!is.null(config$template$bootstrap)) { + config$template$bootstrap <- 5 + } + yaml::write_yaml(config, file.path(pkg, config_path)) + } + cat(paste(cli::symbol$tick, "Registered poissontemplate in pkgdown config.\n")) + cat(paste(cli::symbol$bullet, "Check that the config edits are all harmless.\n")) + cat(paste(cli::symbol$bullet, "Git commit and push to origin.\n")) + + # TODO: traffic analytics setup? +} + +find_pkgdown_config <- function(pkg) { + possible_paths <- c( + "_pkgdown.yml", "_pkgdown.yaml", + "pkgdown/_pkgdown.yml", "pkgdown/_pkgdown.yaml", + "inst/_pkgdown.yml", "inst/_pkgdown.yaml" + ) + for (path in possible_paths) { + if (file.exists(file.path(pkg, path))) { + return(path) + } + } + NULL +} diff --git a/README.md b/README.md index 6d0e6bd..1dac20a 100644 --- a/README.md +++ b/README.md @@ -8,29 +8,6 @@ poissontemplate provides a custom [pkgdown](https://pkgdown.r-lib.org/) template for Poisson consulting packages. Please don’t use it for your own package. -## Setup - -### Register the use of the template - -In `_pkgdown.yml` that you can create through `usethis::use_pkgdown()` if absent, -have these lines: - -```yaml -template: - package: poissontemplate -``` - -### For continuous integration - -For all sites, ensure that DESCRIPTION contains: - -```yaml -Config/Needs/website: poissonconsulting/poissontemplate -``` - -by editing the file manually or by running `desc::desc_set("Config/Needs/website", "poissonconsulting/poissontemplate")`. - - ## Installation You can install the development version of poissontemplate from [GitHub](https://github.com/) with: @@ -40,6 +17,14 @@ You can install the development version of poissontemplate from [GitHub](https:/ pak::pak("poissonconsulting/poissontemplate") ``` +## Setup + +In every package where you need to set up poissontemplate usage, run + +``` r +poissontemplate::use_poisson_pkgdown() +``` + ## Relevant resources - pkgdown documentation about theming: https://pkgdown.r-lib.org/articles/customise.html#theming diff --git a/man/use_poisson_pkgdown.Rd b/man/use_poisson_pkgdown.Rd new file mode 100644 index 0000000..b60be0e --- /dev/null +++ b/man/use_poisson_pkgdown.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use-poisson-pkgdown.R +\name{use_poisson_pkgdown} +\alias{use_poisson_pkgdown} +\title{Set up poissontemplate usage} +\usage{ +use_poisson_pkgdown(pkg = getwd()) +} +\arguments{ +\item{pkg}{Path to package} +} +\value{ +None +} +\description{ +This function +\itemize{ +\item registers poissontemplate usage in the pkgdown config; +\item explains what the next steps are. +} +}