Skip to content

Commit 314aea9

Browse files
Add force argument to java_download() to allow overwritting of downloaded file
1 parent 1de37c0 commit 314aea9

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

R/java_download.R

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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.
77
#' @param arch The architecture for which to download the Java distribution. Defaults to the current architecture.
8+
#' @param force A logical. Whether the distribution file should be overwritten or not. Defaults to `FALSE`.
89
#' @param temp_dir A logical. Whether the file should be saved in a temporary directory. Defaults to `FALSE`.
910
#' @inheritParams global_quiet_param
1011
#'
@@ -13,10 +14,10 @@
1314
#'
1415
#' @examples
1516
#' \dontrun{
16-
#'
17+
#'
1718
#' # download distribution of Java version 17
1819
#' java_download(version = "17", temp_dir = TRUE)
19-
#'
20+
#'
2021
#' # download default Java distribution (version 21)
2122
#' java_download(temp_dir = TRUE)
2223
#' }
@@ -27,9 +28,10 @@ java_download <- function(
2728
platform = platform_detect()$os,
2829
arch = platform_detect()$arch,
2930
quiet = FALSE,
31+
force = FALSE,
3032
temp_dir = FALSE
3133
) {
32-
34+
3335
# override cache_path if temp_dir is set to TRUE
3436
if (temp_dir) {
3537
temp_dir <- tempdir()
@@ -98,11 +100,32 @@ java_download <- function(
98100
cli::cli_inform("Downloading Java {version} ({distribution}) for {platform} {arch} to {dest_file}", .envir = environment())
99101
}
100102

101-
if (file.exists(dest_file)) {
103+
if (file.exists(dest_file) & !force) {
102104
if (!quiet) {
103105
cli::cli_inform("File already exists. Skipping download.", .envir = environment())
104106
}
105-
} else {
107+
} else if(file.exists(dest_file) & force){
108+
if (!quiet) {
109+
cli::cli_inform("Removing existing installation.", .envir = environment())
110+
}
111+
file.remove(dest_file)
112+
curl::curl_download(url, dest_file, quiet = FALSE)
113+
curl::curl_download(url_md5, dest_file_md5, quiet = TRUE)
114+
if (!quiet) {
115+
cli::cli_inform("Download completed.", .envir = environment())
116+
117+
md5sum <- tools::md5sum(dest_file)
118+
md5sum_expected <- readLines(dest_file_md5, warn = FALSE)
119+
120+
if (md5sum != md5sum_expected) {
121+
cli::cli_alert_danger("MD5 checksum mismatch. Please try downloading the file again.", .envir = environment())
122+
unlink(dest_file)
123+
return(NULL)
124+
} else {
125+
cli::cli_inform("MD5 checksum verified.", .envir = environment())
126+
}
127+
}
128+
} else if (!file.exists(dest_file)){
106129
curl::curl_download(url, dest_file, quiet = FALSE)
107130
curl::curl_download(url_md5, dest_file_md5, quiet = TRUE)
108131
if (!quiet) {

man/java_download.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)