Skip to content

Commit 48f8236

Browse files
committed
qgis_configure(): optionally detect and propose newer QGIS (win/mac) *
This only has effect (and happens while loading the package) if the following conditions are all true: - an option qgisprocess.detect_newer_qgis or env var R_QGISPROCESS_DETECT_NEWER_QGIS is set as TRUE - system is Windows or macOS - two or more QGIS installations are present in their own default path _and_ these paths contain the QGIS version number (typical for the standalone installs) - the qgisprocess cache does not point at the newest version of those QGIS paths If so, the user will be offered the possibility to switch to the newer QGIS version. This is a further addition wrt #187.
1 parent a1f4b5b commit 48f8236

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

R/qgis-configure.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,70 @@ qgis_configure <- function(quiet = FALSE, use_cached_data = FALSE) {
158158
return(invisible(has_qgis()))
159159
}
160160

161+
# CACHE CONDITION: the path element does not contradict the
162+
# environment variable/option to automatically switch to a newer
163+
# available QGIS version
164+
if (is_windows() || is_macos()) {
165+
opt <- getOption(
166+
"qgisprocess.detect_newer_qgis",
167+
Sys.getenv("R_QGISPROCESS_DETECT_NEWER_QGIS")
168+
)
169+
assert_that(
170+
assertthat::is.flag(opt) ||
171+
(assertthat::is.string(opt) && opt %in% c("", "TRUE", "FALSE", "true", "false")),
172+
msg = "Option 'qgisprocess.detect_newer_qgis' must be 'TRUE' or 'FALSE'."
173+
)
174+
if (identical(opt, "")) opt <- NA
175+
opt || grepl("TRUE|true", opt)
176+
177+
first_qgis <- qgis_detect_paths()[1]
178+
newer_available <- !is.na(extract_version_from_paths(first_qgis)) &&
179+
!identical(cached_data$path, first_qgis)
180+
181+
if (isTRUE(opt) && isTRUE(newer_available) && interactive()) {
182+
packageStartupMessage()
183+
packageStartupMessage(glue(
184+
"A newer QGIS installation seems to be available: ",
185+
"{extract_version_from_paths(first_qgis)}."
186+
))
187+
answer <- ""
188+
while (!grepl("^[Yy](?:[Ee][Ss])?$|^[Nn](?:[Oo])?$", answer)) {
189+
answer <- readline("Do you want to try it and rebuild the cache? (y/n) ")
190+
}
191+
if (grepl("^[Yy]", answer)) {
192+
newer_ok <- FALSE
193+
tryCatch(
194+
{
195+
qgis_run(path = first_qgis)
196+
newer_ok <- TRUE
197+
},
198+
error = function(e) {
199+
packageStartupMessage(
200+
glue(
201+
"'{first_qgis}' does not work as expected.\n",
202+
"So will not try it further."
203+
)
204+
)
205+
}
206+
)
207+
if (newer_ok) {
208+
packageStartupMessage(
209+
"Will try to reconfigure qgisprocess and build new cache ..."
210+
)
211+
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
212+
return(invisible(has_qgis()))
213+
}
214+
} else if (!quiet) {
215+
packageStartupMessage(
216+
"\nNOTE: if you don't want to autodetect QGIS version updates ",
217+
"in the future, unset the qgisprocess.detect_newer_qgis ",
218+
"option or the R_QGISPROCESS_DETECT_NEWER_QGIS environment ",
219+
"variable."
220+
)
221+
}
222+
}
223+
}
224+
161225
# CACHE CONDITION: the cached QGIS version equals the one reported by
162226
# qgis_process
163227

0 commit comments

Comments
 (0)