Skip to content

Commit

Permalink
Merge pull request #63 from jhelvy/pingfan-ui-design
Browse files Browse the repository at this point in the history
version.R update
pingfan-hu authored Aug 7, 2024
2 parents d4e3b93 + f5f9d93 commit 9a5c91d
Showing 3 changed files with 96 additions and 15 deletions.
78 changes: 66 additions & 12 deletions R/version.R
Original file line number Diff line number Diff line change
@@ -50,33 +50,87 @@ sd_update_surveydown <- function(force = FALSE) {

#' Check Surveydown Versions
#'
#' This function checks the versions of both the surveydown R package and its
#' associated Quarto extension, reporting any mismatches and suggesting an update
#' if necessary.
#' This function checks if the local surveydown R package and Quarto extension
#' are up-to-date with the latest online version.
#'
#' @return No return value, called for side effects (prints version information).
#' @export
#'
#' @examples
#' sd_check_versions()
#' Check Surveydown Versions
#'
#' This function checks if the local surveydown R package and Quarto extension
#' are up-to-date with the latest online version.
#'
#' @return No return value, called for side effects (prints version information).
#' @export
#'
#' @examples
#' sd_check_versions()
sd_check_versions <- function() {
pkg_version <- packageVersion("surveydown")
ext_version <- get_extension_version()
# Get local versions
local_pkg_version <- packageVersion("surveydown")
local_ext_version <- get_extension_version()

message("Surveydown R package version: ", pkg_version)
if (is.null(ext_version)) {
message("Surveydown Quarto extension: Not found")
# Get latest online versions
latest_pkg_version <- get_latest_version_from_url("https://raw.githubusercontent.com/jhelvy/surveydown/main/DESCRIPTION", "Version: ")
latest_ext_version <- get_latest_version_from_url("https://raw.githubusercontent.com/jhelvy/surveydown-ext/main/_extensions/jhelvy/surveydown/_extension.yml", "version: ")

# Display version information
message("surveydown R package (local): ", local_pkg_version)
message("surveydown R package (latest): ",
if(is.null(latest_pkg_version)) "Unable to fetch" else latest_pkg_version)

if (is.null(local_ext_version)) {
message("surveydown Quarto ext (local): Not found")
} else {
message("Surveydown Quarto extension version: ", ext_version)
message("surveydown Quarto ext (local): ", local_ext_version)
}
message("surveydown Quarto ext (latest): ",
if(is.null(latest_ext_version)) "Unable to fetch" else latest_ext_version)

if (is.null(ext_version) || pkg_version != ext_version) {
message("To update both the package and extension to the latest version, run: sd_update_surveydown()")
# Check if updates are needed
if (is.null(latest_pkg_version) || is.null(latest_ext_version)) {
message("\nUnable to determine if updates are available.")
message("Please ensure you have an active internet connection and try again later.")
} else {
message("Versions match and are up to date.")
pkg_needs_update <- local_pkg_version < latest_pkg_version
ext_needs_update <- is.null(local_ext_version) || local_ext_version < latest_ext_version

if (pkg_needs_update || ext_needs_update) {
message("\nUpdates are available. To update both the package and extension to the latest version, run: surveydown::sd_update_surveydown()")
} else {
message("\nBoth the R package and Quarto extension are up to date.")
}
}
}

#' Get Latest Version from URL
#'
#' This function fetches the latest version from a file at a given URL.
#'
#' @param url The URL of the file containing the version information
#' @param pattern The pattern to search for in the file (e.g., "Version: " or "version: ")
#' @return A package_version object representing the latest version, or NULL if unable to fetch
#' @keywords internal
get_latest_version_from_url <- function(url, pattern) {
tryCatch({
content <- readLines(url)
version_line <- grep(pattern, content, value = TRUE)
if (length(version_line) > 0) {
version <- sub(pattern, "", version_line[1])
return(package_version(trimws(version)))
} else {
message("Version information not found in the file at ", url)
return(NULL)
}
}, error = function(e) {
message("Error occurred while fetching version from ", url, ": ", e$message)
return(NULL)
})
}

#' Get Surveydown Extension Version
#'
#' This function reads the version of the surveydown Quarto extension from its
20 changes: 20 additions & 0 deletions man/get_latest_version_from_url.Rd

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

13 changes: 10 additions & 3 deletions man/sd_check_versions.Rd

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

0 comments on commit 9a5c91d

Please sign in to comment.