3 require 'rake/gempackagetask'
5 task :default => [:buildext]
7 rule '.1' => ['.xml'] do |t|
8 sh "xsltproc -nonet -o #{t.name} /usr/share/sgml/docbook/stylesheet/xsl/nwalsh/manpages/docbook.xsl #{t.source}"
11 SPECFILE = 'rbot.gemspec'
12 # The Rakefile is also used after installing the gem, to build
13 # the .mo files. Since in this case the SPECFILE is not available,
14 # we must (and can) skip defining the gem packaging tasks.
15 if File.exist? SPECFILE
16 spec = eval(File.read(SPECFILE), nil, SPECFILE)
17 Rake::GemPackageTask.new(spec) do |pkg|
23 # normalize a po/pot file
25 content = File.read(fn)
27 # sort the messages by file location
29 sorted = `#{MSGCAT} --width=79 --sort-by-file #{fn}`
36 # replace project-id-version placholder
37 modified |= content.sub!(/^("Project-Id-Version: )PACKAGE VERSION(\\n")$/) {
42 File.open(fn, 'w') {|f| f.write content}
46 PLUGIN_FILES = FileList['data/rbot/plugins/**/*.rb']
47 NON_PLUGIN_FILES = FileList["{lib,bin,data}/**/*.{rb,rhtml}"] - PLUGIN_FILES
49 # this task defines how po files and pot files are made. those rules are not defined
50 # normally because po and pot files should be only updated in the updatepo task,
51 # but po files are also prereqs for makemo
52 task :define_po_rules do
53 # generate pot file from rb files
54 rgettext_proc = proc do |t|
55 require 'gettext/utils'
56 source_files, pot_file = t.prerequisites, t.name
57 new_pot_file = "#{pot_file}.new"
58 puts "#{source_files.join(', ')} => #{pot_file}"
59 GetText.rgettext(source_files, new_pot_file)
61 # only use the new pot file if it contains unique messages
62 if File.exists?(pot_file) && MSGCOMM && `#{MSGCOMM} --unique #{pot_file} #{new_pot_file}`.empty?
65 mv new_pot_file, pot_file
68 normalize_po(pot_file)
70 # save all this work until rb files are updated again
74 # generate pot file for non-plugin files
75 file('po/rbot.pot' => NON_PLUGIN_FILES, &rgettext_proc)
77 # generate pot files for plugin files
78 rule(%r'^po/.+\.pot$' => proc {|fn|
79 PLUGIN_FILES.select {|f| f.pathmap('rbot-%n') == fn.pathmap('%n')}
82 # map the po file to its source pot file
83 pot_for_po = proc {|fn| fn.pathmap '%{^po/.+/,po/}X.pot'}
85 # update po file from pot file
86 msgmerge_proc = proc do |t|
87 require 'gettext/utils'
88 po_file, pot_file = t.name, t.source
89 puts "#{pot_file} => #{po_file}"
90 if File.exists? po_file
91 sh "#{MSGMERGE} --backup=off --update #{po_file} #{pot_file}"
93 locale = po_file[%r'^po/(.+)/.+\.po$', 1]
94 sh "#{MSGINIT} --locale=#{locale} --no-translator --input=#{pot_file} --output-file=#{po_file}"
96 warn "#{po_file} is missing and cannot be generated without msginit"
103 # generate English po files
104 file(%r'^po/en/.+\.po$' => pot_for_po) do |t|
105 po_file, pot_file = t.name, t.source
107 sh "#{MSGEN} --output-file=#{po_file} #{pot_file}"
108 normalize_po(po_file)
116 rule(%r'^po/.+/.+\.po$' => pot_for_po, &msgmerge_proc)
120 rule(%r'^data/locale/.+/LC_MESSAGES/.+\.mo$' => proc {|fn|
121 [ fn.pathmap('%{^data/locale,po;LC_MESSAGES/,}X.po'),
122 # the directory is created if not existing
125 po_file, mo_file = t.source, t.name
126 puts "#{po_file} => #{mo_file}"
127 require 'gettext/utils'
128 GetText.rmsgfmt po_file, mo_file
131 task :check_po_tools do
136 :options => %w[--backup= --update],
137 :message => 'Cannot update po files' },
139 :options => %w[--locale= --no-translator --input= --output-file=],
140 :message => 'Cannot generate missing po files' },
142 :options => %w[--unique],
143 :message => 'Pot files may be modified even without message change' },
145 :options => %w[--output-file],
146 :message => 'English po files will not be generated' },
148 :options => %w[--width= --sort-by-file],
149 :message => 'Pot files will not be normalized' }
152 po_tools.each_pair do |command, value|
153 path = ENV["#{command.upcase}_PATH"] || command
154 have_it = have[command] = value[:options].all? do |option|
155 `#{path} --help`.include? option
157 Object.const_set(command.upcase, have_it ? path : false)
158 warn "#{command} not found. #{value[:message]}" unless have_it
160 abort unless MSGMERGE
163 PLUGIN_BASENAMES = PLUGIN_FILES.map {|f| f.pathmap('%n')}
164 LOCALES = FileList['po/*/'].map {|d| d.pathmap('%n')}
167 directory "data/locale/#{l}/LC_MESSAGES"
170 desc 'Update po files'
171 task :updatepo => [:define_po_rules, :check_po_tools] + LOCALES.map {|l|
172 ["po/#{l}/rbot.po"] +
173 PLUGIN_BASENAMES.map {|n| "po/#{l}/rbot-#{n}.po"}
176 desc 'Normalize po files'
177 task :normalizepo => :check_po_tools do
178 FileList['po/*/*.po'].each {|fn| normalize_po(fn)}
181 # this task invokes makemo if ruby-gettext is available, but otherwise succeeds
182 # with a warning instead of failing. it is to be used by Gem's extension builder
183 # to make installation not fail because of lack of ruby-gettext
186 require 'gettext/utils'
187 Rake::Task[:makemo].invoke
189 warn 'Ruby-gettext cannot be located, so mo files cannot be built and installed'
193 desc 'Generate mo files'
195 FileList['po/*/*.po'].pathmap('%{^po,data/locale}d/LC_MESSAGES/%n.mo')