Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar committed Sep 18, 2024
1 parent 29ae5ec commit f968686
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 105 deletions.
4 changes: 1 addition & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Generated by roxygen2: do not edit by hand

S3method("[",orderly_compare_packets)
S3method(format,orderly_compare_packets)
S3method(format,orderly_packet_diff)
S3method(format,orderly_query)
S3method(print,orderly_cleanup_status)
S3method(print,orderly_compare_packets)
S3method(print,orderly_query_explain)
S3method(summary,orderly_compare_packets)
export(orderly_artefact)
export(orderly_cleanup)
export(orderly_cleanup_status)
Expand Down
195 changes: 99 additions & 96 deletions R/compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,125 +107,128 @@ compare_files <- function(target, current, files, root, search_options) {
##' display of the result.
##'
##' @export
orderly_compare_packets <- function(
target, current, what = c("everything", "metadata", "files", "artefacts"),
search_options = NULL, root = NULL, locate = TRUE) {
orderly_compare_packets <- function(target, current, search_options = NULL,
root = NULL, locate = TRUE) {
validate_outpack_id(target, call = environment())
validate_outpack_id(current, call = environment())
what <- rlang::arg_match(what)

root <- root_open(root, locate = locate, require_orderly = FALSE,
call = environment())

meta_target <- orderly_metadata(target, root = root)
meta_current <- orderly_metadata(current, root = root)
if (what %in% c("everything", "metadata")) {
metadata_diff <- compare_metadata(meta_target, meta_current)
} else {
metadata_diff <- NULL
}

if (what == "artefacts") {
if (is.null(meta_target$custom$orderly) ||
is.null(meta_current$custom$orderly)) {
cli::cli_abort("Cannot compare artefacts of non-orderly packets")
}

artefacts_target <- unlist(meta_target$custom$orderly$artefacts$paths)
artefacts_current <- unlist(meta_current$custom$orderly$artefacts$paths)
files <- compare_filesets(
meta_target$files[meta_target$files$path %in% artefacts_target, ],
meta_current$files[meta_current$files$path %in% artefacts_current, ])
} else if (what %in% c("everything", "files")) {
files <- compare_filesets(meta_target$files, meta_current$files)
} else {
files <- data.frame(path = NULL, status = NULL)
}
metadata <- compare_metadata(meta_target, meta_current)

if (is.null(metadata_diff) && all(files$status == "unchanged")) {
ret <- TRUE
} else {
idx <- files$status == "modified"
files$diff[idx] <- compare_files(target, current, files[idx, ]$path,
search_options = search_options,
root = root)

ret <- list(packets = c(target = target, current = current),
metadata_diff = metadata_diff,
files = files)
}
files <- compare_filesets(meta_target$files, meta_current$files)

class(ret) <- "orderly_compare_packets"
idx <- files$status == "modified"
files$diff[idx] <- compare_files(target, current, files[idx, ]$path,
search_options = search_options,
root = root)

ret
orderly_packet_diff$new(target, current, metadata, files)
}


#' @export
format.orderly_compare_packets <- function(x, ...) {
cli::cli_format_method({
if (isTRUE(x)) {
cli::cli_alert_success("Packets are identical")
} else {
target <- x$packets[[1]]
current <- x$packets[[2]]

cli::cli_alert_info("Comparing packets {target} and {current}")

if (!is.null(x$metadata_diff)) {
cli::cli_alert_warning("Packet metadata differs:")
cli::cli_div(theme = list(div = list("margin-left" = 2)))
cli::cli_verbatim(as.character(x$metadata_diff))
cli::cli_end()
}

removed <- x$files[x$files$status == "removed", ]
if (nrow(removed) > 0) {
cli::cli_alert_warning(
"The following files only exist in packet {current}:")
cli::cli_ul(removed$path)
}

added <- x$files[x$files$status == "added", ]
if (nrow(added) > 0) {
cli::cli_alert_warning(
"The following files only exist in packet {target}:")
cli::cli_ul(added$path)
}

modified <- x$files[x$files$status == "modified", ]
if (nrow(modified) > 0) {
cli::cli_alert_warning(paste("The following files exist in both",
"packets but have different contents:"))
cli::cli_ul()
for (i in seq_len(nrow(modified))) {
cli::cli_li("{modified$path[[i]]}")
if (!is.null(modified$diff[[i]])) {
# if (what == "artefacts") {
# if (is.null(meta_target$custom$orderly) ||
# is.null(meta_current$custom$orderly)) {
# cli::cli_abort("Cannot compare artefacts of non-orderly packets")
# }

# artefacts_target <- unlist(meta_target$custom$orderly$artefacts$paths)
# artefacts_current <- unlist(meta_current$custom$orderly$artefacts$paths)
# files <- compare_filesets(
# meta_target$files[meta_target$files$path %in% artefacts_target, ],
# meta_current$files[meta_current$files$path %in% artefacts_current, ])

orderly_packet_diff <- R6::R6Class(
"orderly_packet_diff",
private = list(
target = NULL,
current = NULL,
metadata = NULL,
files = NULL
),
public = list(
initialize = function(target, current, metadata, files) {
private$target <- target
private$current <- current
private$metadata <- metadata
private$files <- files
},

is_equal = function(what = c("metadata", "files")) {
what <- rlang::arg_match(what, multiple = TRUE)

(!("metadata" %in% what) || is.null(private$metadata)) &&
!("files" %in% what) || all(private$files$status == "unchanged")
},

format = function(what = c("metadata", "files")) {
what <- rlang::arg_match(what, multiple = TRUE)

target <- private$packets$target
current <- private$packets$current

cli::cli_format_method({
if (self$is_equal(what)) {
cli::cli_alert_success("Packets {target} and {current} are identical")
} else {
cli::cli_alert_info("Comparing packets {target} and {current}")

if (("metadata" %in% what) && !is.null(private$metadata)) {
cli::cli_alert_warning("Packet metadata differs:")
cli::cli_div(theme = list(div = list("margin-left" = 2)))
cli::cli_verbatim(as.character(modified$diff[[i]]))
cli::cli_verbatim(as.character(private$metadata))
cli::cli_end()
}

if ("files" %in% what) {
removed <- private$files[private$files$status == "removed", ]
if (nrow(removed) > 0) {
cli::cli_alert_warning(
"The following files only exist in packet {current}:")
cli::cli_ul(removed$path)
}

added <- private$files[private$files$status == "added", ]
if (nrow(added) > 0) {
cli::cli_alert_warning(
"The following files only exist in packet {target}:")
cli::cli_ul(added$path)
}

modified <- private$files[private$files$status == "modified", ]
if (nrow(modified) > 0) {
cli::cli_alert_warning(
paste("The following files exist in both packets but have",
"different contents:"))

cli::cli_ul()
for (i in seq_len(nrow(modified))) {
cli::cli_li("{modified$path[[i]]}")
cli::cli_div(theme = list(div = list("margin-left" = 2)))
cli::cli_verbatim(as.character(modified$diff[[i]]))
cli::cli_end()
}
cli::cli_end()
}
}
}
cli::cli_end()
}
})
}
})
}
)
)


#' @export
print.orderly_compare_packets <- function(x, ...) {
cat(format(x, ...), sep = "\n")
}

#' @export
`[.orderly_compare_packets` <- function(x, paths) {
x$files <- x$files[x$files$path %in% paths, , drop = FALSE]
x
format.orderly_packet_diff <- function(x, ...) {
x$format(...)
}


#' @export
summary.orderly_compare_packets <- function(object, ...) {
object$files$diff <- c()
object
print.orderly_compare_packets <- function(x, ...) {
cat(format(x, ...), sep = "\n")
}
11 changes: 5 additions & 6 deletions man/orderly_compare_packets.Rd

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

0 comments on commit f968686

Please sign in to comment.