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 task :manpages => ['man/rbot.1', 'man/rbot-remote.1']
13 SPECFILE = 'rbot.gemspec'
14 # The Rakefile is also used after installing the gem, to build
15 # the .mo files. Since in this case the SPECFILE is not available,
16 # we must (and can) skip defining the gem packaging tasks.
17 if File.exist? SPECFILE
18 spec = eval(File.read(SPECFILE), nil, SPECFILE)
19 Rake::GemPackageTask.new(spec) do |pkg|
25 # normalize a po/pot file
27 content = File.read(fn)
29 # sort the messages by file location
31 sorted = `#{MSGCAT} --width=79 --sort-by-file #{fn}`
38 # replace project-id-version placholder
39 modified |= content.sub!(/^("Project-Id-Version: )PACKAGE VERSION(\\n")$/) {
44 File.open(fn, 'w') {|f| f.write content}
48 PLUGIN_FILES = FileList['data/rbot/plugins/**/*.rb']
49 NON_PLUGIN_FILES = FileList["{lib,bin,data}/**/*.{rb,rhtml}"] - PLUGIN_FILES
51 # this task defines how po files and pot files are made. those rules are not defined
52 # normally because po and pot files should be only updated in the updatepo task,
53 # but po files are also prereqs for makemo
54 task :define_po_rules do
55 # generate pot file from rb files
56 rgettext_proc = proc do |t|
57 require 'gettext/utils'
58 source_files, pot_file = t.prerequisites, t.name
59 new_pot_file = "#{pot_file}.new"
60 puts "#{source_files.join(', ')} => #{pot_file}"
61 GetText.rgettext(source_files, new_pot_file)
63 # only use the new pot file if it contains unique messages
64 if File.exists?(pot_file) && MSGCOMM && `#{MSGCOMM} --unique #{pot_file} #{new_pot_file}`.empty?
67 mv new_pot_file, pot_file
70 normalize_po(pot_file)
72 # save all this work until rb files are updated again
76 # generate pot file for non-plugin files
77 file('po/rbot.pot' => NON_PLUGIN_FILES, &rgettext_proc)
79 # generate pot files for plugin files
80 rule(%r'^po/.+\.pot$' => proc {|fn|
81 PLUGIN_FILES.select {|f| f.pathmap('rbot-%n') == fn.pathmap('%n')}
84 # map the po file to its source pot file
85 pot_for_po = proc {|fn| fn.pathmap '%{^po/.+/,po/}X.pot'}
87 # update po file from pot file
88 msgmerge_proc = proc do |t|
89 require 'gettext/utils'
90 po_file, pot_file = t.name, t.source
91 puts "#{pot_file} => #{po_file}"
92 if File.exists? po_file
93 sh "#{MSGMERGE} --backup=off --update #{po_file} #{pot_file}"
95 locale = po_file[%r'^po/(.+)/.+\.po$', 1]
96 sh "#{MSGINIT} --locale=#{locale} --no-translator --input=#{pot_file} --output-file=#{po_file}"
98 warn "#{po_file} is missing and cannot be generated without msginit"
101 normalize_po(po_file)
105 # generate English po files
106 file(%r'^po/en/.+\.po$' => pot_for_po) do |t|
107 po_file, pot_file = t.name, t.source
109 sh "#{MSGEN} --output-file=#{po_file} #{pot_file}"
110 normalize_po(po_file)
118 rule(%r'^po/.+/.+\.po$' => pot_for_po, &msgmerge_proc)
122 rule(%r'^data/locale/.+/LC_MESSAGES/.+\.mo$' => proc {|fn|
123 [ fn.pathmap('%{^data/locale,po;LC_MESSAGES/,}X.po'),
124 # the directory is created if not existing
127 po_file, mo_file = t.source, t.name
128 puts "#{po_file} => #{mo_file}"
129 require 'gettext/utils'
130 GetText.rmsgfmt po_file, mo_file
133 task :check_po_tools do
138 :options => %w[--backup= --update],
139 :message => 'Cannot update po files' },
141 :options => %w[--locale= --no-translator --input= --output-file=],
142 :message => 'Cannot generate missing po files' },
144 :options => %w[--unique],
145 :message => 'Pot files may be modified even without message change' },
147 :options => %w[--output-file],
148 :message => 'English po files will not be generated' },
150 :options => %w[--width= --sort-by-file],
151 :message => 'Pot files will not be normalized' }
154 po_tools.each_pair do |command, value|
155 path = ENV["#{command.upcase}_PATH"] || command
156 have_it = have[command] = value[:options].all? do |option|
157 `#{path} --help`.include? option
159 Object.const_set(command.upcase, have_it ? path : false)
160 warn "#{command} not found. #{value[:message]}" unless have_it
162 abort unless MSGMERGE
165 PLUGIN_BASENAMES = PLUGIN_FILES.map {|f| f.pathmap('%n')}
166 LOCALES = FileList['po/*/'].map {|d| d.pathmap('%n')}
169 directory "data/locale/#{l}/LC_MESSAGES"
172 desc 'Update po files'
173 task :updatepo => [:define_po_rules, :check_po_tools] + LOCALES.map {|l|
174 ["po/#{l}/rbot.po"] +
175 PLUGIN_BASENAMES.map {|n| "po/#{l}/rbot-#{n}.po"}
178 desc 'Normalize po files'
179 task :normalizepo => :check_po_tools do
180 FileList['po/*/*.po'].each {|fn| normalize_po(fn)}
183 # this task invokes makemo if ruby-gettext is available, but otherwise succeeds
184 # with a warning instead of failing. it is to be used by Gem's extension builder
185 # to make installation not fail because of lack of ruby-gettext
188 require 'gettext/utils'
189 Rake::Task[:makemo].invoke
191 warn 'Ruby-gettext cannot be located, so mo files cannot be built and installed'
195 desc 'Generate mo files'
197 FileList['po/*/*.po'].pathmap('%{^po,data/locale}d/LC_MESSAGES/%n.mo')