From 1a87af07cb6e52762f1e3316f5c956bc7538e649 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Fri, 5 Mar 2021 11:14:30 +0100 Subject: [PATCH] Replace rubygems-tasks by bundler and do more release automation Also remove obsolete stripping of windows so files. This is done by rake-compiler-dock already. --- Gemfile | 5 +-- Rakefile | 39 ++++++++++------------- rakelib/ffi_gem_helper.rb | 65 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 27 deletions(-) create mode 100644 rakelib/ffi_gem_helper.rb diff --git a/Gemfile b/Gemfile index ec88154b6..40bd4fe79 100644 --- a/Gemfile +++ b/Gemfile @@ -5,10 +5,7 @@ group :development do gem 'rake-compiler', '~> 1.0.3' gem 'rake-compiler-dock', '~> 1.0' gem 'rspec', '~> 3.0' - # irb is a dependency of rubygems-tasks 0.2.5. - # irb versions > 1.1.1 depend on reline, - # which sometimes causes 'bundle install' to fail on Ruby <= 2.4: https://github.com/rubygems/rubygems/issues/3463 - gem 'rubygems-tasks', '>= 0.2', '< 0.2.5', :require => 'rubygems/tasks' + gem 'bundler', '~> 2.0' end group :doc do diff --git a/Rakefile b/Rakefile index 3ceba7f35..1b2298a2f 100644 --- a/Rakefile +++ b/Rakefile @@ -1,20 +1,17 @@ -require 'rubygems/tasks' require 'rbconfig' -require 'rake/clean' -require_relative "lib/ffi/version" - require 'date' require 'fileutils' require 'rbconfig' require 'rspec/core/rake_task' require 'rubygems/package_task' +require 'rake/extensiontask' +require_relative "lib/ffi/version" +require_relative "rakelib/ffi_gem_helper" BUILD_DIR = "build" BUILD_EXT_DIR = File.join(BUILD_DIR, "#{RbConfig::CONFIG['arch']}", 'ffi_c', RUBY_VERSION) -def gem_spec - @gem_spec ||= Gem::Specification.load('ffi.gemspec') -end +gem_spec = Bundler.load_gemspec('ffi.gemspec') RSpec::Core::RakeTask.new(:spec => :compile) do |config| config.rspec_opts = YAML.load_file 'spec/spec.opts' @@ -87,6 +84,10 @@ end task 'gem:java' => 'java:gem' +FfiGemHelper.install_tasks +# Register windows gems to be pushed to rubygems.org +Bundler::GemHelper.instance.cross_platforms = %w[i386-mingw32 x64-mingw32] + if RUBY_ENGINE == 'ruby' || RUBY_ENGINE == 'rbx' require 'rake/extensiontask' Rake::ExtensionTask.new('ffi_c', gem_spec) do |ext| @@ -94,21 +95,11 @@ if RUBY_ENGINE == 'ruby' || RUBY_ENGINE == 'rbx' # ext.lib_dir = BUILD_DIR # put binaries into this folder. ext.tmp_dir = BUILD_DIR # temporary folder used during compilation. ext.cross_compile = true # enable cross compilation (requires cross compile toolchain) - ext.cross_platform = %w[i386-mingw32 x64-mingw32] # forces the Windows platform instead of the default one + ext.cross_platform = Bundler::GemHelper.instance.cross_platforms ext.cross_compiling do |spec| spec.files.reject! { |path| File.fnmatch?('ext/*', path) } end - end - - # To reduce the gem file size strip mingw32 dlls before packaging - ENV['RUBY_CC_VERSION'].to_s.split(':').each do |ruby_version| - task "build/i386-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so" do |t| - sh "i686-w64-mingw32-strip -S build/i386-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so" - end - task "build/x64-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so" do |t| - sh "x86_64-w64-mingw32-strip -S build/x64-mingw32/stage/lib/#{ruby_version[/^\d+\.\d+/]}/ffi_c.so" - end end else task :compile do @@ -116,6 +107,7 @@ else end end + desc "build a windows gem without all the ceremony" task "gem:windows" do require "rake_compiler_dock" @@ -132,7 +124,7 @@ task :libffi => "ext/ffi_c/libffi/autogen.sh" LIBFFI_GIT_FILES = `git --git-dir ext/ffi_c/libffi/.git ls-files -z`.split("\x0") -# Generate files in gemspec but not in libffi's git repo by running autogen.sh +# Generate files which are in the gemspec but not in libffi's git repo by running autogen.sh gem_spec.files.select do |f| f =~ /ext\/ffi_c\/libffi\/(.*)/ && !LIBFFI_GIT_FILES.include?($1) end.each do |f| @@ -148,6 +140,11 @@ end.each do |f| end end +# Make sure we have all gemspec files before packaging +task :build => gem_spec.files +task :gem => :build + + require_relative "lib/ffi/platform" types_conf = File.expand_path(File.join(FFI::Platform::CONF_DIR, 'types.conf')) logfile = File.join(File.dirname(__FILE__), 'types_log') @@ -168,10 +165,6 @@ end desc "Create or update type information for platform #{FFI::Platform::NAME}" task :types_conf => types_conf -Gem::Tasks.new do |t| - t.scm.tag.format = '%s' -end - begin require 'yard' diff --git a/rakelib/ffi_gem_helper.rb b/rakelib/ffi_gem_helper.rb new file mode 100644 index 000000000..2bdf1ed2b --- /dev/null +++ b/rakelib/ffi_gem_helper.rb @@ -0,0 +1,65 @@ +require 'bundler' +require 'bundler/gem_helper' + +class FfiGemHelper < Bundler::GemHelper + attr_accessor :cross_platforms + + def install + super + + task "release:guard_clean" => ["release:update_history"] + + task "release:update_history" do + update_history + end + + task "release:rubygem_push" => ["gem:windows", "gem:java"] + end + + def hfile + "CHANGELOG.md" + end + + def headline + '([^\w]*)(\d+\.\d+\.\d+(?:\.\w+)?)([^\w]+)([2Y][0Y][0-9Y][0-9Y]-[0-1M][0-9M]-[0-3D][0-9D])([^\w]*|$)' + end + + def reldate + Time.now.strftime("%Y-%m-%d") + end + + def update_history + hin = File.read(hfile) + hout = hin.sub(/#{headline}/) do + raise "#{hfile} isn't up-to-date for version #{version}" unless $2==version.to_s + $1 + $2 + $3 + reldate + $5 + end + if hout != hin + Bundler.ui.confirm "Updating #{hfile} for release." + File.write(hfile, hout) + Rake::FileUtilsExt.sh "git", "commit", hfile, "-m", "Update release date in #{hfile}" + end + end + + def tag_version + Bundler.ui.confirm "Tag release with annotation:" + m = File.read(hfile).match(/(?#{headline}.*?)#{headline}/m) || raise("Unable to find release notes in #{hfile}") + Bundler.ui.info(m[:annotation].gsub(/^/, " ")) + IO.popen(["git", "tag", "--file=-", version_tag], "w") do |fd| + fd.write m[:annotation] + end + yield if block_given? + rescue + Bundler.ui.error "Untagging #{version_tag} due to error." + sh_with_code "git tag -d #{version_tag}" + raise + end + + def rubygem_push(path) + cross_platforms.each do |ruby_platform| + super(path.gsub(/\.gem\z/, "-#{ruby_platform}.gem")) + end + super(path.gsub(/\.gem\z/, "-java.gem")) + super(path) + end +end