Skip to content

Commit 0090c46

Browse files
authored
Merge pull request #75 from e-kotov/74-optionsrjavaenvvalid_major_java_versions-is-missing
replace getOption major java ver with java_valid_versions function
2 parents dc8eff7 + fb914b0 commit 0090c46

File tree

4 files changed

+46
-30
lines changed

4 files changed

+46
-30
lines changed

R/java_install.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ java_install <- function(
4040

4141
platforms <- c("windows", "linux", "macos")
4242
architectures <- c("x64", "aarch64", "arm64")
43-
java_versions <- getOption("rJavaEnv.valid_major_java_versions")
43+
java_versions <- java_valid_versions()
4444

4545
# Extract information from the file name
4646
filename <- basename(java_distrib_path)

R/java_unpack.R

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,51 @@
11
#' Unpack a Java distribution file into cache directory
2-
#'
2+
#'
33
#' @description
44
#' Unpack the Java distribution file into cache directory and return the path to the unpacked Java directory with Java binaries.
5-
#'
6-
#'
5+
#'
6+
#'
77
#' @inheritParams java_install
88
#' @inheritParams global_quiet_param
99
#' @return A `character` vector containing of length 1 containing the path to the unpacked Java directory.
1010
#' @export
1111
#' @examples
1212
#' \dontrun{
13-
#'
13+
#'
1414
#' # set cache dir to temporary directory
1515
#' options(rJavaEnv.cache_path = tempdir())
16-
#'
16+
#'
1717
#' # download Java 17 distrib and unpack it into cache dir
1818
#' java_17_distrib <- java_download(version = "17")
1919
#' java_home <- java_unpack(java_distrib_path = java_17_distrib)
20-
#'
20+
#'
2121
#' # set the JAVA_HOME environment variable in the current session
2222
#' # to the cache dir without touching any files in the current project directory
2323
#' java_env_set(where = "session", java_home = java_home)
2424
#' }
25-
#'
25+
#'
2626
java_unpack <- function(
2727
java_distrib_path,
2828
quiet = FALSE
2929
) {
3030
platforms <- c("windows", "linux", "macos")
3131
architectures <- c("x64", "aarch64", "arm64")
32-
java_versions <- getOption("rJavaEnv.valid_major_java_versions")
32+
java_versions <- java_valid_versions()
3333

3434
# Extract information from the file name
3535
filename <- basename(java_distrib_path)
3636
parts <- strsplit(gsub("\\.tar\\.gz|\\.zip", "", filename), "-")[[1]]
3737

3838
# Guess the version, architecture, and platform
39-
version <- parts[parts %in% java_versions][1]
40-
arch <- parts[parts %in% architectures][1]
41-
platform <- parts[parts %in% platforms][1]
39+
version <- parts[parts %in% java_versions][1]
40+
arch <- parts[parts %in% architectures][1]
41+
platform <- parts[parts %in% platforms][1]
4242

43-
if (is.na(version)) cli::cli_abort("Unable to detect Java version from filename.")
44-
if (is.na(arch)) cli::cli_abort("Unable to detect architecture from filename.")
45-
if (is.na(platform)) cli::cli_abort("Unable to detect platform from filename.")
43+
if (is.na(version))
44+
cli::cli_abort("Unable to detect Java version from filename.")
45+
if (is.na(arch))
46+
cli::cli_abort("Unable to detect architecture from filename.")
47+
if (is.na(platform))
48+
cli::cli_abort("Unable to detect platform from filename.")
4649

4750
# Create the installation path in the package cache
4851
cache_path <- getOption("rJavaEnv.cache_path")
@@ -87,12 +90,19 @@ java_unpack <- function(
8790
}
8891

8992
# Move the extracted files to the installation path
90-
file.copy(list.files(extracted_dir, full.names = TRUE), installed_path, recursive = TRUE)
93+
file.copy(
94+
list.files(extracted_dir, full.names = TRUE),
95+
installed_path,
96+
recursive = TRUE
97+
)
9198

9299
# Clean up temporary directory
93100
unlink(temp_dir, recursive = TRUE)
94101
} else {
95-
if (!quiet) cli::cli_inform("Java distribution {filename} already unpacked at {.path {installed_path}}")
102+
if (!quiet)
103+
cli::cli_inform(
104+
"Java distribution {filename} already unpacked at {.path {installed_path}}"
105+
)
96106
}
97107
return(installed_path)
98108
}

