Class: Templatecop::Cli

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

Overview

Call this from your executable.

Examples:

Templatecop.call(
  default_configuration_path: File.expand_path('../default.yml', __dir__),
  implicit_configuration_paths: %w[.slimcop.yml .rubocop.yml],
  executable_name: 'slimcop',
  ruby_extractor: Slimcop::RubyExtractor.new
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv:, default_configuration_path:, default_path_patterns:, executable_name:, implicit_configuration_paths:, ruby_extractor:) ⇒ Cli

Returns a new instance of Cli.



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

def initialize(
  argv:,
  default_configuration_path:,
  default_path_patterns:,
  executable_name:,
  implicit_configuration_paths:,
  ruby_extractor:
)
  @argv = argv
  @default_configuration_path = default_configuration_path
  @default_path_patterns = default_path_patterns
  @executable_name = executable_name
  @ruby_extractor = ruby_extractor
  @implicit_configuration_paths = implicit_configuration_paths
end

Class Method Details

.call(executable_name:, ruby_extractor:, implicit_configuration_paths:, argv: ::ARGV.dup, default_configuration_path: nil, default_path_patterns: []) ⇒ Object

Parameters:

  • argv (Array<String>) (defaults to: ::ARGV.dup)
  • default_configuration_path (String) (defaults to: nil)

    (e.g. “default.yml”)

  • default_path_patterns (Array<String>) (defaults to: [])
  • executable_name (String)

    (e.g. “slimcop”)

  • implicit_configuration_paths (Array<String>)
  • ruby_extractor (#call)

    An object that converts template code into Ruby codes.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/templatecop/cli.rb', line 23

def call(
  executable_name:,
  ruby_extractor:,
  implicit_configuration_paths:,
  argv: ::ARGV.dup,
  default_configuration_path: nil,
  default_path_patterns: []
)
  new(
    argv: argv,
    default_configuration_path: default_configuration_path,
    default_path_patterns: default_path_patterns,
    executable_name: executable_name,
    implicit_configuration_paths: implicit_configuration_paths,
    ruby_extractor: ruby_extractor
  ).call
end

Instance Method Details

#callObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/templatecop/cli.rb', line 58

def call
  options = parse_options!
  formatter = ::RuboCop::Formatter::ProgressFormatter.new($stdout, color: options[:color])
  rubocop_config = RuboCopConfigGenerator.new(
    default_configuration_path: @default_configuration_path,
    forced_configuration_path: options[:forced_configuration_path],
    implicit_configuration_paths: @implicit_configuration_paths
  ).call
  file_paths = PathFinder.new(
    default_patterns: @default_path_patterns,
    exclude_patterns: rubocop_config.for_all_cops['Exclude'],
    patterns: @argv
  ).call

  offenses = Runner.new(
    autocorrect: options[:autocorrect],
    debug: options[:debug],
    file_paths: file_paths,
    formatter: formatter,
    rubocop_config: rubocop_config,
    ruby_extractor: @ruby_extractor
  ).call

  exit(offenses.empty? ? 0 : 1)
end