Skip to content

Commit d92d6f3

Browse files
committed
feat: implement use_readme_rmd
- Rework of @ilyaZar first implem Issue #1011
1 parent 11eeea2 commit d92d6f3

File tree

7 files changed

+193
-83
lines changed

7 files changed

+193
-83
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export(use_internal_file)
8080
export(use_internal_html_template)
8181
export(use_internal_js_file)
8282
export(use_module_test)
83+
export(use_readme_rmd)
8384
export(use_recommended_deps)
8485
export(use_recommended_tests)
8586
export(use_utils_server)

R/bootstrap_usethis.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,14 @@ usethis_use_spell_check <- function(
114114
error
115115
)
116116
}
117+
118+
usethis_use_readme_rmd <- function(
119+
open = rlang::is_interactive()
120+
) {
121+
check_usethis_installed(
122+
reason = "to create a readme."
123+
)
124+
usethis::use_readme_rmd(
125+
open = open
126+
)
127+
}

R/use_readme.R

Lines changed: 51 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,74 @@
11
#' Generate a README.Rmd
2-
#'
2+
#' @inheritParams usethis::use_readme_rmd
3+
#' @inheritParams add_module
4+
#' @inheritParams fill_desc
35
#' @param overwrite an optional \code{logical} flag; if \code{TRUE}, overwrite
46
#' existing \code{README.Rmd}, else throws an error if \code{README.Rmd} exists
57
#'
68
#' @return pure side-effect function that generates template \code{README.Rmd}
79
#' @export
8-
use_readme_rmd <- function(overwrite = FALSE) {
10+
use_readme_rmd <- function(
11+
open = rlang::is_interactive(),
12+
pkg_name = golem::get_golem_name(),
13+
overwrite = FALSE,
14+
pkg = golem::get_golem_wd()
15+
) {
916
stopifnot(`Arg. 'overwrite' must be logical` = is.logical(overwrite))
1017

11-
tmp_pth <- get_rmd_pth()
12-
check_overwrite(overwrite, tmp_pth)
18+
# We move the working directory to perform this action,
19+
# in case we're launching the action from somewhere else
20+
old <- setwd(pkg)
21+
on.exit(setwd(old))
1322

14-
readme_tmpl <- generate_readme_tmpl(
15-
pkg_name = pkg_name()
23+
# Guess the readme path
24+
readme_path <- file.path(
25+
pkg,
26+
"README.Rmd"
1627
)
1728

18-
writeLines(
19-
text = readme_tmpl,
20-
con = file.path(tmp_pth)
29+
# Removing the README if it already exists and overwrite is TRUE
30+
check_overwrite(
31+
overwrite,
32+
readme_path
2133
)
22-
}
23-
get_rmd_pth <- function() {
24-
file.path(
25-
get_golem_wd(),
26-
"README.Rmd"
34+
35+
usethis_use_readme_rmd()
36+
37+
readme_tmpl <- generate_readme_tmpl(
38+
pkg_name = pkg_name
2739
)
40+
41+
write(
42+
x = readme_tmpl,
43+
file = readme_path,
44+
append = FALSE,
45+
sep = "\n"
46+
)
47+
return(invisible(TRUE))
2848
}
49+
2950
check_overwrite <- function(overwrite, tmp_pth) {
30-
if (isTRUE(overwrite)) {
31-
file.create(tmp_pth)
32-
} else {
33-
if (file.exists(tmp_pth)) {
51+
# If the user wants to overwrite, we remove the file
52+
# Otherwise, error if the file already exists
53+
if (file.exists(tmp_pth)){
54+
if (isTRUE(overwrite)) {
55+
unlink(tmp_pth, TRUE, TRUE)
56+
} else {
3457
stop("README.Rmd already exists. Set `overwrite = TRUE` to overwrite.")
3558
}
3659
}
37-
}
38-
generate_readme_tmpl <- function(pkg_name) {
39-
tmp_file <- '---
40-
output: github_document
41-
---
42-
43-
<!-- README.md is generated from README.Rmd. Please edit that file -->
44-
45-
```{r, include = FALSE}
46-
knitr::opts_chunk$set(
47-
collapse = TRUE,
48-
comment = "#>",
49-
fig.path = "man/figures/README-",
50-
out.width = "100%"
51-
)
52-
```
53-
54-
# `{PKG}`
5560

56-
<!-- badges: start -->
57-
<!-- badges: end -->
58-
59-
## Installation
60-
61-
You can install the development version of `{PKG}` like so:
62-
63-
```{r}
64-
# FILL THIS IN! HOW CAN PEOPLE INSTALL YOUR DEV PACKAGE?
65-
```
66-
67-
## Run
68-
69-
You can launch the application by running:
70-
71-
```{r, eval = FALSE}
72-
PKG::run_app()
73-
```
74-
75-
## About
76-
77-
You are reading the doc about version : `r golem::pkg_version()`
78-
79-
This README has been compiled on the
80-
81-
```{r}
82-
Sys.time()
83-
```
84-
85-
Here are the tests results and package coverage:
86-
87-
```{r, error = TRUE}
88-
devtools::check(quiet = TRUE)
89-
```
61+
}
9062

91-
```{r echo = FALSE}
92-
unloadNamespace("PKG")
93-
```
63+
generate_readme_tmpl <- function(pkg_name) {
9464

95-
```{r}
96-
covr::package_coverage()
97-
```
98-
'
99-
tmp_file <- stringr::str_replace_all(
100-
tmp_file,
101-
"PKG",
102-
pkg_name
65+
tmp_file <- readLines(
66+
golem_sys("utils/empty_readme.Rmd")
67+
)
68+
return(
69+
sprintf(
70+
tmp_file,
71+
pkg_name
72+
)
10373
)
104-
return(tmp_file)
10574
}

inst/shinyexample/dev/01_start.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ golem::install_dev_deps()
3838
## Create Common Files ----
3939
## See ?usethis for more information
4040
usethis::use_mit_license("Golem User") # You can set another license here
41-
usethis::use_readme_rmd(open = FALSE)
41+
golem::use_readme_rmd(open = FALSE)
4242
devtools::build_readme()
4343
# Note that `contact` is required since usethis version 2.1.5
4444
# If your {usethis} version is older, you can remove that param

inst/utils/empty_readme.Rmd

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
output: github_document
3+
---
4+
5+
<!-- README.md is generated from README.Rmd. Please edit that file -->
6+
7+
```{r, include = FALSE}
8+
knitr::opts_chunk$set(
9+
collapse = TRUE,
10+
comment = "#>",
11+
fig.path = "man/figures/README-",
12+
out.width = "100%%"
13+
)
14+
```
15+
16+
# `{%s}`
17+
18+
<!-- badges: start -->
19+
<!-- badges: end -->
20+
21+
## Installation
22+
23+
You can install the development version of `{%s}` like so:
24+
25+
```{r}
26+
# FILL THIS IN! HOW CAN PEOPLE INSTALL YOUR DEV PACKAGE?
27+
```
28+
29+
## Run
30+
31+
You can launch the application by running:
32+
33+
```{r, eval = FALSE}
34+
%s::run_app()
35+
```
36+
37+
## About
38+
39+
You are reading the doc about version : `r golem::pkg_version()`
40+
41+
This README has been compiled on the
42+
43+
```{r}
44+
Sys.time()
45+
```
46+
47+
Here are the tests results and package coverage:
48+
49+
```{r, error = TRUE}
50+
devtools::check(quiet = TRUE)
51+
```
52+
53+
```{r echo = FALSE}
54+
unloadNamespace("%s")
55+
```
56+
57+
```{r, error = TRUE}
58+
covr::package_coverage()
59+
```

man/use_readme_rmd.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-use_readme.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
test_that("generate_readme_tmpl works", { res <- generate_readme_tmpl("my_pkg")
2+
expect_true(
3+
grepl("my_pkg", paste(res, collapse = " "))
4+
)
5+
expect_true(
6+
grepl("my_pkg::run_app()", paste(res, collapse = " "))
7+
)
8+
expect_true(
9+
grepl("covr::package_coverage", paste(res, collapse = " "))
10+
)
11+
expect_true(
12+
grepl("unloadNamespace", paste(res, collapse = " "))
13+
)
14+
expect_true(
15+
grepl("devtools::check", paste(res, collapse = " "))
16+
)
17+
})
18+
19+
20+
test_that("check_overwrite works", {
21+
expect_error(
22+
check_overwrite(FALSE, golem_sys("utils/empty_readme.Rmd")),
23+
"README.Rmd already exists. Set `overwrite = TRUE` to overwrite."
24+
)
25+
})
26+
27+
test_that("use_readme_rmd works", {
28+
expect_true(
29+
use_readme_rmd(
30+
open = FALSE,
31+
overwrite = TRUE,
32+
pkg = getwd(),
33+
pkg_name = "rand_name"
34+
)
35+
)
36+
expect_true(
37+
file.exists("README.Rmd")
38+
)
39+
devtools:::build_readme()
40+
})

0 commit comments

Comments
 (0)