Skip to content

Commit

Permalink
qgis_configure(): optionally detect and propose newer QGIS (win/mac) *
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
florisvdh committed Dec 20, 2023
1 parent a1f4b5b commit 48f8236
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions R/qgis-configure.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Check warning on line 167 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L165-L167

Added lines #L165 - L167 were not covered by tests
)
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'."

Check warning on line 172 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L169-L172

Added lines #L169 - L172 were not covered by tests
)
if (identical(opt, "")) opt <- NA
opt || grepl("TRUE|true", opt)

Check warning on line 175 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L174-L175

Added lines #L174 - L175 were not covered by tests

first_qgis <- qgis_detect_paths()[1]
newer_available <- !is.na(extract_version_from_paths(first_qgis)) &&
!identical(cached_data$path, first_qgis)

Check warning on line 179 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L177-L179

Added lines #L177 - L179 were not covered by tests

if (isTRUE(opt) && isTRUE(newer_available) && interactive()) {
packageStartupMessage()
packageStartupMessage(glue(
"A newer QGIS installation seems to be available: ",
"{extract_version_from_paths(first_qgis)}."

Check warning on line 185 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L181-L185

Added lines #L181 - L185 were not covered by tests
))
answer <- ""
while (!grepl("^[Yy](?:[Ee][Ss])?$|^[Nn](?:[Oo])?$", answer)) {
answer <- readline("Do you want to try it and rebuild the cache? (y/n) ")

Check warning on line 189 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L187-L189

Added lines #L187 - L189 were not covered by tests
}
if (grepl("^[Yy]", answer)) {
newer_ok <- FALSE
tryCatch(

Check warning on line 193 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L191-L193

Added lines #L191 - L193 were not covered by tests
{
qgis_run(path = first_qgis)
newer_ok <- TRUE

Check warning on line 196 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L195-L196

Added lines #L195 - L196 were not covered by tests
},
error = function(e) {
packageStartupMessage(
glue(
"'{first_qgis}' does not work as expected.\n",
"So will not try it further."

Check warning on line 202 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L198-L202

Added lines #L198 - L202 were not covered by tests
)
)
}
)
if (newer_ok) {
packageStartupMessage(
"Will try to reconfigure qgisprocess and build new cache ..."

Check warning on line 209 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L207-L209

Added lines #L207 - L209 were not covered by tests
)
qgis_reconfigure(cache_data_file = cache_data_file, quiet = quiet)
return(invisible(has_qgis()))

Check warning on line 212 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L211-L212

Added lines #L211 - L212 were not covered by tests
}
} 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."

Check warning on line 219 in R/qgis-configure.R

View check run for this annotation

Codecov / codecov/patch

R/qgis-configure.R#L214-L219

Added lines #L214 - L219 were not covered by tests
)
}
}
}

# CACHE CONDITION: the cached QGIS version equals the one reported by
# qgis_process

Expand Down

0 comments on commit 48f8236

Please sign in to comment.