Created
August 26, 2025 01:35
-
-
Save noahd1/a56e4b8967e642aaa9c718e3bc4f9405 to your computer and use it in GitHub Desktop.
Extract SimpleCov resultsets
This file contains hidden or 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 ruby | |
| # frozen_string_literal: true | |
| # | |
| # collect_resultsets.rb | |
| # | |
| # Usage: | |
| # ruby collect_resultsets.rb /path/to/folder_with_zips | |
| # | |
| require 'fileutils' | |
| begin | |
| require 'zip' # rubyzip | |
| rescue LoadError | |
| warn "This script requires the 'rubyzip' gem. Install with: gem install rubyzip" | |
| exit 1 | |
| end | |
| # --- Args & setup ------------------------------------------------------------- | |
| input_dir = ARGV[0] | |
| if input_dir.nil? || input_dir.strip.empty? | |
| abort "Usage: ruby #{File.basename(__FILE__)} /path/to/folder_with_zips" | |
| end | |
| input_dir = File.expand_path(input_dir) | |
| abort "No such directory: #{input_dir}" unless Dir.exist?(input_dir) | |
| # Output directory is now a subdirectory of input_dir | |
| out_dir = File.join(input_dir, 'collected_resultsets') | |
| FileUtils.mkdir_p(out_dir) | |
| ENTRY_REGEX = %r{\Araw_files/.*?/\.resultset\..+\.json\z}.freeze | |
| # --- Work -------------------------------------------------------------------- | |
| zips = Dir.glob(File.join(input_dir, '*.zip')).sort | |
| if zips.empty? | |
| puts "No .zip files found in: #{input_dir}" | |
| exit 0 | |
| end | |
| total_found = 0 | |
| total_extracted = 0 | |
| zips.each do |zip_path| | |
| zip_base = File.basename(zip_path, '.zip') | |
| found_in_zip = 0 | |
| extracted_in_zip = 0 | |
| Zip::File.open(zip_path) do |zip| | |
| zip.each do |entry| | |
| next unless entry.file? | |
| next unless entry.name.match?(ENTRY_REGEX) | |
| found_in_zip += 1 | |
| total_found += 1 | |
| dest_name = "#{zip_base}__#{File.basename(entry.name)}" | |
| dest_path = File.join(out_dir, dest_name) | |
| zip.extract(entry, dest_path) { true } | |
| extracted_in_zip += 1 | |
| total_extracted += 1 | |
| end | |
| end | |
| puts "[#{File.basename(zip_path)}] matched #{found_in_zip}, extracted #{extracted_in_zip}." | |
| end | |
| puts "Done." | |
| puts "Output directory: #{out_dir}" | |
| puts "Matched #{total_found} file(s) across #{zips.size} zip(s); extracted #{total_extracted}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment