2015-07-24 13:05:51 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
require "fileutils"
|
2015-09-16 18:04:14 +00:00
|
|
|
require "pathname"
|
2016-04-10 13:22:19 +00:00
|
|
|
require "rake"
|
2015-07-24 13:05:51 +00:00
|
|
|
require "shellwords"
|
|
|
|
require "tmpdir"
|
|
|
|
|
2016-04-10 13:22:19 +00:00
|
|
|
$mtx_dir = File.absolute_path(File.dirname(__FILE__) + "/../..")
|
|
|
|
$po_dir = "#{$mtx_dir}/po"
|
|
|
|
$man_po_dir = "#{$mtx_dir}doc/man/po4a/po"
|
|
|
|
|
2016-05-23 07:31:01 +00:00
|
|
|
require "#{$mtx_dir}/rake.d/extensions"
|
2016-04-10 13:22:19 +00:00
|
|
|
require "#{$mtx_dir}/rake.d/helpers"
|
|
|
|
require "#{$mtx_dir}/rake.d/po"
|
2015-07-24 13:05:51 +00:00
|
|
|
|
|
|
|
module AddPo
|
|
|
|
def self.handle_po file_name
|
2016-05-30 17:19:08 +00:00
|
|
|
file_name = File.absolute_path(file_name)
|
|
|
|
Dir.chdir($mtx_dir) { self.handle_po_impl file_name }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.handle_po_impl file_name
|
2015-07-24 13:05:51 +00:00
|
|
|
file_name = File.absolute_path file_name
|
|
|
|
content = IO.
|
|
|
|
readlines(file_name).
|
|
|
|
map(&:chomp)
|
|
|
|
|
|
|
|
language = content.
|
2015-09-29 18:43:45 +00:00
|
|
|
map { |line| /^\" Language: \s+ ([a-z_.@]+)/ix.match(line) ? $1 : nil }.
|
2015-07-24 13:05:51 +00:00
|
|
|
compact.
|
|
|
|
first
|
|
|
|
|
|
|
|
fail "Couldn't extract language from #{file_name}" unless language
|
|
|
|
|
2015-10-16 16:44:03 +00:00
|
|
|
is_man_po = content.detect { |line| /<manvolnum>/.match line }
|
|
|
|
dir = is_man_po ? $man_po_dir : $po_dir
|
|
|
|
target = "#{dir}/#{language}.po"
|
2015-07-24 13:05:51 +00:00
|
|
|
|
|
|
|
puts "#{file_name} → #{target}"
|
2016-04-10 13:22:19 +00:00
|
|
|
|
2016-05-23 07:31:01 +00:00
|
|
|
runq_git target, "checkout HEAD -- #{target}"
|
|
|
|
|
|
|
|
orig_items = read_po target
|
|
|
|
|
2018-01-02 14:40:24 +00:00
|
|
|
puts_qaction "merge", :target => target
|
2016-05-23 07:31:01 +00:00
|
|
|
|
|
|
|
updated_items = read_po file_name
|
2016-11-20 16:27:08 +00:00
|
|
|
merged_items = merge_po orig_items, updated_items, :headers_to_update => %w{Project-Id-Version}
|
2016-05-23 07:31:01 +00:00
|
|
|
|
|
|
|
write_po target, merged_items
|
|
|
|
|
|
|
|
File.unlink file_name
|
2015-07-24 13:05:51 +00:00
|
|
|
end
|
|
|
|
|
2015-08-16 15:32:27 +00:00
|
|
|
def self.unpack_p7z file_name
|
|
|
|
system "7z x #{Shellwords.escape(file_name)}"
|
|
|
|
fail "7z failed for #{file_name}" unless $?.exitstatus == 0
|
|
|
|
end
|
|
|
|
|
2015-08-15 06:05:08 +00:00
|
|
|
def self.unpack_rar file_name
|
|
|
|
system "unrar x #{Shellwords.escape(file_name)}"
|
|
|
|
fail "unrar failed for #{file_name}" unless $?.exitstatus == 0
|
|
|
|
end
|
|
|
|
|
2015-07-24 13:05:51 +00:00
|
|
|
def self.unpack_zip file_name
|
|
|
|
system "unzip #{Shellwords.escape(file_name)}"
|
|
|
|
fail "unzip failed for #{file_name}" unless $?.exitstatus == 0
|
|
|
|
end
|
|
|
|
|
2015-08-23 19:47:04 +00:00
|
|
|
def self.unpack_tar file_name
|
|
|
|
system "bsdtar xvf #{Shellwords.escape(file_name)}"
|
|
|
|
fail "bsdtar failed for #{file_name}" unless $?.exitstatus == 0
|
|
|
|
end
|
|
|
|
|
2015-09-16 18:04:14 +00:00
|
|
|
def self.handle_qm file_name
|
|
|
|
Dir.mktmpdir do |dir|
|
|
|
|
tm_name = Pathname.new(file_name).basename.sub_ext(".tm").to_s
|
|
|
|
system "lconvert -i #{Shellwords.escape(file_name)} -o #{Shellwords.escape(tm_name)}"
|
|
|
|
|
|
|
|
fail "lconvert failed for #{file_name}" unless $?.exitstatus == 0
|
|
|
|
|
|
|
|
handle_ts tm_name
|
|
|
|
|
|
|
|
File.unlink file_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.handle_ts file_name, language = nil
|
|
|
|
content = IO.
|
|
|
|
readlines(file_name).
|
|
|
|
map(&:chomp)
|
|
|
|
|
2015-11-25 20:14:59 +00:00
|
|
|
base = nil
|
2015-09-16 18:04:14 +00:00
|
|
|
language = content.
|
|
|
|
map { |line| /<TS [^>]* language="([a-z_@]+)"/ix.match(line) ? $1 : nil }.
|
|
|
|
compact.
|
|
|
|
first
|
|
|
|
|
|
|
|
if !language
|
2015-11-25 20:14:59 +00:00
|
|
|
language = ENV['TS'] if ENV['TS'] && !ENV['TS'].empty?
|
|
|
|
base, language = $1, $2 if !language && /qt(base)?_([a-zA-Z_@]+)/.match(file_name)
|
2015-09-16 18:04:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
fail "Unknown language for Qt TS file #{file_name} (set TS)" if !language
|
|
|
|
|
2015-11-25 20:14:59 +00:00
|
|
|
base ||= ''
|
|
|
|
target = "#{$po_dir}/qt/qt#{base}_#{language}.ts"
|
2015-09-16 18:04:14 +00:00
|
|
|
|
|
|
|
if !FileTest.exists?(target) && /^([a-z]+)_[a-z]+/i.match(language)
|
2015-11-25 20:14:59 +00:00
|
|
|
target = "#{$po_dir}/qt/qt#{base}_#{$1}.ts"
|
2015-09-16 18:04:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
fail "target file does not exist yet: #{target} (wrong language?)" if !FileTest.exists?(target)
|
|
|
|
|
|
|
|
File.open(target, "w") { |file| file.puts content.map(&:chomp).join("\n") }
|
|
|
|
File.unlink file_name
|
|
|
|
File.chmod 0644, target
|
|
|
|
end
|
|
|
|
|
2015-07-24 13:05:51 +00:00
|
|
|
def self.handle_archive file_name, archive_type
|
|
|
|
Dir.mktmpdir do |dir|
|
|
|
|
Dir.chdir(dir) do
|
|
|
|
send "unpack_#{archive_type}".to_sym, file_name
|
|
|
|
Dir["**/*.po"].each { |unpacked_file| handle_po unpacked_file }
|
2015-09-16 18:04:14 +00:00
|
|
|
Dir["**/*.qm"].each { |unpacked_file| handle_qm unpacked_file }
|
|
|
|
Dir["**/*.ts"].each { |unpacked_file| handle_ts unpacked_file }
|
2015-07-24 13:05:51 +00:00
|
|
|
end
|
|
|
|
end
|
2015-07-24 13:36:08 +00:00
|
|
|
|
|
|
|
File.unlink file_name
|
2015-07-24 13:05:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.add file_name
|
2015-08-16 15:32:27 +00:00
|
|
|
return handle_archive(file_name, :rar) if /\.rar$/i.match file_name
|
|
|
|
return handle_archive(file_name, :zip) if /\.zip$/i.match file_name
|
|
|
|
return handle_archive(file_name, :p7z) if /\.7z$/i.match file_name
|
2015-08-23 19:47:04 +00:00
|
|
|
return handle_archive(file_name, :tar) if /\.tar(?:\.(?:gz|bz2|xz))?$/i.match file_name
|
2015-08-16 15:32:27 +00:00
|
|
|
return handle_po(file_name) if /\.po$/i.match file_name
|
2015-09-16 18:04:14 +00:00
|
|
|
return handle_qm(file_name) if /\.qm$/i.match file_name
|
|
|
|
return handle_ts(file_name) if /\.ts$/i.match file_name
|
2015-07-24 13:05:51 +00:00
|
|
|
fail "Don't know how to handle #{file_name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-10 13:22:19 +00:00
|
|
|
ARGV.each { |file_name| AddPo::add File.absolute_path(file_name) }
|