Class: Reek::CLI::Application

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

Overview

Represents an instance of a Reek application. This is the entry point for all invocations of Reek from the command line.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Application

Returns a new instance of Application.



22
23
24
25
26
27
28
# File 'lib/reek/cli/application.rb', line 22

def initialize(argv)
  @options = configure_options(argv)
  @configuration = configure_app_configuration(options.config_file)
  @command = command_class.new(options: options,
                               sources: sources,
                               configuration: configuration)
end

Instance Attribute Details

#commandObject (readonly, private)

Returns the value of attribute command.



37
38
39
# File 'lib/reek/cli/application.rb', line 37

def command
  @command
end

#configurationObject (readonly)

Returns the value of attribute configuration.



20
21
22
# File 'lib/reek/cli/application.rb', line 20

def configuration
  @configuration
end

#optionsObject (readonly, private)

Returns the value of attribute options.



37
38
39
# File 'lib/reek/cli/application.rb', line 37

def options
  @options
end

Instance Method Details

#argvObject (private)



93
94
95
# File 'lib/reek/cli/application.rb', line 93

def argv
  options.argv
end

#command_classObject (private)



76
77
78
# File 'lib/reek/cli/application.rb', line 76

def command_class
  options.generate_todo_list ? Command::TodoListCommand : Command::ReportCommand
end

#configure_app_configuration(config_file) ⇒ Object (private)



46
47
48
49
50
51
# File 'lib/reek/cli/application.rb', line 46

def configure_app_configuration(config_file)
  Configuration::AppConfiguration.from_path(config_file)
rescue Errors::ConfigFileError => error
  warn "Error: #{error}"
  exit Status::DEFAULT_ERROR_EXIT_CODE
end

#configure_options(argv) ⇒ Object (private)



39
40
41
42
43
44
# File 'lib/reek/cli/application.rb', line 39

def configure_options(argv)
  Options.new(argv).parse
rescue OptionParser::InvalidOption => error
  warn "Error: #{error}"
  exit Status::DEFAULT_ERROR_EXIT_CODE
end

#disable_progress_output_unless_verboseObject (private)



120
121
122
# File 'lib/reek/cli/application.rb', line 120

def disable_progress_output_unless_verbose
  options.progress_format = :quiet unless options.show_empty
end

#executeObject



30
31
32
33
# File 'lib/reek/cli/application.rb', line 30

def execute
  show_configuration_path
  command.execute
end

#input_was_piped?Boolean (private)

Returns:

  • (Boolean)


98
99
100
# File 'lib/reek/cli/application.rb', line 98

def input_was_piped?
  !$stdin.tty?
end

#no_source_files_given?Boolean (private)

Returns:

  • (Boolean)


102
103
104
105
106
# File 'lib/reek/cli/application.rb', line 102

def no_source_files_given?
  # At this point we have deleted all options from argv. The only remaining entries
  # are paths to the source files. If argv is empty, this means that no files were given.
  argv.empty?
end

#path_relative_to_working_directory(path) ⇒ Pathname (private)

Returns the path that is relative to the current working directory given an absolute path. E.g. if the given absolute path is “/foo/bar/baz/.reek.yml” and your working directory is “/foo/bar” this method would return “baz/.reek.yml”

:reek:UtilityFunction

Parameters:

  • path (String)

    Absolute path

Returns:

  • (Pathname)

    , e.g. ‘config/.reek.yml’



72
73
74
# File 'lib/reek/cli/application.rb', line 72

def path_relative_to_working_directory(path)
  Pathname(path).realpath.relative_path_from(Pathname.pwd)
end

#show_configuration_pathObject (private)



53
54
55
56
57
58
59
60
61
62
# File 'lib/reek/cli/application.rb', line 53

def show_configuration_path
  return unless options.show_configuration_path

  path = Configuration::ConfigurationFileFinder.find(path: options.config_file)
  if path
    puts "Using '#{path_relative_to_working_directory(path)}' as configuration file."
  else
    puts 'Not using any configuration file.'
  end
end

#source_from_pipeObject (private)



116
117
118
# File 'lib/reek/cli/application.rb', line 116

def source_from_pipe
  [Source::SourceCode.from($stdin, origin: options.stdin_filename)]
end

#sourcesObject (private)



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/reek/cli/application.rb', line 80

def sources
  if no_source_files_given?
    if input_was_piped?
      disable_progress_output_unless_verbose
      source_from_pipe
    else
      working_directory_as_source
    end
  else
    sources_from_argv
  end
end

#sources_from_argvObject (private)



112
113
114
# File 'lib/reek/cli/application.rb', line 112

def sources_from_argv
  Source::SourceLocator.new(argv, configuration: configuration, options: options).sources
end

#working_directory_as_sourceObject (private)



108
109
110
# File 'lib/reek/cli/application.rb', line 108

def working_directory_as_source
  Source::SourceLocator.new(['.'], configuration: configuration, options: options).sources
end