-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinflate_all.R
205 lines (193 loc) · 6.94 KB
/
inflate_all.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# WARNING - Generated by {fusen} from dev/flat_inflate_all.Rmd: do not edit by hand
#' Inflate all your flat files
#'
#' Inflate all the flat files stored in "dev/" and starting with "flat_"
#'
#' @param pkg Path to package
#' @param check_unregistered Logical. Whether to help detect unregistered files.
#' Typically files not created from a flat file and added manually in the repository.
#' @param stylers Function to be run at the end of the process,
#' like `styler::style_pkg` or `lintr::lint_package`
#' or a lambda function combining functions like:
#' `function() {styler::style_pkg(); lintr::lint_package()}`.
#' For a unique function, use the format without parenthesis `()`
#' at the end of the command.
#' @inheritParams inflate
#'
#' @importFrom yaml read_yaml
#' @importFrom cli cat_rule
#' @importFrom devtools check
#'
#' @return side effect. Inflates all your flat files that can be inflated.
#'
#' @details This requires to [inflate()] all flat files
#' individually at least once, so that their specific
#' inflate configurations are stored.
#'
#' This also requires to register all R,
#' tests and vignettes files of your package,
#' even if not created with an inflate.
#' Run [inflate_all()] once and read the messages.
#' The first time, you will probably need to run
#' [register_all_to_config()] if your package is not new.
#'
#' For more information, read the
#' `vignette("inflate-all-your-flat-files", package = "fusen")`
#'
#' @seealso
#' [inflate()] for the options of a single file inflate,
#' [check_not_registered_files()] for the list of files
#' not already associated with a flat file in the config file,
#' [register_all_to_config()] for automatically registering
#' all files already present in the project before the first `inflate_all()`
#'
#' @export
#' @examples
#' \dontrun{
#' # Usually, in the current package run inflate_all() directly
#' # These functions change the current user workspace
#' inflate_all()
#' # Or inflate_all_no_check() to prevent checks to run
#' inflate_all_no_check()
#' # Or inflate with the styler you want
#' inflate_all(stylers = styler::style_pkg)
#' }
#'
#' # You can also inflate_all flats of another package as follows
#' # Example with a dummy package with a flat file
#' dummypackage <- tempfile("inflateall.otherpkg")
#' dir.create(dummypackage)
#' fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package"))
#' flat_files <- add_minimal_package(
#' pkg = dummypackage,
#' overwrite = TRUE,
#' open = FALSE
#' )
#' flat_file <- flat_files[grep("flat", basename(flat_files))]
#' # Inflate the flat file once
#' usethis::with_project(dummypackage, {
#' # if you are starting from a brand new package, inflate_all() will crash
#' # it's because of the absence of a fusen config file
#' #
#' # inflate_all() # will crash
#'
#' # Add licence
#' usethis::use_mit_license("John Doe")
#'
#' # you need to inflate manually your flat file first
#' inflate(
#' pkg = dummypackage,
#' flat_file = flat_file,
#' vignette_name = "Get started",
#' check = FALSE,
#' open_vignette = FALSE,
#' document = TRUE,
#' overwrite = "yes"
#' )
#'
#' # your config file has been created
#' config_yml_ref <-
#' yaml::read_yaml(getOption("fusen.config_file", default = "dev/config_fusen.yaml"))
#' })
#'
#' # Next time, you can run inflate_all() directly
#' usethis::with_project(dummypackage, {
#' # now you can run inflate_all()
#' inflate_all(check = FALSE, document = TRUE)
#' })
#'
#' # Clean the temporary directory
#' unlink(dummypackage, recursive = TRUE)
inflate_all <- function(
pkg = ".",
document = TRUE,
check = TRUE,
open_vignette = FALSE,
overwrite = TRUE,
check_unregistered = TRUE,
stylers, ...) {
config_file <- getOption("fusen.config_file", default = "dev/config_fusen.yaml")
if (!file.exists(config_file)) {
config_yml <- list()
stop_after_infos <- TRUE
} else {
config_yml <- read_yaml(config_file)
stop_after_infos <- FALSE
}
diag <- pre_inflate_all_diagnosis(config_yml = config_yml, pkg = pkg)
# Run stop first only
pre_inflate_all_diagnosis_eval(diag, type_stop = TRUE)
# If message or warnings about flat files, show at the end of the process
# => Is going to be
pre_inflate_all_diagnosis_eval(diag, type_stop = FALSE)
if (stop_after_infos) {
cli::cli_abort(
c(
" {.fn fusen::inflate_all} requires a configuration file to work properly.",
" There is no configuration file at this place in your package: '{config_file}'",
"\nYour active flat files must be individually inflated at least once manually before you can use {.fn inflate_all}.",
" This will create a proper configuration file with a section for each flat file.",
"\nThis error is common if you were using {.pkg fusen} prior to v0.5.1.",
"Read `vignette('{.vignette [inflate-all-your-flat-files](fusen::inflate-all-your-flat-files)}', package = 'fusen')` for more information."
)
)
return(NULL)
}
inflate_params <- read_inflate_params(config_yml = config_yml)
if (length(inflate_params) == 0) {
message("No flat files were inflated")
} else {
apply_inflate <- function(inflate_params, pkg, overwrite, open_vignette) {
config_file <- getOption("fusen.config_file",
default = "dev/config_fusen.yaml"
)
# Change config option temporary, to be able to modify it on the fly
config_file_tmp <- tempfile(pattern = "tempconfig")
on.exit(file.remove(config_file_tmp))
file.copy(config_file, to = config_file_tmp)
options("fusen.config_file" = config_file_tmp)
on.exit(options("fusen.config_file" = config_file))
invisible(
lapply(inflate_params, function(flat_file) {
flat_file$pkg <- pkg
flat_file$overwrite <- overwrite
flat_file$open_vignette <- open_vignette
flat_file$document <- FALSE
flat_file$check <- FALSE
suppressMessages(do.call(inflate, flat_file))
})
)
}
apply_inflate(inflate_params,
pkg = pkg, overwrite = overwrite,
open_vignette = open_vignette
)
}
if (isTRUE(check_unregistered)) {
cli::cat_rule("check not registered files")
invisible(check_not_registered_files(path = pkg))
}
if (!missing(stylers)) {
cli::cat_rule("Let's apply stylers to the package")
if (is.function(stylers)) {
stylers()
} else if (is.character(stylers)) {
eval(parse(text = stylers))
} else {
stylers
}
}
# Document and check package
document_and_check_pkg(
pkg = pkg,
check = check,
document = document,
...
)
invisible(pkg)
}
#' @rdname inflate_all
#' @export
inflate_all_no_check <- function(pkg = ".", document = TRUE, open_vignette = FALSE, overwrite = TRUE, check_unregistered = TRUE, stylers, ...) {
inflate_all(pkg = pkg, document = document, check = FALSE, open_vignette = open_vignette, overwrite = overwrite, check_unregistered = check_unregistered, stylers, ...)
}