| 1 | require 'rake' |
|---|
| 2 | require 'rake/clean' |
|---|
| 3 | require 'rake/gempackagetask' |
|---|
| 4 | require 'rake/rdoctask' |
|---|
| 5 | require 'rake/testtask' |
|---|
| 6 | require 'fileutils' |
|---|
| 7 | include FileUtils |
|---|
| 8 | |
|---|
| 9 | RbConfig = Config unless defined?(RbConfig) |
|---|
| 10 | |
|---|
| 11 | NAME = "hpricot" |
|---|
| 12 | REV = `svn info 2>/dev/null`[/Revision: (\d+)/, 1] || `git svn log --limit=1 --oneline 2>/dev/null`[/^r(\d+)/, 1] rescue nil |
|---|
| 13 | VERS = ENV['VERSION'] || "0.6" + (REV ? ".#{REV}" : "") |
|---|
| 14 | PKG = "#{NAME}-#{VERS}" |
|---|
| 15 | BIN = "*.{bundle,jar,so,obj,pdb,lib,def,exp,class}" |
|---|
| 16 | ARCHLIB = "lib/#{::Config::CONFIG['arch']}" |
|---|
| 17 | CLEAN.include ["ext/hpricot_scan/#{BIN}", "ext/fast_xs/#{BIN}", "lib/**/#{BIN}", ARCHLIB, |
|---|
| 18 | 'ext/fast_xs/Makefile', 'ext/hpricot_scan/Makefile', |
|---|
| 19 | '**/.*.sw?', '*.gem', '.config', 'pkg'] |
|---|
| 20 | RDOC_OPTS = ['--quiet', '--title', 'The Hpricot Reference', '--main', 'README', '--inline-source'] |
|---|
| 21 | PKG_FILES = %w(CHANGELOG COPYING README Rakefile) + |
|---|
| 22 | Dir.glob("{bin,doc,test,lib,extras}/**/*") + |
|---|
| 23 | Dir.glob("ext/**/*.{h,java,c,rb,rl}") + |
|---|
| 24 | %w[ext/hpricot_scan/hpricot_scan.c ext/hpricot_scan/HpricotScanService.java] # needed because they are generated later |
|---|
| 25 | RAGEL_C_CODE_GENERATION_STYLES = { |
|---|
| 26 | "table_driven" => 'T0', |
|---|
| 27 | "faster_table_driven" => 'T1', |
|---|
| 28 | "flat_table_driven" => 'F0', |
|---|
| 29 | "faster_flat_table_driven" => 'F1', |
|---|
| 30 | "goto_driven" => 'G0', |
|---|
| 31 | "faster_goto_driven" => 'G1', |
|---|
| 32 | "really_fast goto_driven" => 'G2' |
|---|
| 33 | # "n_way_split_really_fast_goto_driven" => 'P<N>' |
|---|
| 34 | } |
|---|
| 35 | DEFAULT_RAGEL_C_CODE_GENERATION = "really_fast goto_driven" |
|---|
| 36 | SPEC = |
|---|
| 37 | Gem::Specification.new do |s| |
|---|
| 38 | s.name = NAME |
|---|
| 39 | s.version = VERS |
|---|
| 40 | s.platform = Gem::Platform::RUBY |
|---|
| 41 | s.has_rdoc = true |
|---|
| 42 | s.rdoc_options += RDOC_OPTS |
|---|
| 43 | s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"] |
|---|
| 44 | s.summary = "a swift, liberal HTML parser with a fantastic library" |
|---|
| 45 | s.description = s.summary |
|---|
| 46 | s.author = "why the lucky stiff" |
|---|
| 47 | s.email = 'why@ruby-lang.org' |
|---|
| 48 | s.homepage = 'http://code.whytheluckystiff.net/hpricot/' |
|---|
| 49 | s.files = PKG_FILES |
|---|
| 50 | s.require_paths = [ARCHLIB, "lib"] |
|---|
| 51 | s.extensions = FileList["ext/**/extconf.rb"].to_a |
|---|
| 52 | s.bindir = "bin" |
|---|
| 53 | end |
|---|
| 54 | |
|---|
| 55 | desc "Does a full compile, test run" |
|---|
| 56 | task :default => [:compile, :test] |
|---|
| 57 | |
|---|
| 58 | desc "Packages up Hpricot." |
|---|
| 59 | task :package => [:clean, :ragel] |
|---|
| 60 | |
|---|
| 61 | desc "Releases packages for all Hpricot packages and platforms." |
|---|
| 62 | task :release => [:package, :package_win32, :package_jruby] |
|---|
| 63 | |
|---|
| 64 | desc "Run all the tests" |
|---|
| 65 | Rake::TestTask.new do |t| |
|---|
| 66 | t.libs << "test" << ARCHLIB |
|---|
| 67 | t.test_files = FileList['test/test_*.rb'] |
|---|
| 68 | t.verbose = true |
|---|
| 69 | end |
|---|
| 70 | |
|---|
| 71 | Rake::RDocTask.new do |rdoc| |
|---|
| 72 | rdoc.rdoc_dir = 'doc/rdoc' |
|---|
| 73 | rdoc.options += RDOC_OPTS |
|---|
| 74 | rdoc.main = "README" |
|---|
| 75 | rdoc.rdoc_files.add ['README', 'CHANGELOG', 'COPYING', 'lib/**/*.rb'] |
|---|
| 76 | end |
|---|
| 77 | |
|---|
| 78 | Rake::GemPackageTask.new(SPEC) do |p| |
|---|
| 79 | p.need_tar = true |
|---|
| 80 | p.gem_spec = SPEC |
|---|
| 81 | end |
|---|
| 82 | |
|---|
| 83 | ['hpricot_scan', 'fast_xs'].each do |extension| |
|---|
| 84 | ext = "ext/#{extension}" |
|---|
| 85 | ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}" |
|---|
| 86 | ext_files = FileList[ |
|---|
| 87 | "#{ext}/*.c", |
|---|
| 88 | "#{ext}/*.h", |
|---|
| 89 | "#{ext}/*.rl", |
|---|
| 90 | "#{ext}/extconf.rb", |
|---|
| 91 | "#{ext}/Makefile", |
|---|
| 92 | "lib" |
|---|
| 93 | ] |
|---|
| 94 | |
|---|
| 95 | desc "Builds just the #{extension} extension" |
|---|
| 96 | task extension.to_sym => ["#{ext}/Makefile", ext_so ] |
|---|
| 97 | |
|---|
| 98 | file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do |
|---|
| 99 | Dir.chdir(ext) do ruby "extconf.rb" end |
|---|
| 100 | end |
|---|
| 101 | |
|---|
| 102 | file ext_so => ext_files do |
|---|
| 103 | Dir.chdir(ext) do |
|---|
| 104 | sh(RUBY_PLATFORM =~ /win32/ ? 'nmake' : 'make') |
|---|
| 105 | end |
|---|
| 106 | mkdir_p ARCHLIB |
|---|
| 107 | cp ext_so, ARCHLIB |
|---|
| 108 | end |
|---|
| 109 | end |
|---|
| 110 | |
|---|
| 111 | task "lib" do |
|---|
| 112 | directory "lib" |
|---|
| 113 | end |
|---|
| 114 | |
|---|
| 115 | desc "Compiles the Ruby extension" |
|---|
| 116 | task :compile => [:hpricot_scan, :fast_xs] do |
|---|
| 117 | if Dir.glob(File.join(ARCHLIB,"hpricot_scan.*")).length == 0 |
|---|
| 118 | STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" |
|---|
| 119 | STDERR.puts "Gem actually failed to build. Your system is" |
|---|
| 120 | STDERR.puts "NOT configured properly to build hpricot." |
|---|
| 121 | STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" |
|---|
| 122 | exit(1) |
|---|
| 123 | end |
|---|
| 124 | end |
|---|
| 125 | task :hpricot_scan => [:ragel] |
|---|
| 126 | |
|---|
| 127 | desc "Determines the Ragel version and displays it on the console along with the location of the Ragel binary." |
|---|
| 128 | task :ragel_version do |
|---|
| 129 | @ragel_v = `ragel -v`[/(version )(\S*)/,2].to_f |
|---|
| 130 | puts "Using ragel version: #{@ragel_v}, location: #{`which ragel`}" |
|---|
| 131 | @ragel_v |
|---|
| 132 | end |
|---|
| 133 | |
|---|
| 134 | desc "Generates the C scanner code with Ragel." |
|---|
| 135 | task :ragel => [:ragel_version] do |
|---|
| 136 | if @ragel_v >= 6.1 |
|---|
| 137 | @ragel_c_code_generation_style = RAGEL_C_CODE_GENERATION_STYLES[DEFAULT_RAGEL_C_CODE_GENERATION] |
|---|
| 138 | sh %{ragel ext/hpricot_scan/hpricot_scan.rl -#{@ragel_c_code_generation_style} -o ext/hpricot_scan/hpricot_scan.c} |
|---|
| 139 | else |
|---|
| 140 | STDERR.puts "Ragel 6.1 or greater is required." |
|---|
| 141 | exit(1) |
|---|
| 142 | end |
|---|
| 143 | end |
|---|
| 144 | |
|---|
| 145 | # Java only supports the table-driven code |
|---|
| 146 | # generation style at this point. |
|---|
| 147 | desc "Generates the Java scanner code using the Ragel table-driven code generation style." |
|---|
| 148 | task :ragel_java => [:ragel_version] do |
|---|
| 149 | if @ragel_v >= 6.1 |
|---|
| 150 | puts "compiling with ragel version #{@ragel_v}" |
|---|
| 151 | sh %{ragel -J -o ext/hpricot_scan/HpricotScanService.java ext/hpricot_scan/hpricot_scan.java.rl} |
|---|
| 152 | else |
|---|
| 153 | STDERR.puts "Ragel 6.1 or greater is required." |
|---|
| 154 | exit(1) |
|---|
| 155 | end |
|---|
| 156 | end |
|---|
| 157 | |
|---|
| 158 | ### Win32 Packages ### |
|---|
| 159 | |
|---|
| 160 | Win32Spec = SPEC.dup |
|---|
| 161 | Win32Spec.platform = Gem::Platform::CURRENT |
|---|
| 162 | Win32Spec.files = PKG_FILES + ["#{ARCHLIB}/hpricot_scan.so", "#{ARCHLIB}/fast_xs.so"] |
|---|
| 163 | Win32Spec.extensions = [] |
|---|
| 164 | |
|---|
| 165 | WIN32_PKG_DIR = "#{PKG}-mswin32" |
|---|
| 166 | |
|---|
| 167 | desc "Package up the Win32 distribution." |
|---|
| 168 | file WIN32_PKG_DIR => [:package] do |
|---|
| 169 | sh "tar zxf pkg/#{PKG}.tgz" |
|---|
| 170 | mv PKG, WIN32_PKG_DIR |
|---|
| 171 | end |
|---|
| 172 | |
|---|
| 173 | desc "Cross-compile the hpricot_scan extension for win32" |
|---|
| 174 | file "hpricot_scan_win32" => [WIN32_PKG_DIR] do |
|---|
| 175 | cp "extras/mingw-rbconfig.rb", "#{WIN32_PKG_DIR}/ext/hpricot_scan/rbconfig.rb" |
|---|
| 176 | sh "cd #{WIN32_PKG_DIR}/ext/hpricot_scan/ && ruby -I. extconf.rb && make" |
|---|
| 177 | mv "#{WIN32_PKG_DIR}/ext/hpricot_scan/hpricot_scan.so", "#{WIN32_PKG_DIR}/#{ARCHLIB}" |
|---|
| 178 | end |
|---|
| 179 | |
|---|
| 180 | desc "Build the binary RubyGems package for win32" |
|---|
| 181 | task :package_win32 => ["hpricot_scan_win32"] do |
|---|
| 182 | Dir.chdir("#{WIN32_PKG_DIR}") do |
|---|
| 183 | Gem::Builder.new(Win32Spec).build |
|---|
| 184 | verbose(true) { |
|---|
| 185 | mv Dir["*.gem"].first, "../pkg/#{WIN32_PKG_DIR}.gem" |
|---|
| 186 | } |
|---|
| 187 | end |
|---|
| 188 | end |
|---|
| 189 | |
|---|
| 190 | CLEAN.include WIN32_PKG_DIR |
|---|
| 191 | |
|---|
| 192 | ### JRuby Packages ### |
|---|
| 193 | |
|---|
| 194 | def java_classpath_arg |
|---|
| 195 | # A myriad of ways to discover the JRuby classpath |
|---|
| 196 | classpath = begin |
|---|
| 197 | require 'java' |
|---|
| 198 | # Already running in a JRuby JVM |
|---|
| 199 | Java::java.lang.System.getProperty('java.class.path') |
|---|
| 200 | rescue LoadError |
|---|
| 201 | ENV['JRUBY_PARENT_CLASSPATH'] || ENV['JRUBY_HOME'] && FileList["#{ENV['JRUBY_HOME']}/lib/*.jar"].join(File::PATH_SEPARATOR) |
|---|
| 202 | end |
|---|
| 203 | classpath ? "-cp #{classpath}" : "" |
|---|
| 204 | end |
|---|
| 205 | |
|---|
| 206 | def compile_java(filename, jarname) |
|---|
| 207 | sh %{javac -source 1.4 -target 1.4 #{java_classpath_arg} #{filename}} |
|---|
| 208 | sh %{jar cf #{jarname} *.class} |
|---|
| 209 | end |
|---|
| 210 | |
|---|
| 211 | task :hpricot_scan_java => [:ragel_java] do |
|---|
| 212 | Dir.chdir "ext/hpricot_scan" do |
|---|
| 213 | compile_java("HpricotScanService.java", "hpricot_scan.jar") |
|---|
| 214 | end |
|---|
| 215 | end |
|---|
| 216 | |
|---|
| 217 | task :fast_xs_java do |
|---|
| 218 | Dir.chdir "ext/fast_xs" do |
|---|
| 219 | compile_java("FastXsService.java", "fast_xs.jar") |
|---|
| 220 | end |
|---|
| 221 | end |
|---|
| 222 | |
|---|
| 223 | desc "Compiles the JRuby extensions" |
|---|
| 224 | task :hpricot_java => [:hpricot_scan_java, :fast_xs_java] do |
|---|
| 225 | mkdir_p "#{ARCHLIB}" |
|---|
| 226 | %w(hpricot_scan fast_xs).each {|ext| mv "ext/#{ext}/#{ext}.jar", "#{ARCHLIB}"} |
|---|
| 227 | end |
|---|
| 228 | |
|---|
| 229 | JRubySpec = SPEC.dup |
|---|
| 230 | JRubySpec.platform = 'jruby' |
|---|
| 231 | JRubySpec.files = PKG_FILES + ["#{ARCHLIB}/hpricot_scan.jar", "#{ARCHLIB}/fast_xs.jar"] |
|---|
| 232 | JRubySpec.extensions = [] |
|---|
| 233 | |
|---|
| 234 | JRUBY_PKG_DIR = "#{PKG}-jruby" |
|---|
| 235 | |
|---|
| 236 | desc "Package up the JRuby distribution." |
|---|
| 237 | file JRUBY_PKG_DIR => [:ragel_java, :package] do |
|---|
| 238 | sh "tar zxf pkg/#{PKG}.tgz" |
|---|
| 239 | mv PKG, JRUBY_PKG_DIR |
|---|
| 240 | end |
|---|
| 241 | |
|---|
| 242 | desc "Build the RubyGems package for JRuby" |
|---|
| 243 | task :package_jruby => JRUBY_PKG_DIR do |
|---|
| 244 | Dir.chdir("#{JRUBY_PKG_DIR}") do |
|---|
| 245 | Rake::Task[:hpricot_java].invoke |
|---|
| 246 | Gem::Builder.new(JRubySpec).build |
|---|
| 247 | verbose(true) { |
|---|
| 248 | mv Dir["*.gem"].first, "../pkg/#{JRUBY_PKG_DIR}.gem" |
|---|
| 249 | } |
|---|
| 250 | end |
|---|
| 251 | end |
|---|
| 252 | |
|---|
| 253 | CLEAN.include JRUBY_PKG_DIR |
|---|
| 254 | |
|---|
| 255 | task :install do |
|---|
| 256 | sh %{rake package} |
|---|
| 257 | sh %{sudo gem install pkg/#{NAME}-#{VERS}} |
|---|
| 258 | end |
|---|
| 259 | |
|---|
| 260 | task :uninstall => [:clean] do |
|---|
| 261 | sh %{sudo gem uninstall #{NAME}} |
|---|
| 262 | end |
|---|