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