R/pruneApps.R

Defines functions .pruneApp pruneApps

Documented in pruneApps

#' Prune the koopa applications directory
#'
#' @export
#' @note Updated 2023-10-16.
#'
#' @param dryRun `logical(1)`.
#' Perform a dry run. Just return the directory paths, but don't
#' delete anything.
#'
#' @return Invisible `logical(1)`.
#'
#' @examples
#' out <- pruneApps(dryRun = TRUE)
#' print(out)
pruneApps <- function(dryRun = FALSE) {
    assert(isFlag(dryRun))
    prefix <- koopaAppPrefix()
    paths <- sort(list.dirs(
        path = prefix,
        full.names = TRUE,
        recursive = FALSE
    ))
    if (!hasLength(paths)) {
        alertWarning(sprintf("No apps to prune in {.dir %s}.", prefix))
        return(invisible(character()))
    }
    noPrune <- .noPrune()
    paths <- paths[!basename(paths) %in% noPrune]
    if (!hasLength(paths)) {
        alertWarning(sprintf("No apps to prune in {.dir %s}.", prefix))
        return(invisible(character()))
    }
    prunePaths <- lapply(X = paths, FUN = .pruneApp)
    prunePaths <- unlist(prunePaths, recursive = FALSE, use.names = FALSE)
    if (!hasLength(prunePaths)) {
        alertWarning(sprintf(
            "No outdated app versions to prune in {.dir %s}.",
            prefix
        ))
        return(invisible(character()))
    }
    if (isTRUE(dryRun)) {
        alertInfo("Dry run mode.")
        cat(prunePaths, sep = "\n")
        return(invisible(prunePaths))
    }
    invisible(lapply(
        X = prunePaths,
        FUN = function(path) {
            alert(sprintf("Pruning {.dir %s}.", path))
            unlink(path, recursive = TRUE)
        }
    ))
    alertSuccess("Successfully pruned apps.")
    invisible(prunePaths)
}



#' Return directory paths to prune
#'
#' @note Updated 2023-01-31.
#' @noRd
#'
#' @return `character`.
#' Directory paths.
.pruneApp <- function(path) {
    paths <- list.dirs(path = path, full.names = TRUE, recursive = FALSE)
    if (length(paths) < 2L) {
        return(NULL)
    }
    df <- file.info(paths)
    idx <- order(df[["ctime"]], decreasing = TRUE)
    paths <- paths[idx]
    out <- paths[seq(from = 2L, to = length(paths))]
    out
}
acidgenomics/r-koopa documentation built on Oct. 31, 2023, 9:21 a.m.