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`[/Revision: (\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}"
16 ARCHLIB = "lib/#{::RbConfig::CONFIG['arch']}"
17
18
19
20
21
22
23
24 p ARCHLIB
25 CLEAN.include ["ext/hpricot_scan/#{BIN}", "lib/**/#{BIN}", 'ext/hpricot_scan/Makefile',
26 '**/.*.sw?', '*.gem', '.config']
27 RDOC_OPTS = ['--quiet', '--title', 'The Hpricot Reference', '--main', 'README', '--inline-source']
28 PKG_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]
32 SPEC =
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
51 desc "Does a full compile, test run"
52 task :default => [:compile, :test]
53
54 desc "Packages up Hpricot."
55 task :package => [:clean, :ragel]
56
57 desc "Releases packages for all Hpricot packages and platforms."
58 task :release => [:package, :package_win32, :package_jruby]
59
60 desc "Run all the tests"
61 Rake::TestTask.new do |t|
62 t.libs << "test" << ARCHLIB
63 t.test_files = FileList['test/test_*.rb']
64 t.verbose = true
65 end
66
67 Rake::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']
72 end
73
74 Rake::GemPackageTask.new(SPEC) do |p|
75 p.need_tar = true
76 p.gem_spec = SPEC
77 end
78
79 extension = "hpricot_scan"
80 ext = "ext/hpricot_scan"
81 ext_so = "#{ext}/#{extension}.#{RbConfig::CONFIG['DLEXT']}"
82 ext_files = FileList[
83 "#{ext}/*.c",
84 "#{ext}/*.h",
85 "#{ext}/*.rl",
86 "#{ext}/extconf.rb",
87 "#{ext}/Makefile",
88 "lib"
89 ]
90
91 task "lib" do
92 directory "lib"
93 end
94
95 desc "Compiles the Ruby extension"
96 task :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
104 end
105 task :hpricot_scan => [:ragel]
106
107 desc "Builds just the #{extension} extension"
108 task extension.to_sym => ["#{ext}/Makefile", ext_so ]
109
110 file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
111 Dir.chdir(ext) do ruby "extconf.rb" end
112 end
113
114 file 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
120 end
121
122 desc "returns the ragel version"
123 task :ragel_version do
124 @ragel_v = `ragel -v`[/(version )(\S*)/,2].to_f
125 end
126
127 desc "Generates the C scanner code with Ragel."
128 task :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
138 end
139
140 desc "Generates the Java scanner code with Ragel."
141 task :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}
143 end
144
145
146
147 Win32Spec = SPEC.dup
148 Win32Spec.platform = Gem::Platform::CURRENT
149 Win32Spec.files = PKG_FILES + ["#{ARCHLIB}/hpricot_scan.so"]
150 Win32Spec.extensions = []
151
152 WIN32_PKG_DIR = "#{PKG}-mswin32"
153
154 desc "Package up the Win32 distribution."
155 file WIN32_PKG_DIR => [:package] do
156 sh "tar zxf pkg/#{PKG}.tgz"
157 mv PKG, WIN32_PKG_DIR
158 end
159
160 desc "Cross-compile the hpricot_scan extension for win32"
161 file "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}"
165 end
166
167 desc "Build the binary RubyGems package for win32"
168 task :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
175 end
176
177 CLEAN.include WIN32_PKG_DIR
178
179
180
181 compile_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}
184 end
185
186 desc "Compiles the JRuby extension"
187 task :hpricot_scan_java => [:ragel_java] do
188 Dir.chdir("ext/hpricot_scan", &compile_java)
189 end
190
191 JRubySpec = SPEC.dup
192 JRubySpec.platform = 'jruby'
193 JRubySpec.files = PKG_FILES + ["#{ARCHLIB}/hpricot_scan.jar"]
194 JRubySpec.extensions = []
195
196 JRUBY_PKG_DIR = "#{PKG}-jruby"
197
198 desc "Package up the JRuby distribution."
199 file JRUBY_PKG_DIR => [:ragel_java, :package] do
200 sh "tar zxf pkg/#{PKG}.tgz"
201 mv PKG, JRUBY_PKG_DIR
202 end
203
204 desc "Cross-compile the hpricot_scan extension for JRuby"
205 file "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}"
208 end
209
210 desc "Build the RubyGems package for JRuby"
211 task :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
218 end
219
220 CLEAN.include JRUBY_PKG_DIR
221
222 task :install do
223 sh %{rake package}
224 sh %{sudo gem install pkg/#{NAME}-#{VERS}}
225 end
226
227 task :uninstall => [:clean] do
228 sh %{sudo gem uninstall #{NAME}}
229 end