module Aigu class CLI COMMANDS = { 'rails-import' => 'RailsImporter', 'rails-export' => 'RailsExporter', 'android-import' => 'AndroidImporter', 'android-export' => 'AndroidExporter', 'core-import' => 'CoreImporter', 'core-export' => 'CoreExporter', 'ios-import' => 'IOSImporter', 'ios-export' => 'IOSExporter', 'ember-import' => 'EmberImporter', 'ember-export' => 'EmberExporter', 'ember-pod-import' => 'EmberPodImporter', 'ember-pod-export' => 'EmberPodExporter' }.freeze def initialize(env, argv) @env = env @command = argv.first =~ /^[^-]/ ? argv.shift : nil @command = @command.tr('_', '-') if @command @argv = argv @options = {} @options = parse_options_from_yaml(@options) @options = parse_options_from_arguments(@options) end def run if COMMANDS[@command] service_class = Aigu.const_get(COMMANDS[@command]) else puts "The #{@command} command doesn’t exist. Nice try." exit 0 end service_object = service_class.new(@options) service_object.process! end protected def parse_options_from_yaml(options) file = File.join(Dir.pwd, '.aigu.yml') if File.exist?(file) # Load YAML content content = YAML.load_file(file) # Symbolize keys and merge with existing options options = options.merge(content.symbolize_keys) end options end # rubocop:disable Metrics/MethodLength def parse_options_from_arguments(options) OptionParser.new do |opts| opts.banner = < [options]" Commands: #{COMMANDS.keys.map { |command| " #{command}" }.join("\n")} Options: DOC opts.on('--input-directory=', 'The directory in which the Rails YAML localization files are stored') do |directory| options[:'input-directory'] = directory end opts.on('--output-directory=', 'The directory in which the Rails YAML localization files will be generated') do |directory| options[:'output-directory'] = directory end opts.on('--input-file=', 'The JSON file generated by Accent') do |file| options[:'input-file'] = file end opts.on('--output-file=', 'The JSON file that will be generated for Accent') do |file| options[:'output-file'] = file end opts.on('--locale=', 'The locale to use. Importers and exporters may need it to process files') do |locale| options[:locale] = locale end opts.on('--ignore=', 'Patterns to ignore, separated by commas') do |ignore| options[:ignore] = ignore.split(',') end opts.on_tail('-v', '--version', 'Output Aigu’s version') do puts "aigu-v#{Aigu::VERSION}" exit end opts.on_tail('-h', '--help', 'Show this message') do puts opts exit end end.parse! @argv options end # rubocop:enable Metrics/MethodLength end end