22
33# ' @rdname add_flat_template
44# ' @export
5- add_additional <- function (pkg = " ." ,
6- dev_dir = " dev" ,
7- flat_name = " additional" ,
8- overwrite = FALSE ,
9- open = TRUE ) {
5+ add_additional <- function (
6+ pkg = " ." ,
7+ dev_dir = " dev" ,
8+ flat_name = " additional" ,
9+ overwrite = FALSE ,
10+ open = TRUE ) {
1011 add_flat_template(
1112 template = " additional" ,
12- pkg = pkg ,
13+ pkg = pkg ,
1314 dev_dir = dev_dir ,
1415 flat_name = flat_name ,
1516 overwrite = overwrite ,
16- open = open
17- )
17+ open = open )
1818}
1919
2020# ' @rdname add_flat_template
2121# ' @export
22- add_minimal <- function (pkg = " ." ,
23- dev_dir = " dev" ,
24- flat_name = " minimal" ,
25- overwrite = FALSE ,
26- open = TRUE ) {
22+ add_minimal <- function (
23+ pkg = " ." ,
24+ dev_dir = " dev" ,
25+ flat_name = " minimal" ,
26+ overwrite = FALSE ,
27+ open = TRUE ) {
2728 add_flat_template(
2829 template = " minimal" ,
29- pkg = pkg ,
30+ pkg = pkg ,
3031 dev_dir = dev_dir ,
3132 flat_name = flat_name ,
3233 overwrite = overwrite ,
33- open = open
34- )
34+ open = open )
3535}
3636
3737# ' @rdname add_flat_template
3838# ' @export
39- add_full <- function (pkg = " ." ,
40- dev_dir = " dev" ,
41- flat_name = " full" ,
42- overwrite = FALSE ,
43- open = TRUE ) {
39+ add_full <- function (
40+ pkg = " ." ,
41+ dev_dir = " dev" ,
42+ flat_name = " full" ,
43+ overwrite = FALSE ,
44+ open = TRUE ) {
4445 add_flat_template(
4546 template = " full" ,
46- pkg = pkg ,
47+ pkg = pkg ,
4748 dev_dir = dev_dir ,
4849 flat_name = flat_name ,
4950 overwrite = overwrite ,
50- open = open
51- )
51+ open = open )
5252}
5353
5454# ' Add flat Rmd file that drives package development
@@ -59,7 +59,7 @@ add_full <- function(pkg = ".",
5959# ' @param open Logical. Whether to open file after creation
6060# ' @param dev_dir Name of directory for development Rmarkdown files. Default to "dev".
6161# ' @param flat_name Name of the file to write in dev.
62- # ' Use the name of the main function of your template to get chunks pre-filled with this function name.
62+ # ' Use the name of the main function of your template to get chunks pre-filled with this function name.
6363# '
6464# ' @importFrom tools file_path_sans_ext
6565# ' @details
@@ -76,7 +76,7 @@ add_full <- function(pkg = ".",
7676# '
7777# ' Abbreviated names can also be used for the different templates:
7878# ' "add" for additional, "min" for minimal, "teach" for teaching, "dev" for "dev_history".
79- # '
79+ # '
8080# ' `add_additional()`, `add_minimal()`, `add_full()` are wrapper around `add_flat_template("additional")`, ...
8181# ' However, `add_dev_history()` is a deprecated function from a previous version.
8282# '
@@ -89,67 +89,62 @@ add_full <- function(pkg = ".",
8989# ' # Create a new project
9090# ' dummypackage <- tempfile("dummypackage")
9191# ' dir.create(dummypackage)
92- # '
92+ # '
9393# ' # Add
9494# ' add_flat_template(template = "teaching", pkg = dummypackage)
9595# ' # Delete dummy package
9696# ' unlink(dummypackage, recursive = TRUE)
97- # '
97+ # '
9898# ' # For classical use in your package
9999# ' \dontrun{
100100# ' # first time ever using 'fusen'
101- # ' add_flat_template("full")
102- # '
101+ # ' add_flat_template("full")
102+ # '
103103# ' # first time in your new package
104104# ' add_flat_template("minimal")
105- # '
105+ # '
106106# ' # add new flat file for new functions
107107# ' add_flat_template("add")
108- # '
108+ # '
109109# ' # add new flat template for teaching (a reduced full template)
110110# ' add_flat_template("teaching")
111- # ' #'
112- # ' }
113- add_flat_template <- function (template = c(" full" , " minimal" , " additional" , " teaching" , " dev_history" ),
114- pkg = " ." ,
115- dev_dir = " dev" ,
116- flat_name = template ,
117- overwrite = FALSE ,
118- open = TRUE ) {
111+ # ' #'}
112+ add_flat_template <- function (
113+ template = c(" full" , " minimal" , " additional" , " teaching" , " dev_history" ),
114+ pkg = " ." ,
115+ dev_dir = " dev" ,
116+ flat_name = template ,
117+ overwrite = FALSE ,
118+ open = TRUE ) {
119+
119120 project_name <- get_pkg_name(pkg = pkg )
120-
121+
121122 if (project_name != asciify_name(project_name , to_pkg = TRUE )) {
122- stop(
123- " Please rename your project/directory with: `" , asciify_name(project_name , to_pkg = TRUE ),
124- " ` as a package name should only contain letters, numbers and dots."
125- )
123+ stop(" Please rename your project/directory with: `" , asciify_name(project_name , to_pkg = TRUE ),
124+ " ` as a package name should only contain letters, numbers and dots." )
126125 }
127-
126+
128127 template <- match.arg(template )
129- if (! template %in% c(" full" , " teaching" , " dev_history" ) &
130- ! flat_name %in% c(" minimal" , " additional" )) {
128+ if (! template %in% c(" full" , " teaching" , " dev_history" )
129+ & ! flat_name %in% c(" minimal" , " additional" )) {
131130 fun_name <- gsub(" -" , " _" , asciify_name(flat_name ))
132131 } else {
133132 fun_name <- NA
134133 }
135- flat_name <- paste0(
136- " flat_" ,
137- asciify_name(gsub(" [.]Rmd$" , " " , flat_name [1 ])), " .Rmd"
138- )
139-
134+ flat_name <- paste0(" flat_" ,
135+ asciify_name(gsub(" [.]Rmd$" , " " , flat_name [1 ])), " .Rmd" )
136+
140137 pkg <- normalizePath(pkg )
141138 full_dev_dir <- file.path(pkg , dev_dir )
142- if (! dir.exists(full_dev_dir )) {
143- dir.create(full_dev_dir )
144- }
145- dev_file_path <- file.path(full_dev_dir , flat_name ) # "dev_history.Rmd")
146-
139+ if (! dir.exists(full_dev_dir )) {dir.create(full_dev_dir )}
140+ dev_file_path <- file.path(full_dev_dir , flat_name ) # "dev_history.Rmd")
141+
147142 # Which template ----
148143 if (template == " dev_history" ) {
149144 dev_file_path <- character (0 )
150145 } else {
151146 template_file <- system.file(paste0(" flat-template-" , template , " .Rmd" ), package = " fusen" )
152-
147+
153148 if (file.exists(dev_file_path ) & overwrite == FALSE ) {
154149 n <- length(list.files(full_dev_dir , pattern = " ^flat_.*[.]Rmd" ))
155150 dev_file_path <- file.path(full_dev_dir , paste0(file_path_sans_ext(flat_name ), " _" , n + 1 , " .Rmd" ))
@@ -160,50 +155,40 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
160155 )
161156 }
162157 dev_name <- basename(dev_file_path )
163-
158+
164159 # Change lines asking for pkg name
165160 lines_template <- readLines(template_file )
166-
161+
167162 lines_template [grepl(" <my_package_name>" , lines_template )] <-
168- gsub(
169- " <my_package_name>" , project_name ,
170- lines_template [grepl(" <my_package_name>" , lines_template )]
171- )
172-
163+ gsub(" <my_package_name>" , project_name ,
164+ lines_template [grepl(" <my_package_name>" , lines_template )])
165+
173166 # Change flat_template file name
174167 # _inflate
175168 lines_template [grepl(" dev/flat_template.Rmd" , lines_template )] <-
176- gsub(
177- " dev/flat_template.Rmd" , file.path(dev_dir , dev_name ),
178- lines_template [grepl(" dev/flat_template.Rmd" , lines_template )]
179- )
169+ gsub(" dev/flat_template.Rmd" , file.path(dev_dir , dev_name ),
170+ lines_template [grepl(" dev/flat_template.Rmd" , lines_template )])
180171 # _title
181172 lines_template [grepl(" flat_template.Rmd" , lines_template )] <-
182- gsub(
183- " flat_template.Rmd" , dev_name ,
184- lines_template [grepl(" flat_template.Rmd" , lines_template )]
185- )
186-
173+ gsub(" flat_template.Rmd" , dev_name ,
174+ lines_template [grepl(" flat_template.Rmd" , lines_template )])
175+
187176 # Change my_fun to fun_name
188177 if (! is.na(fun_name )) {
189178 lines_template [grepl(" my_fun" , lines_template )] <-
190- gsub(
191- " my_fun" , fun_name ,
192- lines_template [grepl(" my_fun" , lines_template )]
193- )
179+ gsub(" my_fun" , fun_name ,
180+ lines_template [grepl(" my_fun" , lines_template )])
194181 }
195-
182+
196183 cat(enc2utf8(lines_template ), file = dev_file_path , sep = " \n " )
197184 }
198-
185+
199186 # Add the-dev-history when needed ----
200187 if (template %in% c(" full" , " minimal" , " dev_history" )) {
201188 dev_file <- file.path(full_dev_dir , " 0-dev_history.Rmd" )
202189 if (file.exists(dev_file ) & ! isTRUE(overwrite )) {
203- message(
204- " '0-dev_history.Rmd' already exists. It was not overwritten. " ,
205- " Set `add_flat_template(overwrite = TRUE)` if you want to do so."
206- )
190+ message(" '0-dev_history.Rmd' already exists. It was not overwritten. " ,
191+ " Set `add_flat_template(overwrite = TRUE)` if you want to do so." )
207192 } else {
208193 copy <- file.copy(
209194 system.file(" the-dev-history.Rmd" , package = " fusen" ),
@@ -215,8 +200,9 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
215200 }
216201 dev_file_path <- c(dev_file_path , dev_file )
217202 }
203+
218204 }
219-
205+
220206 # Add data for the full template exemple
221207 if (template %in% c(" full" )) {
222208 inst_dir <- file.path(pkg , " inst" )
@@ -227,15 +213,15 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
227213 # Example dataset
228214 file.copy(system.file(" nyc_squirrels_sample.csv" , package = " fusen" ), inst_dir )
229215 }
230-
216+
231217 # .Rbuildignore ----
232218 # usethis::use_build_ignore(dev_dir) # Cannot be used outside project
233219 if (length(list.files(pkg , pattern = " [.]Rproj" )) == 0 ) {
234220 lines <- c(paste0(" ^" , dev_dir , " $" ), " ^\\ .here$" )
235221 } else {
236222 lines <- c(paste0(" ^" , dev_dir , " $" ))
237223 }
238-
224+
239225 buildfile <- normalizePath(file.path(pkg , " .Rbuildignore" ), mustWork = FALSE )
240226 if (! file.exists(buildfile )) {
241227 existing_lines <- " "
@@ -247,11 +233,11 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
247233 all <- c(existing_lines , new )
248234 cat(enc2utf8(all ), file = buildfile , sep = " \n " )
249235 }
250-
236+
251237 # Add a gitignore file in dev_dir ----
252238 # Files to ignore
253- lines <- c(" *.html" )
254-
239+ lines <- c(" *.html" , " *.R " )
240+
255241 gitfile <- normalizePath(file.path(full_dev_dir , " .gitignore" ), mustWork = FALSE )
256242 if (! file.exists(gitfile )) {
257243 existing_lines <- " "
@@ -263,13 +249,11 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
263249 all <- c(existing_lines , new )
264250 cat(enc2utf8(all ), file = gitfile , sep = " \n " )
265251 }
266-
252+
267253 if (length(list.files(pkg , pattern = " [.]Rproj" )) == 0 ) {
268254 here :: set_here(pkg )
269255 }
270- if (isTRUE(open ) & interactive()) {
271- usethis :: edit_file(dev_file_path )
272- }
273-
256+ if (isTRUE(open ) & interactive()) {usethis :: edit_file(dev_file_path )}
257+
274258 dev_file_path
275259}
0 commit comments