]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - Rakefile
Package man files
[user/henk/code/ruby/rbot.git] / Rakefile
1 require 'rubygems'
2 require 'rake'
3 require 'rake/gempackagetask'
4
5 task :default => [:buildext]
6
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}"
9 end
10
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|
18     pkg.need_zip = true
19     pkg.need_tar = true
20   end
21 end
22
23 # normalize a po/pot file
24 def normalize_po(fn)
25   content = File.read(fn)
26
27   # sort the messages by file location
28   if MSGCAT
29     sorted = `#{MSGCAT} --width=79 --sort-by-file #{fn}`
30     if sorted != content
31       content = sorted
32       modified = true
33     end
34   end
35
36   # replace project-id-version placholder
37   modified |= content.sub!(/^("Project-Id-Version: )PACKAGE VERSION(\\n")$/) {
38     "#{$1}rbot#{$2}"
39   }
40
41   if modified
42     File.open(fn, 'w') {|f| f.write content}
43   end
44 end
45
46 PLUGIN_FILES = FileList['data/rbot/plugins/**/*.rb']
47 NON_PLUGIN_FILES = FileList["{lib,bin,data}/**/*.{rb,rhtml}"] - PLUGIN_FILES
48
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)
60
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?
63       rm new_pot_file
64     else
65       mv new_pot_file, pot_file
66     end
67
68     normalize_po(pot_file)
69     
70     # save all this work until rb files are updated again
71     touch pot_file
72   end
73
74   # generate pot file for non-plugin files
75   file('po/rbot.pot' => NON_PLUGIN_FILES, &rgettext_proc)
76
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')}
80   }, &rgettext_proc)
81
82   # map the po file to its source pot file
83   pot_for_po = proc {|fn| fn.pathmap '%{^po/.+/,po/}X.pot'}
84
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}"
92     elsif MSGINIT
93       locale = po_file[%r'^po/(.+)/.+\.po$', 1]
94       sh "#{MSGINIT} --locale=#{locale} --no-translator --input=#{pot_file} --output-file=#{po_file}"
95     else
96       warn "#{po_file} is missing and cannot be generated without msginit"
97       next
98     end
99     normalize_po(po_file)
100     touch po_file
101   end
102
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
106     if MSGEN
107       sh "#{MSGEN} --output-file=#{po_file} #{pot_file}"
108       normalize_po(po_file)
109       touch po_file
110     else
111       msgmerge_proc.call t
112     end
113   end
114
115   # update po files
116   rule(%r'^po/.+/.+\.po$' => pot_for_po, &msgmerge_proc)
117 end
118
119 # generate mo files
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
123     fn.pathmap('%d') ]
124 }) do |t|
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
129 end
130
131 task :check_po_tools do
132   have = {}
133
134   po_tools = {
135     'msgmerge' => {
136       :options => %w[--backup= --update],
137       :message => 'Cannot update po files' },
138     'msginit' => {
139       :options => %w[--locale= --no-translator --input= --output-file=],
140       :message => 'Cannot generate missing po files' },
141     'msgcomm' => {
142       :options => %w[--unique],
143       :message => 'Pot files may be modified even without message change' },
144     'msgen' => {
145       :options => %w[--output-file],
146       :message => 'English po files will not be generated' },
147     'msgcat' => {
148       :options => %w[--width= --sort-by-file],
149       :message => 'Pot files will not be normalized' }
150   }
151
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
156     end
157     Object.const_set(command.upcase, have_it ? path : false)
158     warn "#{command} not found. #{value[:message]}" unless have_it
159   end
160   abort unless MSGMERGE
161 end
162
163 PLUGIN_BASENAMES = PLUGIN_FILES.map {|f| f.pathmap('%n')}
164 LOCALES = FileList['po/*/'].map {|d| d.pathmap('%n')}
165
166 LOCALES.each do |l|
167   directory "data/locale/#{l}/LC_MESSAGES"
168 end
169
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"}
174 }.flatten
175
176 desc 'Normalize po files'
177 task :normalizepo => :check_po_tools do
178   FileList['po/*/*.po'].each {|fn| normalize_po(fn)}
179 end
180
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
184 task :buildext do
185   begin
186     require 'gettext/utils'
187     Rake::Task[:makemo].invoke
188   rescue LoadError
189     warn 'Ruby-gettext cannot be located, so mo files cannot be built and installed' 
190   end
191 end
192
193 desc 'Generate mo files'
194 task :makemo =>
195   FileList['po/*/*.po'].pathmap('%{^po,data/locale}d/LC_MESSAGES/%n.mo')
196
197