R/project-maintenance.R

Defines functions onedrive_sanitize

Documented in onedrive_sanitize

#' Sanitize a project folder for compatibility with OneDrive
#'
#' Run this function after creating a new RStudio project if it is stored within
#' a OneDrive folder. This function performs the following steps: (1) copy the
#' selected folders (.Rproj.user and/or .git) onto the local file system outside
#' of OneDrive, (2) write a function to an Rscript file within the project
#' directory, (3) write the Windows cmd text to run that script to the clipboard.
#' At that point, the user will need to close RStudio and paste the text into a
#' Windows cmd prompt. This will run the newly-created script, which will
#' (4) delete the selected files from within the RStudio project directory and
#' replace them with symbolic links to the copies outside of OneDrive.
#'
#'
#' @param what A character vector. Either "rproj", "git", or c("rproj", "git") (the default).
#' @param dest_root The file path to which to move the specified files (should be outside OneDrive).
#'
#' @return Run for its side effects.
#' @export
#'
#' @examples

onedrive_sanitize <- function(what = c("rproj", "git", "rhistory"),
                              dest_root = fs::path("C:", "Users",
                                                   Sys.info()[["user"]],
                                                   "rstudio-projects")) {
  what <- dplyr::recode(what, rproj = ".Rproj.user", git = ".git",
                        rhistory = ".Rhistory")
  proj_root_path <- rprojroot::find_rstudio_root_file()
  paths_proj <- fs::path(proj_root_path, what)
  paths_proj_dir <- paths_proj[fs::is_dir(paths_proj)]
  paths_proj_file <- paths_proj[fs::is_file(paths_proj)]
  # paths_proj_file <- purrr::keep(paths_proj, .p = ~fs::is_file(.))
  paths_external <- fs::path(dest_root, fs::path_file(proj_root_path), what)
  ls_proj <- fs::dir_ls(all = TRUE)
  ls_proj_excl <- ls_proj[!ls_proj %in% what]
  ls_proj_excl_dir <- ls_proj_excl[fs::is_dir(ls_proj_excl)]
  ls_proj_excl_file <- ls_proj_excl[fs::is_file(ls_proj_excl)]
  sys::exec_wait("robocopy", c(fs::path_dir(paths_proj)[[1]],
                               fs::path_dir(paths_external)[[1]], "/E",
                               "/XF", ls_proj_excl_file,
                               "/XD", ls_proj_excl_dir))
  paths_external_dir <- paths_external[fs::is_dir(paths_external)]
  paths_external_file <- paths_external[fs::is_file(paths_external)]
  stopifnot(fs::dir_exists(fs::path_dir(paths_external)), fs::file_exists(paths_external))
  delete_proj_copies <- rlang::expr(fs::file_delete(!!paths_proj))
  link_dirs <- rlang::expr(fs::link_create(path = !!paths_external_dir,
                                           new_path = !!paths_proj_dir))
  link_files <- rlang::expr(fs::link_create(path = !!paths_external_file,
                                            new_path = !!paths_proj_file,
                                            symbolic = FALSE))
  out <- rlang::expr({
    !!delete_proj_copies
    !!link_dirs
    !!link_files
  })
  cmd <- paste0("Rscript ", paste0("-e \"", out[-1], "\"", collapse = " "))
  writeClipboard(gsub("(?<!-e )\"(?! -e|$)", "\'", cmd, perl = TRUE))
  message(paste("Not done yet! Cleanup code copied to clipboard.",
                "\n",
                "1. Close RStudio",
                "2. Open Windows cmd",
                "3. Paste text (right click) to run.", sep = "\n", collapse = "\n"))
}


# onedrive_sanitize <- function(what = c("rproj", "git", "rhistory"),
#                               dest_root = fs::path("C:", "Users",
#                                                    Sys.info()[["user"]],
#                                                    "rstudio-projects")) {
#   what <- dplyr::recode(what, rproj = ".Rproj.user", git = ".git",
#                         rhistory = ".Rhistory")
#   proj_root_path <- rprojroot::find_rstudio_root_file()
#   paths_proj <- fs::path(proj_root_path, what)
#   paths_proj_dir <- purrr::keep(paths_proj, .p = ~fs::is_dir(.))
#   paths_proj_file <- purrr::keep(paths_proj, .p = ~fs::is_file(.))
#   paths_external <- fs::path(dest_root, fs::path_file(proj_root_path), what)
#   purrr::map2(.x = paths_proj, .y = paths_external,
#               ~sys::exec_wait("robocopy", c(fs::path_dir(.x), fs::path_dir(.y), fs::path_file(.x), "/E")))
#   stopifnot(fs::dir_exists(fs::path_dir(paths_external)), fs::file_exists(paths_external))
#   # if (!fs::dir_exists(dir_paths_external) || !fs::file_exists(file_paths_external)) {}
#   delete_proj_dir <- rlang::expr(fs::dir_delete(!!paths_proj_dir))
#   delete_proj_file <- rlang::expr(fs::file_delete(!!paths_proj_file))
#   link <- rlang::expr(fs::link_create(path = !!paths_external,
#                                       new_path = !!paths_proj))
#   out <- rlang::expr({
#     !!delete_proj_dir
#     !!delete_proj_file
#     !!link
#   })
#   cmd <- paste0("Rscript ", paste0("-e \"", out[-1], "\"", collapse = " "))
#   writeClipboard(gsub("(?<!-e )\"(?! -e|$)", "\'", cmd, perl = TRUE))
#   message(paste("Not done yet! Cleanup code copied to clipboard.",
#                 "\n",
#                 "1. Close RStudio",
#                 "2. Open Windows cmd",
#                 "3. Paste text (right click) to run.", sep = "\n", collapse = "\n"))
# }
danielbride/dfunc documentation built on Jan. 1, 2020, 12:54 a.m.