Skip to content

Commit

Permalink
Split Rakefile into rakelib
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Feb 11, 2021
1 parent 8e4d9a8 commit 5f993b8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 77 deletions.
74 changes: 0 additions & 74 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,90 +1,16 @@
require "bundler/gem_tasks"
require "rake/testtask"

helper = Bundler::GemHelper.instance

Rake::TestTask.new(:test) do |t|
t.test_files = FileList["test/**/test_*.rb"]
end

task :default => :test

task "build" => "date_epoch"
task "build" => "changelogs"

task "date_epoch" do
ENV["SOURCE_DATE_EPOCH"] = IO.popen(%W[git -C #{__dir__} log -1 --format=%ct], &:read).chomp
end

def helper.update_gemspec
path = "#{__dir__}/#{gemspec.name}.gemspec"
File.open(path, "r+b") do |f|
if (d = f.read).sub!(/^(version\s*=\s*)".*"/) {$1 + gemspec.version.to_s.dump}
f.rewind
f.truncate(0)
f.print(d)
end
end
end

def helper.commit_bump
sh(%W[git -C #{__dir__} commit -m bump\ up\ to\ #{gemspec.version}
#{gemspec.name}.gemspec])
end

def helper.version=(v)
gemspec.version = v
update_gemspec
commit_bump
end
major, minor, teeny = helper.gemspec.version.segments

task "bump:teeny" do
helper.version = Gem::Version.new("#{major}.#{minor}.#{teeny+1}")
end

task "bump:minor" do
raise "can't bump up minor"
end

task "bump:major" do
raise "can't bump up major"
end

task "bump" => "bump:teeny"

task "tag" do
helper.__send__(:tag_version)
end

def changelog(output, ver = nil, prev = nil)
ver &&= Gem::Version.new(ver)
range = [[prev], [ver, "HEAD"]].map {|ver, branch| ver ? "v#{ver.to_s}" : branch}.compact.join("..")
IO.popen(%W[git log --format=fuller --topo-order --no-merges #{range}]) do |log|
line = log.gets
FileUtils.mkpath(File.dirname(output))
File.open(output, "wb") do |f|
f.print "-*- coding: utf-8 -*-\n\n", line
log.each_line do |line|
line.sub!(/^(?!:)(?:Author|Commit)?(?:Date)?: /, ' \&')
line.sub!(/ +$/, '')
f.print(line)
end
end
end
end

tags = IO.popen(%w[git tag -l v0.*]).grep(/v(.*)/) {$1}
tags.sort_by! {|tag| tag.scan(/\d+/).map(&:to_i)}
tags.inject(nil) do |prev, tag|
task("logs/ChangeLog-#{tag}") {|t| changelog(t.name, tag, prev)}
tag
end

desc "Make ChangeLog"
task "ChangeLog", [:ver, :prev] do |t, ver: nil, prev: tags.last|
changelog(t.name, ver, prev)
end
changelogs = ["ChangeLog", *tags.map {|tag| "logs/ChangeLog-#{tag}"}]
task "changelogs" => changelogs
CLOBBER.concat(changelogs) << "logs"
34 changes: 34 additions & 0 deletions rakelib/changelogs.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
task "build" => "changelogs"

changelog = proc do |output, ver = nil, prev = nil|
ver &&= Gem::Version.new(ver)
range = [[prev], [ver, "HEAD"]].map {|ver, branch| ver ? "v#{ver.to_s}" : branch}.compact.join("..")
IO.popen(%W[git log --format=fuller --topo-order --no-merges #{range}]) do |log|
line = log.gets
FileUtils.mkpath(File.dirname(output))
File.open(output, "wb") do |f|
f.print "-*- coding: utf-8 -*-\n\n", line
log.each_line do |line|
line.sub!(/^(?!:)(?:Author|Commit)?(?:Date)?: /, ' \&')
line.sub!(/ +$/, '')
f.print(line)
end
end
end
end

tags = IO.popen(%w[git tag -l v[0-9]*]).grep(/v(.*)/) {$1}
tags.sort_by! {|tag| tag.scan(/\d+/).map(&:to_i)}
tags.inject(nil) do |prev, tag|
task("logs/ChangeLog-#{tag}") {|t| changelog[t.name, tag, prev]}
tag
end

desc "Make ChangeLog"
task "ChangeLog", [:ver, :prev] do |t, ver: nil, prev: tags.last|
changelog[t.name, ver, prev]
end

changelogs = ["ChangeLog", *tags.map {|tag| "logs/ChangeLog-#{tag}"}]
task "changelogs" => changelogs
CLOBBER.concat(changelogs) << "logs"
5 changes: 5 additions & 0 deletions rakelib/epoch.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
task "build" => "date_epoch"

task "date_epoch" do
ENV["SOURCE_DATE_EPOCH"] = IO.popen(%W[git -C #{__dir__} log -1 --format=%ct], &:read).chomp
end
44 changes: 44 additions & 0 deletions rakelib/version.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class << (helper = Bundler::GemHelper.instance)
def update_gemspec
path = gemspec.loaded_from
File.open(path, "r+b") do |f|
d = f.read
if d.sub!(/^(_VERSION\s*=\s*)".*"/) {$1 + gemspec.version.to_s.dump}
f.rewind
f.truncate(0)
f.print(d)
end
end
end

def commit_bump
sh(%W[git -C #{__dir__} commit -m bump\ up\ to\ #{gemspec.version}
#{gemspec.loaded_from}])
end

def version=(v)
gemspec.version = v
update_gemspec
commit_bump
end
end

major, minor, teeny = helper.gemspec.version.segments

task "bump:teeny" do
helper.version = Gem::Version.new("#{major}.#{minor}.#{teeny+1}")
end

task "bump:minor" do
helper.version = Gem::Version.new("#{major}.#{minor+1}.0")
end

task "bump:major" do
helper.version = Gem::Version.new("#{major+1}.0.0")
end

task "bump" => "bump:teeny"

task "tag" do
helper.__send__(:tag_version)
end
6 changes: 3 additions & 3 deletions ruby2_keywords.gemspec
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version = "0.0.5"
abort "Version must not reach 1" if version[/\d+/].to_i >= 1
_VERSION = "0.0.5"
abort "Version must not reach 1" if _VERSION[/\d+/].to_i >= 1

Gem::Specification.new do |s|
s.name = "ruby2_keywords"
s.version = version
s.version = _VERSION
s.summary = "Shim library for Module#ruby2_keywords"
s.homepage = "https://github.com/ruby/ruby2_keywords"
s.licenses = ["Ruby", "BSD-2-Clause"]
Expand Down

0 comments on commit 5f993b8

Please sign in to comment.