ddfreyne/slow_enumerator_tools

View on GitHub
scripts/release

Summary

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

require 'fileutils'
require 'octokit'

def run(*args)
  puts 'I will execute the following:'
  puts '    ' + args.map { |a| a =~ /\s/ ? a.inspect : a }.join(' ')
  print 'Is this correct? [y/N] '
  res = gets
  unless res.strip.casecmp('y').zero?
    warn 'Answer was not Y; release aborted.'
    exit 1
  end

  system('echo', *args)
  system(*args)

  print 'Continue? [y/N] '
  res = gets
  unless res.strip.casecmp('y').zero?
    warn 'Answer was not Y; release aborted.'
    exit 1
  end
end

puts '=== Logging in to GitHub’s API…'
client = Octokit::Client.new(netrc: true)
puts

puts '=== Deleting old *.gem files…'
Dir['*.gem'].each do |fn|
  puts "  #{fn}…"
  FileUtils.rm_f(fn)
end
puts

puts '=== Verifying presence of release date…'
unless File.readlines('NEWS.md').drop(2).first =~ / \(\d{4}-\d{2}-\d{2}\)$/
  warn 'No proper release date found!'
  exit 1
end
puts

puts '=== Building new gem…'
run('gem', 'build', 'slow_enumerator_tools.gemspec')
puts

puts '=== Reading version…'
require './lib/slow_enumerator_tools/version'
puts "Version = #{SlowEnumeratorTools::VERSION}"
puts

puts '=== Verifying that release does not yet exist…'
releases = client.releases('ddfreyne/slow_enumerator_tools')
release = releases.find { |r| r.tag_name == SlowEnumeratorTools::VERSION }
if release
  warn 'Release already exists!'
  warn 'ABORTED!'
  exit 1
end
puts

puts '=== Creating Git tag…'
run('git', 'tag', '--sign', '--annotate', SlowEnumeratorTools::VERSION, '--message', "Version #{SlowEnumeratorTools::VERSION}")
puts

puts '=== Pushing Git data…'
run('git', 'push', 'origin', '--tags')
puts

puts '=== Pushing gem…'
run('gem', 'push', "slow_enumerator_tools-#{SlowEnumeratorTools::VERSION}.gem")
puts

puts '=== Reading release notes…'
release_notes =
  File.readlines('NEWS.md')
      .drop(4)
      .take_while { |l| l !~ /^## / }
      .join
puts

puts '=== Creating release on GitHub…'
sleep 3 # Give GitHub some time to detect the new tag
is_prerelease = SlowEnumeratorTools::VERSION =~ /a|b|rc/ || SlowEnumeratorTools::VERSION =~ /^0/
client.create_release(
  'ddfreyne/slow_enumerator_tools', SlowEnumeratorTools::VERSION,
  prerelease: !is_prerelease.nil?,
  body: release_notes
)
puts

puts 'DONE!'