|
#!/usr/bin/env ruby |
|
# See help text below or run with --help for docs |
|
require 'cri' # gem install cri |
|
require 'fileutils' |
|
require 'pathname' |
|
require 'tempfile' |
|
|
|
def translation_dir translation |
|
d = ROOT.join translation |
|
d if d.directory? or raise "No such translation #{translation}" |
|
end |
|
|
|
def case_file case_num, translation |
|
translation_dir(translation).join "case-#{case_num}.txt" |
|
end |
|
|
|
def available? case_num, translation |
|
case_file(case_num, translation).exist? |
|
end |
|
|
|
def case_numbers translation |
|
cases = Pathname.glob translation_dir(translation).join('case-[0-9]*.txt') |
|
cases.map { |each| /(\d+)\.txt/.match(each.to_path)[1].to_i }.sort |
|
end |
|
|
|
def untranslated_cases src, dest |
|
case_numbers(src) - case_numbers(dest) |
|
end |
|
|
|
def root_command |
|
Cri::Command.define do |
|
name NAME |
|
usage "#{NAME} [case] [src=#{DEFAULT_SOURCE}] [dest=#{DEFAULT_DESTINATION}]" |
|
summary "copies a case before starting its translation" |
|
description <<EOS |
|
Copies selected case from an authoritative translation (src) to an in-progress translation (dest). |
|
|
|
The case can be specified either by its explicit number, or by one of the following keywords: |
|
current (latest translated case), or |
|
next/last/random (among the cases not yet in the destination translation). |
|
|
|
Environment variables: |
|
|
|
CODELESS_TRANSLATION |
|
-- Name of the translation directory to copy cases to. |
|
|
|
CODELESS_SOURCE |
|
-- Override the default source translation (en-qi). |
|
|
|
CODELESS_BRANCH |
|
-- Format string used to name branches; %{translation} and %{case} will be expanded. |
|
EOS |
|
flag :h, :help, "show command usage" do |_, cmd| |
|
puts cmd.help |
|
exit |
|
end |
|
|
|
flag nil, :dryrun, "only say what would be done" |
|
flag nil, :overwrite, "copy even if destination case exists" |
|
flag :e, :edit, "open case in $EDITOR" |
|
flag :b, :branch, "checkout branch from master (named after $CODELESS_BRANCH)" |
|
flag :a, :add, "add case to git" |
|
|
|
run do |opts, args, cmd| |
|
selection = args[0] || DEFAULT_SELECTION |
|
src = args[1] || DEFAULT_SOURCE |
|
dest = args[2] || DEFAULT_DESTINATION || raise(StandardError, "No destination translation given, please set $CODELESS_TRANSLATION") |
|
language, translator = dest.split('-') |
|
|
|
case_num = case selection |
|
when /^\d+$/ then args[0].to_i |
|
when /^c(ur(rent)?)?$/ then case_numbers(dest).last |
|
when /^n(ext)?$/ then untranslated_cases(src, dest).first |
|
when /^l(a(te)?st)?$/ then untranslated_cases(src, dest).last |
|
when /^r(and(om)?)?$/ then untranslated_cases(src, dest).sample |
|
else raise "Invalid case selector #{selection}" |
|
end |
|
raise "No available cases to translate from #{src} to #{dest}" if case_num.nil? |
|
raise "No case ##{case_num} in translation #{src}" unless available?(case_num, src) |
|
raise "Case ##{case_num} already exists in translation #{dest}" if available?(case_num, dest) unless opts[:overwrite] || opts[:edit] |
|
|
|
if opts[:branch] |
|
branch = (BRANCH_FORMAT) % { translation: dest, case: case_num } |
|
exists = system 'git', 'show-branch', branch |
|
system 'git', 'checkout', *(exists ? [branch] : ['-b', branch, 'master']) unless opts[:dryrun] |
|
end |
|
|
|
unless available?(case_num, dest) && opts[:edit] |
|
$stderr.puts "Copying case ##{case_num} (#{src} -> #{dest})" |
|
dest_path = case_file(case_num, dest) |
|
|
|
unless opts[:dryrun] |
|
FileUtils.cp case_file(case_num, src), dest_path |
|
system 'git', 'add', dest_path.to_path if opts[:add] |
|
|
|
Tempfile.open(dest) do |tmp| |
|
tmp.write <<EOF |
|
Lang: #{language} |
|
Translator: #{translator} |
|
EOF |
|
tmp.write File.read(dest_path) |
|
File.rename tmp, dest_path |
|
end |
|
end |
|
|
|
puts dest_path.expand_path.relative_path_from(Pathname.pwd) |
|
end |
|
|
|
if opts[:edit] |
|
exec ENV['EDITOR'], case_file(case_num, dest).to_path unless opts[:dryrun] |
|
end |
|
end |
|
end |
|
end |
|
|
|
# ...and here we go |
|
begin |
|
NAME = File.basename(__FILE__) |
|
ROOT = Pathname.new(`git rev-parse --show-cdup`.strip).join('the-codeless-code') |
|
DEFAULT_SELECTION = ENV['CODELESS_SELECTION'] || 'next' |
|
DEFAULT_SOURCE = ENV['CODELESS_SOURCE'] || 'en-qi' |
|
DEFAULT_DESTINATION = ENV['CODELESS_TRANSLATION'] |
|
BRANCH_FORMAT = ENV['CODELESS_BRANCH'] || '%{translation}-%{case}' |
|
|
|
root_command.run(ARGV) |
|
|
|
rescue StandardError => e |
|
puts e.message |
|
exit 1 |
|
end |