class Rails5XHRUpdate::Cli

Provide the entry point to this program.

Public Instance Methods

run() click to toggle source
# File lib/rails5_xhr_update/cli.rb, line 8
def run
  parse_options
  paths.each do |path|
    buffer = Parser::Source::Buffer.new(path)
    buffer.read
    new_source = XHRToRails5.new.rewrite(
      buffer, Parser::CurrentRuby.new.parse(buffer)
    )
    output(new_source, path)
  end
  0
end

Private Instance Methods

output(source, path) click to toggle source
# File lib/rails5_xhr_update/cli.rb, line 27
def output(source, path)
  if @options[:write]
    File.open(path, 'w') do |file|
      file.write(source)
    end
  else
    puts source
  end
end
parse_options() click to toggle source
# File lib/rails5_xhr_update/cli.rb, line 37
    def parse_options
      @options = {}
      OptionParser.new do |config|
        config.banner = <<~USAGE
          Usage: rails5_update.rb [options] FILE...

          Files can also be provided via stdin when not provided as a command line argument.
        USAGE
        config.on('-w', '--write', 'Write changes back to files') do |write|
          @options[:write] = write
        end
        config.version = Rails5XHRUpdate::VERSION
      end.parse!
    end
paths() click to toggle source
# File lib/rails5_xhr_update/cli.rb, line 23
def paths
  ARGV.empty? ? STDIN.read.split("\n") : ARGV
end