Skip to content

Commit f7076aa

Browse files
authored
Merge pull request #58 from nationalparkservice/sarah-dev
Check for out of date github packages
2 parents 9f6292a + ce50a75 commit f7076aa

9 files changed

Lines changed: 143 additions & 110 deletions

File tree

DESCRIPTION

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ Roxygen: list(markdown = TRUE)
1919
RoxygenNote: 7.3.2
2020
Imports:
2121
cli,
22-
crayon,
23-
DPchecker (>= 0.0.0.9000),
22+
DPchecker (>= 1.0.0),
2423
EML,
25-
EMLassemblyline,
26-
EMLeditor (>= 0.0.1.0),
27-
NPSutils (>= 0.1.0),
28-
QCkit (>= 0.1.0),
29-
rstudioapi,
24+
EMLassemblyline (>= 3.5.7),
25+
EMLeditor (>= 1.0.0),
26+
NPSutils (>= 1.0.0),
27+
QCkit (>= 1.0.0),
3028
utils,
31-
remotes,
32-
lifecycle
29+
lifecycle,
30+
gh,
31+
readr,
32+
stringr,
33+
tibble
3334
Remotes:
3435
nationalparkservice/DPchecker,
3536
nationalparkservice/EMLeditor,
3637
nationalparkservice/NPSutils,
3738
nationalparkservice/QCkit,
38-
EDIorg/EMLassemblyline@v3.5.5
39+
EDIorg/EMLassemblyline
3940
Suggests:
4041
knitr,
4142
rmarkdown,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export(NPSdataverse_packages)
44
export(detach_NPSdataverse)
55
export(is_online)
66
importFrom(lifecycle,deprecated)
7+
importFrom(utils,packageVersion)

R/NPSdataverse-package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
## usethis namespace: start
55
#' @importFrom lifecycle deprecated
6+
#' @importFrom utils packageVersion
67
## usethis namespace: end
78
NULL

R/attach.R

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
pkgs <- c("DPchecker", "EMLeditor", "NPSutils", "QCkit", "EML", "EMLassemblyline")
32

