From 698e046aac64e46cc0253ab62ffa57c0e8073cf7 Mon Sep 17 00:00:00 2001 From: Denis Defreyne Date: Fri, 6 Dec 2019 08:34:56 +0100 Subject: [PATCH] Use new release script (from Nanoc) --- scripts/release | 161 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 117 insertions(+), 44 deletions(-) diff --git a/scripts/release b/scripts/release index 03ba713..2daa94b 100755 --- a/scripts/release +++ b/scripts/release @@ -2,28 +2,60 @@ # frozen_string_literal: true require 'fileutils' +require 'json' +require 'netrc' require 'octokit' +require 'shellwords' +require 'uri' 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.' + puts(' ' + args.map { |s| Shellwords.escape(s) }.join(' ')) + system(*args) +end + +if ARGV.size != 1 + warn "usage: #{$PROGRAM_NAME} [gem-name]" + exit 1 +end + +gem_name = ARGV[0] + +version_constant = + { + 'adsf' => 'Adsf::VERSION', + 'adsf-live' => 'Adsf::Live::VERSION', + }.fetch(gem_name) do + warn "Error: Unknown gem name: #{gem_name}" exit 1 end - system('echo', *args) - system(*args) +gem_dir = + { + 'adsf' => 'adsf', + 'adsf-live' => 'adsf-live', + }.fetch(gem_name) do + warn "Error: Unknown gem name: #{gem_name}" + exit 1 + end - print 'Continue? [y/N] ' - res = gets - unless res.strip.casecmp('y').zero? - warn 'Answer was not Y; release aborted.' +gem_path = + { + 'adsf' => 'adsf', + 'adsf-live' => 'adsf/live', + }.fetch(gem_name) do + warn "Error: Unknown gem name: #{gem_name}" exit 1 end -end + +puts "version_constant = #{version_constant}" +puts "gem_dir = #{gem_dir}" +puts "gem_path = #{gem_path}" +puts + +puts "=== Entering gem dir (#{gem_dir})…" +Dir.chdir(gem_dir) +puts Dir.getwd +puts puts '=== Logging in to GitHub’s API…' client = Octokit::Client.new(netrc: true) @@ -31,39 +63,87 @@ puts puts '=== Deleting old *.gem files…' Dir['*.gem'].each do |fn| - puts " #{fn}…" + puts "deleting #{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 +unless %w[adsf-live].include?(gem_name) + puts '=== Verifying presence of release date…' + release_line = File.readlines('NEWS.md').drop(2).first + unless / \(\d{4}-\d{2}-\d{2}\)$/.match?(release_line) + warn 'Error: No proper release date found!' + exit 1 + end + unless release_line.include?(Time.now.strftime('%Y-%m-%d')) + warn 'Error: The release date does not match today’s date!' + exit 1 + end + puts end + +puts '=== Reading version…' +require "./lib/#{gem_path}/version" +version = eval(version_constant) # rubocop:disable Security/Eval +puts "Version = #{version}" puts puts '=== Building gems…' run('bundle', 'exec', 'rake', 'gem') puts -puts '=== Reading version…' -require './adsf/lib/adsf/version' -puts "Version = #{Adsf::VERSION}" +puts '=== Verifying that gems were built properly…' +gem_filename = "#{gem_name}-#{version}.gem" +unless File.file?(gem_filename) + warn "Error: Could not find gem: #{gem_filename}" + exit 1 +end +puts + +puts '=== Verifying that gem version does not yet exist…' +url = URI.parse("https://rubygems.org/api/v1/versions/#{gem_name}.json") +response = Net::HTTP.get_response(url) +existing_versions = + case response.code + when '404' + [] + when '200' + JSON.parse(response.body).map { |e| e.fetch('number') } + else + warn "Error: Couldn’t fetch version information for #{gem_name} (status #{response.code})" + exit 1 + end +if existing_versions.include?(version) + warn "Error: #{gem_name} v#{version} already exists" + exit 1 +end puts puts '=== Verifying that release does not yet exist…' releases = client.releases('ddfreyne/adsf') -release = releases.find { |r| r.tag_name == Adsf::VERSION } +release = releases.find { |r| r.tag_name == version } if release - warn 'Release already exists!' - warn 'ABORTED!' + warn 'Error: GitHub release already exists!' exit 1 end puts +puts '=== Reading release notes…' +release_notes = + File.readlines('NEWS.md') + .drop(4) + .take_while { |l| l !~ /^## / } + .join +puts + puts '=== Creating Git tag…' -run('git', 'tag', '--sign', '--annotate', Adsf::VERSION, '--message', "Version #{Adsf::VERSION}") +annotation = + if gem_name == 'adsf' + version + else + "#{gem_name}-v#{version}" + end +run('git', 'tag', '--sign', '--annotate', annotation, '--message', "#{gem_name} v#{version}") puts puts '=== Pushing Git data…' @@ -71,26 +151,19 @@ run('git', 'push', 'origin', '--tags') puts puts '=== Pushing gem…' -run('gem', 'push', "adsf/adsf-#{Adsf::VERSION}.gem") -run('gem', 'push', "adsf-live/adsf-live-#{Adsf::VERSION}.gem") -puts - -puts '=== Reading release notes…' -release_notes = - File.readlines('NEWS.md') - .drop(4) - .take_while { |l| l !~ /^## / } - .join +run('gem', 'push', gem_filename) puts -puts '=== Creating release on GitHub…' -sleep 3 # Give GitHub some time to detect the new tag -is_prerelease = Adsf::VERSION =~ /a|b|rc/ || Adsf::VERSION =~ /^0/ -client.create_release( - 'ddfreyne/adsf', Adsf::VERSION, - prerelease: !is_prerelease.nil?, - body: release_notes -) -puts +if gem_name == 'adsf' + puts '=== Creating release on GitHub…' + sleep 3 # Give GitHub some time to detect the new tag + is_prerelease = version =~ /a|b|rc/ || version =~ /^0/ + client.create_release( + 'ddfreyne/adsf', version, + prerelease: !is_prerelease.nil?, + body: release_notes + ) + puts +end puts 'DONE!'