PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/rakefile

https://github.com/mcskrzypczak/totalfinder-i18n
Rakefile | 309 lines | 229 code | 62 blank | 18 comment | 19 complexity | 3f4c2eae4879a2b5ab419ffba37a1d53 MD5 | raw file
  1. require 'rake'
  2. ################################################################################################
  3. # constants
  4. ROOT_DIR = File.expand_path('.')
  5. FINDER_DIR = '/System/Library/CoreServices/Finder.app'
  6. FINDER_RESOURCES_DIR = File.join(FINDER_DIR, 'Contents/Resources')
  7. PLUGIN_RESOURCES_DIR = File.join(ROOT_DIR, 'plugin', 'Resources')
  8. CZECH_LPROJ = File.join(PLUGIN_RESOURCES_DIR, 'Czech.lproj')
  9. TOTALFINDER_PLUGIN_SOURCES = File.join(ROOT_DIR, '..', 'totalfinder-plugin', 'plugin')
  10. ################################################################################################
  11. # dependencies
  12. begin
  13. require 'colored'
  14. rescue LoadError
  15. raise 'You must "gem install colored" to use terminal colors'
  16. end
  17. ################################################################################################
  18. # helpers
  19. def die(msg, status=1)
  20. puts "Error[#{status||$?}]: #{msg}".red
  21. exit status||$?
  22. end
  23. def sys(cmd)
  24. puts "> #{cmd}".yellow
  25. system(cmd)
  26. end
  27. ################################################################################################
  28. # routines
  29. def extract_code_strings
  30. strings = ""
  31. Dir.chdir(TOTALFINDER_PLUGIN_SOURCES) do
  32. strings << `ack -oh '\\$\\(@".*?"\\)'` # `ack -oh '\$\(@".*?"\)'`
  33. strings << `ack -oh '\\$\\$\\(@".*?"\\)'` # `ack -oh '\$\$\(@".*?"\)'`
  34. end
  35. strings = strings.split("\n").map do |s|
  36. s =~ /(\".*\")/
  37. $1
  38. end
  39. strings.uniq
  40. end
  41. def extract_ui_strings
  42. strings = ""
  43. Dir.chdir(PLUGIN_RESOURCES_DIR) do
  44. strings << `ack -o -G '\\.xib' '\\^.*?\\<'` # search only xibs for ^SOME STRING
  45. end
  46. strings = strings.split("\n").map do |s|
  47. s =~ /\^(.*)\</
  48. '"'+$1+'"'
  49. end
  50. strings.uniq
  51. end
  52. def parse_strings_file(filename)
  53. lines = []
  54. File.open(filename, "r") do |f|
  55. f.each do |line|
  56. lines << line
  57. end
  58. end
  59. lines
  60. end
  61. def update_czech_totalfinder_strings
  62. target = File.join(CZECH_LPROJ, 'TotalFinder.strings')
  63. code_strings = extract_code_strings
  64. totalfinder_strings = parse_strings_file(target)
  65. # comment out all existing strings as REMOVED
  66. totalfinder_strings.map! do |line|
  67. if (line[0...1]=='"') then
  68. line = "/* REMOVED "+line.strip+" */\n" # ***
  69. end
  70. line
  71. end
  72. # go through code strings and try to unmark them in the file
  73. epilogue_emmited = false
  74. code_strings.each do |code|
  75. found = false
  76. totalfinder_strings.map! do |line|
  77. if line =~ /^\/\* REMOVED/
  78. if line.index code then
  79. line.gsub!("REMOVED", "DUPLICIT") if found
  80. line = line[11..-5] + "\n" unless found # see ***
  81. found = true
  82. end
  83. end
  84. line
  85. end
  86. unless found then
  87. # put new strings at the end of the file
  88. unless epilogue_emmited then
  89. totalfinder_strings << "\n"
  90. totalfinder_strings << "/* NEW STRINGS - TODO: SORT THEM IN OR CREATE A NEW SECTION */\n"
  91. epilogue_emmited = true
  92. end
  93. totalfinder_strings << code + " = " + code + ";\n"
  94. end
  95. end
  96. File.open(target, "w") do |f|
  97. f << totalfinder_strings.join
  98. end
  99. end
  100. def update_czech_localizable_strings
  101. target = File.join(CZECH_LPROJ, 'Localizable.strings')
  102. ui_strings = extract_ui_strings
  103. localizable_strings = parse_strings_file(target)
  104. # comment out all existing strings as REMOVED
  105. localizable_strings.map! do |line|
  106. if (line[0...1]=='"') then
  107. line = "/* REMOVED "+line.strip+" */\n" # ***
  108. end
  109. line
  110. end
  111. # go through code strings and try to unmark them in the file
  112. epilogue_emmited = false
  113. ui_strings.each do |code|
  114. found = false
  115. localizable_strings.map! do |line|
  116. if line =~ /^\/\* REMOVED/
  117. if line.index code then
  118. line.gsub!("REMOVED", "DUPLICIT") if found
  119. line = line[11..-5] + "\n" unless found # see ***
  120. found = true
  121. end
  122. end
  123. line
  124. end
  125. unless found then
  126. # put new strings at the end of the file
  127. unless epilogue_emmited then
  128. localizable_strings << "\n"
  129. localizable_strings << "/* NEW STRINGS - TODO: SORT THEM IN OR CREATE A NEW SECTION */\n"
  130. epilogue_emmited = true
  131. end
  132. localizable_strings << code + " = " + code + ";\n"
  133. end
  134. end
  135. File.open(target, "w") do |f|
  136. f << localizable_strings.join
  137. end
  138. end
  139. def inprint_strings(source, dest)
  140. return [] if dest =~ /English\.lproj\/TotalFinder\.strings/ # a special file - see the comment inside
  141. strings = parse_strings_file(source)
  142. originals = parse_strings_file(dest)
  143. # transform czech back to english
  144. index = 0
  145. strings.map! do |line|
  146. index+=1
  147. next line unless (line.strip[0...1]=='"')
  148. line =~ /^\s*?(".*")\s*?=\s*?(".*")\s*?;\s*?/
  149. die "syntax error in " + source.blue+":"+index.to_s unless $1
  150. line = $1 + " = " + $1 + ";\n";
  151. line
  152. end
  153. # replace translations we already know from previsous version
  154. index = 0
  155. originals.each do |original|
  156. index+=1
  157. next unless (original.strip[0...1]=='"')
  158. original =~ /^\s*?(".*")\s*?=\s*?(".*")\s*?;\s*?/
  159. needle = $1
  160. haystack = $2
  161. die "syntax error in " + dest.blue+":"+index.to_s unless $1 and $2
  162. found = false
  163. strings.map! do |line|
  164. if (line.index needle) == 0 then
  165. line = needle + " = " + haystack + ";\n";
  166. found = true
  167. end
  168. line
  169. end
  170. end
  171. File.open(dest, "w") do |f|
  172. f << strings.join
  173. end
  174. strings
  175. end
  176. def propagate_czech_to_cwd
  177. puts Dir.pwd.blue
  178. total = 0
  179. total += inprint_strings(File.join(CZECH_LPROJ, 'Localizable.strings'), File.join(Dir.pwd, 'Localizable.strings')).size
  180. total += inprint_strings(File.join(CZECH_LPROJ, 'TotalFinder.strings'), File.join(Dir.pwd, 'TotalFinder.strings')).size
  181. puts " -> "+total.to_s.green+" strings processed"
  182. end
  183. def propagate_from_czech_to_other_lprojs
  184. glob = ENV["to"] || "*.lproj"
  185. Dir.glob(File.join(PLUGIN_RESOURCES_DIR, glob)) do |dir|
  186. Dir.chdir dir do
  187. propagate_czech_to_cwd
  188. end
  189. end
  190. end
  191. def exec_cmd_in_lprojs(cmd)
  192. glob = ENV["to"] || "*.lproj"
  193. Dir.glob(File.join(PLUGIN_RESOURCES_DIR, glob)) do |dir|
  194. puts dir.blue
  195. Dir.chdir dir do
  196. sys(cmd)
  197. end
  198. end
  199. end
  200. ################################################################################################
  201. # tasks
  202. desc "switch /Applications/TotalFinder.app into dev mode"
  203. task :dev do
  204. sys("./dev.sh")
  205. end
  206. desc "switch /Applications/TotalFinder.app into non-dev mode"
  207. task :undev do
  208. sys("./undev.sh")
  209. end
  210. desc "compile XIBs to NIBs (after modified the UI)"
  211. task :compile do
  212. sys("./compile.sh")
  213. end
  214. desc "restart Finder.app"
  215. task :restart do
  216. sys("./restart.sh")
  217. end
  218. desc "normalize Finder.app so it contains all our language folders (run with sudo)"
  219. task :normalize do
  220. lprojs = File.join(ROOT_DIR, 'plugin', 'Resources', '*.lproj')
  221. Dir.glob(lprojs) do |folder|
  222. dir = File.join(FINDER_RESOURCES_DIR, File.basename(folder))
  223. if File.exists? dir then
  224. puts dir.blue + " exists".yellow
  225. else
  226. if !sys("mkdir -p \"#{dir}\"") then
  227. die("Unable to create a folder. Hint: you should run this as sudo rake normalize")
  228. end
  229. puts dir.blue + " created".green
  230. end
  231. end
  232. end
  233. desc "cherrypicks strings from sources and applies missing strings to Czech.lproj"
  234. task :cherrypick do
  235. die "install ack 1.92+ | for example via homebrew:> brew install ack" if `which ack`==""
  236. update_czech_totalfinder_strings
  237. update_czech_localizable_strings
  238. end
  239. desc "propagates structure of Czech.lproj to all other language folders while keeping already translated strings"
  240. task :propagate do
  241. propagate_from_czech_to_other_lprojs
  242. end
  243. desc "exec command in all lproj folders"
  244. task :exec do
  245. exec_cmd_in_lprojs(ENV["cmd"] || "ls")
  246. end
  247. task :default => :restart