Skip to content

Commit

Permalink
handle different ides
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Rodrigues committed Jul 16, 2023
1 parent 83d6c7d commit a3e14c2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
24 changes: 18 additions & 6 deletions R/find_rev.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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') {} }:
Expand All @@ -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))
}

26 changes: 19 additions & 7 deletions dev/build_envs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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') {} }:
Expand All @@ -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))
}
```
8 changes: 5 additions & 3 deletions man/rix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/dev-build_envs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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:


0 comments on commit a3e14c2

Please sign in to comment.