Skip to content

Commit

Permalink
Replace rubygems-tasks by bundler and do more release automation
Browse files Browse the repository at this point in the history
Also remove obsolete stripping of windows so files.
This is done by rake-compiler-dock already.
  • Loading branch information
larskanis committed Mar 5, 2021
1 parent 334c3af commit 1a87af0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 27 deletions.
5 changes: 1 addition & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 16 additions & 23 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -87,35 +84,30 @@ 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|
ext.name = 'ffi_c' # indicate the name of the extension.
# 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
STDERR.puts "Nothing to compile on #{RUBY_ENGINE}"
end
end


desc "build a windows gem without all the ceremony"
task "gem:windows" do
require "rake_compiler_dock"
Expand All @@ -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|
Expand All @@ -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')
Expand All @@ -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'

Expand Down
65 changes: 65 additions & 0 deletions rakelib/ffi_gem_helper.rb
Original file line number Diff line number Diff line change
@@ -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(/(?<annotation>#{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

0 comments on commit 1a87af0

Please sign in to comment.