Skip to content

Commit 2ffaa87

Browse files
authored
Merge pull request #68 from e-kotov/67-support-java-24
add dynamic corretto version check, support java 23, 24 and up
2 parents 02ce031 + e8bec4b commit 2ffaa87

17 files changed

+419
-44
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export(java_install)
1010
export(java_list)
1111
export(java_quick_install)
1212
export(java_unpack)
13+
export(java_valid_versions)
1314
export(rje_consent)
1415
export(use_java)
1516
importFrom(utils,getFromNamespace)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## New features
44

5+
- `Java` version (currently still only for `Amazon Corretto`) is now determined dynamically using the official GitHub `json` with releases, so when new `Java` version becomes available, you will not be depenent on `rJavaEnv` to be updated. As a fallback, versions up to 24 are hardcoded.
56
- `force` argument in `java_download()`. When set to `TRUE`, allows to overwrite the distribution file if it already exist in the cache.
67

78
## Improvements

R/internal_utilities.R

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
platform_detect <- function(quiet = TRUE) {
88
sys_info <- tolower(Sys.info())
99

10-
os <- switch(sys_info["sysname"],
10+
os <- switch(
11+
sys_info["sysname"],
1112
"windows" = "windows",
1213
"linux" = "linux",
1314
"darwin" = "macos",
1415
stop(cli::cli_abort("Unsupported platform"))
1516
)
1617

17-
arch <- switch(sys_info["machine"],
18+
arch <- switch(
19+
sys_info["machine"],
1820
"x86-64" = "x64",
1921
"x86_64" = "x64",
2022
"i386" = "x86",
2123
"i686" = "x86",
22-
"aarch64" = "arm64",
23-
"arm64" = "arm64",
24+
"aarch64" = "aarch64",
25+
"arm64" = "aarch64",
2426
stop(cli::cli_abort("Unsupported architecture"))
2527
)
2628

@@ -67,7 +69,10 @@ urls_test_all <- function() {
6769

6870
try(
6971
{
70-
response <- curl::curl_fetch_memory(url, handle = curl::new_handle(nobody = TRUE))
72+
response <- curl::curl_fetch_memory(
73+
url,
74+
handle = curl::new_handle(nobody = TRUE)
75+
)
7176
status <- response$status_code
7277
},
7378
silent = TRUE
@@ -113,9 +118,18 @@ java_version_check_rscript <- function(java_home) {
113118
Sys.setenv(PATH = paste(new_path, old_path, sep = .Platform$path.sep))
114119

115120
suppressWarnings(rJava::.jinit())
116-
suppressWarnings(java_version <- rJava::.jcall("java.lang.System", "S", "getProperty", "java.version"))
121+
suppressWarnings(
122+
java_version <- rJava::.jcall(
123+
"java.lang.System",
124+
"S",
125+
"getProperty",
126+
"java.version"
127+
)
128+
)
117129

118-
message <- cli::format_message("rJava and other rJava/Java-based packages will use Java version: {.val {java_version}}")
130+
message <- cli::format_message(
131+
"rJava and other rJava/Java-based packages will use Java version: {.val {java_version}}"
132+
)
119133

120134
message
121135
},

R/java_download.R

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Download a Java distribution
22
#'
3-
#' @param version `Integer` or `character` vector of length 1 for major version of Java to download or install. If not specified, defaults to the latest LTS version. Can be "8", "11", "17", "21", "22", or 8, 11, 17, 21, or 22.
3+
#' @param version `Integer` or `character` vector of length 1 for major version of Java to download or install. If not specified, defaults to the latest LTS version. Can be "8", "11", "17", "21", "22", "23", "24", or 8, 11, 17, 21, 22, 23, 24 or any newer version if it is available for the selected distribution.
44
#' @param distribution The Java distribution to download. If not specified, defaults to "Amazon Corretto". Currently only \href{https://aws.amazon.com/corretto/}{"Amazon Corretto"} is supported.
55
#' @param cache_path The destination directory to download the Java distribution to. Defaults to a user-specific data directory.
66
#' @param platform The platform for which to download the Java distribution. Defaults to the current platform.
@@ -31,10 +31,8 @@ java_download <- function(
3131
force = FALSE,
3232
temp_dir = FALSE
3333
) {
34-
3534
# Download distribution and check MD5 checksum
3635
download_dist_check_md5 <- function(url, dest_file, quiet) {
37-
3836
curl::curl_download(url, dest_file, quiet = FALSE)
3937
curl::curl_download(url_md5, dest_file_md5, quiet = TRUE)
4038

@@ -45,7 +43,10 @@ java_download <- function(
4543
md5sum_expected <- readLines(dest_file_md5, warn = FALSE)
4644

4745
if (md5sum != md5sum_expected) {
48-
cli::cli_abort("MD5 checksum mismatch. Please try downloading the file again.", .envir = environment())
46+
cli::cli_abort(
47+
"MD5 checksum mismatch. Please try downloading the file again.",
48+
.envir = environment()
49+
)
4950
unlink(dest_file)
5051
return(NULL)
5152
} else {
@@ -75,7 +76,10 @@ java_download <- function(
7576
# Checks for the parameters
7677
checkmate::check_vector(version, len = 1)
7778
version <- as.character(version)
78-
checkmate::assert_choice(version, getOption("rJavaEnv.valid_major_java_versions"))
79+
checkmate::assert_choice(
80+
as.character(version),
81+
java_valid_versions()
82+
)
7983

8084
checkmate::assert_choice(distribution, valid_distributions)
8185

@@ -101,15 +105,24 @@ java_download <- function(
101105
}
102106

103107
if (!distribution %in% names(java_urls)) {
104-
cli::cli_abort("Unsupported distribution: {.val {distribution}}", .envir = environment())
108+
cli::cli_abort(
109+
"Unsupported distribution: {.val {distribution}}",
110+
.envir = environment()
111+
)
105112
}
106113

107114
if (!platform %in% names(java_urls[[distribution]])) {
108-
cli::cli_abort("Unsupported platform: {.val {platform}}", .envir = environment())
115+
cli::cli_abort(
116+
"Unsupported platform: {.val {platform}}",
117+
.envir = environment()
118+
)
109119
}
110120

111121
if (!arch %in% names(java_urls[[distribution]][[platform]])) {
112-
cli::cli_abort("Unsupported architecture: {.val {arch}}", .envir = environment())
122+
cli::cli_abort(
123+
"Unsupported architecture: {.val {arch}}",
124+
.envir = environment()
125+
)
113126
}
114127

115128
url_template <- java_urls[[distribution]][[platform]][[arch]]
@@ -120,17 +133,23 @@ java_download <- function(
120133
dest_file_md5 <- paste0(file.path(cache_path, basename(url_md5)), ".md5")
121134

122135
if (!quiet) {
123-
cli::cli_inform("Downloading Java {version} ({distribution}) for {platform} {arch} to {dest_file}", .envir = environment())
136+
cli::cli_inform(
137+
"Downloading Java {version} ({distribution}) for {platform} {arch} to {dest_file}",
138+
.envir = environment()
139+
)
124140
}
125141

126142
if (file.exists(dest_file) & !force) {
127143
if (!quiet) {
128-
cli::cli_inform("File already exists. Skipping download.", .envir = environment())
144+
cli::cli_inform(
145+
"File already exists. Skipping download.",
146+
.envir = environment()
147+
)
129148
}
130-
} else if(file.exists(dest_file) & force) {
149+
} else if (file.exists(dest_file) & force) {
131150
if (!quiet) {
132151
cli::cli_inform("Removing existing installation.", .envir = environment())
133-
}
152+
}
134153
file.remove(dest_file)
135154
download_dist_check_md5(url, dest_file, quiet)
136155
} else if (!file.exists(dest_file)) {

R/java_install.R

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' @description
44
#' Unpack Java distribution file into cache directory and link the installation into a project directory, optionally setting the `JAVA_HOME` and `PATH` environment variables to the Java version that was just installed.
5-
#'
5+
#'
66
#' @param java_distrib_path A `character` vector of length 1 containing the path to the Java distribution file.
77
#' @param project_path A `character` vector of length 1 containing the project directory where Java should be installed. If not specified or `NULL`, defaults to the current working directory.
88
#' @param autoset_java_env A `logical` indicating whether to set the `JAVA_HOME` and `PATH` environment variables to the installed Java directory. Defaults to `TRUE`.
@@ -13,7 +13,7 @@
1313
#'
1414
#' @examples
1515
#' \dontrun{
16-
#'
16+
#'
1717
#' # set cache dir to temporary directory
1818
#' options(rJavaEnv.cache_path = tempdir())
1919
#' # download, install and autoset environmnet variables for Java 17
@@ -47,12 +47,20 @@ java_install <- function(
4747
parts <- strsplit(gsub("\\.tar\\.gz|\\.zip", "", filename), "-")[[1]]
4848

4949
# Guess the version, architecture, and platform
50-
version <- parts[vapply(parts, function(x) x %in% java_versions, logical(1))][1]
50+
version <- parts[vapply(parts, function(x) x %in% java_versions, logical(1))][
51+
1
52+
]
5153
arch <- parts[vapply(parts, function(x) x %in% architectures, logical(1))][1]
5254
platform <- parts[vapply(parts, function(x) x %in% platforms, logical(1))][1]
5355

5456
# Create a symlink in the project directory
55-
project_version_path <- file.path(project_path, "rjavaenv", platform, arch, version)
57+
project_version_path <- file.path(
58+
project_path,
59+
"rjavaenv",
60+
platform,
61+
arch,
62+
version
63+
)
5664
if (!dir.exists(dirname(project_version_path))) {
5765
dir.create(dirname(project_version_path), recursive = TRUE)
5866
}
@@ -61,10 +69,14 @@ java_install <- function(
6169
if (.Platform$OS.type == "windows") {
6270
try(
6371
{
64-
if( file.exists(project_version_path) ){
72+
if (file.exists(project_version_path)) {
6573
unlink(project_version_path, recursive = TRUE)
6674
}
67-
cmd <- sprintf("mklink /J \"%s\" \"%s\"", gsub("/", "\\\\", project_version_path), gsub("/", "\\\\", installed_path))
75+
cmd <- sprintf(
76+
"mklink /J \"%s\" \"%s\"",
77+
gsub("/", "\\\\", project_version_path),
78+
gsub("/", "\\\\", installed_path)
79+
)
6880
result <- tryCatch(
6981
system2("cmd.exe", args = c("/c", cmd), stdout = TRUE, stderr = TRUE),
7082
warning = function(w) {
@@ -83,15 +95,24 @@ java_install <- function(
8395
silent = TRUE
8496
)
8597
if (!link_success) {
86-
if (!quiet) cli::cli_inform("Junction creation failed. This is likely because the project directory is not on the same disk as the R package cache directory. Java files will instead be copied to {.path {project_version_path}}")
98+
if (!quiet)
99+
cli::cli_inform(
100+
"Junction creation failed. This is likely because the project directory is not on the same disk as the R package cache directory. Java files will instead be copied to {.path {project_version_path}}"
101+
)
87102
dir.create(project_version_path, recursive = TRUE)
88-
file.copy(installed_path, project_version_path, recursive = TRUE, overwrite = TRUE)
89-
if (!quiet) cli::cli_inform("Java copied to project {.path {project_version_path}}")
103+
file.copy(
104+
installed_path,
105+
project_version_path,
106+
recursive = TRUE,
107+
overwrite = TRUE
108+
)
109+
if (!quiet)
110+
cli::cli_inform("Java copied to project {.path {project_version_path}}")
90111
}
91112
} else {
92113
tryCatch(
93114
{
94-
if( file.exists(project_version_path) ){
115+
if (file.exists(project_version_path)) {
95116
unlink(project_version_path, recursive = TRUE)
96117
}
97118
file.symlink(installed_path, project_version_path)
@@ -102,19 +123,34 @@ java_install <- function(
102123
error = function(e) {
103124
if (!quiet) cli::cli_inform("Error: {e}")
104125
dir.create(project_version_path, recursive = TRUE)
105-
file.copy(installed_path, project_version_path, recursive = TRUE, overwrite = TRUE)
106-
if (!quiet) cli::cli_inform("Symlink creation failed. Files copied to {.path {project_version_path}}")
126+
file.copy(
127+
installed_path,
128+
project_version_path,
129+
recursive = TRUE,
130+
overwrite = TRUE
131+
)
132+
if (!quiet)
133+
cli::cli_inform(
134+
"Symlink creation failed. Files copied to {.path {project_version_path}}"
135+
)
107136
}
108137
)
109138
}
110139

111-
112-
113140
# Write the JAVA_HOME to the .Rprofile and environment after installation
114141
if (autoset_java_env) {
115-
java_env_set(installed_path, where = "both", quiet = quiet, project_path = project_path)
142+
java_env_set(
143+
installed_path,
144+
where = "both",
145+
quiet = quiet,
146+
project_path = project_path
147+
)
116148
}
117149

118-
if (!quiet) cli::cli_inform("Java {version} ({filename}) for {platform} {arch} installed at {.path {installed_path}} and symlinked to {.path {project_version_path}}", .envir = environment())
150+
if (!quiet)
151+
cli::cli_inform(
152+
"Java {version} ({filename}) for {platform} {arch} installed at {.path {installed_path}} and symlinked to {.path {project_version_path}}",
153+
.envir = environment()
154+
)
119155
return(installed_path)
120156
}

0 commit comments

Comments
 (0)