bin/crowdin-cli in crowdin-cli-0.1.5 vs bin/crowdin-cli in crowdin-cli-0.1.6

- old
+ new

@@ -85,33 +85,36 @@ # @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) + if pattern_regexp.names.include?('double_asterisk') and 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 +# Provides a partial implementation of translate a glob +pattern+ to a regular expression +# NOTE: +# `**` surrounded by backslashes `/` in the +pattern+ +# `**` used only once in the +pattern+ # -def translate_pattern_to_regexp(pat) +def translate_pattern_to_regexp(pattern) i = 0 - n = pat.size + n = pattern.size res = '' while i < n - c = pat[i] + c = pattern[i] i = i + 1 if c == '*' j = i - if j < n and pat[j] == '*' - res << '(?<double_asterisk>.*)' + if j < n and pattern[j] == '*' + res[-1] = '(\/)?(?<double_asterisk>.*)?' i = j + 1 else res << '(.*)' end elsif c == '?' @@ -172,11 +175,11 @@ unmatched_files = [] Zip::ZipFile.open(zip) do |zip_file| zip_file.select{ |f| f.file? }.each do |f| - file = files_list[f.name] + file = files_list['/' + f.name] if file f_path = File.join(dest, file) FileUtils.mkdir_p(File.dirname(f_path)) puts "Download: #{file}" zip_file.extract(f, f_path) @@ -245,13 +248,14 @@ get_invalid_placeholders(file['translation']).each do |placeholder| 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'] + dest = file['source'] + dest_files << dest - local_file = { dest: file['source'], source: "#{@base_path}#{file['source']}", export_pattern: file['translation'] } + local_file = { dest: dest, source: "#{@base_path}#{file['source']}", export_pattern: file['translation'] } # Used only when uploading CSV file to define data columns mapping. 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 @@ -273,11 +277,11 @@ if dest_files.empty? 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) + common_dir = @preserve_hierarchy ? '' : find_common_directory_path(dest_files) local_project_tree = get_local_files_hierarchy(local_files.collect{ |h| h[:dest].sub(common_dir, '') }) local_files.each{ |file| file[:dest].sub!(common_dir, '') } @@ -294,39 +298,39 @@ # # array containing elements common to the two arrays update_files = local_project_tree[:files] & remote_project_tree[:files] files_for_upload = local_files.select{ |file| update_files.include?(file[:dest]) } files_for_upload.each do |file| - print "Updating file `#{file[:dest]}'" + print "Updating source file `#{file[:dest]}'" params = {} params[:scheme] = file.delete(:sheme) params[:first_line_contains_header] = file.delete(:first_line_contains_header) resp = @crowdin.update_file([] << file, params) case resp['files'].first[1] when 'skipped' - puts "\rUpdating file `#{file[:dest]}' - Skipped" + puts "\rUpdating source file `#{file[:dest]}' - Skipped" when 'updated' - puts "\rUpdating file `#{file[:dest]}' - OK" + puts "\rUpdating source file `#{file[:dest]}' - OK" end end end # Add new files to Crowdin project # add_files = local_project_tree[:files] - remote_project_tree[:files] files_for_add = local_files.select{ |file| add_files.include?(file[:dest]) } files_for_add.each do |file| - print "Uploading file `#{file[:dest]}'" + print "Uploading source file `#{file[:dest]}'" params = {} params[:scheme] = file.delete(:sheme) params[:first_line_contains_header] = file.delete(:first_line_contains_header) resp = @crowdin.add_file([] << file, params) - puts "\rUploading file `#{file[:dest]}' - OK" + puts "\rUploading source file `#{file[:dest]}' - OK" end end # action end # command @@ -414,26 +418,26 @@ if dest_files.empty? 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) + common_dir = @preserve_hierarchy ? '' : find_common_directory_path(dest_files) translated_files.each do |language, files| files.each do |file| file[:dest].sub!(common_dir, '') if remote_project_tree[:files].include?(file[:dest]) if File.exist?(file[:source]) - print "Uploading `#{file[:source].sub(@base_path, '')}'" + print "Uploading translation file `#{file[:source].sub(@base_path, '')}'" resp = @crowdin.upload_translation([] << file, language, params) case resp['files'].first[1] when 'skipped' - puts "\rUploading `#{file[:source].sub(@base_path, '')}' - Skipped" + puts "\rUploading translation `#{file[:source].sub(@base_path, '')}' - Skipped" when 'uploaded' - puts "\rUploading `#{file[:source].sub(@base_path, '')}' - OK" + puts "\rUploading translation `#{file[:source].sub(@base_path, '')}' - OK" end else puts "Warning: Local file `#{file[:source]}' does not exists" end else @@ -557,36 +561,65 @@ else exit_now! <<EOS Can't find configuration file (default `crowdin.yaml'). Type `crowdin-cli help` to know how to specify custom configuration file -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 @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. + if file['source'].include?('**') + if file['source'].scan('**').size > 1 + exit_now! <<EOS +Source 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. + elsif file['source'].scan('**').size == 1 and !file['source'].match(/\/\*\*\//) + exit_now! <<EOS +Source pattern `#{file['source']}` is not valid. The mask `**` must be surrounded by backslashes `/` in the source pattern. EOS + end + else + if file['translation'].include?('**') + exit_now! <<EOS +Translation pattern `#{file['translation']}` is not valid. The mask `**` can't be used. +When using `**` in 'translation' pattern it will always contain sub-path from 'source' for certain file. +EOS + end end - end + end # @config['files'] if @config['base_path'] @base_path = @config['base_path'] else @base_path = Dir.pwd puts <<EOS Warning: Configuration file misses parameter `base_path` that defines your project root directory. Using `#{@base_path}` as a root directory. EOS + end + + if @config['target_path'] + @target_path = @config['target_path'] + else + @target_path = '' + end + + @preserve_hierarchy = false + if @config['preserve_hierarchy'] + @preserve_hierarchy = case @config['preserve_hierarchy'] + when true + true + when false + false + else + exit_now! <<EOS +Parameter `preserve_hierarchy` allows values of true or false. +EOS + end end Crowdin::API.log = Logger.new($stderr) if global[:v] @crowdin = Crowdin::API.new(api_key: @config['api_key'], project_id: @config['project_identifier'], base_url: @config['base_url'] || 'http://api.crowdin.net') begin