Rakefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. begin
  2. require 'rubygems/package_task'
  3. rescue LoadError
  4. end
  5. require 'rbconfig'
  6. include\
  7. begin
  8. RbConfig
  9. rescue NameError
  10. Config
  11. end
  12. require 'rake/clean'
  13. CLOBBER.include 'doc', 'Gemfile.lock'
  14. CLEAN.include FileList['diagrams/*.*'], 'doc', 'coverage', 'tmp',
  15. FileList["ext/**/{Makefile,mkmf.log}"], 'build', 'dist', FileList['**/*.rbc'],
  16. FileList["{ext,lib}/**/*.{so,bundle,#{CONFIG['DLEXT']},o,obj,pdb,lib,manifest,exp,def,jar,class,dSYM}"],
  17. FileList['java/src/**/*.class']
  18. require 'rake/testtask'
  19. class UndocumentedTestTask < Rake::TestTask
  20. def desc(*) end
  21. end
  22. MAKE = ENV['MAKE'] || %w[gmake make].find { |c| system(c, '-v') }
  23. BUNDLE = ENV['BUNDLE'] || %w[bundle].find { |c| system(c, '-v') }
  24. PKG_NAME = 'json'
  25. PKG_TITLE = 'JSON Implementation for Ruby'
  26. PKG_VERSION = File.read('VERSION').chomp
  27. PKG_FILES = FileList[`git ls-files`.split(/\n/)]
  28. EXT_ROOT_DIR = 'ext/json/ext'
  29. EXT_PARSER_DIR = "#{EXT_ROOT_DIR}/parser"
  30. EXT_PARSER_DL = "#{EXT_PARSER_DIR}/parser.#{CONFIG['DLEXT']}"
  31. RAGEL_PATH = "#{EXT_PARSER_DIR}/parser.rl"
  32. EXT_PARSER_SRC = "#{EXT_PARSER_DIR}/parser.c"
  33. EXT_GENERATOR_DIR = "#{EXT_ROOT_DIR}/generator"
  34. EXT_GENERATOR_DL = "#{EXT_GENERATOR_DIR}/generator.#{CONFIG['DLEXT']}"
  35. EXT_GENERATOR_SRC = "#{EXT_GENERATOR_DIR}/generator.c"
  36. JAVA_DIR = "java/src/json/ext"
  37. JAVA_RAGEL_PATH = "#{JAVA_DIR}/Parser.rl"
  38. JAVA_PARSER_SRC = "#{JAVA_DIR}/Parser.java"
  39. JAVA_SOURCES = FileList["#{JAVA_DIR}/*.java"]
  40. JAVA_CLASSES = []
  41. JRUBY_PARSER_JAR = File.expand_path("lib/json/ext/parser.jar")
  42. JRUBY_GENERATOR_JAR = File.expand_path("lib/json/ext/generator.jar")
  43. RAGEL_CODEGEN = %w[rlcodegen rlgen-cd ragel].find { |c| system(c, '-v') }
  44. RAGEL_DOTGEN = %w[rlgen-dot rlgen-cd ragel].find { |c| system(c, '-v') }
  45. desc "Installing library (pure)"
  46. task :install_pure => :version do
  47. ruby 'install.rb'
  48. end
  49. task :install_ext_really do
  50. sitearchdir = CONFIG["sitearchdir"]
  51. cd 'ext' do
  52. for file in Dir["json/ext/*.#{CONFIG['DLEXT']}"]
  53. d = File.join(sitearchdir, file)
  54. mkdir_p File.dirname(d)
  55. install(file, d)
  56. end
  57. warn " *** Installed EXT ruby library."
  58. end
  59. end
  60. desc "Installing library (extension)"
  61. task :install_ext => [ :compile, :install_pure, :install_ext_really ]
  62. desc "Installing library (extension)"
  63. task :install => :install_ext
  64. if defined?(Gem) and defined?(Gem::PackageTask)
  65. spec_pure = Gem::Specification.new do |s|
  66. s.name = 'json_pure'
  67. s.version = PKG_VERSION
  68. s.summary = PKG_TITLE
  69. s.description = "This is a JSON implementation in pure Ruby."
  70. s.files = PKG_FILES
  71. s.require_path = 'lib'
  72. s.add_development_dependency 'permutation'
  73. s.add_development_dependency 'sdoc'
  74. s.add_development_dependency 'rake', '~>0.9.2'
  75. s.extra_rdoc_files << 'README.rdoc'
  76. s.rdoc_options <<
  77. '--title' << 'JSON implemention for ruby' << '--main' << 'README.rdoc'
  78. s.test_files.concat Dir['./tests/test_*.rb']
  79. s.author = "Florian Frank"
  80. s.email = "flori@ping.de"
  81. s.homepage = "http://flori.github.com/#{PKG_NAME}"
  82. s.rubyforge_project = "json"
  83. end
  84. desc 'Creates a json_pure.gemspec file'
  85. task :gemspec_pure => :version do
  86. File.open('json_pure.gemspec', 'w') do |gemspec|
  87. gemspec.write spec_pure.to_ruby
  88. end
  89. end
  90. Gem::PackageTask.new(spec_pure) do |pkg|
  91. pkg.need_tar = true
  92. pkg.package_files = PKG_FILES
  93. end
  94. spec_ext = Gem::Specification.new do |s|
  95. s.name = 'json'
  96. s.version = PKG_VERSION
  97. s.summary = PKG_TITLE
  98. s.description = "This is a JSON implementation as a Ruby extension in C."
  99. s.files = PKG_FILES
  100. s.extensions = FileList['ext/**/extconf.rb']
  101. s.require_path = 'lib'
  102. s.add_development_dependency 'permutation'
  103. s.add_development_dependency 'sdoc'
  104. s.extra_rdoc_files << 'README.rdoc'
  105. s.rdoc_options <<
  106. '--title' << 'JSON implemention for Ruby' << '--main' << 'README.rdoc'
  107. s.test_files.concat Dir['./tests/test_*.rb']
  108. s.author = "Florian Frank"
  109. s.email = "flori@ping.de"
  110. s.homepage = "http://flori.github.com/#{PKG_NAME}"
  111. s.rubyforge_project = "json"
  112. end
  113. desc 'Creates a json.gemspec file'
  114. task :gemspec_ext => :version do
  115. File.open('json.gemspec', 'w') do |gemspec|
  116. gemspec.write spec_ext.to_ruby
  117. end
  118. end
  119. Gem::PackageTask.new(spec_ext) do |pkg|
  120. pkg.need_tar = true
  121. pkg.package_files = PKG_FILES
  122. end
  123. desc 'Create all gemspec files'
  124. task :gemspec => [ :gemspec_pure, :gemspec_ext ]
  125. end
  126. desc m = "Writing version information for #{PKG_VERSION}"
  127. task :version do
  128. puts m
  129. File.open(File.join('lib', 'json', 'version.rb'), 'w') do |v|
  130. v.puts <<EOT
  131. module JSON
  132. # JSON version
  133. VERSION = '#{PKG_VERSION}'
  134. VERSION_ARRAY = VERSION.split(/\\./).map { |x| x.to_i } # :nodoc:
  135. VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
  136. VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
  137. VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
  138. end
  139. EOT
  140. end
  141. end
  142. desc "Testing library (pure ruby)"
  143. task :test_pure => [ :clean, :do_test_pure ]
  144. UndocumentedTestTask.new do |t|
  145. t.name = 'do_test_pure'
  146. t.libs << 'lib'
  147. t.test_files = FileList['tests/test_*.rb']
  148. t.verbose = true
  149. t.options = '-v'
  150. end
  151. desc "Testing library (pure ruby and extension)"
  152. task :test do
  153. sh "env JSON=pure #{BUNDLE} exec rake test_pure" or exit 1
  154. sh "env JSON=ext #{BUNDLE} exec rake test_ext" or exit 1
  155. end
  156. namespace :gems do
  157. desc 'Install all development gems'
  158. task :install do
  159. sh "#{BUNDLE}"
  160. end
  161. end
  162. if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
  163. if ENV.key?('JAVA_HOME')
  164. warn " *** JAVA_HOME was set to #{ENV['JAVA_HOME'].inspect}"
  165. else File.directory?(local_java = '/usr/local/java/jdk')
  166. ENV['JAVA_HOME'] = local_java
  167. warn " *** JAVA_HOME is set to #{ENV['JAVA_HOME'].inspect}"
  168. ENV['PATH'] = ENV['PATH'].split(/:/).unshift(java_path = "#{ENV['JAVA_HOME']}/bin") * ':'
  169. warn " *** java binaries are assumed to be in #{java_path.inspect}"
  170. end
  171. file JAVA_PARSER_SRC => JAVA_RAGEL_PATH do
  172. cd JAVA_DIR do
  173. if RAGEL_CODEGEN == 'ragel'
  174. sh "ragel Parser.rl -J -o Parser.java"
  175. else
  176. sh "ragel -x Parser.rl | #{RAGEL_CODEGEN} -J"
  177. end
  178. end
  179. end
  180. desc "Generate parser for java with ragel"
  181. task :ragel => JAVA_PARSER_SRC
  182. desc "Delete the ragel generated Java source"
  183. task :ragel_clean do
  184. rm_rf JAVA_PARSER_SRC
  185. end
  186. JRUBY_JAR = File.join(CONFIG["libdir"], "jruby.jar")
  187. if File.exist?(JRUBY_JAR)
  188. JAVA_SOURCES.each do |src|
  189. classpath = (Dir['java/lib/*.jar'] << 'java/src' << JRUBY_JAR) * ':'
  190. obj = src.sub(/\.java\Z/, '.class')
  191. file obj => src do
  192. sh 'javac', '-classpath', classpath, '-source', '1.5', '-target', '1.5', src
  193. end
  194. JAVA_CLASSES << obj
  195. end
  196. else
  197. warn "WARNING: Cannot find jruby in path => Cannot build jruby extension!"
  198. end
  199. desc "Compiling jruby extension"
  200. task :compile => JAVA_CLASSES
  201. desc "Package the jruby gem"
  202. task :jruby_gem => :create_jar do
  203. sh 'gem build json-java.gemspec'
  204. mkdir_p 'pkg'
  205. mv "json-#{PKG_VERSION}-java.gem", 'pkg'
  206. end
  207. desc "Testing library (jruby)"
  208. task :test_ext => [ :create_jar, :do_test_ext ]
  209. UndocumentedTestTask.new do |t|
  210. t.name = 'do_test_ext'
  211. t.libs << 'lib'
  212. t.test_files = FileList['tests/test_*.rb']
  213. t.verbose = true
  214. t.options = '-v'
  215. end
  216. file JRUBY_PARSER_JAR => :compile do
  217. cd 'java/src' do
  218. parser_classes = FileList[
  219. "json/ext/ByteListTranscoder*.class",
  220. "json/ext/OptionsReader*.class",
  221. "json/ext/Parser*.class",
  222. "json/ext/RuntimeInfo*.class",
  223. "json/ext/StringDecoder*.class",
  224. "json/ext/Utils*.class"
  225. ]
  226. sh 'jar', 'cf', File.basename(JRUBY_PARSER_JAR), *parser_classes
  227. mv File.basename(JRUBY_PARSER_JAR), File.dirname(JRUBY_PARSER_JAR)
  228. end
  229. end
  230. desc "Create parser jar"
  231. task :create_parser_jar => JRUBY_PARSER_JAR
  232. file JRUBY_GENERATOR_JAR => :compile do
  233. cd 'java/src' do
  234. generator_classes = FileList[
  235. "json/ext/ByteListTranscoder*.class",
  236. "json/ext/OptionsReader*.class",
  237. "json/ext/Generator*.class",
  238. "json/ext/RuntimeInfo*.class",
  239. "json/ext/StringEncoder*.class",
  240. "json/ext/Utils*.class"
  241. ]
  242. sh 'jar', 'cf', File.basename(JRUBY_GENERATOR_JAR), *generator_classes
  243. mv File.basename(JRUBY_GENERATOR_JAR), File.dirname(JRUBY_GENERATOR_JAR)
  244. end
  245. end
  246. desc "Create generator jar"
  247. task :create_generator_jar => JRUBY_GENERATOR_JAR
  248. desc "Create parser and generator jars"
  249. task :create_jar => [ :create_parser_jar, :create_generator_jar ]
  250. desc "Build all gems and archives for a new release of the jruby extension."
  251. task :build => [ :clean, :version, :jruby_gem ]
  252. task :release => :build
  253. else
  254. desc "Compiling extension"
  255. task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ]
  256. file EXT_PARSER_DL => EXT_PARSER_SRC do
  257. cd EXT_PARSER_DIR do
  258. ruby 'extconf.rb'
  259. sh MAKE
  260. end
  261. cp "#{EXT_PARSER_DIR}/parser.#{CONFIG['DLEXT']}", EXT_ROOT_DIR
  262. end
  263. file EXT_GENERATOR_DL => EXT_GENERATOR_SRC do
  264. cd EXT_GENERATOR_DIR do
  265. ruby 'extconf.rb'
  266. sh MAKE
  267. end
  268. cp "#{EXT_GENERATOR_DIR}/generator.#{CONFIG['DLEXT']}", EXT_ROOT_DIR
  269. end
  270. desc "Testing library (extension)"
  271. task :test_ext => [ :compile, :do_test_ext ]
  272. UndocumentedTestTask.new do |t|
  273. t.name = 'do_test_ext'
  274. t.libs << 'ext' << 'lib'
  275. t.test_files = FileList['tests/test_*.rb']
  276. t.verbose = true
  277. t.options = '-v'
  278. end
  279. desc "Create RDOC documentation"
  280. task :doc => [ :version, EXT_PARSER_SRC ] do
  281. sh "sdoc -o doc -t '#{PKG_TITLE}' -m README.rdoc README.rdoc lib/json.rb #{FileList['lib/json/**/*.rb']} #{EXT_PARSER_SRC} #{EXT_GENERATOR_SRC}"
  282. end
  283. desc "Generate parser with ragel"
  284. task :ragel => EXT_PARSER_SRC
  285. desc "Delete the ragel generated C source"
  286. task :ragel_clean do
  287. rm_rf EXT_PARSER_SRC
  288. end
  289. desc "Update the tags file"
  290. task :tags do
  291. system 'ctags', *Dir['**/*.{rb,c,h,java}']
  292. end
  293. file EXT_PARSER_SRC => RAGEL_PATH do
  294. cd EXT_PARSER_DIR do
  295. if RAGEL_CODEGEN == 'ragel'
  296. sh "ragel parser.rl -G2 -o parser.c"
  297. else
  298. sh "ragel -x parser.rl | #{RAGEL_CODEGEN} -G2"
  299. end
  300. src = File.read("parser.c").gsub(/[ \t]+$/, '')
  301. File.open("parser.c", "w") {|f| f.print src}
  302. end
  303. end
  304. desc "Generate diagrams of ragel parser (ps)"
  305. task :ragel_dot_ps do
  306. root = 'diagrams'
  307. specs = []
  308. File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
  309. for s in specs
  310. if RAGEL_DOTGEN == 'ragel'
  311. sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tps -o#{root}/#{s}.ps"
  312. else
  313. sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tps -o#{root}/#{s}.ps"
  314. end
  315. end
  316. end
  317. desc "Generate diagrams of ragel parser (png)"
  318. task :ragel_dot_png do
  319. root = 'diagrams'
  320. specs = []
  321. File.new(RAGEL_PATH).grep(/^\s*machine\s*(\S+);\s*$/) { specs << $1 }
  322. for s in specs
  323. if RAGEL_DOTGEN == 'ragel'
  324. sh "ragel #{RAGEL_PATH} -S#{s} -p -V | dot -Tpng -o#{root}/#{s}.png"
  325. else
  326. sh "ragel -x #{RAGEL_PATH} -S#{s} | #{RAGEL_DOTGEN} -p|dot -Tpng -o#{root}/#{s}.png"
  327. end
  328. end
  329. end
  330. desc "Generate diagrams of ragel parser"
  331. task :ragel_dot => [ :ragel_dot_png, :ragel_dot_ps ]
  332. desc "Build all gems and archives for a new release of json and json_pure."
  333. task :build => [ :clean, :gemspec, :package ]
  334. task :release => :build
  335. end
  336. desc "Compile in the the source directory"
  337. task :default => [ :clean, :gemspec, :test ]