|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# Script from https://github.com/rubygems/guides/pull/325 |
| 4 | +require "digest/sha2" |
| 5 | + |
| 6 | +# Final clause of Regex `(?=\.gem)` is a positive lookahead assertion |
| 7 | +# See: https://learnbyexample.github.io/Ruby_Regexp/lookarounds.html#positive-lookarounds |
| 8 | +# Used to pattern match against a gem package name, which always ends with .gem. |
| 9 | +# The positive lookahead ensures it is present, and prevents it from being captured. |
| 10 | +VERSION_REGEX = /((\d+\.\d+\.\d+)([-.][0-9A-Za-z-]+)*)(?=\.gem)/ |
| 11 | + |
| 12 | +gem_path_parts = ARGV.first&.split("/") |
| 13 | + |
| 14 | +if gem_path_parts&.any? |
| 15 | + gem_name = gem_path_parts.last |
| 16 | + gem_pkg = File.join(gem_path_parts) |
| 17 | + puts "Looking for: #{gem_pkg.inspect}" |
| 18 | + gems = Dir[gem_pkg] |
| 19 | + puts "Found: #{gems.inspect}" |
| 20 | +else |
| 21 | + gem_pkgs = File.join("pkg", "*.gem") |
| 22 | + puts "Looking for: #{gem_pkgs.inspect}" |
| 23 | + gems = Dir[gem_pkgs] |
| 24 | + raise "Unable to find gems #{gem_pkgs}" if gems.empty? |
| 25 | + |
| 26 | + # Sort by newest last |
| 27 | + # [ "my_gem-2.3.9.gem", "my_gem-2.3.11.pre.alpha.4.gem", "my_gem-2.3.15.gem", ... ] |
| 28 | + gems.sort_by! { |gem| Gem::Version.new(gem[VERSION_REGEX]) } |
| 29 | + gem_pkg = gems.last |
| 30 | + gem_path_parts = gem_pkg.split("/") |
| 31 | + gem_name = gem_path_parts.last |
| 32 | + puts "Found: #{gems.length} gems; latest is #{gem_name}" |
| 33 | +end |
| 34 | + |
| 35 | +checksum512 = Digest::SHA512.new.hexdigest(File.read(gem_pkg)) |
| 36 | +checksum512_path = "checksums/#{gem_name}.sha512" |
| 37 | +File.write(checksum512_path, checksum512) |
| 38 | + |
| 39 | +checksum256 = Digest::SHA256.new.hexdigest(File.read(gem_pkg)) |
| 40 | +checksum256_path = "checksums/#{gem_name}.sha256" |
| 41 | +File.write(checksum256_path, checksum256) |
| 42 | + |
| 43 | +version = gem_name[VERSION_REGEX] |
| 44 | + |
| 45 | +git_cmd = <<~GIT_MSG |
| 46 | + git add checksums/* && \ |
| 47 | + git commit -m "🔒️ Checksums for v#{version}" |
| 48 | +GIT_MSG |
| 49 | + |
| 50 | +puts <<~RESULTS |
| 51 | + [ GEM: #{gem_name} ] |
| 52 | + [ VERSION: #{version} ] |
| 53 | + [ GEM PKG LOCATION: #{gem_pkg} ] |
| 54 | + [ CHECKSUM SHA-256: #{checksum256} ] |
| 55 | + [ CHECKSUM SHA-512: #{checksum512} ] |
| 56 | + [ CHECKSUM SHA-256 PATH: #{checksum256_path} ] |
| 57 | + [ CHECKSUM SHA-512 PATH: #{checksum512_path} ] |
| 58 | + |
| 59 | + ... Running ... |
| 60 | + |
| 61 | + #{git_cmd} |
| 62 | +RESULTS |
| 63 | + |
| 64 | +# This will replace the current process with the git process, and exit. |
| 65 | +# Any command placed after this will not be run: |
| 66 | +# See: https://www.akshaykhot.com/call-shell-commands-in-ruby |
| 67 | +exec(git_cmd) |
0 commit comments