From a3e14c26824260592cfeb904801fcab9225b8b54 Mon Sep 17 00:00:00 2001 From: Bruno Rodrigues Date: Sun, 16 Jul 2023 14:22:21 +0200 Subject: [PATCH] handle different ides --- R/find_rev.R | 24 ++++++++++++++++++------ dev/build_envs.Rmd | 26 +++++++++++++++++++------- man/rix.Rd | 8 +++++--- vignettes/dev-build_envs.Rmd | 2 +- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/R/find_rev.R b/R/find_rev.R index f55f26d4..1a7c3cf4 100644 --- a/R/find_rev.R +++ b/R/find_rev.R @@ -56,8 +56,10 @@ available_r <- function(){ #' versions are available using `available_r` #' @param pkgs Vector of characters. List the required packages for your #' analysis here. -#' @param rstudio Logical, defaults to FALSE. If TRUE, RStudio gets installed in -#' this environment to enable interactive work. +#' @param ide Character. Defaults to "other". If you wish to use RStudio to work +#' interactively use "rstudio", "code" for Visual Studio Code. For other editors, +#' use "other". This has been tested with RStudio, VS Code and Emacs. If other +#' editors don't work, please open an issue. #' @param path Character. Where to write `default.nix`. #' @details This function will write a `default.nix` in the chosen path. Using #' the Nix package manager, it is then possible to build a reproducible @@ -75,7 +77,18 @@ available_r <- function(){ #' the environment using `nix-build`, you can drop into an interactive session #' using `nix-shell`. See the "How-to Nix" vignette for more details. #' @export -rix <- function(r_ver, pkgs, rstudio = FALSE, path = "."){ +rix <- function(r_ver, pkgs, ide = "other", path = "default.nix"){ + + stopifnot("'ide' has to be one of 'other', 'rstudio' or 'code'" = (ide %in% c("other", "rstudio", "code"))) + + pkgs <- if(ide == "code"){ + c(pkgs, "languageserver") + } else { + pkgs + } + + packages <- paste(pkgs, collapse = ' ') + nixTemplate <- " { pkgs ? import (fetchTarball 'https://github.com/NixOS/nixpkgs/archive/RE_VERSION.tar.gz') {} }: @@ -97,10 +110,9 @@ USE_RSTUDIO}; }" nixFile <- gsub('RE_VERSION', find_rev(r_ver) , nixTemplate) - packages <- paste(pkgs, collapse = ' ') nixFile <- gsub('PACKAGE_LIST', packages, nixFile) - nixFile <- gsub('USE_RSTUDIO', ifelse(rstudio, '', '#'), nixFile) + nixFile <- gsub('USE_RSTUDIO', ifelse(!is.null(ide) & ide == "rstudio", '', '#'), nixFile) - writeLines(nixFile, normalizePath(paste0(path, "/default.nix"))) + writeLines(nixFile, normalizePath(path)) } diff --git a/dev/build_envs.Rmd b/dev/build_envs.Rmd index 3147ecaf..be013337 100644 --- a/dev/build_envs.Rmd +++ b/dev/build_envs.Rmd @@ -83,7 +83,7 @@ This next function returns a `default.nix` file that can be used to build a reproducible environment. This function takes an R version as an input (the correct Nix revision is found using `find_rev()`), a list of R packages, and whether the user wants to work with RStudio or not in that environment. If -you use another IDE, you can leave the "rstudio" argument blank: +you use another IDE, you can leave the "ide" argument blank: ```{r function-rix} #' rix Build a reproducible development environment definition @@ -96,8 +96,10 @@ you use another IDE, you can leave the "rstudio" argument blank: #' versions are available using `available_r` #' @param pkgs Vector of characters. List the required packages for your #' analysis here. -#' @param rstudio Logical, defaults to FALSE. If TRUE, RStudio gets installed in -#' this environment to enable interactive work. +#' @param ide Character. Defaults to "other". If you wish to use RStudio to work +#' interactively use "rstudio", "code" for Visual Studio Code. For other editors, +#' use "other". This has been tested with RStudio, VS Code and Emacs. If other +#' editors don't work, please open an issue. #' @param path Character. Where to write `default.nix`. #' @details This function will write a `default.nix` in the chosen path. Using #' the Nix package manager, it is then possible to build a reproducible @@ -115,7 +117,18 @@ you use another IDE, you can leave the "rstudio" argument blank: #' the environment using `nix-build`, you can drop into an interactive session #' using `nix-shell`. See the "How-to Nix" vignette for more details. #' @export -rix <- function(r_ver, pkgs, rstudio = FALSE, path = "."){ +rix <- function(r_ver, pkgs, ide = "other", path = "default.nix"){ + + stopifnot("'ide' has to be one of 'other', 'rstudio' or 'code'" = (ide %in% c("other", "rstudio", "code"))) + + pkgs <- if(ide == "code"){ + c(pkgs, "languageserver") + } else { + pkgs + } + + packages <- paste(pkgs, collapse = ' ') + nixTemplate <- " { pkgs ? import (fetchTarball 'https://github.com/NixOS/nixpkgs/archive/RE_VERSION.tar.gz') {} }: @@ -137,11 +150,10 @@ USE_RSTUDIO}; }" nixFile <- gsub('RE_VERSION', find_rev(r_ver) , nixTemplate) - packages <- paste(pkgs, collapse = ' ') nixFile <- gsub('PACKAGE_LIST', packages, nixFile) - nixFile <- gsub('USE_RSTUDIO', ifelse(rstudio, '', '#'), nixFile) + nixFile <- gsub('USE_RSTUDIO', ifelse(!is.null(ide) & ide == "rstudio", '', '#'), nixFile) - writeLines(nixFile, normalizePath(paste0(path, "/default.nix"))) + writeLines(nixFile, normalizePath(path)) } ``` diff --git a/man/rix.Rd b/man/rix.Rd index 0da14562..c469056a 100644 --- a/man/rix.Rd +++ b/man/rix.Rd @@ -4,7 +4,7 @@ \alias{rix} \title{rix Build a reproducible development environment definition} \usage{ -rix(r_ver, pkgs, rstudio = FALSE, path = ".") +rix(r_ver, pkgs, ide = "other", path = "default.nix") } \arguments{ \item{r_ver}{Character. The required R version. Leave this argument empty if @@ -14,8 +14,10 @@ versions are available using \code{available_r}} \item{pkgs}{Vector of characters. List the required packages for your analysis here.} -\item{rstudio}{Logical, defaults to FALSE. If TRUE, RStudio gets installed in -this environment to enable interactive work.} +\item{ide}{Character. Defaults to "other". If you wish to use RStudio to work +interactively use "rstudio", "code" for Visual Studio Code. For other editors, +use "other". This has been tested with RStudio, VS Code and Emacs. If other +editors don't work, please open an issue.} \item{path}{Character. Where to write \code{default.nix}.} } diff --git a/vignettes/dev-build_envs.Rmd b/vignettes/dev-build_envs.Rmd index b45980ed..65ff5887 100644 --- a/vignettes/dev-build_envs.Rmd +++ b/vignettes/dev-build_envs.Rmd @@ -35,6 +35,6 @@ This next function returns a `default.nix` file that can be used to build a reproducible environment. This function takes an R version as an input (the correct Nix revision is found using `find_rev()`), a list of R packages, and whether the user wants to work with RStudio or not in that environment. If -you use another IDE, you can leave the "rstudio" argument blank: +you use another IDE, you can leave the "ide" argument blank: