bin/crowdin-cli in crowdin-cli-0.1.3 vs bin/crowdin-cli in crowdin-cli-0.1.4

- old
+ new

@@ -1,7 +1,8 @@ #!/usr/bin/env ruby +require 'pp' require 'crowdin-cli' # GLI_DEBUG=true bundle exec bin/crowdin-cli # Return +hierarchy+ of directories and files in Crowdin project @@ -78,10 +79,53 @@ end export_pattern.gsub(/%(#{placeholders.join('|')})%/, pattern) end +# @param [String] path relative path to file in Crowdin project +# @param [String] source basically, is a file['source'] from crowdin.yaml +# @param [String] translation basically, is a file['translation'] from crowdin.yaml +# +def construct_export_pattern(path, source, translation) + pattern_regexp = translate_pattern_to_regexp(source) + if path.match(pattern_regexp) + double_asterisk = path.match(pattern_regexp)['double_asterisk'] + translation = translation.sub('**', double_asterisk) + end + + export_pattern = translation.split('/').reject(&:empty?).join('/').insert(0, '/') + + return export_pattern +end + +# Translate a glob pattern to a regular expression +# +def translate_pattern_to_regexp(pat) + i = 0 + n = pat.size + res = '' + while i < n + c = pat[i] + i = i + 1 + if c == '*' + j = i + if j < n and pat[j] == '*' + res << '(?<double_asterisk>.*)' + i = j + 1 + else + res << '(.*)' + end + elsif c == '?' + res << '.' + else + res << Regexp.escape(c) + end + end + + return Regexp.new(res) +end + def android_locale_code(locale_code) locale_code = case locale_code when 'he-IL' then 'iw-IL' when 'yi-DE' then 'ji-DE' when 'id-ID' then 'in-ID' @@ -153,11 +197,11 @@ include GLI::App version Crowdin::CLI::VERSION program_desc 'is a command line tool that allows you to manage and synchronize your localization resources with Crowdin project' -program_long_desc 'This tool requires configuration file to be created. See http://crowdin.net/page/cli-client for more details.' +program_long_desc 'This tool requires configuration file to be created. See http://crowdin.net/page/cli-tool#configuration-file for more details.' sort_help :manually # help commands are ordered in the order declared wrap_help_text :to_terminal desc 'Be verbose' switch [:v, :verbose] @@ -197,11 +241,11 @@ local_files = [] dest_files = [] @config['files'].each do |file| get_invalid_placeholders(file['translation']).each do |placeholder| - puts "Warning: #{placeholder} is not valid variable supported by Crowdin. See http://crowdin.net/page/cli-client#configuration-file for more details." + puts "Warning: #{placeholder} is not valid variable supported by Crowdin. See http://crowdin.net/page/cli-tool#configuration-file for more details." end if File.exist?("#{@base_path}#{file['source']}") dest_files << file['source'] @@ -214,26 +258,23 @@ else Dir.glob("#{@base_path}#{file['source']}").each do |source| dest = source.sub("#{@base_path}", '') # relative path in Crowdin dest_files << dest - file_pattern = export_pattern_to_path(dest, file['translation'], source_language) + export_pattern = construct_export_pattern(dest, file['source'], file['translation']) - diff = (dest.split('/') - file_pattern.split('/')).join('/') - export_pattern = file['translation'].sub('**', diff) - local_file = { dest: dest, source: source, export_pattern: export_pattern } local_file.merge!({ sheme: file['scheme'] }) if file.has_key?('scheme') local_file.merge!({ first_line_contains_header: file['first_line_contains_header'] }) if file.has_key?('first_line_contains_header') local_files << local_file end end # if end # @config['files'] if dest_files.empty? - exit_now!("nothing to upload. See http://crowdin.net/page/cli-client#configuration-file for more details.") + exit_now!("nothing to upload. See http://crowdin.net/page/cli-tool#configuration-file for more details.") end common_dir = find_common_directory_path(dest_files) local_project_tree = get_local_files_hierarchy(local_files.collect{ |h| h[:dest].sub(common_dir, '') }) @@ -311,11 +352,10 @@ c.switch ['import-eq-suggestions'] c.desc 'Automatically approve uploaded translations' c.switch ['auto-approve-imported'] - c.action do |global_options, options, args| params = {} params[:import_duplicates] = options['import-dublicates'] ? 1 : 0 params[:import_eq_suggestions] = options['import-eq-suggestions'] ? 1 : 0 params[:auto_approve_imported] = options['auto-approve-imported'] ? 1 : 0 @@ -341,11 +381,11 @@ translated_files = Hash.new{ |hash, key| hash[key] = Array.new } dest_files = [] @config['files'].each do |file| get_invalid_placeholders(file['translation']).each do |placeholder| - puts "Warning: #{placeholder} is not valid variable supported by Crowdin. See http://crowdin.net/page/cli-client#configuration-file for more details." + puts "Warning: #{placeholder} is not valid variable supported by Crowdin. See http://crowdin.net/page/cli-tool#configuration-file for more details." end languages_mapping = file['languages_mapping'] if File.exists?("#{@base_path}#{file['source']}") @@ -359,26 +399,23 @@ else Dir.glob("#{@base_path}#{file['source']}").each do |source| dest = source.sub("#{@base_path}", '') # relative path in Crowdin dest_files << dest - file_pattern = export_pattern_to_path(dest, file['translation'], source_language) + export_pattern = construct_export_pattern(dest, file['source'], file['translation']) - diff = (dest.split('/') - file_pattern.split('/')).join('/') - export_pattern = file['translation'].sub('**', diff) # !!! - translation_languages.each do |lang| source = export_pattern_to_path(dest, export_pattern, lang, languages_mapping) translated_files[lang['crowdin_code']] << { source: "#{@base_path}#{source}", dest: dest } end end end # if end # @config['files'] if dest_files.empty? - exit_now!("Warning: nothing to upload. See http://crowdin.net/page/cli-client#configuration-file for more details.") + exit_now!("Warning: nothing to upload. See http://crowdin.net/page/cli-tool#configuration-file for more details.") end common_dir = find_common_directory_path(dest_files) translated_files.each do |language, files| @@ -460,15 +497,12 @@ end else Dir.glob("#{@base_path}#{file['source']}").each do |source| dest = source.sub("#{@base_path}", '') # relative path in Crowdin - file_pattern = export_pattern_to_path(dest, file['translation'], source_language) - - diff = (dest.split('/') - file_pattern.split('/')).join('/') - export_pattern = file['translation'].sub('**', diff) - + export_pattern = construct_export_pattern(dest, file['source'], file['translation']) + translation_languages.each do |lang| zipped_file = export_pattern_to_path(dest, export_pattern, lang) local_file = export_pattern_to_path(dest, export_pattern, lang, languages_mapping) downloadable_files[zipped_file] = local_file @@ -505,20 +539,20 @@ ['api_key', 'project_identifier'].each do |key| unless @config[key] exit_now! <<EOS Configuration file misses required option `#{key}` -See http://crowdin.net/page/cli-client#configuration-file for more details +See http://crowdin.net/page/cli-tool#configuration-file for more details EOS end end unless @config['files'] exit_now! <<EOS Configuration file misses required section `files` -See http://crowdin.net/page/cli-client#configuration-file for more details +See http://crowdin.net/page/cli-tool#configuration-file for more details EOS end else exit_now! <<EOS @@ -530,9 +564,20 @@ end @config['files'].each do |file| file['source'] = '/' + file['source'] unless file['source'].start_with?('/') file['translation'] = '/' + file['translation'] unless file['translation'].start_with?('/') + + if file['source'].scan('**').size > 1 + exit_now! <<EOS +Pattern `#{file['source']}` is not valid. The mask `**` can be used only once in the source pattern. +EOS + elsif file['source'].scan('**').size == 1 and !file['source'].match(/\/\*\*\//) + exit_now! <<EOS +Pattern `#{file['source']}` is not valid. The mask `**` must be surrounded by backslashes `/` in the source pattern. +EOS + end + end if @config['base_path'] @base_path = @config['base_path'] else