Skip to content

Instantly share code, notes, and snippets.

@malcolmbarrett
Last active February 26, 2021 01:33
Show Gist options
  • Save malcolmbarrett/3e4311daa26f327af1d4e5fb0ffb248c to your computer and use it in GitHub Desktop.
Save malcolmbarrett/3e4311daa26f327af1d4e5fb0ffb248c to your computer and use it in GitHub Desktop.
use_custom_build <- function(file) {
stopifnot(file_exists(file))
build_pane_active <- has_buildtype()
bail_out <- check_buildtype()
if (bail_out) {
return(invisible(NULL))
}
file_chmod(file, "+x")
write_buildtype()
if (!build_pane_active) rstudio_tickle_build()
invisible(NULL)
}
has_buildtype <- function() {
any(stringr::str_detect(readLines(rproj_path()), "BuildType"))
}
rstudio_tickle_build <- function() {
restart_rstudio <- ui_yeah("RStudio needs to restart to activate the \\
build pane. Restart now?")
if (restart_rstudio) {
rstudioapi::openProject(proj_get(), newSession = FALSE)
}
}
write_buildtype <- function() {
rproj_text <- readLines(rproj_path())
remove <- stringr::str_detect(
readLines(rproj_path()),
"BuildType|PackageInstallArgs|PackageUseDevtools|CustomScriptPath",
negate = TRUE
)
rproj_text <- c(
rproj_text[remove],
"", "BuildType: Custom", "CustomScriptPath: _make.R", ""
)
ui_done("Writing {ui_path(rproj_path())}")
writeLines(
rproj_text,
rproj_path()
)
}
check_buildtype <- function() {
if (has_buildtype()) {
change_it <- ui_yeah(
"Detected an existing build binding in {ui_path(rproj_path())}. \\
Override existing binding?"
)
if (!change_it) {
ui_info("In RStudio, change your build settings in \\
`Tools > Project Options > Build Tools`.")
return(invisible(TRUE))
}
}
invisible(FALSE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment