Class: Transpec::CLI

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

Constant Summary collapse

CONFIG_ATTRS_FOR_CLI_TYPES =
{
  expect_to_matcher: :convert_to_expect_to_matcher=,
  expect_to_receive: :convert_to_expect_to_receive=,
   allow_to_receive: :convert_to_allow_to_receive=,
         deprecated: :replace_deprecated_method=
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



30
31
32
33
34
35
# File 'lib/transpec/cli.rb', line 30

def initialize
  @configuration = Configuration.new
  @forced = false
  @generates_commit_message = false
  @report = Report.new
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



22
23
24
# File 'lib/transpec/cli.rb', line 22

def configuration
  @configuration
end

#forcedObject (readonly) Also known as: forced?

Returns the value of attribute forced.



22
23
24
# File 'lib/transpec/cli.rb', line 22

def forced
  @forced
end

#generates_commit_messageObject (readonly) Also known as: generates_commit_message?

Returns the value of attribute generates_commit_message.



22
23
24
# File 'lib/transpec/cli.rb', line 22

def generates_commit_message
  @generates_commit_message
end

Class Method Details

.run(args = ARGV) ⇒ Object



26
27
28
# File 'lib/transpec/cli.rb', line 26

def self.run(args = ARGV)
  new.run(args)
end

Instance Method Details

#parse_options(args) ⇒ Object

rubocop:disable MethodLength



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/transpec/cli.rb', line 74

def parse_options(args)
  parser = OptionParser.new
  parser.banner = "Usage: transpec [options] [files or directories]\n\n"

  parser.on(
    '-f', '--force',
    'Force processing even if the current Git',
    'repository is not clean.'
  ) do
    @forced = true
  end

  parser.on(
    '-m', '--commit-message',
    'Generate commit message that describes',
    'conversion summary. Only Git is supported.'
  ) do
    unless Git.inside_of_repository?
      fail '-m/--commit-message option is specified but not in a Git repository'
    end

    @generates_commit_message = true
  end

  parser.on(
    '-d', '--disable TYPE[,TYPE...]',
    'Disable specific conversions.',
    'Available conversion types:',
    '  expect_to_matcher (from `should`)',
    '  expect_to_receive (from `should_receive`)',
    '  allow_to_receive  (from `stub`)',
    '  deprecated (e.g. from `stub!` to `stub`)',
    'These are all enabled by default.'
  ) do |types|
    types.split(',').each do |type|
      config_attr = CONFIG_ATTRS_FOR_CLI_TYPES[type.to_sym]
      fail ArgumentError, "Unknown conversion type #{type.inspect}" unless config_attr
      @configuration.send(config_attr, false)
    end
  end

  parser.on(
    '-n', '--negative-form FORM',
    'Specify negative form of `to` that is used',
    'in `expect(...).to` syntax.',
    'Either `not_to` or `to_not`.',
    'Default: not_to'
  ) do |form|
    @configuration.negative_form_of_to = form
  end

  parser.on(
    '-p', '--no-parentheses-matcher-arg',
    'Suppress parenthesizing argument of matcher',
    'when converting operator to non-operator',
    'in `expect` syntax. Note that it will be',
    'parenthesized even if this option is',
    'specified when parentheses are necessary to',
    'keep the meaning of the expression.',
    'By default, arguments of the following',
    'operator matchers will be parenthesized.',
    '  `== 10` to `eq(10)`',
    '  `=~ /pattern/` to `match(/pattern/)`',
    '  `=~ [1, 2]` to `match_array([1, 2])`'
  ) do
    @configuration.parenthesize_matcher_arg = false
  end

  parser.on('--no-color', 'Disable color in the output.') do
    Sickill::Rainbow.enabled = false
  end

  parser.on('--version', 'Show Transpec version.') do
    puts Version.to_s
    exit
  end

  args = args.dup
  parser.parse!(args)
  args
end

#process_file(file_path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/transpec/cli.rb', line 57

def process_file(file_path)
  puts "Processing #{file_path}"

  rewriter = Rewriter.new(@configuration, @report)
  rewriter.rewrite_file!(file_path)

  @report.invalid_context_errors.concat(rewriter.invalid_context_errors)

  rewriter.invalid_context_errors.each do |error|
    warn_invalid_context_error(error)
  end
rescue Parser::SyntaxError => error
  @report.syntax_errors << error
  warn_syntax_error(error)
end

#run(args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/transpec/cli.rb', line 37

def run(args)
  non_option_args = parse_options(args)

  fail_if_should_not_continue!

  base_paths = base_target_paths(non_option_args)

  target_files(base_paths).each do |file_path|
    process_file(file_path)
  end

  display_summary
  generate_commit_message if generates_commit_message?

  true
rescue => error
  warn error.message
  false
end

#target_files(paths) ⇒ Object

rubocop:enable MethodLength



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/transpec/cli.rb', line 157

def target_files(paths)
  paths.reduce([]) do |file_paths, path|
    if File.directory?(path)
      file_paths.concat(ruby_files_in_directory(path))
    elsif File.file?(path)
      file_paths << path
    elsif !File.exists?(path)
      fail ArgumentError, "No such file or directory #{path.inspect}"
    end
  end
end