From 48f8236ee15c898fc3898b361646d6d0d956d0b4 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Wed, 20 Dec 2023 11:24:55 +0100 Subject: [PATCH] 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 https://github.com/r-spatial/qgisprocess/issues/187. --- R/qgis-configure.R | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/R/qgis-configure.R b/R/qgis-configure.R index 7bfab41a..cc774125 100644 --- a/R/qgis-configure.R +++ b/R/qgis-configure.R @@ -158,6 +158,70 @@ qgis_configure <- function(quiet = FALSE, use_cached_data = FALSE) { return(invisible(has_qgis())) } + # CACHE CONDITION: the path element does not contradict the + # environment variable/option to automatically switch to a newer + # available QGIS version + if (is_windows() || is_macos()) { + opt <- getOption( + "qgisprocess.detect_newer_qgis", + Sys.getenv("R_QGISPROCESS_DETECT_NEWER_QGIS") + ) + assert_that( + assertthat::is.flag(opt) || + (assertthat::is.string(opt) && opt %in% c("", "TRUE", "FALSE", "true", "false")), + msg = "Option 'qgisprocess.detect_newer_qgis' must be 'TRUE' or 'FALSE'." + ) + if (identical(opt, "")) opt <- NA + opt || grepl("TRUE|true", opt) + + first_qgis <- qgis_detect_paths()[1] + newer_available <- !is.na(extract_version_from_paths(first_qgis)) && + !identical(cached_data$path, first_qgis) + + if (isTRUE(opt) && isTRUE(newer_available) && interactive()) { + packageStartupMessage() + packageStartupMessage(glue( + "A newer QGIS installation seems to be available: ", + "{extract_version_from_paths(first_qgis)}." + )) + answer <- "" + while (!grepl("^[Yy](?:[Ee][Ss])?$|^[Nn](?:[Oo])?$", answer)) { + answer <- readline("Do you want to try it and rebuild the cache? (y/n) ") + } + if (grepl("^[Yy]", answer)) { + newer_ok <- FALSE + tryCatch( + { + qgis_run(path = first_qgis) + newer_ok <- TRUE + }, + error = function(e) { + packageStartupMessage( + glue( + "'{first_qgis}' does not work as expected.\n", + "So will not try it further." + ) + ) + } + ) + if (newer_ok) { + packageStartupMessage( + "Will try to reconfigure qgisprocess and build new cache ..." + ) + qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet) + return(invisible(has_qgis())) + } + } else if (!quiet) { + packageStartupMessage( + "\nNOTE: if you don't want to autodetect QGIS version updates ", + "in the future, unset the qgisprocess.detect_newer_qgis ", + "option or the R_QGISPROCESS_DETECT_NEWER_QGIS environment ", + "variable." + ) + } + } + } + # CACHE CONDITION: the cached QGIS version equals the one reported by # qgis_process