pboling/gem_bench

View on GitHub
bin/checksum

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

require "digest/sha2"

VERSION_REGEX = /\d+\.\d+\.\d+(-.*)?/.freeze

gem_path_parts = ARGV.first&.split("/")

if gem_path_parts&.any?
  gem_name = gem_path_parts.last
  gem_pkg = File.join(gem_path_parts)
  puts "Looking for: #{gem_pkg.inspect}"
  gems = Dir[gem_pkg]
  puts "Found: #{gems.inspect}"
else
  gem_pkgs = File.join("pkg", "*.gem")
  puts "Looking for: #{gem_pkgs.inspect}"
  gems = Dir[gem_pkgs]
  raise "Unable to find gems #{gem_pkgs}" if gems.empty?

  # Sort by newest last
  # [ "vc_ruby-2.3.9.gem", "vc_ruby-2.3.11-alpha.4.gem", "vc_ruby-2.3.15.gem", ... ]
  gems.sort_by! { |gem| Gem::Version.new(gem[VERSION_REGEX]) }
  gem_pkg = gems.last
  gem_path_parts = gem_pkg.split("/")
  gem_name = gem_path_parts.last
  puts "Found: #{gems.length} gems; latest is #{gem_name}"
end

checksum512 = Digest::SHA512.new.hexdigest(File.read(gem_pkg))
checksum512_path = "checksums/#{gem_name}.sha512"
File.write(checksum512_path, checksum512)

checksum256 = Digest::SHA256.new.hexdigest(File.read(gem_pkg))
checksum256_path = "checksums/#{gem_name}.sha256"
File.write(checksum256_path, checksum256)

version = checksum256_path[VERSION_REGEX]

git_cmd = <<~GIT
  git add checksums/* && \
  git commit -m "🔒️ Checksums for v#{version}"
GIT

puts <<~RESULTS
  [GEM: #{gem_name}]
  [VERSION: #{version}]
  [CHECKSUM SHA256 PATH: #{checksum256_path}]
  [CHECKSUM SHA512 PATH: #{checksum512_path}]
  
  ... Running ...
  
  #{git_cmd}
RESULTS

# This will replace the current process with the git process, and exit.
# Any command placed after this will not be run:
#   See: https://www.akshaykhot.com/call-shell-commands-in-ruby
exec(git_cmd)
# EOF