/
repo-helper.R
144 lines (125 loc) · 4.09 KB
/
repo-helper.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#' Create example repo for fledge demos
#'
#' @param open Whether to open the new project.
#' @param name Package name.
#' @param maintainer Name for DESCRIPTION and git.
#' @param email Email for DESCRIPTION and git.
#' @param date String of time for DESCRIPTION and git.
#' @param dir Directory within which to create the mock package folder.
#' @param news If TRUE, create a NEWS.md file.
#'
#' @return The path to the newly created mock package.
#' @export
#'
create_demo_project <- function(open = rlang::is_interactive(),
name = "tea",
maintainer = NULL,
email = NULL,
date = "2021-09-27",
dir = file.path(tempdir(), "fledge"),
news = FALSE) {
if (is.null(maintainer)) {
maintainer <- whoami::fullname(fallback = "Kirill M\u00fcller")
}
if (is.null(email)) {
email <- whoami::email_address(fallback = "mail@example.com")
}
if (!dir.exists(dir)) dir.create(dir, recursive = TRUE)
withr::local_options(usethis.quiet = TRUE)
# https://github.com/r-lib/usethis/issues/1504
if (Sys.getenv("IN_PKGDOWN") != "") {
withr::local_options(cli.num_colors = 1)
}
set_usethis_desc(maintainer = maintainer, email = email, date = date)
pkg <- usethis::create_package(
file.path(dir, name),
fields = list(Date = as.Date(date)),
rstudio = TRUE,
open = open
)
withr::local_dir(new = pkg)
desc::desc_del("LazyData")
gert::git_init()
gert::git_config_set("user.name", maintainer)
gert::git_config_set("user.email", email)
gert::git_add(".")
gert::git_commit(
"First commit",
author = default_gert_author(),
committer = default_gert_committer()
)
current_branch <- gert::git_branch()
if (current_branch != "main") {
gert::git_branch_move(current_branch, "main")
current_branch <- gert::git_branch()
}
# Setting the option as get_main_branch() will rely on it
gert::git_config_set(name = "init.defaultbranch", value = "main")
if (news) {
news_lines <- c(
news_preamble(), "",
sprintf("# %s %s", name, as.character(desc::desc_get_version())), "",
"* Added a `NEWS.md` file to track changes to the package."
)
brio::write_lines(news_lines, news_path())
gert::git_add(news_path())
gert::git_commit(
"Add NEWS.md to track changes.",
author = default_gert_author(),
committer = default_gert_committer()
)
}
return(pkg)
}
set_usethis_desc <- function(maintainer, email, date) {
withr::local_options(
usethis.full_name = maintainer,
usethis.protocol = "ssh",
usethis.description = list(
"Authors@R" = utils::person(
maintainer,
email = email,
role = c("aut", "cre"),
),
Version = "0.0.0.9000",
context = "fledge-example",
RoxygenNote = "42"
),
.local_envir = parent.frame(n = 2)
)
}
#' Run code in temporary project
#'
#' @inheritParams create_demo_project
#' @inheritParams usethis::with_project
#' @param quiet Whether to show messages from usethis
#'
#' @return `with_demo_project()` returns the result of evaluating `code`.
#' @export
#'
#' @example man/examples/with_demo_project.R
with_demo_project <- function(code, dir = NULL, news = TRUE, quiet = FALSE) {
local_demo_project(dir = dir, news = news, quiet = quiet)
force(code)
invisible()
}
#' @return `local_demo_project()` is called for its side effect and returns `NULL`, invisibly.
#' @rdname with_demo_project
#' @export
local_demo_project <- function(dir = NULL,
news = TRUE,
quiet = FALSE,
.local_envir = parent.frame()) {
dir <- dir %||%
withr::local_tempdir(pattern = "fledge", .local_envir = .local_envir)
if (!dir.exists(dir)) {
cli::cli_abort(c(x = "Can't find the directory {.file {dir}}."))
}
repo <- create_demo_project(dir = dir, news = news, open = FALSE)
usethis::local_project(
path = repo,
quiet = quiet,
.local_envir = .local_envir
)
invisible()
}