Class: Rails5::SpecConverter::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rails5/spec_converter/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails5/spec_converter/cli.rb', line 9

def initialize
  @options = TextTransformerOptions.new
  OptionParser.new do |opts|
    opts.banner = "Usage: rails5-spec-converter [options] [files]"

    opts.on("--version", "Print version number") do |q|
      puts Rails5::SpecConverter::VERSION
      exit
    end

    opts.on("-q", "--quiet", "Run quietly") do |q|
      @options.quiet = q
    end

    opts.on("-i", "--indent INDENT", "Use specified string for indentation (default is two spaces)") do |indent|
      @options.indent = indent.gsub("\\t", "\t")
    end

    opts.on("--[no-]hash-spacing", "Always/never add space around hashes ({foo: 'bar'} vs { foo: 'bar' })") do |hash_spacing|
      @options.hash_spacing = hash_spacing
    end

    opts.on("-s", "--strategy STRATEGY", "Set unknown hash parameters strategy (see README)") do |strategy|
      @options.strategy = strategy.to_sym
    end

    opts.on("--warn-if-ambiguous", "Emit warnings when hash parameters are unknowable (see README)") do |warn_if_ambigous|
      @options.warn_if_ambiguous = warn_if_ambigous
    end
  end.parse!

  @files = ARGV
end

Instance Method Details

#log(str) ⇒ Object



58
59
60
61
62
# File 'lib/rails5/spec_converter/cli.rb', line 58

def log(str)
  return if @options.quiet?

  puts str
end

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails5/spec_converter/cli.rb', line 43

def run
  paths = @files.length > 0 ? @files : ["spec/**/*_spec.rb"]

  paths.each do |path|
    Dir.glob(path) do |file_path|
      log "Processing: #{file_path}"

      original_content = File.read(file_path)
      @options.file_path = file_path
      transformed_content = Rails5::SpecConverter::TextTransformer.new(original_content, @options).transform
      File.write(file_path, transformed_content)
    end
  end
end