Last active
February 26, 2021 01:33
-
-
Save malcolmbarrett/3e4311daa26f327af1d4e5fb0ffb248c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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