Skip to content

Commit

Permalink
feat: implement use_readme_rmd
Browse files Browse the repository at this point in the history
- Rework of @ilyaZar first implem

Issue #1011
  • Loading branch information
ColinFay committed Apr 4, 2023
1 parent 11eeea2 commit d92d6f3
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 83 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions R/bootstrap_usethis.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
133 changes: 51 additions & 82 deletions R/use_readme.R
Original file line number Diff line number Diff line change
@@ -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
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# `{PKG}`

<!-- badges: start -->
<!-- badges: end -->
## 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)
}
2 changes: 1 addition & 1 deletion inst/shinyexample/dev/01_start.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions inst/utils/empty_readme.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%%"
)
```

# `{%s}`

<!-- badges: start -->
<!-- badges: end -->

## 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()
```
30 changes: 30 additions & 0 deletions man/use_readme_rmd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions tests/testthat/test-use_readme.R
Original file line number Diff line number Diff line change
@@ -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()
})

0 comments on commit d92d6f3

Please sign in to comment.