R/rstudio-full_setup.R

Defines functions install_cfBashAliases install_cfsnippets install_cfkeybindings update_thunarshortcuts_in_cf install_thunarshortcuts install_cfreservedWords install_vscode cf_install_rstudio printCFShortcuts

Documented in cf_install_rstudio install_cfBashAliases install_cfkeybindings install_cfreservedWords install_cfsnippets install_thunarshortcuts install_vscode printCFShortcuts update_thunarshortcuts_in_cf

#' Title
#'
#' @return
#' @export
#'
#' @examples
printCFShortcuts <- function() {
  fl <- system.file("setup_rstudio/keybindings/addins.json", package = "conveniencefunctions")
  text <- readLines(fl)
  text <- text[-c(1, length(text))]
  text <- gsub('"',"", text)
  text <- gsub(',',"", text)
  text <- gsub('conveniencefunctions::',"", text)
  text <- trimws(text)
  text <- strsplit(text, " : ", T)
  text <- lapply(text, function(x) data.table(Function = x[1], Shortcut = x[2]))
  text <- rbindlist(text)
  cfoutput_MdTable(text)
}



#' Quick setup of RStudio on a new machine
#'
#' 
#' @author Daniel Lill (daniel.lill@intiquan.com)
#' @md
#' @return called for side-effect
#' @export
cf_install_rstudio <- function(
  FLAGtheme = FALSE,
  FLAGoverwrite = TRUE, 
  FLAGThunarShortcuts = FALSE,
  FLAGVScodeSettings = FALSE,
  FLAGRprofileTimezone = FALSE,
  FLAGGitConfig = FALSE,
  FLAGRprofilePrintDt = FALSE
  ) {
  
  # 1. Theme 
  if (FLAGtheme) try(rstudioapi::applyTheme("pastel on dark"))
  # 2. Keybindings
  install_cfkeybindings(FLAGoverwrite = FLAGoverwrite)
  # 3. Snippets
  install_cfsnippets(FLAGoverwrite = FLAGoverwrite)  
  # 4. Bash alias for git
  install_cfBashAliases()
  # 5. Install shortcuts for Thunar
  if (FLAGThunarShortcuts) install_thunarshortcuts(FLAGThunarShortcuts)
  # 8. vscode keybindings and settings
  if (FLAGVScodeSettings) install_vscode()
  # 9. write timezone in Rprofile if no Rprofile exists
  if (FLAGRprofileTimezone && !file.exists("~/.Rprofile")) writeLines('TZ="Europe/Berlin"', "~/.Rprofile")
  # 10. git config
  if (FLAGGitConfig) file.copy(system.file("setup_IQDesktop/Setup/Resources/.gitconfig"), "~/.gitconfig")
  # 11. print.data.table
  if (FLAGRprofilePrintDt) cat("print.data.frame <- data.table:::print.data.table", 
                               "print.tbl_df <- data.table:::print.data.table", 
                               ".d <- data.table::data.table",
                               file = "~/.Rprofile", sep = "\n", append = TRUE)
}





#' Title
#'
#' @return
#' @export
#'
#' @examples
install_vscode <- function() {
  # install keybindings by copying
  keyBindingsFile <- system.file("setup_IQDesktop/Setup/Resources/vscode/keybindings.json", package = "conveniencefunctions")
  file.copy(keyBindingsFile, "~/.config/Code/User/keybindings.json", overwrite = TRUE)
  # Hacky thing, but I feel like I'm just learning to walk in Vscode
  installVSCodeAddinsBindings()
  cat("VSCode keybindings overwritten\n")
  

  # install settings by merging the json
  stop("Implement with JSONlite that no settings are overridden")
  settingsFileCF <- system.file("setup_IQDesktop/Setup/Resources/vscode/settings.json", package = "conveniencefunctions")
  settingsFileVS <- "~/.config/Code/User/settings.json"
  
  CF <- readLines(settingsFileCF)
  VS <- readLines(settingsFileVS)
  VS <- VS[-length(VS)]
  CF <- CF[-1]
  
  # IQDesktop specific - delete autostart of terminal
  VS <- VS[trimws(VS) != trimws('        \"command\": \"workbench.action.terminal.new\",')]
  
  VS <- c(VS, CF)
  
  file.copy(settingsFileVS, paste0(settingsFileVS, ".backup"), overwrite = F)
  writeLines(VS,settingsFileVS)
  cat("VSCode settings appended\n")
}



