]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - Rakefile
Rakefile: ensure that the placeholder PACKAGE VERSION is properly replaced
[user/henk/code/ruby/rbot.git] / Rakefile
1 require 'rubygems'
2 require 'rake'
3 require 'rake/gempackagetask'
4
5 task :default => [:buildext]
6
7 spec = Gem::Specification.new do |s|
8   s.name = 'rbot'
9   s.version = '0.9.11'
10   s.summary = <<-EOF
11     A modular ruby IRC bot.
12   EOF
13   s.description = <<-EOF
14     A modular ruby IRC bot specifically designed for ease of extension via plugins.
15   EOF
16   s.requirements << 'Ruby, version 1.8.0 (or newer)'
17
18   #  s.files = Dir.glob("**/*").delete_if { |item| item.include?(".svn") }
19   s.files = FileList['lib/**/*.rb', 'bin/*', 'data/rbot/**/*', 'AUTHORS', 'COPYING', 'README', 'REQUIREMENTS', 'TODO', 'ChangeLog', 'INSTALL',  'Usage_en.txt', 'setup.rb', 'po/*.pot', 'po/**/*.po'].to_a.delete_if {|item| item == ".svn"}
20   s.bindir = 'bin'
21   s.executables = ['rbot', 'rbot-remote']
22   s.default_executable = 'rbot'
23   s.extensions = 'Rakefile'
24
25 #  s.autorequire = 'rbot/ircbot'
26   s.has_rdoc = true
27   s.rdoc_options = ['--exclude', 'post-install.rb',
28   '--title', 'rbot API Documentation', '--main', 'README', 'README']
29
30   s.author = 'Tom Gilbert'
31   s.email = 'tom@linuxbrit.co.uk'
32   s.homepage = 'http://ruby-rbot.org'
33   s.rubyforge_project = 'rbot'
34 end
35
36 Rake::GemPackageTask.new(spec) do |pkg|
37   pkg.need_zip = true
38   pkg.need_tar = true
39 end
40
41 # normalize a po/pot file
42 def normalize_po(fn)
43   content = File.read(fn)
44
45   # sort the messages by file location
46   if MSGCAT
47     sorted = `#{MSGCAT} --width=79 --sort-by-file #{fn}`
48     if sorted != content
49       content = sorted
50       modified = true
51     end
52   end
53
54   # replace project-id-version placholder
55   modified |= content.sub!(/^("Project-Id-Version: )PACKAGE VERSION(\\n")$/) {
56     "#{$1}rbot#{$2}"
57   }
58
59   if modified
60     File.open(fn, 'w') {|f| f.write content}
61   end
62 end
63
64 PLUGIN_FILES = FileList['data/rbot/plugins/**/*.rb']
65 NON_PLUGIN_FILES = FileList["{lib,bin,data}/**/*.{rb,rhtml}"] - PLUGIN_FILES
66
67 # this task defines how po files and pot files are made. those rules are not defined
68 # normally because po and pot files should be only updated in the updatepo task,
69 # but po files are also prereqs for makemo
70 task :define_po_rules do
71   # generate pot file from rb files
72   rgettext_proc = proc do |t|
73     require 'gettext/utils'
74     source_files, pot_file = t.prerequisites, t.name
75     new_pot_file = "#{pot_file}.new"
76     puts "#{source_files.join(', ')} => #{pot_file}"
77     GetText.rgettext(source_files, new_pot_file)
78
79     # only use the new pot file if it contains unique messages
80     if File.exists?(pot_file) && MSGCOMM && `#{MSGCOMM} --unique #{pot_file} #{new_pot_file}`.empty?
81       rm new_pot_file
82     else
83       mv new_pot_file, pot_file
84     end
85
86     normalize_po(pot_file)
87     
88     # save all this work until rb files are updated again
89     touch pot_file
90   end
91
92   # generate pot file for non-plugin files
93   file('po/rbot.pot' => NON_PLUGIN_FILES, &rgettext_proc)
94
95   # generate pot files for plugin files
96   rule(%r'^po/.+\.pot$' => proc {|fn|
97     PLUGIN_FILES.select {|f| f.pathmap('rbot-%n') == fn.pathmap('%n')}
98   }, &rgettext_proc)
99
100   # map the po file to its source pot file
101   pot_for_po = proc {|fn| fn.pathmap '%{^po/.+/,po/}X.pot'}
102
103   # update po file from pot file
104   msgmerge_proc = proc do |t|
105     require 'gettext/utils'
106     po_file, pot_file = t.name, t.source
107     puts "#{pot_file} => #{po_file}"
108     if File.exists? po_file
109       sh "#{MSGMERGE} --backup=off --update #{po_file} #{pot_file}"
110     else
111       cp pot_file, po_file
112     end
113     normalize_po(po_file)
114     touch po_file
115   end
116
117   # generate English po files
118   file(%r'^po/en_US/.+\.po$' => pot_for_po) do |t|
119     po_file, pot_file = t.name, t.source
120     if MSGEN
121       sh "#{MSGEN} --output-file=#{po_file} #{pot_file}"
122       normalize_po(po_file)
123       touch po_file
124     else
125       msgmerge_proc.call t
126     end
127   end
128
129   # update po files
130   rule(%r'^po/.+/.+\.po$' => pot_for_po, &msgmerge_proc)
131 end
132
133 # generate mo files
134 rule(%r'^data/locale/.+/LC_MESSAGES/.+\.mo$' => proc {|fn|
135   [ fn.pathmap('%{^data/locale,po;LC_MESSAGES/,}X.po'), 
136     # the directory is created if not existing
137     fn.pathmap('%d') ]
138 }) do |t|
139   po_file, mo_file = t.source, t.name
140   puts "#{po_file} => #{mo_file}"
141   require 'gettext/utils'
142   GetText.rmsgfmt po_file, mo_file
143 end
144
145 task :check_po_tools do
146   have = {}
147
148   po_tools = {
149     'msgmerge' => {
150       :options => %w[--backup= --update],
151       :message => 'Cannot update po files' },
152     'msgcomm' => {
153       :options => %w[--unique],
154       :message => 'Pot files may be modified even without message change' },
155     'msgen' => {
156       :options => %w[--output-file],
157       :message => 'English po files will not be generated' },
158     'msgcat' => {
159       :options => %w[--width= --sort-by-file],
160       :message => 'Pot files will not be normalized' }
161   }
162
163   po_tools.each_pair do |command, value|
164     path = ENV["#{command.upcase}_PATH"] || command
165     have_it = have[command] = value[:options].all? do |option|
166       `#{path} --help`.include? option
167     end
168     Object.const_set(command.upcase, have_it ? path : false)
169     warn "#{command} not found. #{value[:message]}" unless have_it
170   end
171   abort unless MSGMERGE
172 end
173
174 PLUGIN_BASENAMES = PLUGIN_FILES.map {|f| f.pathmap('%n')}
175 LOCALES = FileList['po/*/'].map {|d| d.pathmap('%n')}
176
177 LOCALES.each do |l|
178   directory "data/locale/#{l}/LC_MESSAGES"
179 end
180
181 desc 'Update po files'
182 task :updatepo => [:define_po_rules, :check_po_tools] + LOCALES.map {|l|
183   ["po/#{l}/rbot.po"] +
184   PLUGIN_BASENAMES.map {|n| "po/#{l}/rbot-#{n}.po"}
185 }.flatten
186
187 desc 'Normalize po files'
188 task :normalizepo => :check_po_tools do
189   FileList['po/*/*.po'].each {|fn| normalize_po(fn)}
190 end
191
192 # this task invokes makemo if ruby-gettext is available, but otherwise succeeds
193 # with a warning instead of failing. it is to be used by Gem's extension builder
194 # to make installation not fail because of lack of ruby-gettext
195 task :buildext do
196   begin
197     require 'gettext/utils'
198     Rake::Task[:makemo].invoke
199   rescue LoadError
200     warn 'Ruby-gettext cannot be located, so mo files cannot be built and installed' 
201   end
202 end
203
204 desc 'Generate mo files'
205 task :makemo => LOCALES.map {|l|
206   ["data/locale/#{l}/LC_MESSAGES/rbot.mo"] +
207   PLUGIN_BASENAMES.map {|n| "data/locale/#{l}/LC_MESSAGES/rbot-#{n}.mo"}
208 }.flatten
209