Skip to content

Commit

Permalink
Provide all used kwb.utils functions locally
Browse files Browse the repository at this point in the history
  • Loading branch information
hsonne committed Apr 12, 2024
1 parent fb883de commit c1fe536
Show file tree
Hide file tree
Showing 24 changed files with 187 additions and 143 deletions.
14 changes: 9 additions & 5 deletions NAMESPACE
Expand Up @@ -40,28 +40,32 @@ importFrom(gh,gh)
importFrom(grDevices,rainbow)
importFrom(graphics,arrows)
importFrom(graphics,plot)
importFrom(kwb.utils,assignPackageObjects)
importFrom(kwb.utils,catAndRun)
importFrom(kwb.utils,createDirectory)
importFrom(kwb.utils,catIf)
importFrom(kwb.utils,defaultIfNA)
importFrom(kwb.utils,defaultIfNULL)
importFrom(kwb.utils,excludeNULL)
importFrom(kwb.utils,extractSubstring)
importFrom(kwb.utils,getAttribute)
importFrom(kwb.utils,hsRenameColumns)
importFrom(kwb.utils,moveColumnsToFront)
importFrom(kwb.utils,get_homedir)
importFrom(kwb.utils,hsRestoreAttributes)
importFrom(kwb.utils,noFactorDataFrame)
importFrom(kwb.utils,noSuchElements)
importFrom(kwb.utils,orderBy)
importFrom(kwb.utils,rbindAll)
importFrom(kwb.utils,removeColumns)
importFrom(kwb.utils,resetRowNames)
importFrom(kwb.utils,runInDirectory)
importFrom(kwb.utils,safePath)
importFrom(kwb.utils,safeRowBind)
importFrom(kwb.utils,selectColumns)
importFrom(kwb.utils,selectElements)
importFrom(kwb.utils,stopFormatted)
importFrom(mvbutils,foodweb)
importFrom(networkD3,sankeyNetwork)
importFrom(remotes,github_remote)
importFrom(remotes,install_github)
importFrom(remotes,remote_download)
importFrom(utils,available.packages)
importFrom(utils,download.file)
importFrom(utils,download.packages)
importFrom(utils,install.packages)
Expand Down
2 changes: 1 addition & 1 deletion R/allDeps.R
Expand Up @@ -31,7 +31,7 @@ allDeps <- function(
allDeps(deps$name[i], deps$version[i], depth + 1L, max_depth)
})

child_deps <- kwb.utils::excludeNULL(child_deps, dbg = FALSE)
child_deps <- excludeNull(child_deps, dbg = FALSE)