R/use_java.R

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
#' Install specified Java version and set the `JAVA_HOME` and `PATH` environment variables in current R session
2-
#'
2+
#'
33
#' @description
44
#' Using specified Java version, set the `JAVA_HOME` and `PATH` environment variables in the current R session. If Java distribtuion has not been downloaded yet, download it. If it was not installed into cache directory yet, install it there and then set the environment variables. This is intended as a quick and easy way to use different Java versions in R scripts that are in the same project, but require different Java versions. For example, one could use this in scripts that are called by `targets` package or `callr` package.
55
#' @inheritParams java_download
66
#' @inheritParams java_install
77
#' @inheritParams global_quiet_param
88
#' @return `NULL`. Prints the message that Java was set in the current R session if `quiet` is set to `FALSE`.
9-
#'
9+
#'
1010
#' @export
11-
#'
11+
#'
1212
#' @examples
1313
#' \dontrun{
14-
#'
14+
#'
1515
#' # set cache directory for Java to be in temporary directory
1616
#' options(rJavaEnv.cache_path = tempdir())
17-
#'
17+
#'
1818
#' # install and set Java 8 in current R session
1919
#' use_java(8)
2020
#' # check Java version
2121
#' "8" == java_check_version_cmd(quiet = TRUE)
2222
#' "8" == java_check_version_rjava(quiet = TRUE)
23-
#'
23+
#'
2424
#' # install and set Java 17 in current R session
2525
#' use_java(17)
2626
#' # check Java version
2727
#' "17" == java_check_version_cmd(quiet = TRUE)
2828
#' "17" == java_check_version_rjava(quiet = TRUE)
29-
#'
29+
#'
3030
#' }
31-
#'
31+
#'
3232
use_java <- function(
3333
version = NULL,
3434
distribution = "Corretto",
3535
cache_path = getOption("rJavaEnv.cache_path"),
3636
platform = platform_detect()$os,
3737
arch = platform_detect()$arch,
3838
quiet = TRUE
39-
){
39+
) {
4040
checkmate::check_vector(version, len = 1)
4141
version <- as.character(version)
42-
checkmate::assert_choice(version, getOption("rJavaEnv.valid_major_java_versions"))
42+
checkmate::assert_choice(version, java_valid_versions())
4343

4444
java_distrib_path <- java_download(
4545
version = version,
@@ -62,7 +62,9 @@ use_java <- function(
6262
)
6363

6464
if (!quiet) {
65-
cli::cli_alert_success("Java version {version} was set in the current R session")
65+
cli::cli_alert_success(
66+
"Java version {version} was set in the current R session"
67+
)
6668
}
6769
invisible()
6870
}

vignettes/multiple-java-with-targets-callr.qmd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ First, load the package and check the valid major versions of `Java`:
2525

2626
```{r}
2727
library(rJavaEnv)
28-
getOption("rJavaEnv.valid_major_java_versions")
28+
java_valid_versions()
2929
```
3030

3131

3232
```
33-
[1] "8" "11" "17" "21" "22"
33+
[1] "8" "11" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24"
3434
```
3535

36+
::: {.callout-note}
37+
The available versions of `Java` depend on your OS and architecture, so you might see a shorter list on your system.
38+
:::
39+
3640
Now select any two or three versions and run `use_java()`, checking every time that correct java was set in the current environment.
3741

3842
```{r}

0 commit comments

Comments
 (0)