# Builds a tiny throwaway package, packs it with pkglite into a plain-text # file (.txt), restores it to an unpacked directory, and verifies file # trees and byte-level contents. # # Rscript pkglite-demo.R create_demo_package <- function(pkg_dir) { if (!dir.exists(pkg_dir)) { dir.create(pkg_dir, recursive = TRUE) } dir.create(file.path(pkg_dir, "R"), showWarnings = FALSE) dir.create(file.path(pkg_dir, "data"), showWarnings = FALSE) desc_content <- c( "Package: tinypkg", "Title: Minimal Demo Package for pkglite", "Version: 0.1.0", "Description: Minimal package for packing and unpacking with pkglite.", "License: Apache License (>= 2)", "Encoding: UTF-8" ) writeLines(desc_content, file.path(pkg_dir, "DESCRIPTION")) writeLines("export(add_one)", file.path(pkg_dir, "NAMESPACE")) r_content <- c( "#' Add one to a numeric value", "#' @param x Numeric input", "#' @return x + 1L", "#' @export", "add_one <- function(x) {", " x + 1L", "}" ) writeLines(r_content, file.path(pkg_dir, "R", "add_one.R")) toy <- data.frame( subjid = c("01", "02"), val = c(10L, 20L), stringsAsFactors = FALSE ) save(toy, file = file.path(pkg_dir, "data", "toy.rda")) invisible(pkg_dir) } pack_demo_package <- function( pkg_dir, txt_path = file.path(tempdir(), "tinypkg.txt"), spec = pkglite::file_default() ) { fc <- pkglite::collate(pkg_dir, spec) pkglite::pack(fc, output = txt_path, quiet = TRUE) list(txt_path = txt_path, file_collection = fc) } unpack_demo_package <- function(txt_path, out_dir) { if (!dir.exists(out_dir)) { dir.create(out_dir, recursive = TRUE) } pkglite::unpack(input = txt_path, output = out_dir, quiet = TRUE) file.path(out_dir, "tinypkg") } compare_packages <- function(orig_dir, restored_dir, rel_paths) { results <- lapply(rel_paths, function(rel_path) { orig_file <- file.path(orig_dir, rel_path) restored_file <- file.path(restored_dir, rel_path) exists_orig <- file.exists(orig_file) exists_restored <- file.exists(restored_file) if (!exists_orig || !exists_restored) { return(data.frame( file = rel_path, exists_orig = exists_orig, exists_restored = exists_restored, byte_identical = FALSE, stringsAsFactors = FALSE )) } orig_bytes <- readBin(orig_file, "raw", n = file.info(orig_file)$size) restored_bytes <- readBin( restored_file, "raw", n = file.info(restored_file)$size ) data.frame( file = rel_path, exists_orig = exists_orig, exists_restored = exists_restored, byte_identical = identical(orig_bytes, restored_bytes), stringsAsFactors = FALSE ) }) do.call(rbind, results) } if (sys.nframe() == 0L) { demo_root <- file.path(tempdir(), "pkglite-demo-run") pkg_dir <- file.path(demo_root, "tinypkg") unpack_dir <- file.path(demo_root, "restored") txt_path <- file.path(demo_root, "tinypkg.txt") message("Creating throwaway package...") create_demo_package(pkg_dir) message("Packing package into plain text with pkglite...") pack_res <- pack_demo_package(pkg_dir, txt_path = txt_path) is_ascii <- pkglite::verify_ascii(txt_path, quiet = TRUE) message("ASCII verification: ", if (is_ascii) "PASS" else "FAIL") message("Restoring package to new directory...") restored_pkg <- unpack_demo_package(txt_path, unpack_dir) rel_paths <- pack_res$file_collection$df$path_rel comparison <- compare_packages(pkg_dir, restored_pkg, rel_paths) print(comparison) all_match <- all(comparison$byte_identical) message( "Round trip status: ", if (all_match) "ALL FILES MATCH EXACTLY" else "DISCREPANCIES FOUND" ) }