-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
461 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#' Get NEWS for a package | ||
#' @description Get NEWS for a package from CRAN or local | ||
#' @inheritParams standard_args | ||
#' @param repos `character` vector repositories URLs to use. Used only for the validation. Default `https://cran.rstudio.com/` | ||
#' @return `character` with NEWS content. | ||
#' @note Results are cached for 30 minutes with `memoise` package. | ||
#' @export | ||
#' @examples | ||
#' \dontrun{ | ||
#' pacs::pac_news("dplyr", version = "0.8.0") | ||
#' pacs::pac_news("dplyr", at = as.Date("2019-02-01")) | ||
#' } | ||
pac_news <- function( | ||
pac, | ||
version = NULL, | ||
at = NULL, | ||
local = FALSE, | ||
lib.loc = .libPaths(), | ||
repos = "https://cran.rstudio.com/" | ||
) { | ||
|
||
validate_pac_input(pac, version, at, local, lib.loc, repos) | ||
|
||
is_installed <- isTRUE(pac %in% rownames(installed_packages(lib.loc = lib.loc))) | ||
if ((!is_installed && local) || (!local && !is_online())) { | ||
return(NA) | ||
} | ||
|
||
version_installed <- if (is_installed) { | ||
utils::packageDescription(pac)$Version | ||
} else { | ||
NA | ||
} | ||
version_null <- is.null(version) | ||
|
||
if (local && is_installed && is.null(at) && (version_null || isTRUE(utils::compareVersion(version, version_installed) == 0))) { | ||
news_name <- intersect(list.files(system.file(package = pac)), c("NEWS.md", "NEWS", "NEWS.Rmd")) | ||
if (length(news_name) == 0) { | ||
return(NA) | ||
} | ||
return(readLines(system.file(package = pac, news_name[1]), warn = FALSE)) | ||
} else if (isTRUE(is_isin(pac, "https://cran.rstudio.com/"))) { | ||
last_version <- pac_last(pac, repos = repos) | ||
version <- if (!version_null) { | ||
version | ||
} else if (!is.null(at)) { | ||
vv <- utils::tail(pac_timemachine(pac, at = at)$Version, 1) | ||
if (isNA(vv) || is.null(vv)) { | ||
return(NA) | ||
} | ||
vv | ||
} else { | ||
last_version | ||
} | ||
news_lines <- pac_readnews(pac, version, repos) | ||
if (isTRUE(is.na(news_lines))) { | ||
return(NA) | ||
} else if (length(news_lines) == 0) { | ||
return(NA) | ||
} else { | ||
return(news_lines) | ||
} | ||
} else { | ||
return(NA) | ||
} | ||
} | ||
|
||
|
||
pac_readnews_raw <- function(pac, version, repos = "https://cran.rstudio.com/") { | ||
ee <- tempfile() | ||
d_url <- sprintf( | ||
"https://raw.githubusercontent.com/cran/%s/%s/NEWS", | ||
pac, | ||
version | ||
) | ||
tt <- try( | ||
{ | ||
suppressWarnings(utils::download.file(d_url, | ||
destfile = ee, | ||
quiet = TRUE | ||
)) | ||
}, | ||
silent = TRUE | ||
) | ||
if (inherits(tt, "try-error")) { | ||
result <- cran_archive_file(pac, version, "NEWS", repos) | ||
} else { | ||
res <- readLines(ee) | ||
unlink(ee) | ||
} | ||
} | ||
|
||
pac_readnews <- memoise::memoise(pac_readnews_raw, cache = cachem::cache_mem(max_age = 30 * 60)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#' Read a file from CRAN | ||
#' @description Read a file from CRAN package source. | ||
#' @param pac `character` package name. | ||
#' @param version `character` package version. | ||
#' @param repos `character` vector repositories URLs to use. Used only for the validation. Default `https://cran.rstudio.com/` | ||
#' @param file `character` file name to read. Possible values are `DESCRIPTION` and `NAMESPACE`. | ||
#' @keywords internal | ||
cran_archive_file <- function(pac, version, file, repos = "https://cran.rstudio.com/") { | ||
last_version <- pac_last(pac, repos) | ||
|
||
if (isTRUE(!is.null(version) && version != last_version)) { | ||
base_url <- sprintf("https://cran.r-project.org/src/contrib/Archive/%s", pac) | ||
} else { | ||
base_url <- "https://cran.r-project.org/src/contrib" | ||
version <- last_version | ||
} | ||
|
||
d_url <- sprintf( | ||
"%s/%s_%s.tar.gz", | ||
base_url, | ||
pac, | ||
version | ||
) | ||
|
||
temp_tar <- tempfile(fileext = ".tar.gz") | ||
|
||
download <- try( | ||
expr = { | ||
suppressWarnings(utils::download.file(d_url, | ||
destfile = temp_tar, | ||
quiet = TRUE | ||
)) | ||
}, | ||
silent = TRUE | ||
) | ||
|
||
if (inherits(download, "try-error")) { | ||
result <- structure(list(), package = pac, version = version) | ||
} else { | ||
temp_dir <- tempdir() | ||
utils::untar(temp_tar, exdir = temp_dir) | ||
if (file == "DESCRIPTION") { | ||
result <- as.list(read.dcf(file.path(temp_dir, pac, "DESCRIPTION"))[1, ]) | ||
} else if (file == "NAMESPACE") { | ||
result <- readLines(file.path(temp_dir, pac, "NAMESPACE"), warn = FALSE) | ||
} else if (file == "NEWS") { | ||
news_name <- intersect(list.files(file.path(temp_dir, pac)), c("NEWS.md", "NEWS", "NEWS.Rmd")) | ||
if (length(news_name) == 0) { | ||
warning("NEWS file not found") | ||
return(NA) | ||
} | ||
result <- readLines(file.path(temp_dir, pac, news_name[1]), warn = FALSE) | ||
} else { | ||
stop("Invalid file name") | ||
} | ||
} | ||
unlink(temp_tar) | ||
result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#' Read a file from a GitHub CRAN repository | ||
#' @description Read a file from a GitHub CRAN repository. | ||
#' @param pac `character` package name. | ||
#' @param version `character` package version. | ||
#' @param file `character` file name to read. Possible values are `DESCRIPTION` and `NAMESPACE`. | ||
#' @param repos `character` vector repositories URLs to use. Used only for the validation. Default `https://cran.rstudio.com/` | ||
#' @note if the file is not found in the GitHub repository, it will try to find it in the CRAN archive. | ||
#' @keywords internal | ||
read_github_file <- function(pac, version, file, repos = "https://cran.rstudio.com/") { | ||
ee <- tempfile() | ||
d_url <- sprintf( | ||
"https://raw.githubusercontent.com/cran/%s/%s/%s", | ||
pac, | ||
version, | ||
file | ||
) | ||
tt <- try( | ||
expr = { | ||
suppressWarnings(utils::download.file(d_url, | ||
destfile = ee, | ||
quiet = TRUE | ||
)) | ||
}, | ||
silent = TRUE | ||
) | ||
if (inherits(tt, "try-error")) { | ||
result <- cran_archive_file(pac, version, repos, file) | ||
} else { | ||
res <- readLines(ee) | ||
unlink(ee) | ||
} | ||
} |
Oops, something went wrong.