/rakefile
Rakefile | 309 lines | 229 code | 62 blank | 18 comment | 19 complexity | 3f4c2eae4879a2b5ab419ffba37a1d53 MD5 | raw file
- require 'rake'
- ################################################################################################
- # constants
- ROOT_DIR = File.expand_path('.')
- FINDER_DIR = '/System/Library/CoreServices/Finder.app'
- FINDER_RESOURCES_DIR = File.join(FINDER_DIR, 'Contents/Resources')
- PLUGIN_RESOURCES_DIR = File.join(ROOT_DIR, 'plugin', 'Resources')
- CZECH_LPROJ = File.join(PLUGIN_RESOURCES_DIR, 'Czech.lproj')
- TOTALFINDER_PLUGIN_SOURCES = File.join(ROOT_DIR, '..', 'totalfinder-plugin', 'plugin')
- ################################################################################################
- # dependencies
- begin
- require 'colored'
- rescue LoadError
- raise 'You must "gem install colored" to use terminal colors'
- end
- ################################################################################################
- # helpers
- def die(msg, status=1)
- puts "Error[#{status||$?}]: #{msg}".red
- exit status||$?
- end
- def sys(cmd)
- puts "> #{cmd}".yellow
- system(cmd)
- end
- ################################################################################################
- # routines
- def extract_code_strings
- strings = ""
- Dir.chdir(TOTALFINDER_PLUGIN_SOURCES) do
- strings << `ack -oh '\\$\\(@".*?"\\)'` # `ack -oh '\$\(@".*?"\)'`
- strings << `ack -oh '\\$\\$\\(@".*?"\\)'` # `ack -oh '\$\$\(@".*?"\)'`
- end
- strings = strings.split("\n").map do |s|
- s =~ /(\".*\")/
- $1
- end
-
- strings.uniq
- end
- def extract_ui_strings
- strings = ""
- Dir.chdir(PLUGIN_RESOURCES_DIR) do
- strings << `ack -o -G '\\.xib' '\\^.*?\\<'` # search only xibs for ^SOME STRING
- end
- strings = strings.split("\n").map do |s|
- s =~ /\^(.*)\</
- '"'+$1+'"'
- end
-
- strings.uniq
- end
- def parse_strings_file(filename)
- lines = []
- File.open(filename, "r") do |f|
- f.each do |line|
- lines << line
- end
- end
-
- lines
- end
- def update_czech_totalfinder_strings
- target = File.join(CZECH_LPROJ, 'TotalFinder.strings')
-
- code_strings = extract_code_strings
- totalfinder_strings = parse_strings_file(target)
-
- # comment out all existing strings as REMOVED
- totalfinder_strings.map! do |line|
- if (line[0...1]=='"') then
- line = "/* REMOVED "+line.strip+" */\n" # ***
- end
- line
- end
-
- # go through code strings and try to unmark them in the file
- epilogue_emmited = false
- code_strings.each do |code|
- found = false
- totalfinder_strings.map! do |line|
- if line =~ /^\/\* REMOVED/
- if line.index code then
- line.gsub!("REMOVED", "DUPLICIT") if found
- line = line[11..-5] + "\n" unless found # see ***
- found = true
- end
- end
-
- line
- end
-
- unless found then
- # put new strings at the end of the file
- unless epilogue_emmited then
- totalfinder_strings << "\n"
- totalfinder_strings << "/* NEW STRINGS - TODO: SORT THEM IN OR CREATE A NEW SECTION */\n"
- epilogue_emmited = true
- end
-
- totalfinder_strings << code + " = " + code + ";\n"
- end
- end
- File.open(target, "w") do |f|
- f << totalfinder_strings.join
- end
- end
- def update_czech_localizable_strings
- target = File.join(CZECH_LPROJ, 'Localizable.strings')
-
- ui_strings = extract_ui_strings
- localizable_strings = parse_strings_file(target)
-
- # comment out all existing strings as REMOVED
- localizable_strings.map! do |line|
- if (line[0...1]=='"') then
- line = "/* REMOVED "+line.strip+" */\n" # ***
- end
- line
- end
-
- # go through code strings and try to unmark them in the file
- epilogue_emmited = false
- ui_strings.each do |code|
- found = false
- localizable_strings.map! do |line|
- if line =~ /^\/\* REMOVED/
- if line.index code then
- line.gsub!("REMOVED", "DUPLICIT") if found
- line = line[11..-5] + "\n" unless found # see ***
- found = true
- end
- end
-
- line
- end
-
- unless found then
- # put new strings at the end of the file
- unless epilogue_emmited then
- localizable_strings << "\n"
- localizable_strings << "/* NEW STRINGS - TODO: SORT THEM IN OR CREATE A NEW SECTION */\n"
- epilogue_emmited = true
- end
-
- localizable_strings << code + " = " + code + ";\n"
- end
- end
- File.open(target, "w") do |f|
- f << localizable_strings.join
- end
- end
- def inprint_strings(source, dest)
- return [] if dest =~ /English\.lproj\/TotalFinder\.strings/ # a special file - see the comment inside
-
- strings = parse_strings_file(source)
- originals = parse_strings_file(dest)
- # transform czech back to english
- index = 0
- strings.map! do |line|
- index+=1
- next line unless (line.strip[0...1]=='"')
-
- line =~ /^\s*?(".*")\s*?=\s*?(".*")\s*?;\s*?/
- die "syntax error in " + source.blue+":"+index.to_s unless $1
-
- line = $1 + " = " + $1 + ";\n";
-
- line
- end
-
- # replace translations we already know from previsous version
- index = 0
- originals.each do |original|
- index+=1
- next unless (original.strip[0...1]=='"')
-
- original =~ /^\s*?(".*")\s*?=\s*?(".*")\s*?;\s*?/
- needle = $1
- haystack = $2
- die "syntax error in " + dest.blue+":"+index.to_s unless $1 and $2
-
- found = false
- strings.map! do |line|
- if (line.index needle) == 0 then
- line = needle + " = " + haystack + ";\n";
- found = true
- end
-
- line
- end
- end
-
- File.open(dest, "w") do |f|
- f << strings.join
- end
-
- strings
- end
- def propagate_czech_to_cwd
- puts Dir.pwd.blue
- total = 0
- total += inprint_strings(File.join(CZECH_LPROJ, 'Localizable.strings'), File.join(Dir.pwd, 'Localizable.strings')).size
- total += inprint_strings(File.join(CZECH_LPROJ, 'TotalFinder.strings'), File.join(Dir.pwd, 'TotalFinder.strings')).size
- puts " -> "+total.to_s.green+" strings processed"
- end
- def propagate_from_czech_to_other_lprojs
- glob = ENV["to"] || "*.lproj"
-
- Dir.glob(File.join(PLUGIN_RESOURCES_DIR, glob)) do |dir|
- Dir.chdir dir do
- propagate_czech_to_cwd
- end
- end
- end
- def exec_cmd_in_lprojs(cmd)
- glob = ENV["to"] || "*.lproj"
-
- Dir.glob(File.join(PLUGIN_RESOURCES_DIR, glob)) do |dir|
- puts dir.blue
- Dir.chdir dir do
- sys(cmd)
- end
- end
- end
- ################################################################################################
- # tasks
- desc "switch /Applications/TotalFinder.app into dev mode"
- task :dev do
- sys("./dev.sh")
- end
- desc "switch /Applications/TotalFinder.app into non-dev mode"
- task :undev do
- sys("./undev.sh")
- end
- desc "compile XIBs to NIBs (after modified the UI)"
- task :compile do
- sys("./compile.sh")
- end
- desc "restart Finder.app"
- task :restart do
- sys("./restart.sh")
- end
- desc "normalize Finder.app so it contains all our language folders (run with sudo)"
- task :normalize do
- lprojs = File.join(ROOT_DIR, 'plugin', 'Resources', '*.lproj')
- Dir.glob(lprojs) do |folder|
- dir = File.join(FINDER_RESOURCES_DIR, File.basename(folder))
- if File.exists? dir then
- puts dir.blue + " exists".yellow
- else
- if !sys("mkdir -p \"#{dir}\"") then
- die("Unable to create a folder. Hint: you should run this as sudo rake normalize")
- end
- puts dir.blue + " created".green
- end
- end
- end
- desc "cherrypicks strings from sources and applies missing strings to Czech.lproj"
- task :cherrypick do
- die "install ack 1.92+ | for example via homebrew:> brew install ack" if `which ack`==""
-
- update_czech_totalfinder_strings
- update_czech_localizable_strings
- end
- desc "propagates structure of Czech.lproj to all other language folders while keeping already translated strings"
- task :propagate do
- propagate_from_czech_to_other_lprojs
- end
- desc "exec command in all lproj folders"
- task :exec do
- exec_cmd_in_lprojs(ENV["cmd"] || "ls")
- end
- task :default => :restart