bin/crowdin-cli in crowdin-cli-0.1.9 vs bin/crowdin-cli in crowdin-cli-0.2.0

- old
+ new

@@ -1,8 +1,9 @@ #!/usr/bin/env ruby require 'pp' +require 'find' require 'crowdin-cli' # GLI_DEBUG=true bundle exec bin/crowdin-cli # Setup i18n @@ -172,24 +173,30 @@ i = 0 i += 1 while first[i] == last[i] && i <= first.length first.slice(0, i).join('/') end -def unzip_file(zip, dest, files_list) +# Extract compressed files +files_list+ in a ZIP archive +zipfile_name+ to +dest_path+ +# +# +files_list+ is a Hash of key-value pairs. Where key is a posible archive filename based on current project configuration +# and value is the expanded filename +# +def unzip_file_with_translations(zipfile_name, dest_path, files_list) # overwrite files if they already exist inside of the extracted path Zip.options[:on_exists_proc] = true + # files that exists in archive and doesn't match current project configuration unmatched_files = [] - Zip::ZipFile.open(zip) do |zip_file| - zip_file.select{ |f| f.file? }.each do |f| + Zip::ZipFile.open(zipfile_name) do |zipfile| + zipfile.select{ |zip_entry| zip_entry.file? }.each do |f| file = files_list['/' + f.name] if file - f_path = File.join(dest, file) - FileUtils.mkdir_p(File.dirname(f_path)) + fpath = File.join(dest_path, file) + FileUtils.mkdir_p(File.dirname(fpath)) puts "Download: `#{file}'" - zip_file.extract(f, f_path) + zipfile.extract(f, fpath) else unmatched_files << f end end end @@ -260,23 +267,34 @@ 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 else - Dir.glob("#{@base_path}#{file['source']}").select{ |fn| File.file?(fn) }.each do |source| - dest = source.sub("#{@base_path}", '') # relative path in Crowdin - dest_files << dest + Find.find(@base_path) do |source_path| + dest = source_path.sub(@base_path, '') # relative path in Crowdin - export_pattern = construct_export_pattern(dest, file['source'], file['translation']) + if File.directory?(source_path) + ignores = file['ignore'] || [] + if ignores.include?(dest) + Find.prune # Don't look any further into this directory + else + next + end + elsif File.fnmatch?(file['source'], dest) + dest_files << dest - 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') + export_pattern = construct_export_pattern(dest, file['source'], file['translation']) - local_files << local_file - end - end # if + local_file = { dest: dest, source: source_path, 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 # Find + + end # if File.exists? end # @config['files'] if dest_files.empty? exit_now! <<EOS No source files to upload. @@ -358,11 +376,11 @@ c.desc I18n.t('app.commands.upload.commands.translations.switches.auto_approve_imported.desc') c.switch ['auto-approve-imported'] c.action do |global_options, options, args| params = {} - params[:import_duplicates] = options['import-dublicates'] ? 1 : 0 + params[:import_duplicates] = options['import-duplicates'] ? 1 : 0 params[:import_eq_suggestions] = options['import-eq-suggestions'] ? 1 : 0 params[:auto_approve_imported] = options['auto-approve-imported'] ? 1 : 0 language = options[:language] @@ -402,22 +420,33 @@ translation_languages.each do |lang| source = export_pattern_to_path(dest, file['translation'], lang, languages_mapping) translated_files[lang['crowdin_code']] << { source: "#{@base_path}#{source}", dest: dest } end else - Dir.glob("#{@base_path}#{file['source']}").select{ |fn| File.file?(fn) }.each do |source| - dest = source.sub("#{@base_path}", '') # relative path in Crowdin - dest_files << dest + Find.find(@base_path) do |source_path| + dest = source_path.sub(@base_path, '') # relative path in Crowdin - export_pattern = construct_export_pattern(dest, file['source'], file['translation']) + if File.directory?(source_path) + ignores = file['ignore'] || [] + if ignores.include?(dest) + Find.prune # Don't look any further into this directory + else + next + end + elsif File.fnmatch?(file['source'], dest) + dest_files << dest - 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 } + export_pattern = construct_export_pattern(dest, file['source'], file['translation']) + + 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 + end # Find + end # if end # @config['files'] if dest_files.empty? exit_now! <<EOS @@ -446,11 +475,11 @@ end else puts "Warning: Local file `#{file[:source]}' does not exists" end else - # if source file not exist, don't upload translation + # if source file does not exist, don't upload translations puts "Warning: Skip `#{file[:source].sub(@base_path, '')}'" end end end @@ -490,52 +519,62 @@ translation_languages = supported_languages.select{ |lang| project_languages.include?(lang['crowdin_code']) } source_language = project_info['details']['source_language']['code'] source_language = supported_languages.find{ |lang| lang['crowdin_code'] == source_language } - # keys is all possible files in zip archive + # keys is all possible files in .ZIP archive # values is resulted local files - # usually they are equal - downloadable_files = {} + downloadable_files_hash = {} @config['files'].each do |file| - languages_mapping = file['languages_mapping'] #Hash or NilClass + languages_mapping = file['languages_mapping'] # Hash or NilClass if File.exists?("#{@base_path}#{file['source']}") dest = file['source'].sub("#{@base_path}", '') translation_languages.each do |lang| zipped_file = export_pattern_to_path(dest, file['translation'], lang) local_file = export_pattern_to_path(dest, file['translation'], lang, languages_mapping) - downloadable_files[zipped_file] = local_file + downloadable_files_hash[zipped_file] = local_file end else - Dir.glob("#{@base_path}#{file['source']}").select{ |fn| File.file?(fn) }.each do |source| - dest = source.sub("#{@base_path}", '') # relative path in Crowdin + Find.find(@base_path) do |source_path| + dest = source_path.sub(@base_path, '') # relative path in Crowdin - export_pattern = construct_export_pattern(dest, file['source'], file['translation']) + if File.directory?(source_path) + ignores = file['ignore'] || [] + if ignores.include?(dest) + Find.prune # Don't look any further into this directory + else + next + end + elsif File.fnmatch?(file['source'], dest) + 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) + 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 + downloadable_files_hash[zipped_file] = local_file + end end - end + end # Find + end # if end # @config['files'] ## - file = Tempfile.new(language) - path = file.path + tempfile = Tempfile.new(language) + zipfile_name = tempfile.path begin - @crowdin.download_translation(language, output: path) - unzip_file(path, @base_path, downloadable_files) + @crowdin.download_translation(language, output: zipfile_name) + + unzip_file_with_translations(zipfile_name, @base_path, downloadable_files_hash) ensure - file.close - file.unlink # delete the temp file + tempfile.close + tempfile.unlink # delete the tempfile end end end