Created
May 9, 2024 15:17
-
-
Save JosiahParry/a8efb10c756844bf2554a54232860927 to your computer and use it in GitHub Desktop.
Draft of a script that removes all sorts of stuff from vendored rust crates to reduce size
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
#!/usr/bin/env Rscript | |
# we need to vendor the dependencies and then store the checksum | |
rextendr::vendor_pkgs() | |
# make the checksum | |
# writeLines( | |
# tools::md5sum("src/rust/vendor.tar.xz"), | |
# "./tools/vendor.md5" | |
# ) | |
all_vendored_files <- list.files("src/rust/vendor", full.names = TRUE, recursive = TRUE, all.files = TRUE) | |
file_exts <- tools::file_ext(all_vendored_files) | |
# use this to identify directories we might want to remove | |
# list.dirs("src/rust/vendor", T, F) |> | |
# lapply(list.dirs, F, F) |> | |
# unlist() |> | |
# table() |> | |
# as.data.frame() | |
# identify all md files | |
mds <- file_exts == "md" | |
# identify all licenses | |
license_files <- stringr::str_detect( | |
all_vendored_files, | |
stringr::coll("LICENSE", ignore_case = TRUE) | |
) | |
# identify all files in benches, tests, examples, misc, ci, and ui-tests | |
dir_idx <- stringr::str_detect( | |
all_vendored_files, | |
"/benches/|/tests/|/examples/|/misc/|/ci/|/ui-tests/|/guide/" | |
) | |
# if any of these are true, we want to remove them | |
to_remove <- dir_idx | mds | license_files | |
# remove all of these files | |
file.remove(all_vendored_files[to_remove]) | |
# identify all of rust files that havent been removed | |
all_rust_sources <- all_vendored_files[file_exts == "rs" & !to_remove] | |
# remove all of the comments from the source files | |
for (fp in all_rust_sources) { | |
file_lines <- brio::read_lines(fp) | |
brio::write_lines(file_lines[!stringr::str_detect(file_lines, "^\\s*//.*$")], fp) | |
} | |
# we need to recompute the hashes for all files in each .cargo-checksum.json | |
all_checksums <- all_vendored_files[stringr::str_detect( | |
all_vendored_files, | |
".cargo-checksum.json$" | |
)] | |
# remove checksums | |
for (fp in all_checksums) { | |
# read the json file | |
cs <- yyjsonr::read_json_file(fp) | |
# calculate the new checksums | |
cs[["files"]] <- lapply(cs$files, openssl::sha256) | |
# handle null package | |
if (is.null(cs[["package"]])) { | |
cs[["package"]] <- "null" | |
} | |
yyjsonr::write_json_file(cs, fp, opts = list(auto_unbox = TRUE)) | |
} | |
# recompress | |
src_dir <- rprojroot::find_package_root_file("src/rust") | |
compress_res <- withr::with_dir(src_dir, { | |
processx::run( | |
"tar", c( | |
"-cJ", "--no-xattrs", "-f", "vendor.tar.xz", "vendor" | |
) | |
) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment