class CodeShift::CLI

Cli Class

Public Class Methods

new() click to toggle source
# File lib/codeshift/cli.rb, line 12
def initialize
  @options = Codeshift::Options.new
  OptionParser.new do |opts|
    opts.banner = 'Usage: codeshift -t <transform-file> [path]'

    opts.on('--version', 'Print version number') do
      puts Codeshift::VERSION
      exit
    end

    opts.on('-tTRANSFORM', '--transform=TRANSFORM', 'path to the transform file. Can be either a local path or url\n (default: ./transform.rb)') do |f|
      @options.transform = f
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end.parse!

  @files = ARGV
end

Public Instance Methods

process_file(file_path) click to toggle source
# File lib/codeshift/cli.rb, line 35
def process_file(file_path)
  puts "Processing: #{file_path}"
  code = File.read(file_path)
  # transform = open(@options.transform, '&:read')
  response = open(@options.transform)
  transform = response.read
  output = Codeshift::Transformer.new(code, transform).transform
  File.write(file_path, output)
end
run() click to toggle source
# File lib/codeshift/cli.rb, line 45
def run
  paths = @files.empty? ? [] : @files
  paths.each do |path|
    if File.directory?(path)
      glob_path = File.join(path,'**','*.rb')
      Dir.glob(glob_path) do |file_path|
        process_file file_path
      end
    else
      process_file path
    end
  end
end