From d92d6f382c985e6debb1143641ab5e02fade998e Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 4 Apr 2023 12:00:43 +0200 Subject: [PATCH] feat: implement use_readme_rmd - Rework of @ilyaZar first implem Issue #1011 --- NAMESPACE | 1 + R/bootstrap_usethis.R | 11 +++ R/use_readme.R | 133 ++++++++++++------------------- inst/shinyexample/dev/01_start.R | 2 +- inst/utils/empty_readme.Rmd | 59 ++++++++++++++ man/use_readme_rmd.Rd | 30 +++++++ tests/testthat/test-use_readme.R | 40 ++++++++++ 7 files changed, 193 insertions(+), 83 deletions(-) create mode 100644 inst/utils/empty_readme.Rmd create mode 100644 man/use_readme_rmd.Rd create mode 100644 tests/testthat/test-use_readme.R diff --git a/NAMESPACE b/NAMESPACE index 2369a3c4..5c44bf87 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -80,6 +80,7 @@ export(use_internal_file) export(use_internal_html_template) export(use_internal_js_file) export(use_module_test) +export(use_readme_rmd) export(use_recommended_deps) export(use_recommended_tests) export(use_utils_server) diff --git a/R/bootstrap_usethis.R b/R/bootstrap_usethis.R index 1e033f2c..a92ad3a1 100644 --- a/R/bootstrap_usethis.R +++ b/R/bootstrap_usethis.R @@ -114,3 +114,14 @@ usethis_use_spell_check <- function( error ) } + +usethis_use_readme_rmd <- function( + open = rlang::is_interactive() +) { + check_usethis_installed( + reason = "to create a readme." + ) + usethis::use_readme_rmd( + open = open + ) +} diff --git a/R/use_readme.R b/R/use_readme.R index 2306e0b5..6ed54393 100644 --- a/R/use_readme.R +++ b/R/use_readme.R @@ -1,105 +1,74 @@ #' Generate a README.Rmd -#' +#' @inheritParams usethis::use_readme_rmd +#' @inheritParams add_module +#' @inheritParams fill_desc #' @param overwrite an optional \code{logical} flag; if \code{TRUE}, overwrite #' existing \code{README.Rmd}, else throws an error if \code{README.Rmd} exists #' #' @return pure side-effect function that generates template \code{README.Rmd} #' @export -use_readme_rmd <- function(overwrite = FALSE) { +use_readme_rmd <- function( + open = rlang::is_interactive(), + pkg_name = golem::get_golem_name(), + overwrite = FALSE, + pkg = golem::get_golem_wd() +) { stopifnot(`Arg. 'overwrite' must be logical` = is.logical(overwrite)) - tmp_pth <- get_rmd_pth() - check_overwrite(overwrite, tmp_pth) + # We move the working directory to perform this action, + # in case we're launching the action from somewhere else + old <- setwd(pkg) + on.exit(setwd(old)) - readme_tmpl <- generate_readme_tmpl( - pkg_name = pkg_name() + # Guess the readme path + readme_path <- file.path( + pkg, + "README.Rmd" ) - writeLines( - text = readme_tmpl, - con = file.path(tmp_pth) + # Removing the README if it already exists and overwrite is TRUE + check_overwrite( + overwrite, + readme_path ) -} -get_rmd_pth <- function() { - file.path( - get_golem_wd(), - "README.Rmd" + + usethis_use_readme_rmd() + + readme_tmpl <- generate_readme_tmpl( + pkg_name = pkg_name ) + + write( + x = readme_tmpl, + file = readme_path, + append = FALSE, + sep = "\n" + ) + return(invisible(TRUE)) } + check_overwrite <- function(overwrite, tmp_pth) { - if (isTRUE(overwrite)) { - file.create(tmp_pth) - } else { - if (file.exists(tmp_pth)) { + # If the user wants to overwrite, we remove the file + # Otherwise, error if the file already exists + if (file.exists(tmp_pth)){ + if (isTRUE(overwrite)) { + unlink(tmp_pth, TRUE, TRUE) + } else { stop("README.Rmd already exists. Set `overwrite = TRUE` to overwrite.") } } -} -generate_readme_tmpl <- function(pkg_name) { - tmp_file <- '--- -output: github_document ---- - - - -```{r, include = FALSE} - knitr::opts_chunk$set( - collapse = TRUE, - comment = "#>", - fig.path = "man/figures/README-", - out.width = "100%" -) -``` - -# `{PKG}` - - - -## Installation - -You can install the development version of `{PKG}` like so: - -```{r} -# FILL THIS IN! HOW CAN PEOPLE INSTALL YOUR DEV PACKAGE? -``` - -## Run - -You can launch the application by running: - -```{r, eval = FALSE} -PKG::run_app() -``` - -## About - -You are reading the doc about version : `r golem::pkg_version()` - -This README has been compiled on the - -```{r} -Sys.time() -``` - -Here are the tests results and package coverage: - -```{r, error = TRUE} -devtools::check(quiet = TRUE) -``` +} -```{r echo = FALSE} -unloadNamespace("PKG") -``` +generate_readme_tmpl <- function(pkg_name) { -```{r} -covr::package_coverage() -``` -' - tmp_file <- stringr::str_replace_all( - tmp_file, - "PKG", - pkg_name + tmp_file <- readLines( + golem_sys("utils/empty_readme.Rmd") + ) + return( + sprintf( + tmp_file, + pkg_name + ) ) - return(tmp_file) } diff --git a/inst/shinyexample/dev/01_start.R b/inst/shinyexample/dev/01_start.R index 7a963694..9c27f7ee 100644 --- a/inst/shinyexample/dev/01_start.R +++ b/inst/shinyexample/dev/01_start.R @@ -38,7 +38,7 @@ golem::install_dev_deps() ## Create Common Files ---- ## See ?usethis for more information usethis::use_mit_license("Golem User") # You can set another license here -usethis::use_readme_rmd(open = FALSE) +golem::use_readme_rmd(open = FALSE) devtools::build_readme() # Note that `contact` is required since usethis version 2.1.5 # If your {usethis} version is older, you can remove that param diff --git a/inst/utils/empty_readme.Rmd b/inst/utils/empty_readme.Rmd new file mode 100644 index 00000000..b2f575a4 --- /dev/null +++ b/inst/utils/empty_readme.Rmd @@ -0,0 +1,59 @@ +--- +output: github_document +--- + + + +```{r, include = FALSE} + knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + fig.path = "man/figures/README-", + out.width = "100%%" +) +``` + +# `{%s}` + + + + +## Installation + +You can install the development version of `{%s}` like so: + +```{r} +# FILL THIS IN! HOW CAN PEOPLE INSTALL YOUR DEV PACKAGE? +``` + +## Run + +You can launch the application by running: + +```{r, eval = FALSE} +%s::run_app() +``` + +## About + +You are reading the doc about version : `r golem::pkg_version()` + +This README has been compiled on the + +```{r} +Sys.time() +``` + +Here are the tests results and package coverage: + +```{r, error = TRUE} +devtools::check(quiet = TRUE) +``` + +```{r echo = FALSE} +unloadNamespace("%s") +``` + +```{r, error = TRUE} +covr::package_coverage() +``` diff --git a/man/use_readme_rmd.Rd b/man/use_readme_rmd.Rd new file mode 100644 index 00000000..89a04779 --- /dev/null +++ b/man/use_readme_rmd.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/use_readme.R +\name{use_readme_rmd} +\alias{use_readme_rmd} +\title{Generate a README.Rmd} +\usage{ +use_readme_rmd( + open = rlang::is_interactive(), + pkg_name = golem::get_golem_name(), + overwrite = FALSE, + pkg = golem::get_golem_wd() +) +} +\arguments{ +\item{open}{Open the newly created file for editing? Happens in RStudio, if +applicable, or via \code{\link[utils:file.edit]{utils::file.edit()}} otherwise.} + +\item{pkg_name}{The name of the package} + +\item{overwrite}{an optional \code{logical} flag; if \code{TRUE}, overwrite +existing \code{README.Rmd}, else throws an error if \code{README.Rmd} exists} + +\item{pkg}{Path to the root of the package. Default is \code{get_golem_wd()}.} +} +\value{ +pure side-effect function that generates template \code{README.Rmd} +} +\description{ +Generate a README.Rmd +} diff --git a/tests/testthat/test-use_readme.R b/tests/testthat/test-use_readme.R new file mode 100644 index 00000000..33d9f7e0 --- /dev/null +++ b/tests/testthat/test-use_readme.R @@ -0,0 +1,40 @@ +test_that("generate_readme_tmpl works", { res <- generate_readme_tmpl("my_pkg") + expect_true( + grepl("my_pkg", paste(res, collapse = " ")) + ) + expect_true( + grepl("my_pkg::run_app()", paste(res, collapse = " ")) + ) + expect_true( + grepl("covr::package_coverage", paste(res, collapse = " ")) + ) + expect_true( + grepl("unloadNamespace", paste(res, collapse = " ")) + ) + expect_true( + grepl("devtools::check", paste(res, collapse = " ")) + ) +}) + + +test_that("check_overwrite works", { + expect_error( + check_overwrite(FALSE, golem_sys("utils/empty_readme.Rmd")), + "README.Rmd already exists. Set `overwrite = TRUE` to overwrite." + ) +}) + +test_that("use_readme_rmd works", { + expect_true( + use_readme_rmd( + open = FALSE, + overwrite = TRUE, + pkg = getwd(), + pkg_name = "rand_name" + ) + ) + expect_true( + file.exists("README.Rmd") + ) + devtools:::build_readme() +})