Class: KBuilder::Commands::RuboCopCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/k_builder/commands/rubo_cop_command.rb

Overview

Run RuboCop against a file

Instance Attribute Summary collapse

Attributes inherited from BaseCommand

#builder, #valid

Instance Method Summary collapse

Methods inherited from BaseCommand

#debug, #guard, #valid?

Constructor Details

#initialize(file_pattern, **opts) ⇒ RuboCopCommand

Initialize RuboCop command

Examples:

Cop for single file with auto fix turned on for simple issues


RubCopCommand.new('abc.rb', fix_safe: true)

Cop for all spec files to auto simple and unsafe issues


RubCopCommand.new('spec/**/*.rb', fix_unsafe: true)

Parameters:

  • file_pattern (String)

    File name or file pattern

  • **opts (Hash)

    The options

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :fix_safe (Boolean)

    RuboCop -a option will fix simple and safe issues

  • :fix_unsafe (Boolean)

    RuboCop -A option will fix simple but potentially unsafe issues

  • :show_console (Boolean)

    This will show in console, or if false set –out ~/last_cop.txt so that console is redirected to file

  • :rubo_config_file (String)

    YAML file with RuboCop configuration options



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 32

def initialize(file_pattern, **opts)
  super(**opts)

  @valid = true

  self.file_pattern       = file_pattern

  self.fix_safe           = opts[:fix_safe]
  self.fix_unsafe         = opts[:fix_unsafe]
  self.show_console       = opts[:show_console]
  self.rubo_config_file   = opts[:rubo_config_file]
end

Instance Attribute Details

#cop_opt_valuesObject (readonly)

Returns the value of attribute cop_opt_values.



14
15
16
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 14

def cop_opt_values
  @cop_opt_values
end

#cop_optionsObject (readonly)

Returns the value of attribute cop_options.



13
14
15
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 13

def cop_options
  @cop_options
end

#file_patternObject

Returns the value of attribute file_pattern.



7
8
9
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 7

def file_pattern
  @file_pattern
end

#fix_safeObject

Returns the value of attribute fix_safe.



8
9
10
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 8

def fix_safe
  @fix_safe
end

#fix_unsafeObject

Returns the value of attribute fix_unsafe.



9
10
11
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 9

def fix_unsafe
  @fix_unsafe
end

#rubo_config_fileObject

Returns the value of attribute rubo_config_file.



10
11
12
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 10

def rubo_config_file
  @rubo_config_file
end

#show_consoleObject

Returns the value of attribute show_console.



11
12
13
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 11

def show_console
  @show_console
end

Instance Method Details

#cli_optionsObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 51

def cli_options
  cli_options = []
  # quite is the same as simple, except you will see nothing if no offenses
  cli_options << '--format' << 'quiet' # 'simple'
  cli_options << '-a' if fix_safe
  cli_options << '-A' if fix_unsafe
  cli_options << '--config' << rubo_config_file if rubo_config_file
  cli_options << '--out' << File.expand_path('~/last_cop.txt') unless show_console
  cli_options << file_pattern
  cli_options
end

#debug_valuesObject



63
64
65
66
67
68
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 63

def debug_values
  log.kv 'rubocop target', file_pattern
  log.kv '-a', 'automatic fix for safe issues'                  if fix_safe
  log.kv '-A', 'automatic fix for potentially unsafe issues'    if fix_unsafe
  log.kv '-config', rubo_config_file                            if rubo_config_file
end

#executeObject



45
46
47
48
49
# File 'lib/k_builder/commands/rubo_cop_command.rb', line 45

def execute
  return unless valid?

  cop_run
end