Skip to content

Commit 55dc10a

Browse files
e-kotovCopilot
andauthored
add force argument to java unpack and install (#86)
* add force argument to java unpack and install * Update R/java_install.R Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 45a5cdc commit 55dc10a

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

R/java_install.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#' Install Java from a distribution file
2-
#'
2+
#'
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.
55
#'
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`.
9+
#' @param force A logical. Whether to overwrite an existing installation. Defaults to `FALSE`.
910
#' @inheritParams java_download
1011
#' @inheritParams global_quiet_param
1112
#' @return The path to the installed Java directory.
@@ -24,7 +25,8 @@ java_install <- function(
2425
java_distrib_path,
2526
project_path = NULL,
2627
autoset_java_env = TRUE,
27-
quiet = FALSE
28+
quiet = FALSE,
29+
force = FALSE
2830
) {
2931
rje_consent_check()
3032

@@ -35,7 +37,8 @@ java_install <- function(
3537

3638
installed_path <- java_unpack(
3739
java_distrib_path = java_distrib_path,
38-
quiet = quiet
40+
quiet = quiet,
41+
force = force
3942
)
4043

4144
platforms <- c("windows", "linux", "macos")
@@ -95,19 +98,21 @@ java_install <- function(
9598
silent = TRUE
9699
)
97100
if (!link_success) {
98-
if (!quiet)
101+
if (!quiet) {
99102
cli::cli_inform(
100103
"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}}"
101104
)
105+
}
102106
dir.create(project_version_path, recursive = TRUE)
103107
file.copy(
104108
installed_path,
105109
project_version_path,
106110
recursive = TRUE,
107111
overwrite = TRUE
108112
)
109-
if (!quiet)
113+
if (!quiet) {
110114
cli::cli_inform("Java copied to project {.path {project_version_path}}")
115+
}
111116
}
112117
} else {
113118
tryCatch(
@@ -121,18 +126,21 @@ java_install <- function(
121126
if (!quiet) cli::cli_inform("Warning: {w}")
122127
},
123128
error = function(e) {
124-
if (!quiet) cli::cli_inform("Error: {e}")
129+
if (!quiet) {
130+
cli::cli_inform("Error: {e}")
131+
}
125132
dir.create(project_version_path, recursive = TRUE)
126133
file.copy(
127134
installed_path,
128135
project_version_path,
129136
recursive = TRUE,
130137
overwrite = TRUE
131138
)
132-
if (!quiet)
139+
if (!quiet) {
133140
cli::cli_inform(
134141
"Symlink creation failed. Files copied to {.path {project_version_path}}"
135142
)
143+
}
136144
}
137145
)
138146
}
@@ -147,10 +155,11 @@ java_install <- function(
147155
)
148156
}
149157

150-
if (!quiet)
158+
if (!quiet) {
151159
cli::cli_inform(
152160
"Java {version} ({filename}) for {platform} {arch} installed at {.path {installed_path}} and symlinked to {.path {project_version_path}}",
153161
.envir = environment()
154162
)
163+
}
155164
return(installed_path)
156165
}

R/java_unpack.R

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#'
66
#'
77
#' @inheritParams java_install
8+
#' @param force A logical. Whether to overwrite an existing installation. Defaults to `FALSE`.
89
#' @inheritParams global_quiet_param
910
#' @return A `character` vector containing of length 1 containing the path to the unpacked Java directory.
1011
#' @export
@@ -25,7 +26,8 @@
2526
#'
2627
java_unpack <- function(
2728
java_distrib_path,
28-
quiet = FALSE
29+
quiet = FALSE,
30+
force = FALSE
2931
) {
3032
platforms <- c("windows", "linux", "macos")
3133
architectures <- c("x64", "aarch64", "arm64")
@@ -40,17 +42,29 @@ java_unpack <- function(
4042
arch <- parts[parts %in% architectures][1]
4143
platform <- parts[parts %in% platforms][1]
4244

43-
if (is.na(version))
45+
if (is.na(version)) {
4446
cli::cli_abort("Unable to detect Java version from filename.")
45-
if (is.na(arch))
47+
}
48+
if (is.na(arch)) {
4649
cli::cli_abort("Unable to detect architecture from filename.")
47-
if (is.na(platform))
50+
}
51+
if (is.na(platform)) {
4852
cli::cli_abort("Unable to detect platform from filename.")
53+
}
4954

5055
# Create the installation path in the package cache
5156
cache_path <- getOption("rJavaEnv.cache_path")
5257
installed_path <- file.path(cache_path, "installed", platform, arch, version)
5358

59+
if (dir.exists(installed_path) && force) {
60+
if (!quiet) {
61+
cli::cli_inform(
62+
"Forced re-installation. Removing existing installation at {.path {installed_path}}"
63+
)
64+
}
65+
unlink(installed_path, recursive = TRUE)
66+
}
67+
5468
# Check if the distribution has already been unpacked
5569
if (!dir.exists(installed_path) || length(list.files(installed_path)) == 0) {
5670
# Create the directories if they don't exist
@@ -99,10 +113,11 @@ java_unpack <- function(
99113
# Clean up temporary directory
100114
unlink(temp_dir, recursive = TRUE)
101115
} else {
102-
if (!quiet)
116+
if (!quiet) {
103117
cli::cli_inform(
104118
"Java distribution {filename} already unpacked at {.path {installed_path}}"
105119
)
120+
}
106121
}
107122
return(installed_path)
108123
}

man/java_install.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/java_unpack.Rd

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

0 commit comments

Comments
 (0)