if (length(child_deps) > 0L) {
deps <- rbind(deps, do.call(rbind, child_deps))
Expand Down
8 changes: 3 additions & 5 deletions R/archivedCranVersions.R
Expand Up @@ -5,7 +5,6 @@
#' @param package package name
#' @param ref_date default: NULL
#' @export
#' @importFrom kwb.utils extractSubstring noFactorDataFrame
#' @examples
#' packages <- c("ggplot2", "swmmr", "kwb.hantush")
#' archivedCranVersions(packages)
Expand All @@ -21,7 +20,7 @@ archivedCranVersions <- function(package, ref_date = NULL)
src <- readLinesFromUrl(getUrl("cran_archive", package = package))

if (is.null(src)) {
return(kwb.utils::noFactorDataFrame(
return(noFactorDataFrame(
package = character(0L),
version = character(0L),
date = as.Date(character(0L)),
Expand All @@ -34,7 +33,7 @@ archivedCranVersions <- function(package, ref_date = NULL)
"href=\"(%s_(.*)\\.tar\\.gz)\".*(\\d{4}-\\d{2}-\\d{2}) ", package
)

versions <- cbind(package = package, kwb.utils::extractSubstring(
versions <- cbind(package = package, extractSubstring(
pattern = pattern,
x = grep(pattern, src, value = TRUE),
index = c(
Expand All @@ -59,12 +58,11 @@ archivedCranVersions <- function(package, ref_date = NULL)
#' @noRd
#' @keywords internal
#' @importFrom utils tail
#' @importFrom kwb.utils resetRowNames
getLastVersionBefore <- function(version_dates, ref_date)
{
X = unname(split(version_dates, version_dates$package))

last_before <- function(x) utils::tail(x[x$date <= ref_date, ], 1L)

kwb.utils::resetRowNames(do.call(rbind, lapply(X, last_before)))
resetRowNames(do.call(rbind, lapply(X, last_before)))
}
3 changes: 1 addition & 2 deletions R/compareInstalledVersions.R
Expand Up @@ -10,12 +10,11 @@
#' different in the two libraries)
#' @export
#' @importFrom utils installed.packages
#' @importFrom kwb.utils resetRowNames
compareInstalledVersions <- function(lib1, lib2)
{
installed_versions <- function(x) {
versions <- utils::installed.packages(path.expand(x))
kwb.utils::resetRowNames(kwb.utils::noFactorDataFrame(
resetRowNames(noFactorDataFrame(
name = rownames(versions),
version = versions[, "Version", drop = FALSE]
))
Expand Down
3 changes: 1 addition & 2 deletions R/copyBasePackages.R
Expand Up @@ -9,14 +9,13 @@
#' @param packages vector of names of packages to be copied
#' @export
#' @importFrom utils tail
#' @importFrom kwb.utils catAndRun
copyBasePackages <- function(
target_lib,
set_number = 2L,
system_lib = utils::tail(.libPaths(), 1L),
packages = systemPackages(set_number))
{
kwb.utils::catAndRun(paste("Copying base R packages to", target_lib), {
catAndRun(paste("Copying base R packages to", target_lib), {
file.copy(
from = file.path(system_lib, packages),
to = target_lib,
Expand Down
10 changes: 4 additions & 6 deletions R/cranVersions.R
@@ -1,9 +1,7 @@
# cranVersions -----------------------------------------------------------------

#' @noMd
#' @noRd
#' @keywords internal
#' @importFrom kwb.utils removeColumns safeRowBind
#' Get versions of CRAN packages
#'
cranVersions <- function(name, dbg = TRUE)
{
current <- currentCranVersion(name)
Expand Down Expand Up @@ -35,7 +33,7 @@ cranVersions <- function(name, dbg = TRUE)

archived$package_source_url <- sprintf(urlPattern = archived$archive_file)

result <- kwb.utils::safeRowBind(archived, current)
result <- safeRowBind(archived, current)

kwb.utils::removeColumns(result, "archive_file")
removeColumns(result, "archive_file")
}
5 changes: 2 additions & 3 deletions R/cran_helpers.R
Expand Up @@ -3,15 +3,14 @@
#' @noMd
#' @noRd
#' @keywords internal
#' @importFrom kwb.utils noFactorDataFrame
currentCranVersion <- function(name)
{
src <- readLinesFromUrl(getUrl("cran_package", package = name))

was_removed_pattern <- "was removed from the CRAN repository"

if (is.null(src) || any(grepl(was_removed_pattern, src))) {
return(kwb.utils::noFactorDataFrame(
return(noFactorDataFrame(
package = character(0L),
version = character(0L),
date = as.Date(character(0L)),
Expand All @@ -23,7 +22,7 @@ currentCranVersion <- function(name)
gsub("<td>|</td>", "", src[grep(sprintf("<td>%s:</td>", x), src) + 1L])
}

kwb.utils::noFactorDataFrame(
noFactorDataFrame(
package = name,
version = extract("Version"),
date = as.Date(extract("Published")),
Expand Down
5 changes: 2 additions & 3 deletions R/downloadGitHubPackage.R
Expand Up @@ -8,7 +8,6 @@
#' @return path to downloaded file in the \code{destdir} folder with attribute
#' "origin" pointing to the original file in \code{tempdir()}.
#' @export
#' @importFrom kwb.utils getAttribute
#' @importFrom remotes github_remote remote_download
downloadGitHubPackage <- function(repo, destdir = "~/../Downloads")
{
Expand All @@ -19,8 +18,8 @@ downloadGitHubPackage <- function(repo, destdir = "~/../Downloads")
file_downloaded <- packageInDestdir(package, destdir)

if (file_downloaded) {

file <- kwb.utils::getAttribute(file_downloaded, "path")
file <- getAttribute(file_downloaded, "path")
origin <- character(0)

} else{
Expand Down
2 changes: 1 addition & 1 deletion R/downloadPackagesFromSnapshot.R
Expand Up @@ -41,7 +41,7 @@ downloadPackagesFromSnapshot <- function(

if (! packageInDestdir(package, destdir)) {

kwb.utils::catAndRun(
catAndRun(
sprintf("Downloading %s package %d/%d: %s", type, i, n, package),
utils::download.packages(
package,
Expand Down
41 changes: 18 additions & 23 deletions R/githubPackageVersions.R
Expand Up @@ -6,7 +6,6 @@
#'
#' @return data frame with one row per available version
#' @export
#' @importFrom kwb.utils moveColumnsToFront removeColumns selectColumns orderBy
#' @examples
#' githubVersions("kwb.utils")
githubVersions <- function(name, github_user = "KWB-R")
Expand Down Expand Up @@ -40,9 +39,6 @@ githubPackageVersions <- function(
message("Reading ", repo)
}

# Shortcut
get <- kwb.utils::selectColumns

# Column sets
key_columns <- c("package", "version", "date")
extra_columns <- c("sha", "repo", "tag", "release")
Expand All @@ -57,7 +53,7 @@ githubPackageVersions <- function(

# Read the description files of the commits referred to by the releases
descriptions <- lapply(
get(result, "sha"),
selectColumns(result, "sha"),
readGithubPackageDescription,
repo = repo,
auth_token = auth_token
Expand All @@ -67,32 +63,28 @@ githubPackageVersions <- function(

result$remote <- sprintf("github::%s@%s", result$repo, result$tag)

result$version <- sapply(descriptions, kwb.utils::selectElements, "version")
result$version <- sapply(descriptions, selectElements, "version")

if (reduced) {
result <- kwb.utils::removeColumns(result, extra_columns)
result <- removeColumns(result, extra_columns)
}

result <- kwb.utils::moveColumnsToFront(result, key_columns)
result <- moveColumnsToFront(result, key_columns)

result <- result[! is.na(result$date) & ! is.na(result$version), ]

kwb.utils::orderBy(result, "date")
orderBy(result, "date")
}

# getGithubReleaseInfo ---------------------------------------------------------
#' @noMd
#' @noRd
#' @keywords internal
#' @importFrom kwb.utils noFactorDataFrame removeColumns selectElements
#' @importFrom gh gh
getGithubReleaseInfo <- function(
repo, reduced = TRUE, auth_token = remotes_github_pat()
)
{
# Shortcut
get <- kwb.utils::selectElements

get_endpoint <- function(endpoint) {
stopifnot(length(endpoint) == 1L)
gh::gh(endpoint, .token = auth_token)
Expand All @@ -105,20 +97,23 @@ getGithubReleaseInfo <- function(
return(NULL)
}

tag_info <- kwb.utils::noFactorDataFrame(
tag = sapply(tags, get, "name"),
sha = sapply(lapply(tags, get, "commit"), get, "sha")
tag_info <- noFactorDataFrame(
tag = sapply(tags, selectElements, "name"),
sha = sapply(lapply(tags, selectElements, "commit"), selectElements, "sha")
)

release_info <- if (length(releases)) {
kwb.utils::noFactorDataFrame(
tag = sapply(releases, get, "tag_name"),
date = as.Date(sapply(releases, get, "published_at")),
release = sapply(releases, get, "name"),
author = sapply(releases, function(x) get(get(x, "author"), "login"))
noFactorDataFrame(
tag = sapply(releases, selectElements, "tag_name"),
date = as.Date(sapply(releases, selectElements, "published_at")),
release = sapply(releases, selectElements, "name"),
author = sapply(
releases,
function(x) selectElements(selectElements(x, "author"), "login")
)
)
} else {
kwb.utils::noFactorDataFrame(
noFactorDataFrame(
tag = character(0L),
date = as.Date(character(0L)),
release = character(0L),
Expand All @@ -136,6 +131,6 @@ getGithubReleaseInfo <- function(
return(result)
}

kwb.utils::removeColumns(result, "sha")
removeColumns(result, "sha")
}

10 changes: 4 additions & 6 deletions R/helpers.R
Expand Up @@ -8,10 +8,9 @@ dirPackageZips <- function(package, path)
#' @noMd
#' @noRd
#' @keywords internal
#' @importFrom kwb.utils selectElements
getUrl <- function(key, ...)
{
urls <- kwb.utils::resolve(..., x = list(
urls <- resolve(..., x = list(
cran = "https://cran.r-project.org",
cran_rstudio = "https://cran.rstudio.org",
mran_snapshot = "https://mran.microsoft.com/snapshot/<date>",
Expand All @@ -29,7 +28,7 @@ getUrl <- function(key, ...)
cached_desc = "DESCRIPTION_<package>_<version>.txt"
))

kwb.utils::selectElements(urls, key)
selectElements(urls, key)
}

# githubRepo -------------------------------------------------------------------
Expand All @@ -42,17 +41,16 @@ githubRepo <- function(github_user, name)
#' @noMd
#' @noRd
#' @keywords internal
#' @importFrom kwb.utils safePath
packageInDestdir <- function(package, destdir, verbose = TRUE)
{
files <- dirPackageZips(package, kwb.utils::safePath(destdir))
files <- dirPackageZips(package, safePath(destdir))

file_exists <- length(files) > 0L

if (verbose && file_exists) {
message("Skipping already downloaded package '", package, "'")
}

structure(file_exists, path = if (file_exists) kwb.utils::lastElement(files))
structure(file_exists, path = if (file_exists) lastElement(files))
}

0 comments on commit c1fe536

Please sign in to comment.