Locale/localeapp

View on GitHub
bin/localeapp

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'gli'

require 'localeapp'

# Don't connect to the net if we're running under cucumber for testing
# Use FakeWeb to simulate api.localeapp.com
if ENV['FAKE_WEB_DURING_CUCUMBER_RUN']
  require 'fakeweb'
  class FakeWeb::StubSocket
    define_method(:close) {}
  end
  FakeWeb.allow_net_connect = false
  if fake_data_as_json = ENV['FAKE_WEB_FAKES']
    fakes = YAML.load(fake_data_as_json)
    fakes.each do |fake|
      FakeWeb.register_uri fake['method'].to_sym, fake['uri'], { :body => fake['body'], :status => fake['status'] }.merge(fake['headers'])
    end
  end
end

module LocaleappGLIWrapper
  gli2 = GLI::VERSION >= '2.0.0'
  if gli2
    include GLI::App
  else
    include GLI
  end
  extend self

  pre do |global_options, command, options, args|
    global_options[:k] = if global_options[:k]
      global_options[:k]
    elsif ENV['LOCALEAPP_API_KEY']
      ENV['LOCALEAPP_API_KEY']
    elsif File.file?('.env') && IO.read('.env') =~ /^LOCALEAPP_API_KEY=(\w+)$/
      $1
    else
      nil
    end

    if Localeapp.has_config_file? || !global_options[:k].nil?
      true
    else
      puts "Could not load config file and no key specified"
      exit 1
    end
  end

  on_error do |ex|
    case ex
    when Localeapp::APIResponseError
      $stderr.puts "ERROR: #{ex}"
      exit 70
      false
    else
      true
    end
  end

  desc "API Key (for when there is no configuration file)"
  flag [:k, 'api-key']

  version Localeapp::VERSION

  desc "Creates new configuration files and confirms key works"
  skips_pre
  arg_name "<api_key>"
  command :install do |c|
    c.desc "install configuration files in .localeapp/"
    c.switch [:s, 'standalone']

    c.desc "create configuration when using localeapp via a heroku addon (PRE ALPHA)"
    c.switch [:h, 'heroku']

    c.desc "install a skeleton project suitable for Github " \
      "(warning: will write to .gitignore and README.md"
    c.switch [:g, 'github']

    c.desc "write API key to `#{Localeapp.env_file_path}'"
    c.switch [:e, "write-env-file"]

    c.action do |global_options, options, args|
      install_opts = {}
      if options[:"write-env-file"]
        install_opts[:write_env_file] = Localeapp.env_file_path
      end
      key = args.first
      installer = Localeapp::CLI::Install.new
      installer.config_type = :standalone if options[:standalone]
      installer.config_type = :heroku if options[:heroku]
      installer.config_type = :github if options[:github]
      installer.config_type ||= :rails
      unless installer.execute(key, install_opts)
        exit_now! "", 1
      end
    end
  end

  desc "Sends the key and content to localeapp.com"
  arg_name "<key> <locale:content> (<locale:content> ...)"
  command :add do |c|
    c.action do |global_options, options, args|
      key = args.shift
      if key.nil? || args.size.zero?
        exit_now! "localeapp add requires a key name and at least one translation", 1
      else
        Localeapp::CLI::Add.new(global_options).execute(key, *args)
      end
    end
  end

  desc "removes a key from the project"
  arg_name "<key>"
  command :rm do |c|
    c.action do |global_options, options, args|
      key = args.shift
      if key.nil?
        exit_now! "localeapp rm requires a key name", 1
      else
        Localeapp::CLI::Remove.new(global_options).execute(key, *args)
      end
    end
  end

  desc "renames a key in the project"
  arg_name "<current key name> <new key name>"
  command :mv do |c|
    c.action do |global_options, options, args|
      current_name = args.shift
      new_name = args.shift
      if current_name.nil? || new_name.nil?
        exit_now! "localeapp mv requires a current key name and a new key name", 1
      else
        Localeapp::CLI::Rename.new(global_options).execute(current_name, new_name, *args)
      end
    end
  end

  desc "copies a key in the project"
  arg_name "<source key name> <dest key name>"
  command :cp do |c|
    c.action do |global_options, options, args|
      source_name = args.shift
      dest_name = args.shift
      if source_name.nil? || dest_name.nil?
        exit_now! "localeapp cp requires a source key name and a destination key name", 1
      else
        Localeapp::CLI::Copy.new(global_options).execute(source_name, dest_name, *args)
      end
    end
  end

  desc "Pulls one or all translations from localeapp.com"
  arg_name "<key> (optional)"
  command :pull do |c|
    c.action do |global_options, options, args|
      locale_key = args[0]
      Localeapp::CLI::Pull.new(global_options).execute(locale_key)
    end
  end

  desc "Pushes a translation file or directory to localeapp.com"
  arg_name "<file>"
  command :push do |c|
    c.action do |global_options, options, args|
      if args.empty?
        exit_now! "localeapp push requires a file or directory to push", 1
      else
        path = args.first
        pusher = Localeapp::CLI::Push.new(global_options)
        pusher.execute(path)
      end
    end
  end

  desc "Gets any changes since the last poll and updates the yml"
  command :update do |c|
    c.action do |global_options, options, args|
      Localeapp::CLI::Update.new(global_options).execute
    end
  end

  desc "Simple daemon (checks for new translations in the background)"
  command :daemon do |c|
    c.desc "Interval to wait between checks"
    c.arg_name 'interval'
    c.default_value 5
    c.flag [:i, :interval]

    c.desc "run the daemon in the background"
    c.switch [:b, 'background']

    c.action do |global_options, options, args|
      Localeapp::CLI::Daemon.new(global_options).execute(options)
    end
  end

  if gli2
    exit run(ARGV)
  else
    exit GLI.run(ARGV)
  end
end