]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - Rakefile
mo files are no longer included in gem but built as extension
[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   # replace project-id-version placholder
46   modified = content.sub!(/^("Project-Id-Version: )PACKAGE VERSION(\\n")$/) {
47     "#{$1}rbot#{$2}"
48   }
49
50   # sort the messages by file location
51   if MSGCAT
52     sorted = `#{MSGCAT} --width=79 --sort-by-file #{fn}`
53     if sorted != content
54       content = sorted
55       modified = true
56     end
57   end
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 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     sh "#{MSGMERGE} --backup=off --update #{po_file} #{pot_file}"
109     normalize_po(po_file)
110     touch po_file
111   end
112
113   # generate English po files
114   file(%r'^po/en_US/.+\.po$' => pot_for_po) do |t|
115     po_file, pot_file = t.name, t.source
116     if MSGEN
117       sh "#{MSGEN} --output-file=#{po_file} #{pot_file}"
118       normalize_po(po_file)
119       touch po_file
120     else
121       msgmerge_proc.call t
122     end
123   end
124
125   # update po files
126   rule(%r'^po/.+/.+\.po$' => pot_for_po, &msgmerge_proc)
127 end
128
129 # generate mo files
130 rule(%r'^data/locale/.+/LC_MESSAGES/.+\.mo$' => proc {|fn|
131   [ fn.pathmap('%{^data/locale,po;LC_MESSAGES/,}X.po'), 
132     # the directory is created if not existing
133     fn.pathmap('%d') ]
134 }) do |t|
135   po_file, mo_file = t.source, t.name
136   puts "#{po_file} => #{mo_file}"
137   require 'gettext/utils'
138   GetText.rmsgfmt po_file, mo_file
139 end
140
141 task :check_po_tools do
142   have = {}
143
144   po_tools = {
145     'msgmerge' => {
146       :options => %w[--backup= --update],
147       :message => 'Cannot update po files' },
148     'msgcomm' => {
149       :options => %w[--unique],
150       :message => 'Pot files may be modified even without message change' },
151     'msgen' => {
152       :options => %w[--output-file],
153       :message => 'English po files will not be generated' },
154     'msgcat' => {
155       :options => %w[--width= --sort-by-file],
156       :message => 'Pot files will not be normalized' }
157   }
158
159   po_tools.each_pair do |command, value|
160     path = ENV["#{command.upcase}_PATH"] || command
161     have_it = have[command] = value[:options].all? do |option|
162       `#{path} --help`.include? option
163     end
164     Object.const_set(command.upcase, have_it ? path : false)
165     warn "#{command} not found. #{value[:message]}" unless have_it
166   end
167   abort unless MSGMERGE
168 end
169
170 PLUGIN_BASENAMES = PLUGIN_FILES.map {|f| f.pathmap('%n')}
171 LOCALES = FileList['po/*/'].map {|d| d.pathmap('%n')}
172
173 LOCALES.each do |l|
174   directory "data/locale/#{l}/LC_MESSAGES"
175 end
176
177 desc 'Update po files'
178 task :updatepo => [:define_po_rules, :check_po_tools] + LOCALES.map {|l|
179   ["po/#{l}/rbot.po"] +
180   PLUGIN_BASENAMES.map {|n| "po/#{l}/rbot-#{n}.po"}
181 }.flatten
182
183 desc 'Normalize po files'
184 task :normalizepo => :check_po_tools do
185   FileList['po/*/*.po'].each {|fn| normalize_po(fn)}
186 end
187
188 # this task invokes makemo if ruby-gettext is available, but otherwise succeeds
189 # with a warning instead of failing. it is to be used by Gem's extension builder
190 # to make installation not fail because of lack of ruby-gettext
191 task :buildext do
192   begin
193     require 'gettext/utils'
194     Rake::Task[:makemo].invoke
195   rescue LoadError
196     warn 'Ruby-gettext cannot be located, so mo files cannot be built and installed' 
197   end
198 end
199
200 desc 'Generate mo files'
201 task :makemo => LOCALES.map {|l|
202   ["data/locale/#{l}/LC_MESSAGES/rbot.mo"] +
203   PLUGIN_BASENAMES.map {|n| "data/locale/#{l}/LC_MESSAGES/rbot-#{n}.mo"}
204 }.flatten
205