#' Copy a standard setup_options_IQRtools.R for custom settings
#'
#' @return
#' @export
install_cfreservedWords <- function(){
  file.copy(system.file("setup_IQDesktop/Setup/Resources/setup_options_IQRtools.R"), "~/setup_options_IQRtools.R")
}
#' Install thunar shortcuts
#'
#' @param FLAGoverwrite 
#'
#' @return
#' @export
install_thunarshortcuts <- function(FLAGoverwrite) {
  if (!dir.exists("~/.config/gtk-3.0")) dir.create("~/.config/gtk-3.0")
  wup <- file.copy(system.file("setup_IQDesktop/Setup/Resources/thunar_shortcuts/bookmarks", package = "conveniencefunctions"), 
                   "~/.config/gtk-3.0/bookmarks", overwrite = FLAGoverwrite) 
  if (wup) cat("Explorer shortcuts installed \n")
}


#' Title
#'
#' @return
#' @export
update_thunarshortcuts_in_cf <- function(append=FALSE) {
  thunarfile_new <- "~/.config/gtk-3.0/bookmarks"
  thunarfile_old <- "~/PROJECTS/SHARE/PROJTOOLS/conveniencefunctions/inst/setup_IQDesktop/Setup/Resources/thunar_shortcuts/bookmarks"
  
  if (!file.exists(thunarfile_new)) stop("no new bookmarks found")
  new <- readLines(thunarfile_new)
  
  if (!append){
    writeLines(new, thunarfile_old)
    return("Bookmars replaced")
  }
  
  old <- readLines(thunarfile_old)
  cat(c("", setdiff(new, old), ""), sep = "\n", file = thunarfile_old, append = append)
  return("Bookmarks added")
}



#' Install keybindings
#'
#' @export
#' @importFrom stats setNames
install_cfkeybindings <- function(FLAGoverwrite = FALSE){
  keybindings_paths <- c("~/.R/rstudio/keybindings", "~/.config/rstudio/keybindings")
  for (keybindings_path in keybindings_paths) {
    if (!dir.exists(keybindings_path)) dir.create(keybindings_path, FALSE, TRUE)
    keybindings_files <- list.files(system.file("setup_rstudio/keybindings", package = "conveniencefunctions"), "json$", F, T)
    wup <- vapply(stats::setNames(nm = keybindings_files), file.copy, to = keybindings_path, overwrite = FLAGoverwrite, FUN.VALUE = TRUE)
  }
  if (any(wup)) cat(paste0(names(wup)[wup], collapse = " .... \n"),  "\nkeybindings installed\n")
  NULL
}


#' Title
#'
#' @return
#' @export
install_cfsnippets <- function(FLAGoverwrite = FALSE){
  
  pathsToInstall <- c("~/.config/rstudio/snippets/", "~/.R/snippets/")
  
  snippetsfiles <- c(
    system.file("setup_rstudio/snippets/r.snippets", package = "conveniencefunctions"),
    system.file("setup_rstudio/snippets/r.snippetsProjectShortcuts", package = "conveniencefunctions")
  )
  snippets <- do.call(c, lapply(snippetsfiles, readLines))
  
  for (path in pathsToInstall) {
    if(!dir.exists(path)) dir.create(path, recursive = TRUE)
    wup <- writeLines(snippets, file.path(path, "r.snippets"))
  }
  if (FLAGoverwrite) cat("Snippets were overwritten \n")
  NULL
}



#' Title
#'
#' @return
#' @export
#'
#' @examples
install_cfBashAliases <- function() {
  
  bashrcfile <- "~/.bashrc"
  if (Sys.info()["sysname"] == "Windows") bashrcfile <- "~/../.bashrc"
  bashrcLines <- readLines(bashrcfile)
  newLines <- readLines(system.file("setup_IQDesktop/Setup/Resources/bash/bash_aliases", package = "conveniencefunctions")) 
  
  cfline_start <- grep("# ===== CONVENIENCEFUCNTIONS =======", bashrcLines)
  cfline_end <- grep("# ===== END CONVENIENCEFUCNTIONS =======", bashrcLines)
  if (length(cfline_start)) { # Delete cflines if they are present
    if (!length(cfline_end)) cfline_end <- length(bashrcLines)
    bashrcLines <- bashrcLines[-(cfline_start:cfline_end)]
  }
  file.copy(bashrcfile, paste0(bashrcfile, ".backup"))
  writeLines(c(bashrcLines, newLines), bashrcfile)
  
  
  # Copy project shortcuts
  file.copy(system.file("setup_IQDesktop/Setup/Resources/bash/.bashProjectShortcuts", package = "conveniencefunctions"),"~", overwrite = TRUE)
  system("dos2unix ~/.bashProjectShortcuts")
  
  cat("Bash aliases were updated\n")
}
dlill/conveniencefunctions documentation built on Sept. 30, 2022, 4:40 a.m.