-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
193 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |