[NEW] Version 2.0.1 - Internal support for LOC delta between two source files
[ohcount] / Rakefile
1 require 'rake'
2 require 'rake/clean'
3 require 'rake/gempackagetask'
4 require 'rake/rdoctask'
5 require 'rake/testtask'
6 require 'rbconfig'
7 include Config
8
9 NAME = 'ohcount'
10 VERS = '2.0.1'
11
12 EXT_DIR  = "ext/ohcount_native"
13 EXT_DL   = "#{EXT_DIR}/ohcount_native.#{CONFIG['DLEXT']}"
14 ARCH_DIR = "lib/#{::Config::CONFIG['arch']}"
15 ARCH_DL  = "#{ARCH_DIR}/ohcount_native.#{CONFIG['DLEXT']}"
16
17 RAGEL_DIR = File.join(EXT_DIR, 'ragel_parsers')
18
19 CLEAN.include FileList["#{EXT_DIR}/*.{so,bundle,#{CONFIG['DLEXT']}}"],
20                                                   FileList["#{EXT_DIR}/*.o"],
21                                                   FileList["#{EXT_DIR}/Makefile"],
22                                                   FileList["#{RAGEL_DIR}/*.h"]
23                                                   FileList["#{RAGEL_DIR}/*.tmp"]
24
25 RDOC_OPTS = ['--quiet', '--title', 'Ohcount Reference', '--main', 'README', '--inline-source']
26
27 Rake::RDocTask.new do |rdoc|
28         rdoc.rdoc_dir = 'doc'
29         rdoc.options += RDOC_OPTS
30         rdoc.rdoc_files.add ['README' ,'COPYING', 'lib/**/*.rb', 'ext/**/*.rb', 'ext/**/*.c', 'test/test_helper.rb', 'test/unit/detector_test.rb']
31 end
32
33 PKG_FILES = %w(README COPYING Rakefile lib/ohcount.rb) +
34         Dir.glob("#{EXT_DIR}/*.{h,c,rb}") +
35         Dir.glob("#{RAGEL_DIR}/*.h") +
36         Dir.glob("#{RAGEL_DIR}/*.rl") +
37         Dir.glob("lib/**/*.rb") +
38         Dir.glob("bin/*")
39
40 SPEC =
41         Gem::Specification.new do |s|
42                 s.name = NAME
43                 s.version = VERS
44                 s.platform = Gem::Platform::RUBY
45                 s.has_rdoc = true
46                 s.rdoc_options = RDOC_OPTS
47                 s.extra_rdoc_files = ['README']
48                 s.summary = "The Ohloh source code line counter"
49                 s.description = s.summary
50                 s.author = "Ohloh Corporation"
51                 s.email = "info@ohloh.net"
52                 s.homepage = "http://labs.ohloh.net/ohcount"
53                 s.files = PKG_FILES.to_a
54                 s.require_paths = ['lib']
55                 s.extensions << 'ext/ohcount_native/extconf.rb'
56                 s.bindir = 'bin'
57                 s.executables = ['ohcount']
58                 s.add_dependency 'diff-lcs'
59         end
60
61 Rake::GemPackageTask.new(SPEC) do |p|
62         p.need_tar = true
63         p.gem_spec = SPEC
64 end
65
66 task :install => :package do
67         `sudo gem install pkg/#{NAME}-#{VERS}`
68 end
69
70 file ARCH_DL => EXT_DL do
71         mkdir_p ARCH_DIR
72         cp EXT_DL, ARCH_DIR
73 end
74
75 rule ".h" => ".rl" do |t|
76         if has_embedded_language?(t.source)
77                 construct_language(t.source) do |constructed_file|
78                         sh "ragel #{constructed_file} -o #{t.name}"
79                 end
80         else
81                 sh "ragel #{t.source} -o #{t.name}"
82         end
83 end
84
85 def has_embedded_language?(parser_file)
86   return IO.read(parser_file).include?('#EMBED')
87 end
88
89 def construct_language(parser_file)
90   parser_text = IO.read(parser_file).gsub(/#EMBED\([\w_]+\)/) do |elang|
91     lang = elang.scan(/^#EMBED\(([\w_]+)\)/)[0][0]
92     eparser_file = File.join(RAGEL_DIR, lang + '.rl')
93     if File.exists?(eparser_file)
94       eparser = IO.read(eparser_file)
95       ragel = eparser.scan(/%%\{(.+?)\}%%/m)[0][0]
96       # eliminate machine definition, writes, and includes
97       ragel.gsub!(/^\s*machine[^;]+;\s+write[^;]+;\s+include[^;]+;\s+/, '')
98       "}%%\n%%{\n#{ragel}"
99     else
100       ''
101     end
102   end
103         tmp = parser_file + '.tmp'
104   File.open(tmp, 'w') { |f| f.write parser_text }
105         yield tmp
106         File.delete(tmp)
107 end
108
109 file EXT_DL => FileList["#{EXT_DIR}/Makefile", "#{EXT_DIR}/*.{c,h,rb}", "#{RAGEL_DIR}/*.h"] do
110         cd EXT_DIR do
111                 sh 'make'
112         end
113 end
114
115 file "#{EXT_DIR}/ragel_parser.c" => FileList["#{RAGEL_DIR}/*.rl"].gsub(/\.rl/,'.h') do
116         # When any ragel parser changes, we need to rebuild ragel_parser.c.
117         # We force this by deleting the existing object file.
118         # We have no better option than this because C dependencies are controlled by mkmf,
119         # outside of the control of this Rakefile.
120         File.delete(File.join(EXT_DIR, 'ragel_parser.o')) if File.exist?(File.join(EXT_DIR, 'ragel_parser.o'))
121 end
122
123 file "#{EXT_DIR}/Makefile" => "#{EXT_DIR}/extconf.rb" do
124         cd EXT_DIR do
125                 if ENV['DEBUG']
126                         ruby 'extconf.rb', 'debug'
127                 else
128                         ruby 'extconf.rb'
129                 end
130         end
131 end
132
133 Rake::TestTask.new :ohcount_unit_tests => ARCH_DL do |t|
134         # puts File.dirname(__FILE__) + '/test/unit/*_test.rb'
135         t.test_files = FileList[File.dirname(__FILE__) + '/test/unit/**/*_test.rb']
136         # t.verbose = true
137 end
138
139 task :default => :ohcount_unit_tests