From a83a440cc1f4aa5b52cfb2d6212d2d9ea2c396f9 Mon Sep 17 00:00:00 2001 From: Danny Colombara Date: Fri, 9 May 2025 17:28:37 -0700 Subject: [PATCH] fucntion to check if latest version installed - gets latest version from main on GitHub - get local version - compares and gives notice if you're behind - passes all tests --- DESCRIPTION | 1 + R/zzz.R | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 R/zzz.R diff --git a/DESCRIPTION b/DESCRIPTION index 05cc95d..556ff91 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,6 +35,7 @@ Imports: Remotes: github::PHSKC-APDE/rads, github::PHSKC-APDE/dtsurvey Suggests: + devtools, httr, keyring, knitr, diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..d591e0b --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,41 @@ +.onAttach <- function(libname, pkgname) { + tryCatch({ + if (!requireNamespace("httr", quietly = TRUE)) { + packageStartupMessage("apde.chi.tools update check skipped: \n'httr' package is needed to check for updates and is not installed.") + return() + } + + url <- "https://raw.githubusercontent.com/PHSKC-APDE/apde.chi.tools/main/DESCRIPTION" + resp <- httr::GET(url) + + if (httr::status_code(resp) == 200) { # 200 means is okay / was successful + desc_text <- httr::content(resp, "text") + + # use Debian Control Format (.dcf) because the DESCRIPTION file in R uses dcf + desc_vals <- tryCatch(read.dcf(textConnection(desc_text)), error = function(e) NULL) + + remote_version <- NULL + if (!is.null(desc_vals) && "Version" %in% colnames(desc_vals)) { + remote_version <- package_version(desc_vals[1, "Version"]) + } + + local_version <- utils::packageVersion(pkgname) + + if (!is.null(remote_version) && remote_version > local_version) { + packageStartupMessage("-----------------------------------------------------") + packageStartupMessage("\u26A0\ufe0f A newer version of apde.chi.tools is available: ", remote_version) + packageStartupMessage("Your version: ", local_version) + update_msg <- ifelse(requireNamespace("devtools", quietly = TRUE), + '\U0001f504 To update, run: devtools::install_github("PHSKC-APDE/apde.chi.tools", auth_token = NULL)', + '\U0001f504 To update, install devtools and then run: devtools::install_github("PHSKC-APDE/apde.chi.tools", auth_token = NULL)' + ) + packageStartupMessage(update_msg) + packageStartupMessage("-----------------------------------------------------") + } + } else { + packageStartupMessage("Could not retrieve version info from GitHub (status: ", httr::status_code(resp), ")") + } + }, error = function(e) { + packageStartupMessage("Version check skipped: could not connect to GitHub") + }) +}