-
Notifications
You must be signed in to change notification settings - Fork 286
/
readme.R
106 lines (97 loc) · 3.08 KB
/
readme.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#' Create README files
#'
#' @description
#' Creates skeleton README files with possible stubs for
#' * a high-level description of the project/package and its goals
#' * R code to install from GitHub, if GitHub usage detected
#' * a basic example
#'
#' Use `Rmd` if you want a rich intermingling of code and output. Use `md` for a
#' basic README. `README.Rmd` will be automatically added to `.Rbuildignore`.
#' The resulting README is populated with default YAML frontmatter and R fenced
#' code blocks (`md`) or chunks (`Rmd`).
#'
#' If you use `Rmd`, you'll still need to render it regularly, to keep
#' `README.md` up-to-date. `devtools::build_readme()` is handy for this. You
#' could also use GitHub Actions to re-render `README.Rmd` every time you push.
#' An example workflow can be found in the `examples/` directory here:
#' <https://github.com/r-lib/actions/>.
#'
#' If the current project is a Git repo, then `use_readme_rmd()` automatically
#' configures a pre-commit hook that helps keep `README.Rmd` and `README.md`,
#' synchronized. The hook creates friction if you try to commit when
#' `README.Rmd` has been edited more recently than `README.md`. If this hook
#' causes more problems than it solves for you, it is implemented in
#' `.git/hooks/pre-commit`, which you can modify or even delete.
#'
#' @inheritParams use_template
#' @seealso The [other markdown files
#' section](https://r-pkgs.org/other-markdown.html) of [R
#' Packages](https://r-pkgs.org).
#' @export
#' @examples
#' \dontrun{
#' use_readme_rmd()
#' use_readme_md()
#' }
use_readme_rmd <- function(open = rlang::is_interactive()) {
check_is_project()
check_installed("rmarkdown")
is_pkg <- is_package()
repo_spec <- tryCatch(target_repo_spec(ask = FALSE), error = function(e) NULL)
nm <- if (is_pkg) "Package" else "Project"
data <- list2(
!!nm := project_name(),
Rmd = TRUE,
on_github = !is.null(repo_spec),
github_spec = repo_spec
)
new <- use_template(
if (is_pkg) "package-README" else "project-README",
"README.Rmd",
data = data,
ignore = is_pkg,
open = open
)
if (!new) {
return(invisible(FALSE))
}
if (is_pkg && !data$on_github) {
ui_bullets(c(
"_" = "Update {.path {pth('README.Rmd')}} to include installation instructions."
))
}
if (uses_git()) {
use_git_hook(
"pre-commit",
render_template("readme-rmd-pre-commit.sh")
)
}
invisible(TRUE)
}
#' @export
#' @rdname use_readme_rmd
use_readme_md <- function(open = rlang::is_interactive()) {
check_is_project()
is_pkg <- is_package()
repo_spec <- tryCatch(target_repo_spec(ask = FALSE), error = function(e) NULL)
nm <- if (is_pkg) "Package" else "Project"
data <- list2(
!!nm := project_name(),
Rmd = FALSE,
on_github = !is.null(repo_spec),
github_spec = repo_spec
)
new <- use_template(
if (is_pkg) "package-README" else "project-README",
"README.md",
data = data,
open = open
)
if (is_pkg && !data$on_github) {
ui_bullets(c(
"_" = "Update {.path {pth('README.md')}} to include installation instructions."
))
}
invisible(new)
}