| 1 |
require 'rubygems' |
| 2 |
require 'rubygems/package_task' |
| 3 |
require 'fileutils' |
| 4 |
require 'rake/testtask' |
| 5 |
|
| 6 |
# Ignore errors loading rdoc/task (the rdoc tasks will be excluded if |
| 7 |
# rdoc is unavailable). |
| 8 |
begin |
| 9 |
require 'rdoc/task' |
| 10 |
rescue LoadError, RuntimeError |
| 11 |
end |
| 12 |
|
| 13 |
BASE_DIR = File.expand_path(File.dirname(__FILE__)) |
| 14 |
|
| 15 |
task default: [:test] |
| 16 |
|
| 17 |
spec = eval(File.read('tzinfo.gemspec')) |
| 18 |
|
| 19 |
class TZInfoPackageTask < Gem::PackageTask |
| 20 |
alias_method :orig_sh, :sh |
| 21 |
private :orig_sh |
| 22 |
|
| 23 |
def sh(*cmd, &block) |
| 24 |
if cmd[0] == '__tar_with_owner__' && cmd[1] =~ /\A-?[zjcvf]+\z/ |
| 25 |
opts = cmd[1] |
| 26 |
cmd = ['tar', 'c', '--owner', '0', '--group', '0', "#{opts.start_with?('-') ? '' : '-'}#{opts.gsub('c', '')}"] + cmd.drop(2) |
| 27 |
elsif cmd.first =~ /\A__tar_with_owner__ -?([zjcvf]+)(.*)\z/ |
| 28 |
opts = $1 |
| 29 |
args = $2 |
| 30 |
cmd[0] = "tar c --owner 0 --group 0 -#{opts.gsub('c', '')}#{args}" |
| 31 |
end |
| 32 |
|
| 33 |
orig_sh(*cmd, &block) |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
def add_signing_key(spec) |
| 38 |
# Attempt to find the private key and add options to sign the gem if found. |
| 39 |
private_key_path = File.expand_path(File.join(BASE_DIR, '..', 'key', 'gem-private_key.pem')) |
| 40 |
|
| 41 |
if File.exist?(private_key_path) |
| 42 |
spec = spec.clone |
| 43 |
spec.signing_key = private_key_path |
| 44 |
spec.cert_chain = [File.join(BASE_DIR, 'gem-public_cert.pem')] |
| 45 |
else |
| 46 |
puts 'WARNING: Private key not found. Not signing gem file.' |
| 47 |
end |
| 48 |
|
| 49 |
spec |
| 50 |
end |
| 51 |
|
| 52 |
package_task = TZInfoPackageTask.new(add_signing_key(spec)) do |pkg| |
| 53 |
pkg.need_zip = true |
| 54 |
pkg.need_tar_gz = true |
| 55 |
pkg.tar_command = '__tar_with_owner__' |
| 56 |
end |
| 57 |
|
| 58 |
# Skip the rdoc task if RDoc::Task is unavailable |
| 59 |
if defined?(RDoc) && defined?(RDoc::Task) |
| 60 |
RDoc::Task.new do |rdoc| |
| 61 |
rdoc.rdoc_dir = 'doc' |
| 62 |
rdoc.options.concat spec.rdoc_options |
| 63 |
rdoc.rdoc_files.include(spec.extra_rdoc_files) |
| 64 |
rdoc.rdoc_files.include('lib') |
| 65 |
end |
| 66 |
end |
| 67 |
|
| 68 |
Rake::Task[package_task.package_dir_path].enhance do |
| 69 |
recurse_chmod(package_task.package_dir_path) |
| 70 |
end |
| 71 |
|
| 72 |
Rake::Task[:package].enhance do |
| 73 |
FileUtils.rm_rf(package_task.package_dir_path) |
| 74 |
end |
| 75 |
|
| 76 |
def recurse_chmod(dir) |
| 77 |
File.chmod(0755, dir) |
| 78 |
|
| 79 |
Dir.entries(dir).each do |entry| |
| 80 |
if entry != '.' && entry != '..' |
| 81 |
path = File.join(dir, entry) |
| 82 |
if File.directory?(path) |
| 83 |
recurse_chmod(path) |
| 84 |
else |
| 85 |
File.chmod(0644, path) |
| 86 |
end |
| 87 |
end |
| 88 |
end |
| 89 |
end |
| 90 |
|
| 91 |
# Only run coverage tests on MRI. SimpleCov is supported on JRuby 9.1, but |
| 92 |
# generates warnings. It is not supported on Rubinius. |
| 93 |
TEST_COVERAGE = RUBY_ENGINE == 'ruby' |
| 94 |
ENV['TEST_COVERAGE'] = TEST_COVERAGE ? '1' : '0' |
| 95 |
|
| 96 |
desc 'Run tests using RubyDataSource with a format 1 version of TZInfo::Data, RubyDataSource with a format 2 version of TZInfo::Data and ZoneinfoDataSource' |
| 97 |
task test: [:clean_coverage, :test_ruby_format1, :test_ruby_format2, :test_zoneinfo] do |
| 98 |
end |
| 99 |
|
| 100 |
desc 'Remove coverage results' |
| 101 |
task :clean_coverage do |
| 102 |
FileUtils.rm_f(File.join(BASE_DIR, 'coverage', '.resultset.json')) |
| 103 |
end |
| 104 |
|
| 105 |
def setup_tests(test_task, type) |
| 106 |
test_task.libs = [File.join(BASE_DIR, 'lib')] |
| 107 |
test_task.pattern = File.join(BASE_DIR, 'test', "ts_all_#{type}.rb") |
| 108 |
test_task.warning = true |
| 109 |
end |
| 110 |
|
| 111 |
Rake::TestTask.new(:test_ruby_format1) do |t| |
| 112 |
setup_tests(t, :ruby_format1) |
| 113 |
end |
| 114 |
|
| 115 |
Rake::TestTask.new(:test_ruby_format2) do |t| |
| 116 |
setup_tests(t, :ruby_format2) |
| 117 |
end |
| 118 |
|
| 119 |
Rake::TestTask.new(:test_zoneinfo) do |t| |
| 120 |
setup_tests(t, :zoneinfo) |
| 121 |
end |