43
NPSdataverse_attach <- function() {
@@ -16,16 +15,16 @@ NPSdataverse_attach <- function() {
1615
# "Attaching packages" on the left-hand side and
1716
# NPSdataverse with the package version on the right-hand side
1817
load_header <- cli::rule(
19-
left = crayon::bold("Attaching packages"),
18+
left = cli::style_bold("Attaching packages"),
2019
right = paste0("NPSdataverse ", package_version("NPSdataverse"))
2120
)
2221

2322
# Return a character string containing the package version for each of NPSdataverse's constituents
2423
versions <- vapply(to_load, package_version, character(1))
2524

2625
packages <- paste0(
27-
crayon::green(cli::symbol$tick), " ", crayon::blue(format(to_load)), " ",
28-
crayon::col_align(versions, max(crayon::col_nchar(versions)))
26+
cli::col_green(cli::symbol$tick), " ", cli::col_blue(format(to_load)), " ",
27+
cli::ansi_align(versions, max(cli::ansi_nchar(versions)))
2928
)
3029

3130
# Format for two columns
@@ -39,8 +38,8 @@ NPSdataverse_attach <- function() {
3938
info <- paste0(packages[col1], " ", packages[-col1])
4039

4140
# display the message!
42-
msg(load_header)
43-
msg(paste(info, collapse = "\n"))
41+
packageStartupMessage(load_header)
42+
packageStartupMessage(paste(info, collapse = "\n"))
4443

4544
# Load the constituent packages!
4645
# character.only = TRUE must be used in order to
@@ -51,7 +50,6 @@ NPSdataverse_attach <- function() {
5150

5251
# Thanks for playing
5352
invisible(to_load)
54-
5553
}
5654

5755
#' Detach all loaded packages
@@ -87,7 +85,6 @@ NPSdataverse_packages <- function() {
8785
names <- vapply(strsplit(parsed, "\\s+"), "[[", 1, FUN.VALUE = character(1))
8886

8987
return(names)
90-
9188
}
9289

9390
#' Check internet connectivity
@@ -101,11 +98,13 @@ NPSdataverse_packages <- function() {
10198
#'
10299
#' @examples
103100
#' is_online()
104-
is_online <- function(site="https://github.com/") {
105-
tryCatch({
106-
readLines(site,n=1)
107-
TRUE
108-
},
109-
warning = function(w) invokeRestart("muffleWarning"),
110-
error = function(e) FALSE)
101+
is_online <- function(site = "https://github.com/") {
102+
tryCatch(
103+
{
104+
readLines(site, n = 1)
105+
TRUE
106+
},
107+
warning = function(w) invokeRestart("muffleWarning"),
108+
error = function(e) FALSE
109+
)
111110
}

R/updateR.R

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,67 @@
55
#' @return dataframe
66
#' @keywords internal
77
#'
8-
.update_git_repos<-function(){
9-
git_pkgs = c("NPSdataverse",
10-
"QCkit",
11-
"EMLeditor",
12-
"DPchecker",
13-
"NPSutils",
14-
"EMLassemblyline")
8+
.update_git_repos <- function() {
9+
git_pkgs <- tibble::tibble(
10+
package = c(
11+
"QCkit",
12+
"EMLeditor",
13+
"DPchecker",
14+
"NPSutils",
15+
"EMLassemblyline",
16+
"NPSdataverse"
17+
),
18+
repo = c(
19+
"nationalparkservice",
20+
"nationalparkservice",
21+
"nationalparkservice",
22+
"nationalparkservice",
23+
"EDIorg",
24+
"nationalparkservice"),
25+
branch = c(
26+
"master",
27+
"main",
28+
"main",
29+
"master",
30+
"main",
31+
"main"
32+
)
33+
)
1534

16-
pkg_update <- remotes::package_deps(git_pkgs, dependencies = c("Imports",
17-
"Remotes",
18-
"Suggests"))
35+
git_pkgs$local_version <- lapply(git_pkgs$package, packageVersion)
36+
git_pkgs$latest_version <- mapply(function(x, y, z) .latest_github_version(repo = x, repo_owner = y, branch = z), git_pkgs$package, git_pkgs$repo, git_pkgs$branch, USE.NAMES = FALSE, SIMPLIFY = FALSE)
37+
git_pkgs$behind <- mapply(function(x, y) x < y, git_pkgs$local_version, git_pkgs$latest_version)
38+
git_pkgs$ahead <- mapply(function(x, y) x > y, git_pkgs$local_version, git_pkgs$latest_version)
39+
git_pkgs$local_version <- sapply(git_pkgs$local_version, as.character)
40+
git_pkgs$latest_version <- sapply(git_pkgs$latest_version, as.character)
41+
42+
if (any(git_pkgs$behind)) {
43+
old_pkgs <- git_pkgs[git_pkgs$behind, ]
1944

20-
if(any(pkg_update$diff < 0)){
21-
old_pkgs <- NULL
22-
old_users <- NULL
23-
for(i in seq_along(pkg_update$diff)){
24-
if(pkg_update$diff[i] < 0){
25-
old_pkgs <- append(old_pkgs, pkg_update$package[i])
26-
old_users <- append(old_users, pkg_update$remote[[i]]$username)
27-
}
28-
}
2945
load_header <- cli::rule(
3046
left = cli::pluralize(
31-
"The following {cli::qty(length(old_pkgs))}package{?s} {?is/are} out of date:\n"))
32-
msg(load_header)
33-
.print_cust_package_deps(pkg_update)
47+
"The following {cli::qty(length(old_pkgs$package))}package{?s} {?is/are} out of date:\n"
48+
)
49+
)
50+
packageStartupMessage(load_header)
51+
packageStartupMessage(.print_cust_package_deps(old_pkgs))
3452
cli::cat_line()
3553
cli::cli_text(
36-
"{.strong To update {cli::qty(length(old_pkgs))}th{?is/ese} {cli::qty(length(old_pkgs))}package{?s}, please run:\n}")
54+
"{.strong To update {cli::qty(length(old_pkgs$package))}th{?is/ese} {cli::qty(length(old_pkgs$package))}package{?s}, please run:\n}"
55+
)
3756
cli::cat_line("detach_NPSdataverse()")
38-
cli::cat_line("devtools::install_github(\"", old_users, "/", old_pkgs, "\")")
57+
cli::cat_line("devtools::install_github(\"", old_pkgs$repo, "/", old_pkgs$package, "\")")
3958
cli::cat_line("\nClose R and Rstudio. Open a new R session and reload the NPSdataverse.")
4059
cli::cat_line()
41-
}
42-
else{
60+
} else {
4361
load_header <- cli::rule(
4462
left = cli::pluralize(
45-
"All NPSdataverse packages are up to date."))
46-
msg(load_header)
63+
"All NPSdataverse packages are up to date."
64+
)
65+
)
66+
packageStartupMessage(load_header)
4767
cli::cat_line()
4868
}
49-
5069
}
5170

5271
#' Custom print function for github repos to update
@@ -61,25 +80,45 @@
6180
#' @return printed text to console
6281
#' @keywords internal
6382
#'
64-
.print_cust_package_deps<-function (x, show_ok = FALSE, ...){
65-
class(x) <- "data.frame"
66-
x$remote <- lapply(x$remote, format)
67-
ahead <- x$diff > 0L
68-
behind <- x$diff < 0L
69-
same_ver <- x$diff == 0L
70-
x$diff <- NULL
71-
x[] <- lapply(x, remotes:::format_str, width = 12)
72-
if (any(behind)) {
73-
#cat("Needs update -----------------------------\n")
74-
print(x[behind, , drop = FALSE], row.names = FALSE, right = FALSE)
75-
}
76-
if (any(ahead)) {
77-
cat("Not on CRAN ----------------------------\n")
78-
print(x[ahead, , drop = FALSE], row.names = FALSE, right = FALSE)
83+
.print_cust_package_deps <- function(pkgs) {
84+
85+
pkg_names <- paste0(cli::col_yellow(cli::symbol$warning), " ", cli::col_blue(pkgs$package))
86+
vers <- paste0("(", pkgs$local_version, " ", cli::symbol$arrow_right, " ", pkgs$latest_version, ")")
87+
88+
packages <- paste(
89+
cli::ansi_align(pkg_names, max(cli::ansi_nchar(pkg_names))),
90+
vers, collapse = "\n")
91+
92+
return(packages)
93+
}
94+
95+
#' Get the latest version of an R package that is on GitHub
96+
#'
97+
#' @param repo Name of the GitHub repository (e.g. "DPchecker")
98+
#' @param repo_owner Owner of the GitHub repository (e.g. "nationalparkservice")
99+
#' @param branch Branch to use (defaults to "main")
100+
#' @param release_only If TRUE, only looks at GitHub releases. If FALSE (default), looks at the version number in the DESCRIPTION file.
101+
#'
102+
#' @returns A package version number
103+
#' @keywords internal
104+
#'
105+
.latest_github_version <- function(repo, repo_owner, branch = "main", release_only = FALSE) {
106+
if (!release_only) {
107+
# Get version number from DESCRIPTION file on GitHub
108+
description_url <- paste("https://raw.githubusercontent.com", repo_owner, repo, branch, "DESCRIPTION", sep = "/")
109+
description <- readr::read_lines(description_url)
110+
version_line <- grep("^Version:\\s*", description)
111+
version_number <- stringr::str_remove(description[version_line], "^Version:\\s*")
79112
}
80-
if (show_ok && any(same_ver)) {
81-
cat("OK ---------------------------------------\n")
82-
print(x[same_ver, , drop = FALSE], row.names = FALSE,
83-
right = FALSE)
113+
else {
114+
# Get version number of latest release on GitHub
115+
releases_url <- paste("https://api.github.com/repos", repo_owner, repo, "releases?per_page=1", sep = "/")
116+
latest_release <- gh::gh(releases_url)
117+
version_number <- latest_release[[1]]$tag_name
84118
}
119+
120+
version_number <- stringr::str_remove_all(version_number, "[a-zA-Z]")
121+
version_number <- as.package_version(version_number)
122+
123+
return(version_number)
85124
}

R/utils.R

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
2-
text_col <- function(x) {
3-
4-
# If RStudio API is not available and/or does not have the getThemeInfo
5-
# button, exit function leaving default color of black
6-
if (!rstudioapi::isAvailable() || !rstudioapi::hasFun("getThemeInfo")) {
7-
return(x)
8-
}
9-
10-
# Get theme information for RStudio
11-
theme <- rstudioapi::getThemeInfo()
12-
13-
# If it's a dark theme, make the text color white; otherwise black.
14-
if (isTRUE(theme$dark)) crayon::white(x) else crayon::black(x)
15-
16-
}
17-
181
# Format the package version to indicate development versions when printed on
192
# the command line
203
package_version <- function(x) {
@@ -26,20 +9,13 @@ package_version <- function(x) {
269
# as happens with development packages, coerce those
2710
# dev version numbers to red
2811
if (length(version) > 3) {
29-
version[4:length(version)] <- crayon::red(as.character(version[4:length(version)]))
12+
version[4:length(version)] <- cli::col_red(as.character(version[4:length(version)]))
3013
}
3114

3215
# concatenate the result
3316
paste0(version, collapse = ".")
34-
35-
}
36-
37-
# Create a message function for start-up that dynamically changes text color
38-
msg <- function(...) {
39-
packageStartupMessage(text_col(...))
4017
}
4118

42-
4319
# Check which of the main NPSdataverse packages
4420
# are currently loaded and return a list of those that are NOT loaded
4521
# The search() function returns a character vector containing packages
@@ -53,5 +29,3 @@ check_loaded <- function() {
5329
is_attached <- function(x) {
5430
paste0("package:", x) %in% search()
5531
}
56-
57-

R/zzz.R

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.onAttach <- function(...) {
2-
#if internet access is available, check for updated packages:
3-
#check for github packages that need updating
2+
# if internet access is available, check for updated packages:
3+
# check for github packages that need updating
44

5-
if(is_online()) {
5+
if (is_online()) {
66
if (interactive()) {
77
.update_git_repos()
88
}
@@ -13,16 +13,11 @@
1313
# See if any packages are needed
1414
needed <- pkgs[!is_attached(pkgs)]
1515

16-
#check for updates to github repo packages:
17-
#.update_git_repos()
18-
1916
# If no packages are needed, return
2017
if (length(needed) == 0) {
2118
return()
2219
# Otherwise, attach any needed packages
2320
} else {
2421
NPSdataverse_attach()
2522
}
26-
2723
}
28-

README.Rmd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ knitr::opts_chunk$set(
2828
#| NPSutils. The key external packages EML and EMLassemblyline that also load
2929
#| are included in a text box.
3030
knitr::include_graphics("man/figures/NPSdataverse_overview.jpg")
31-
#![alt text]("man/figures/NPSdataverse_overview.jpg")
31+
# ![alt text]("man/figures/NPSdataverse_overview.jpg")
3232
```
3333
NPSdataverse loads a suite of R packages for creating, manipulating, and accessing data packages including interacting with DataStore. This is an early version of the NPSdataverse. Please request enhancements and bug fixes through [Issues](https://github.com/nationalparkservice/NPSdataverse/issues).
3434

@@ -104,7 +104,6 @@ After loading NPSdataverse, from within Rstudio select the "File" menu. Select "
104104
#| A screenshot with highlights indicating how to access the EML script
105105
#| This figure shows step 1.
106106
knitr::include_graphics("man/figures/open_rmd.png")
107-
108107
```
109108

110109

0 commit comments

Comments
 (0)