| 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 | NAME = "hpricot" |
|---|
| 10 | REV = `svn info`[/Revision: (\d+)/, 1] rescue nil |
|---|
| 11 | VERS = ENV['VERSION'] || "0.6" + (REV ? ".#{REV}" : "") |
|---|
| 12 | PKG = "#{NAME}-#{VERS}" |
|---|
| 13 | BIN = "*.{bundle,jar,so,obj,pdb,lib,def,exp}" |
|---|
| 14 | ARCHLIB = "lib/#{::Config::CONFIG['arch']}" |
|---|
| 15 | CLEAN.include ["ext/hpricot_scan/#{BIN}", "lib/**/#{BIN}", 'ext/hpricot_scan/Makefile', |
|---|
| 16 | '**/.*.sw?', '*.gem', '.config'] |
|---|
| 17 | RDOC_OPTS = ['--quiet', '--title', 'The Hpricot Reference', '--main', 'README', '--inline-source'] |
|---|
| 18 | PKG_FILES = %w(CHANGELOG COPYING README Rakefile) + |
|---|
| 19 | Dir.glob("{bin,doc,test,lib,extras}/**/*") + |
|---|
| 20 | Dir.glob("ext/**/*.{h,java,c,rb,rl}") + |
|---|
| 21 | %w[ext/hpricot_scan/hpricot_scan.c] # needed because it's generated later |
|---|
| 22 | SPEC = |
|---|
| 23 | Gem::Specification.new do |s| |
|---|
| 24 | s.name = NAME |
|---|
| 25 | s.version = VERS |
|---|
| 26 | s.platform = Gem::Platform::RUBY |
|---|
| 27 | s.has_rdoc = true |
|---|
| 28 | s.rdoc_options += RDOC_OPTS |
|---|
| 29 | s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"] |
|---|
| 30 | s.summary = "a swift, liberal HTML parser with a fantastic library" |
|---|
| 31 | s.description = s.summary |
|---|
| 32 | s.author = "why the lucky stiff" |
|---|
| 33 | s.email = 'why@ruby-lang.org' |
|---|
| 34 | s.homepage = 'http://code.whytheluckystiff.net/hpricot/' |
|---|
| 35 | s.files = PKG_FILES |
|---|
| 36 | s.require_paths = [ARCHLIB, "lib"] |
|---|
| 37 | s.extensions = FileList["ext/**/extconf.rb"].to_a |
|---|
| 38 | s.bindir = "bin" |
|---|
| 39 | end |
|---|
| 40 | |
|---|
| 41 | desc "Does a full compile, test run" |
|---|
| 42 | task :default => [:compile, :test] |
|---|
| 43 | |
|---|
| 44 | desc "Packages up Hpricot." |
|---|
| 45 | task :package => [:clean, :ragel] |
|---|
| 46 | |
|---|
| 47 | desc "Releases packages for all Hpricot packages and platforms." |
|---|
| 48 | task :release => [:package, :package_win32, :package_jruby] |
|---|
| 49 | |
|---|
| 50 | desc "Run all the tests" |
|---|
| 51 | Rake::TestTask.new do |t| |
|---|
| 52 | t.libs << "test" << ARCHLIB |
|---|
| 53 | t.test_files = FileList['test/test_*.rb'] |
|---|
| 54 | t.verbose = true |
|---|
| 55 | end |
|---|
| 56 | |
|---|
| 57 | Rake::RDocTask.new do |rdoc| |
|---|
| 58 | rdoc.rdoc_dir = 'doc/rdoc' |
|---|
| 59 | rdoc.options += RDOC_OPTS |
|---|
| 60 | rdoc.main = "README" |
|---|
| 61 | rdoc.rdoc_files.add ['README', 'CHANGELOG', 'COPYING', 'lib/**/*.rb'] |
|---|
| 62 | end |
|---|
| 63 | |
|---|
| 64 | Rake::GemPackageTask.new(SPEC) do |p| |
|---|
| 65 | p.need_tar = true |
|---|
| 66 | p.gem_spec = SPEC |
|---|
| 67 | end |
|---|
| 68 | |
|---|
| 69 | extension = "hpricot_scan" |
|---|
| 70 | ext = "ext/hpricot_scan" |
|---|
| 71 | ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}" |
|---|
| 72 | ext_files = FileList[ |
|---|
| 73 | "#{ext}/*.c", |
|---|
| 74 | "#{ext}/*.h", |
|---|
| 75 | "#{ext}/*.rl", |
|---|
| 76 | "#{ext}/extconf.rb", |
|---|
| 77 | "#{ext}/Makefile", |
|---|
| 78 | "lib" |
|---|
| 79 | ] |
|---|
| 80 | |
|---|
| 81 | task "lib" do |
|---|
| 82 | directory "lib" |
|---|
| 83 | end |
|---|
| 84 | |
|---|
| 85 | desc "Compiles the Ruby extension" |
|---|
| 86 | task :compile => [:hpricot_scan] do |
|---|
| 87 | if Dir.glob(File.join(ARCHLIB,"hpricot_scan.*")).length == 0 |
|---|
| 88 | STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" |
|---|
| 89 | STDERR.puts "Gem actually failed to build. Your system is" |
|---|
| 90 | STDERR.puts "NOT configured properly to build hpricot." |
|---|
| 91 | STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" |
|---|
| 92 | exit(1) |
|---|
| 93 | end |
|---|
| 94 | end |
|---|
| 95 | task :hpricot_scan => [:ragel] |
|---|
| 96 | |
|---|
| 97 | desc "Builds just the #{extension} extension" |
|---|
| 98 | task extension.to_sym => ["#{ext}/Makefile", ext_so ] |
|---|
| 99 | |
|---|
| 100 | file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do |
|---|
| 101 | Dir.chdir(ext) do ruby "extconf.rb" end |
|---|
| 102 | end |
|---|
| 103 | |
|---|
| 104 | file ext_so => ext_files do |
|---|
| 105 | Dir.chdir(ext) do |
|---|
| 106 | sh(PLATFORM =~ /win32/ ? 'nmake' : 'make') |
|---|
| 107 | end |
|---|
| 108 | mkdir_p ARCHLIB |
|---|
| 109 | cp ext_so, ARCHLIB |
|---|
| 110 | end |
|---|
| 111 | |
|---|
| 112 | desc "returns the ragel version" |
|---|
| 113 | task :ragel_version do |
|---|
| 114 | @ragel_v = `ragel -v`[/(version )(\S*)/,2].to_f |
|---|
| 115 | end |
|---|
| 116 | |
|---|
| 117 | desc "Generates the C scanner code with Ragel." |
|---|
| 118 | task :ragel => [:ragel_version] do |
|---|
| 119 | sh %{ragel ext/hpricot_scan/hpricot_scan.rl | #{@ragel_v >= 5.18 ? 'rlgen-cd' : 'rlcodegen'} -G2 -o ext/hpricot_scan/hpricot_scan.c} |
|---|
| 120 | end |
|---|
| 121 | |
|---|
| 122 | desc "Generates the Java scanner code with Ragel." |
|---|
| 123 | task :ragel_java => [:ragel_version] do |
|---|
| 124 | sh %{ragel -J ext/hpricot_scan/hpricot_scan.java.rl | #{@ragel_v >= 5.18 ? 'rlgen-java' : 'rlcodegen'} -o ext/hpricot_scan/HpricotScanService.java} |
|---|
| 125 | end |
|---|
| 126 | |
|---|
| 127 | ### Win32 Packages ### |
|---|
| 128 | |
|---|
| 129 | Win32Spec = SPEC.dup |
|---|
| 130 | Win32Spec.platform = Gem::Platform::WIN32 |
|---|
| 131 | Win32Spec.files = PKG_FILES + ["#{ARCHLIB}/hpricot_scan.so"] |
|---|
| 132 | Win32Spec.extensions = [] |
|---|
| 133 | |
|---|
| 134 | WIN32_PKG_DIR = "#{PKG}-mswin32" |
|---|
| 135 | |
|---|
| 136 | desc "Package up the Win32 distribution." |
|---|
| 137 | file WIN32_PKG_DIR => [:package] do |
|---|
| 138 | sh "tar zxf pkg/#{PKG}.tgz" |
|---|
| 139 | mv PKG, WIN32_PKG_DIR |
|---|
| 140 | end |
|---|
| 141 | |
|---|
| 142 | desc "Cross-compile the hpricot_scan extension for win32" |
|---|
| 143 | file "hpricot_scan_win32" => [WIN32_PKG_DIR] do |
|---|
| 144 | cp "extras/mingw-rbconfig.rb", "#{WIN32_PKG_DIR}/ext/hpricot_scan/rbconfig.rb" |
|---|
| 145 | sh "cd #{WIN32_PKG_DIR}/ext/hpricot_scan/ && ruby -I. extconf.rb && make" |
|---|
| 146 | mv "#{WIN32_PKG_DIR}/ext/hpricot_scan/hpricot_scan.so", "#{WIN32_PKG_DIR}/#{ARCHLIB}" |
|---|
| 147 | end |
|---|
| 148 | |
|---|
| 149 | desc "Build the binary RubyGems package for win32" |
|---|
| 150 | task :package_win32 => ["hpricot_scan_win32"] do |
|---|
| 151 | Dir.chdir("#{WIN32_PKG_DIR}") do |
|---|
| 152 | Gem::Builder.new(Win32Spec).build |
|---|
| 153 | verbose(true) { |
|---|
| 154 | mv Dir["*.gem"].first, "../pkg/#{WIN32_PKG_DIR}.gem" |
|---|
| 155 | } |
|---|
| 156 | end |
|---|
| 157 | end |
|---|
| 158 | |
|---|
| 159 | CLEAN.include WIN32_PKG_DIR |
|---|
| 160 | |
|---|
| 161 | ### JRuby Packages ### |
|---|
| 162 | |
|---|
| 163 | compile_java = proc do |
|---|
| 164 | sh %{javac -source 1.4 -target 1.4 -classpath $JRUBY_HOME/lib/jruby.jar HpricotScanService.java} |
|---|
| 165 | sh %{jar cf hpricot_scan.jar HpricotScanService.class} |
|---|
| 166 | end |
|---|
| 167 | |
|---|
| 168 | desc "Compiles the JRuby extension" |
|---|
| 169 | task :hpricot_scan_java => [:ragel_java] do |
|---|
| 170 | Dir.chdir("ext/hpricot_scan", &compile_java) |
|---|
| 171 | end |
|---|
| 172 | |
|---|
| 173 | JRubySpec = SPEC.dup |
|---|
| 174 | JRubySpec.platform = 'jruby' |
|---|
| 175 | JRubySpec.files = PKG_FILES + ["#{ARCHLIB}/hpricot_scan.jar"] |
|---|
| 176 | JRubySpec.extensions = [] |
|---|
| 177 | |
|---|
| 178 | JRUBY_PKG_DIR = "#{PKG}-jruby" |
|---|
| 179 | |
|---|
| 180 | desc "Package up the JRuby distribution." |
|---|
| 181 | file JRUBY_PKG_DIR => [:ragel_java, :package] do |
|---|
| 182 | sh "tar zxf pkg/#{PKG}.tgz" |
|---|
| 183 | mv PKG, JRUBY_PKG_DIR |
|---|
| 184 | end |
|---|
| 185 | |
|---|
| 186 | desc "Cross-compile the hpricot_scan extension for JRuby" |
|---|
| 187 | file "hpricot_scan_jruby" => [JRUBY_PKG_DIR] do |
|---|
| 188 | Dir.chdir("#{JRUBY_PKG_DIR}/ext/hpricot_scan", &compile_java) |
|---|
| 189 | mv "#{JRUBY_PKG_DIR}/ext/hpricot_scan/hpricot_scan.jar", "#{JRUBY_PKG_DIR}/#{ARCHLIB}" |
|---|
| 190 | end |
|---|
| 191 | |
|---|
| 192 | desc "Build the RubyGems package for JRuby" |
|---|
| 193 | task :package_jruby => ["hpricot_scan_jruby"] do |
|---|
| 194 | Dir.chdir("#{JRUBY_PKG_DIR}") do |
|---|
| 195 | Gem::Builder.new(JRubySpec).build |
|---|
| 196 | verbose(true) { |
|---|
| 197 | mv Dir["*.gem"].first, "../pkg/#{JRUBY_PKG_DIR}.gem" |
|---|
| 198 | } |
|---|
| 199 | end |
|---|
| 200 | end |
|---|
| 201 | |
|---|
| 202 | CLEAN.include JRUBY_PKG_DIR |
|---|
| 203 | |
|---|
| 204 | task :install do |
|---|
| 205 | sh %{rake package} |
|---|
| 206 | sh %{sudo gem install pkg/#{NAME}-#{VERS}} |
|---|
| 207 | end |
|---|
| 208 | |
|---|
| 209 | task :uninstall => [:clean] do |
|---|
| 210 | sh %{sudo gem uninstall #{NAME}} |
|---|
| 211 | end |
|---|