| 1 |
require 'date' |
| 2 |
require 'rake/clean' |
| 3 |
require 'digest/md5' |
| 4 |
|
| 5 |
task :default => :test |
| 6 |
|
| 7 |
# ========================================================== |
| 8 |
# Ruby Extension |
| 9 |
# ========================================================== |
| 10 |
|
| 11 |
DLEXT = RbConfig::MAKEFILE_CONFIG['DLEXT'] |
| 12 |
RUBYDIGEST = Digest::MD5.hexdigest(`ruby --version`) |
| 13 |
|
| 14 |
file "ext/ruby-#{RUBYDIGEST}" do |f| |
| 15 |
rm_f FileList["ext/ruby-*"] |
| 16 |
touch f.name |
| 17 |
end |
| 18 |
CLEAN.include "ext/ruby-*" |
| 19 |
|
| •20 |
file 'ext/Makefile' => FileList['ext/*.rb', 'ext/rdiscount.c', "ext/ruby-#{RUBYDIGEST}"] do |
| 21 |
chdir('ext') { ruby 'extconf.rb' } |
| 22 |
end |
| 23 |
CLEAN.include 'ext/Makefile', 'ext/mkmf.log' |
| 24 |
|
| 25 |
file "ext/rdiscount.#{DLEXT}" => FileList["ext/Makefile"] do |f| |
| 26 |
sh 'cd ext && make clean && make && rm -rf conftest.dSYM' |
| 27 |
end |
| 28 |
CLEAN.include 'ext/*.{o,bundle,so,dll}' |
| 29 |
|
| 30 |
file "lib/rdiscount.#{DLEXT}" => "ext/rdiscount.#{DLEXT}" do |f| |
| 31 |
cp f.prerequisites, "lib/", :preserve => true |
| 32 |
end |
| 33 |
|
| 34 |
desc 'Build the rdiscount extension' |
| 35 |
task :build => "lib/rdiscount.#{DLEXT}" |
| 36 |
|
| 37 |
# ========================================================== |
| 38 |
# Manual |
| 39 |
# ========================================================== |
| 40 |
|
| 41 |
file 'man/rdiscount.1' => 'man/rdiscount.1.ronn' do |
| 42 |
sh "ronn --manual=RUBY -b man/rdiscount.1.ronn" |
| 43 |
end |
| 44 |
CLOBBER.include 'man/rdiscount.1' |
| 45 |
|
| 46 |
desc 'Build manpages' |
| 47 |
task :man => 'man/rdiscount.1' |
| 48 |
|
| 49 |
# ========================================================== |
| 50 |
# Testing |
| 51 |
# ========================================================== |
| 52 |
|
| 53 |
require 'rake/testtask' |
| 54 |
Rake::TestTask.new('test:unit') do |t| |
| 55 |
t.test_files = FileList['test/*_test.rb'] |
| 56 |
t.ruby_opts += ['-r rubygems'] if defined? Gem |
| 57 |
end |
| 58 |
task 'test:unit' => [:build] |
| 59 |
|
| 60 |
desc 'Run conformance tests (MARKDOWN_TEST_VER=1.0)' |
| 61 |
task 'test:conformance' => [:build] do |t| |
| 62 |
script = "#{pwd}/bin/rdiscount" |
| 63 |
test_version = ENV['MARKDOWN_TEST_VER'] || '1.0.3' |
| 64 |
lib_dir = "#{pwd}/lib" |
| 65 |
chdir("test/MarkdownTest_#{test_version}") do |
| 66 |
result = `RUBYLIB=#{lib_dir} ./MarkdownTest.pl --script='#{script}' --tidy` |
| 67 |
print result |
| 68 |
fail unless result.include? "; 0 failed." |
| 69 |
end |
| 70 |
|
| 71 |
# Allow to run this rake tasks multiple times |
| 72 |
# https://medium.com/@shaneilske/invoke-a-rake-task-multiple-times-1bcb01dee9d9 |
| 73 |
ENV.delete("MARKDOWN_TEST_VER") |
| 74 |
ENV.delete("RDISCOUNT_EXTENSIONS") |
| 75 |
Rake::Task["test:conformance"].reenable |
| 76 |
end |
| 77 |
|
| 78 |
desc 'Run version 1.0 conformance suite' |
| 79 |
task 'test:conformance:1.0' => [:build] do |t| |
| 80 |
ENV['MARKDOWN_TEST_VER'] = '1.0' |
| 81 |
# see https://github.com/Orc/discount/issues/261 |
| 82 |
# requires flags -f1.0,tabstop,nopants |
| 83 |
ENV['RDISCOUNT_EXTENSIONS'] = "md1compat" |
| 84 |
Rake::Task['test:conformance'].invoke |
| 85 |
end |
| 86 |
|
| 87 |
desc 'Run 1.0.3 conformance suite' |
| 88 |
task 'test:conformance:1.0.3' => [:build] do |t| |
| 89 |
ENV['MARKDOWN_TEST_VER'] = '1.0.3' |
| 90 |
Rake::Task['test:conformance'].invoke |
| 91 |
end |
| 92 |
|
| 93 |
desc 'Run unit and conformance tests' |
| 94 |
task :test => %w[test:unit test:conformance:1.0 test:conformance:1.0.3] |
| 95 |
|
| 96 |
desc 'Run benchmarks' |
| 97 |
task :benchmark => :build do |t| |
| 98 |
$:.unshift 'lib' |
| 99 |
load 'test/benchmark.rb' |
| 100 |
end |
| 101 |
|
| 102 |
# ========================================================== |
| 103 |
# Documentation |
| 104 |
# ========================================================== |
| 105 |
|
| 106 |
desc 'Generate API documentation' |
| 107 |
task :doc => 'doc/index.html' |
| 108 |
|
| 109 |
file 'doc/index.html' => FileList['lib/*.rb'] do |f| |
| 110 |
sh((<<-end).gsub(/\s+/, ' ')) |
| 111 |
hanna --charset utf8 --fmt html --inline-source --line-numbers \ |
| 112 |
--main RDiscount --op doc --title 'RDiscount API Documentation' \ |
| 113 |
#{f.prerequisites.join(' ')} |
| 114 |
end |
| 115 |
end |
| 116 |
|
| 117 |
CLEAN.include 'doc' |
| 118 |
|
| 119 |
# ========================================================== |
| 120 |
# Update package's Discount sources |
| 121 |
# ========================================================== |
| 122 |
|
| 123 |
desc 'Gather required discount sources into extension directory' |
| 124 |
task :gather => 'discount/markdown.h' do |t| |
| 125 |
# Files unique to /ext that should not be overridden |
| 126 |
rdiscount_ext_files = [ |
| 127 |
"config.h", |
| 128 |
"extconf.rb", |
| 129 |
"rdiscount.c", |
| 130 |
] |
| 131 |
|
| 132 |
# Files in /discount that have a main function and should not be copied to /ext |
| 133 |
discount_c_files_with_main_function = [ |
| 134 |
"main.c", |
| 135 |
"makepage.c", |
| 136 |
"mkd2html.c", |
| 137 |
"theme.c", |
| 138 |
] |
| 139 |
|
| 140 |
# Ensure configure.sh was run |
| 141 |
if not File.exist? 'discount/mkdio.h' |
| 142 |
abort "discount/mkdio.h not found. Did you run ./configure.sh in the discount directory?" |
| 143 |
end |
| 144 |
|
| 145 |
# Delete all *.c and *.h files from ext that are not specific to RDiscount. |
| 146 |
Dir.chdir("ext") do |
| 147 |
c_files_to_delete = Dir["*.c"].select { |e| !rdiscount_ext_files.include? e } |
| 148 |
h_files_to_delete = Dir["*.h"].select { |e| !rdiscount_ext_files.include? e } |
| 149 |
|
| 150 |
rm (c_files_to_delete + h_files_to_delete), |
| 151 |
:verbose => true |
| 152 |
end |
| 153 |
|
| 154 |
# Copy all *.c and *.h files from discount -> ext except those that |
| 155 |
# RDiscount overrides. Also exclude Discount files with main functions. |
| 156 |
Dir.chdir("discount") do |
| 157 |
c_files_to_copy = Dir["*.c"].select { |e| |
| 158 |
(!rdiscount_ext_files.include? e) && |
| 159 |
(!discount_c_files_with_main_function.include? e) |
| 160 |
} |
| 161 |
h_files_to_copy = Dir["*.h"].select { |e| !rdiscount_ext_files.include? e } |
| 162 |
|
| 163 |
cp (c_files_to_copy + h_files_to_copy), '../ext/', |
| 164 |
:preserve => true, |
| 165 |
:verbose => true |
| 166 |
end |
| 167 |
|
| 168 |
# Copy special files from discount -> ext |
| 169 |
cp 'discount/blocktags', 'ext/' |
| 170 |
cp 'discount/VERSION', 'ext/' |
| 171 |
|
| 172 |
# Copy man page |
| 173 |
cp 'discount/markdown.7', 'man/' |
| 174 |
|
| 175 |
# Apply patches |
| 176 |
system('cat ext.diff | patch -p1 -d ext') |
| 177 |
end |
| 178 |
|
| 179 |
file 'discount/markdown.h' do |t| |
| 180 |
abort "The discount submodule is required. See the file BUILDING for getting set up." |
| 181 |
end |
| 182 |
|
| 183 |
# PACKAGING ================================================================= |
| 184 |
|
| 185 |
require 'rubygems' |
| 186 |
$spec = eval(File.read('rdiscount.gemspec')) |
| 187 |
|
| 188 |
def package(ext='') |
| 189 |
"pkg/rdiscount-#{$spec.version}" + ext |
| 190 |
end |
| 191 |
|
| 192 |
desc 'Build packages' |
| 193 |
task :package => %w[.gem .tar.gz].map {|e| package(e)} |
| 194 |
|
| 195 |
desc 'Build and install as local gem' |
| 196 |
task :install => package('.gem') do |
| 197 |
sh "gem install #{package('.gem')}" |
| 198 |
end |
| 199 |
|
| 200 |
directory 'pkg/' |
| 201 |
|
| 202 |
file package('.gem') => %w[pkg/ rdiscount.gemspec] + $spec.files do |f| |
| 203 |
sh "gem build rdiscount.gemspec" |
| 204 |
mv File.basename(f.name), f.name |
| 205 |
end |
| 206 |
|
| 207 |
file package('.tar.gz') => %w[pkg/] + $spec.files do |f| |
| 208 |
sh "git archive --format=tar HEAD | gzip > #{f.name}" |
| 209 |
end |
| 210 |
|
| 211 |
# GEMSPEC HELPERS ========================================================== |
| 212 |
|
| 213 |
def source_version |
| 214 |
line = File.read('lib/rdiscount.rb')[/^\s*VERSION = .*/] |
| 215 |
line.match(/.*VERSION = '(.*)'/)[1] |
| 216 |
end |
| 217 |
|
| 218 |
file 'rdiscount.gemspec' => FileList['Rakefile','lib/rdiscount.rb'] do |f| |
| 219 |
# read spec file and split out manifest section |
| 220 |
spec = File.read(f.name) |
| 221 |
head, manifest, tail = spec.split(" # = MANIFEST =\n") |
| 222 |
head.sub!(/\.version = '.*'/, ".version = '#{source_version}'") |
| 223 |
head.sub!(/\.date = '.*'/, ".date = '#{Date.today.to_s}'") |
| 224 |
# determine file list from git ls-files |
| 225 |
files = `git ls-files`. |
| 226 |
split("\n"). |
| 227 |
sort. |
| 228 |
reject{|file| |
| 229 |
file =~ /^\./ || |
| 230 |
file =~ /^test\/MarkdownTest/ || |
| 231 |
file =~ /^CLAUDE\.md|ext\.diff$/ |
| 232 |
}. |
| 233 |
map{ |file| " #{file}" }. |
| 234 |
join("\n") |
| 235 |
# piece file back together and write... |
| 236 |
manifest = " s.files = %w[\n#{files}\n ]\n" |
| 237 |
spec = [head,manifest,tail].join(" # = MANIFEST =\n") |
| 238 |
File.open(f.name, 'w') { |io| io.write(spec) } |
| 239 |
puts "updated #{f.name}" |
| 240 |
end |