Ticket #132: Rakefile

File Rakefile, 6.4 kB (added by manveru, 10 months ago)

Rakefile

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