Conversation
|
✅ All contributors have signed the CLA |
|
I have read the CLA Document and I hereby sign the CLA |
|
Hey @llrs-roche I wrote quickly some dirty code. I was trying to remove testing artifacts (like browser calls and exploratory outputs) ensures the script is clean and production-ready. I also put some repetitive code into functions. Lastly, by focusing only on incomplete cases and avoiding unnecessary loops or operations, the script is optimized for performance. # Professional Script for Automatic Logo Conversion
library("magick")
# Function to retrieve file information from a directory
get_file_info <- function(path) {
files <- list.files(path = path, full.names = TRUE, recursive = FALSE)
tools::file_path_sans_ext(basename(files))
}
# Get file names (sans extensions) from each format directory
svg_files <- get_file_info("SVG")
png_files <- get_file_info("PNG")
ico_files <- get_file_info("ICO")
jpg_files <- get_file_info("jpg")
# Consolidate all unique file names
all_icons <- unique(c(svg_files, png_files, ico_files, jpg_files))
# Create a dataframe indicating the presence of each format
icons_df <- data.frame(
icons = all_icons,
svg = all_icons %in% svg_files,
png = all_icons %in% png_files,
ico = all_icons %in% ico_files,
jpg = all_icons %in% jpg_files
)
# Filter files that need conversion (not present in all formats)
files_to_convert <- icons_df[rowSums(icons_df[, -1]) != ncol(icons_df) - 1, ]
# Function to convert and save logos in missing formats
convert_logos <- function(row) {
file_name <- row["icons"]
available_formats <- names(row[-1])[unlist(row[-1])]
missing_formats <- setdiff(c("svg", "png", "jpeg", "ico"), available_formats)
# Determine the best base format to use for conversion
format_preference <- c("svg", "jpg", "png", "ico")
base_format <- format_preference[min(match(available_formats, format_preference, nomatch = Inf))]
input_file <- file.path(toupper(base_format), paste0(file_name, ".", base_format))
image <- if (base_format == "svg") {
image_read_svg(input_file)
} else {
image_read(input_file)
}
message("Converting ", file_name, " from ", base_format, " to ", paste(missing_formats, collapse = ", "))
for (format in missing_formats) {
if (format == "ico") next # Skip ICO conversion (optional handling)
output_file <- file.path(toupper(format), paste0(file_name, ".", format))
converted_image <- image_convert(image, format = format, matte = TRUE)
image_write(converted_image, path = output_file, format = format, depth = 8)
}
image_destroy(image)
}
# Perform conversion for each incomplete file
apply(files_to_convert, 1, convert_logos)
# Move SVG logos to corresponding package directories
move_logos_to_packages <- function() {
svg_logos <- list.files("SVG", full.names = TRUE, recursive = FALSE)
package_dirs <- list.dirs("..", recursive = FALSE)
package_dirs <- package_dirs[!endsWith(package_dirs, ".Rcheck")]
logo_names <- tools::file_path_sans_ext(basename(svg_logos))
matching_packages <- intersect(tolower(logo_names), basename(package_dirs))
destination_paths <- file.path("..", matching_packages, "man", "figures", "logo.svg")
origin_paths <- svg_logos[tools::file_path_sans_ext(basename(svg_logos)) %in% matching_packages]
valid_destinations <- dir.exists(dirname(destination_paths)) & !file.exists(destination_paths)
if (any(valid_destinations)) {
file.copy(from = origin_paths[valid_destinations], to = destination_paths[valid_destinations])
}
# Remove existing PNG logos if applicable
png_logos <- file.path("..", matching_packages, "man", "figures", "logo.png")
if (any(file.exists(png_logos))) {
file.remove(png_logos[file.exists(png_logos)])
}
}
move_logos_to_packages()
|
|
Using |
This replaces the png logo by the new generated svg logo (see insightsengineering/hex-stickers#39) to a slight reduction on package size. Related to insightsengineering/nestdevs-tasks#93 
**What changes are proposed in this pull request?** Use svg logo instead of the png to reduce package size This is in reference to insightsengineering/nestdevs-tasks#93 The logo was generated with the script at: insightsengineering/hex-stickers#39  -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [ ] **All** GitHub Action workflows pass with a ✅ - [x] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [x] If a bug was fixed, a unit test was added. - [x] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` - [x] Request a reviewer Reviewer Checklist (if item does not apply, mark is as complete) - [ ] If a bug was fixed, a unit test was added. - [ ] Run `pkgdown::build_site()`. Check the R console for errors, and review the rendered website. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cards (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
**What changes are proposed in this pull request?** Uses svg logo instead of the png to reduce package size This is in reference to insightsengineering/nestdevs-tasks#93 The logo was generated with the script at: insightsengineering/hex-stickers#39 -------------------------------------------------------------------------------- Pre-review Checklist (if item does not apply, mark is as complete) - [x] **All** GitHub Action workflows pass with a ✅ - [x] PR branch has pulled the most recent updates from master branch: `usethis::pr_merge_main()` - [x] If a bug was fixed, a unit test was added. - [x] If a new `ard_*()` function was added, it passes the ARD structural checks from `cards::check_ard_structure()`. - [x] If a new `ard_*()` function was added, `set_cli_abort_call()` has been set. - [x] If a new `ard_*()` function was added and it depends on another package (such as, `broom`), `is_pkg_installed("broom")` has been set in the function call and the following added to the roxygen comments: `@examplesIf do.call(asNamespace("cardx")$is_pkg_installed, list(pkg = "broom""))` - [x] Code coverage is suitable for any new functions/features (generally, 100% coverage for new code): `devtools::test_coverage()` Reviewer Checklist (if item does not apply, mark is as complete) - [ ] If a bug was fixed, a unit test was added. - [ ] Code coverage is suitable for any new functions/features: `devtools::test_coverage()` When the branch is ready to be merged: - [ ] Update `NEWS.md` with the changes from this pull request under the heading "`# cardx (development version)`". If there is an issue associated with the pull request, reference it in parentheses at the end update (see `NEWS.md` for examples). - [ ] **All** GitHub Action workflows pass with a ✅ - [ ] Approve Pull Request - [ ] Merge the PR. Please use "Squash and merge" or "Rebase and merge".
This PR adds two scripts:
This is simplifies the process of SVG generation for all the packages.
If this is enough I can commit all the JPG and SVG generated with the script.
Helps with the packages that don't have SVG logos from: insightsengineering/nestdevs-tasks#93