Skip to content

Commit 748736e

Browse files
committed
close #393
1 parent 08bfe62 commit 748736e

9 files changed

+241
-158
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ importFrom(shiny,getShinyOption)
9898
importFrom(shiny,includeScript)
9999
importFrom(testthat,expect)
100100
importFrom(testthat,expect_equal)
101+
importFrom(testthat,expect_true)
101102
importFrom(testthat,quasi_label)
102103
importFrom(testthat,skip_on_appveyor)
103104
importFrom(testthat,skip_on_cran)

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
## Bug fix
2020

21+
+ `add_` functions no longer append to file if it already exists (#393)
22+
2123
+ `config::get()` is no longer exported to prevent clashing with `base::get()`
2224

2325
+ fixed issue with favicon when package is built (#387)

R/add_deploy_helpers.R

+38-28
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,49 @@ add_rstudio_files <- function(
1010
){
1111
service <- match.arg(service)
1212
where <- path(pkg, "app.R")
13-
file_create( where )
1413

15-
write_there <- function(..., here = where){
16-
write(..., here, append = TRUE)
17-
}
1814

19-
use_build_ignore( path_file(where) )
20-
use_build_ignore("rsconnect")
21-
write_there("# Launch the ShinyApp (Do not remove this comment)")
22-
write_there("# To deploy, run: rsconnect::deployApp()")
23-
write_there("# Or use the blue button on top of this file")
24-
write_there("")
25-
write_there("pkgload::load_all(export_all = FALSE,helpers = FALSE,attach_testthat = FALSE)")
26-
write_there("options( \"golem.app.prod\" = TRUE)")
27-
write_there(
28-
sprintf(
29-
"%s::run_app() # add parameters here (if any)",
30-
getOption("golem.app.name", pkg_name())
15+
if (!file_exists(where)){
16+
file_create( where )
17+
18+
write_there <- function(..., here = where){
19+
write(..., here, append = TRUE)
20+
}
21+
22+
use_build_ignore( path_file(where) )
23+
use_build_ignore("rsconnect")
24+
write_there("# Launch the ShinyApp (Do not remove this comment)")
25+
write_there("# To deploy, run: rsconnect::deployApp()")
26+
write_there("# Or use the blue button on top of this file")
27+
write_there("")
28+
write_there("pkgload::load_all(export_all = FALSE,helpers = FALSE,attach_testthat = FALSE)")
29+
write_there("options( \"golem.app.prod\" = TRUE)")
30+
write_there(
31+
sprintf(
32+
"%s::run_app() # add parameters here (if any)",
33+
get_golem_name()
34+
)
3135
)
32-
)
33-
#use_build_ignore(where)
34-
x <- capture.output(use_package("pkgload"))
35-
cat_created(where)
36-
cat_line("To deploy, run:")
37-
cat_bullet(darkgrey("rsconnect::deployApp()\n"))
38-
cat_red_bullet(
39-
sprintf(
40-
"Note that you'll need to upload the whole package to %s",
41-
service
36+
37+
x <- capture.output(use_package("pkgload"))
38+
cat_created(where)
39+
cat_line("To deploy, run:")
40+
cat_bullet(darkgrey("rsconnect::deployApp()\n"))
41+
cat_red_bullet(
42+
sprintf(
43+
"Note that you'll need to upload the whole package to %s",
44+
service
45+
)
4246
)
43-
)
47+
48+
open_or_go_to(where, open)
49+
} else {
50+
file_already_there_dance(
51+
where = where,
52+
open_file = open
53+
)
54+
}
4455

45-
open_or_go_to(where, open)
4656
}
4757

4858
#' Add an app.R at the root of your package to deploy on RStudio Connect

R/add_files.R

+95-67
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,32 @@ add_js_file <- function(
4646
dir, sprintf("%s.js", name)
4747
)
4848

49-
file_create(where)
50-
51-
if (with_doc_ready){
52-
write_there <- function(...){
53-
write(..., file = where, append = TRUE)
54-
}
55-
write_there("$( document ).ready(function() {")
56-
write_there(" ")
57-
write_there("});")
49+
if (!file.exists(where)){
50+
file_create(where)
51+
52+
if (with_doc_ready){
53+
write_there <- function(...){
54+
write(..., file = where, append = TRUE)
55+
}
56+
write_there("$( document ).ready(function() {")
57+
write_there(" ")
58+
write_there("});")
59+
}
60+
file_created_dance(
61+
where,
62+
after_creation_message_js,
63+
pkg,
64+
dir,
65+
name,
66+
open
67+
)
68+
} else {
69+
file_already_there_dance(
70+
where = where,
71+
open_file = open
72+
)
5873
}
5974

60-
file_created_dance(
61-
where,
62-
after_creation_message_js,
63-
pkg,
64-
dir,
65-
name,
66-
open
67-
)
68-
6975
}
7076

7177
#' @export
@@ -105,26 +111,35 @@ add_js_handler <- function(
105111
dir, sprintf("%s.js", name)
106112
)
107113

108-
file_create(where)
109-
110-
write_there <- function(...){
111-
write(..., file = where, append = TRUE)
114+
if (!file.exists(where)){
115+
116+
file_create(where)
117+
118+
write_there <- function(...){
119+
write(..., file = where, append = TRUE)
120+
}
121+
122+
write_there("$( document ).ready(function() {")
123+
write_there(" Shiny.addCustomMessageHandler('fun', function(arg) {")
124+
write_there(" ")
125+
write_there(" })")
126+
write_there("});")
127+
file_created_dance(
128+
where,
129+
after_creation_message_js,
130+
pkg,
131+
dir,
132+
name,
133+
open_file = open
134+
)
135+
} else {
136+
file_already_there_dance(
137+
where,
138+
open_file = open
139+
)
112140
}
113141

114-
write_there("$( document ).ready(function() {")
115-
write_there(" Shiny.addCustomMessageHandler('fun', function(arg) {")
116-
write_there(" ")
117-
write_there(" })")
118-
write_there("});")
119-
120-
file_created_dance(
121-
where,
122-
after_creation_message_js,
123-
pkg,
124-
dir,
125-
name,
126-
open
127-
)
142+
128143

129144
}
130145

@@ -168,15 +183,23 @@ add_css_file <- function(
168183
)
169184
)
170185

171-
file_create(where)
172-
file_created_dance(
173-
where,
174-
after_creation_message_css,
175-
pkg,
176-
dir,
177-
name,
178-
open
179-
)
186+
if (!file_exists(where)){
187+
file_create(where)
188+
file_created_dance(
189+
where,
190+
after_creation_message_css,
191+
pkg,
192+
dir,
193+
name,
194+
open
195+
)
196+
} else {
197+
file_already_there_dance(
198+
where = where,
199+
open_file = open
200+
)
201+
}
202+
180203
}
181204

182205

@@ -209,37 +232,42 @@ add_ui_server_files <- function(
209232
# UI
210233
where <- path( dir, "ui.R")
211234

212-
file_create(where)
213-
214-
write_there <- function(...) write(..., file = where, append = TRUE)
215-
216-
if (is.null(getOption('golem.pkg.name'))){
217-
pkg <- pkgload::pkg_name()
235+
if (!file_exists(where)){
236+
file_create(where)
237+
238+
write_there <- function(...) write(..., file = where, append = TRUE)
239+
240+
pkg <- get_golem_name()
241+
242+
write_there(
243+
sprintf( "%s:::app_ui()", pkg )
244+
)
245+
246+
cat_created(where, "ui file")
218247
} else {
219-
pkg <- getOption('golem.pkg.name')
248+
cat_green_tick("UI file already exists.")
220249
}
221250

222-
write_there(
223-
sprintf( "%s:::app_ui()", pkg )
224-
)
225-
226-
cat_created(where, "ui file")
227-
228251
# server
229252
where <- file.path(
230253
dir, "server.R"
231254
)
232255

233-
file_create(where)
234-
235-
write_there <- function(...) write(..., file = where, append = TRUE)
236256

237-
write_there(
238-
sprintf(
239-
"%s:::app_server",
240-
pkg
257+
if (!file_exists(where)){
258+
file_create(where)
259+
260+
write_there <- function(...) write(..., file = where, append = TRUE)
261+
262+
write_there(
263+
sprintf(
264+
"%s:::app_server",
265+
pkg
266+
)
241267
)
242-
)
243-
cat_created(where, "server file")
268+
cat_created(where, "server file")
269+
} else {
270+
cat_green_tick("server file already exists.")
271+
}
244272

245273
}

0 commit comments

Comments
 (0)