bin/crowdin-cli in crowdin-cli-0.0.13 vs bin/crowdin-cli in crowdin-cli-0.0.14

- old
+ new

@@ -68,11 +68,11 @@ } unless languages_mapping.nil? pattern = Hash[pattern.map{|k, str| [ k, - (languages_mapping[k[/%(.*)%/, 1]][str] rescue nil) || str] + (languages_mapping[k[/%(.*)%/, 1]][lang['crowdin_code']] rescue nil) || str] }] end export_pattern.gsub(/%.+?%/, pattern) end @@ -117,46 +117,40 @@ end ### include GLI::App -program_desc 'A CLI to sync locale files with crowdin.net' - 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.' +sort_help :manually # help commands are ordered in the order declared +wrap_help_text :to_terminal + desc 'Be verbose' switch [:v, :verbose] -desc 'Path to config file' +desc 'Configuration file' default_value File.join(Dir.pwd, 'crowdin.yaml') arg_name '<s>' flag [:c, :config] -desc 'Upload existing translations to Crowdin project' -#arg_name 'Describe arguments to upload here' +desc 'Allows you to upload source files and existing translations to Crowdin project' +long_desc 'This command is used to upload source files and translations to Crowdin. This command is used in combination with sub-commands `sources` and `translations`.' + command :upload do |c| - # Command 'upload' requires a subcommand - # - #c.action do |global_options, options, args| - # puts "upload command ran" - #end - #c.default_command :all + c.desc 'safely upload source files to Crowdin' + c.long_desc <<EOS +Upload source files to Crowdin project. If there are new localization files locally they will be added to Crowdin project. +Othervice files will be updated (if no --no-auto-update option specified). +EOS - #c.desc 'Upload source and translation files' - #c.command :all do |c| - # c.action do |global_options, options, args| - # puts options - # puts "`upload all` command ran" - # end - #end - - c.desc 'Upload source files' c.command :sources do |c| - c.desc 'defines whether to add sources if there is the same file previously added' - c.switch [:ignore_duplicates] + c.desc 'Defines whether to update source files in Crowdin project. --no-auto-update is useful when you just want to upload new files without updating existing ones.' + c.switch ['auto-update'] c.action do |global_options, options, args| project_info = @crowdin.project_info source_language = project_info['details']['source_language']['code'] @@ -204,50 +198,59 @@ create_dirs.each do |dir| puts "Create directory `#{dir}`" @crowdin.add_directory(dir) end - unless options[:ignore_duplicates] + if options['auto-update'].nil? or options['auto-update'] # Update existing files in Crowdin project # # 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| - puts "Update file `#{file[:dest]}`" + print "Updating file `#{file[:dest]}'" @crowdin.update_file([] << file) + puts "\rUpdating file `#{file[:dest]}' - OK" 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| - puts "Add new file `#{file[:dest]}`" + print "Uploading file `#{file[:dest]}'" @crowdin.add_file([] << file) + puts "\rUploading file `#{file[:dest]}' - OK" end end # action end # command - c.desc 'Upload translation files' + c.desc 'upload existing translations to Crowdin project' + c.long_desc <<EOS +Upload existing translations to Crowdin. +See below available options that can be used in combination with this command. +If no options specified uploaded translations will be not approved, +imported to Crowdin project even if they are duplicated and imported even they are equal to souce string. +(In many localization formats they can be considered as not actually translations). +EOS c.command :translations do |c| - c.desc 'the language of translation you need' + c.desc 'Defines what language upload translations to. By default translations will be uploaded for all Crowdin project target languages' c.default_value 'all' - c.arg_name 'language_code' + c.arg_name 'crowdin_language_code' c.flag [:l, :language] - c.desc 'defines whether to add translation if there is the same translation previously added' - c.switch [:import_duplicates] + c.desc 'Defines whether to add translation if there is the same translation already existing in Crowdin project' + c.switch ['import-duplicates'] - c.desc 'defines whether to add translation if it is equal to source string at Crowdin' - c.switch [:import_eq_suggestions] + c.desc 'Defines whether to add translation if it is equal to source string in Crowdin project' + c.switch ['import-eq-suggestions'] - c.desc 'mark uploaded translations as approved' - c.switch [:auto_approve_imported] + c.desc 'Automatically approve uploaded translations' + c.switch ['auto-approve-imported'] c.action do |global_options, options, args| language = options[:language] @@ -301,49 +304,50 @@ end # @config['files'] exit_now!("Nothing to upload") if dest_files.empty? 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 + 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 common_dir = 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]) - puts "Uploading #{file[:source].sub(@base_path, '')}" + print "Uploading `#{file[:source].sub(@base_path, '')}'" @crowdin.upload_translation([] << file, language, params) + puts "\rUploading `#{file[:source].sub(@base_path, '')}' - OK" else - puts "Local file #{file[:source]} not exists" + puts "Warning: Local file `#{file[:source]}' does not exists" end else # if source file not exist, don't upload translation - puts "Skip #{file[:source].sub(@base_path, '')}" + puts "Warning: Skip `#{file[:source].sub(@base_path, '')}'" end end end end # action end # command end -desc 'Download existing translations' +desc 'Download latest translations from Crowdin and put them to the right places in your project' #arg_name 'Describe arguments to download here' command :download do |c| - c.desc 'the language of translation you need' + c.desc 'If the option is defined the translations will be downloaded for single specified language. Othervice (by default) translations are downloaded for all languages' c.arg_name 'language_code' c.flag :l, :language, :default_value => 'all' c.action do |global_options ,options, args| # use export API method before to download the most recent translations - #@crowdin.export_translations + @crowdin.export_translations language = options[:language] project_info = @crowdin.project_info remote_project_tree = get_remote_files_hierarchy(project_info['files']) @@ -415,13 +419,45 @@ # Return true to proceed; false to abourt and not call the # chosen command # Use skips_pre before a command to skip this block # on that command only - @config = YAML.load_file(global[:config]) + if File.exists?(global[:config]) + @config = YAML.load_file(global[:config]) - #@base_path = @config['base_path'] || Dir.pwd + ['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 +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 +EOS + end + + else + exit_now! <<EOS +Can't find configuration file (default `crowdin.yml'). +Type `crowdin-cli help` to know how to specify custom configuration file + +See http://crowdin.net/page/cli-client#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?('/') + end + if @config['base_path'] if @config['base_path'].start_with?('/') @base_path = @config['base_path'] else @base_path = Dir.pwd + '/' + @config['base_path'] @@ -429,20 +465,25 @@ else @base_path = Dir.pwd end Crowdin::API.log = Logger.new($stderr) if global[:v] - @crowdin = Crowdin::API.new(api_key: @config['api_key'], project_id: @config['project_id'], base_url: @config['base_url'] || 'http://api.crowdin.net') + @crowdin = Crowdin::API.new(api_key: @config['api_key'], project_id: @config['project_identifier'], base_url: @config['base_url'] || 'http://api.crowdin.net') + begin + @crowdin.project_info + rescue + exit_now!("Seems Crowdin server API URL is not valid. Please check the `base_url` parameter in the configuration file.") + end - puts "Executing #{command.name}" if global[:v] + #puts "Executing #{command.name}" if global[:v] true end post do |global, command, options, args| # Post logic here # Use skips_post before a command to skip this # block on that command only - puts "Executed #{command.name}" if global[:v] + #puts "Executed #{command.name}" if global[:v] end on_error do |exception| # Error logic here # return false to skip default error handling