Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(check_version)
export(chi_calc)
export(chi_chars_ccs)
export(chi_chars_injury)
Expand Down
113 changes: 113 additions & 0 deletions R/check_version.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#' Check if the installed apde.chi.tools package version is the latest available
#'
#' This function checks if the installed version of the apde.chi.tools package is the latest
#' available on GitHub. It compares the local version with the version in the
#' DESCRIPTION file on the main branch of the GitHub repository.
#'
#' @param print_message Logical. Whether to print messages about version status.
#' Default is TRUE.
#'
#' @return Invisibly returns a list with the following components:
#' \item{is_current}{Logical. TRUE if the installed version is the latest, FALSE otherwise.}
#' \item{local_version}{The version of the installed apde.chi.tools package.}
#' \item{remote_version}{The version on GitHub, or NULL if it couldn't be determined.}
#' \item{status}{Character string. The status of the check (e.g., "success", "error").}
#' \item{message}{Character string. Detailed message about the check result.}
#'
#' @examples
#' \dontrun{
#' # Check if apde.chi.tools is up to date
#' apde.chi.tools::check_version()
#' }
#'
#' @export
check_version <- function(print_message = TRUE) {

# Initialize return values
result <- list(
is_current = TRUE,
local_version = NULL,
remote_version = NULL,
status = "error",
message = ""
)

tryCatch({
# Check if httr is available
if (!requireNamespace("httr", quietly = TRUE)) {
result$message <- "httr package is needed to check for updates and is not installed."
if (print_message) {
message(result$message)
}
return(invisible(result))
}

# Construct URL to raw DESCRIPTION file
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")

# Parse the DESCRIPTION file
# 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)

if (!is.null(desc_vals) && "Version" %in% colnames(desc_vals)) {
remote_version <- package_version(desc_vals[1, "Version"])
result$remote_version <- remote_version

# Get local version
local_version <- utils::packageVersion("apde.chi.tools")
result$local_version <- local_version

# Compare versions
if (remote_version > local_version) {
result$is_current <- FALSE
update_msg <- ifelse(requireNamespace("devtools", quietly = TRUE),
'To update, run: devtools::install_github("PHSKC-APDE/apde.chi.tools", auth_token = NULL)',
'To update, install devtools and then run: devtools::install_github("PHSKC-APDE/apde.chi.tools", auth_token = NULL)'
)

result$message <- paste0(
"A newer version is available: ", remote_version, "\n",
"Your version: ", local_version, "\n",
update_msg
)

if (print_message) {
message("-----------------------------------------------------")
message("\u26A0\ufe0f A newer version of apde.chi.tools is available: ", remote_version)
message("Your version: ", local_version)
message('\U0001f504 ', update_msg)
message("-----------------------------------------------------")
}
} else {
result$message <- paste0("Package apde.chi.tools is up to date (version ", local_version, ").")
if (print_message) {
message(result$message)
}
}

result$status <- "success"
} else {
result$message <- "Could not parse version information from DESCRIPTION file."
if (print_message) {
message(result$message)
}
}
} else {
result$message <- paste0("Could not retrieve version info from GitHub (status: ", httr::status_code(resp), ")")
if (print_message) {
message(result$message)
}
}
}, error = function(e) {
result$message <- paste0("Version check failed: ", conditionMessage(e))
if (print_message) {
message(result$message)
}
})

invisible(result)
}
42 changes: 3 additions & 39 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
.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")
})
if (interactive()) {
check_version()
}
}
32 changes: 32 additions & 0 deletions man/check_version.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/chi_suppress_results.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.