Class: CodeWeb::CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ CLI

Returns a new instance of CLI.



46
47
48
49
50
# File 'lib/code_web/cli.rb', line 46

def initialize(arguments)
  @arguments = arguments
  @code_parser = CodeWeb::CodeParser.new
  @class_map = {}
end

Instance Attribute Details

#argumentsArray<String>

Returns command line arguments.

Returns:

  • (Array<String>)

    command line arguments



38
39
40
# File 'lib/code_web/cli.rb', line 38

def arguments
  @arguments
end

#base_urlObject

defaults to local filesystem

Returns:



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

def base_url
  @base_url
end

#class_mapMap<Regexp,html_class>

Returns files/directories with specal emphasis.

Returns:

  • (Map<Regexp,html_class>)

    files/directories with specal emphasis



34
35
36
# File 'lib/code_web/cli.rb', line 34

def class_map
  @class_map
end

#code_parserObject

Returns the value of attribute code_parser.



9
10
11
# File 'lib/code_web/cli.rb', line 9

def code_parser
  @code_parser
end

#file_and_urlObject

Returns true if both url and file are displayed.

Returns:

  • true if both url and file are displayed



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

def file_and_url
  @file_and_url
end

#filenamesArray<String>

Returns regular expression filenames.

Returns:

  • (Array<String>)

    regular expression filenames



42
43
44
# File 'lib/code_web/cli.rb', line 42

def filenames
  @filenames
end

#outputObject

Returns the value of attribute output.



44
45
46
# File 'lib/code_web/cli.rb', line 44

def output
  @output
end

#report_generatorObject

Returns class that runs the report (i.e.: TextReport, HtmlReport).

Returns:

  • class that runs the report (i.e.: TextReport, HtmlReport)



30
31
32
# File 'lib/code_web/cli.rb', line 30

def report_generator
  @report_generator
end

Class Method Details

.parse(args) ⇒ Object



5
6
7
# File 'lib/code_web/cli.rb', line 5

def self.parse(args)
  new(args).run
end

Instance Method Details

#arg_regexObject



12
# File 'lib/code_web/cli.rb', line 12

def arg_regex ; code_parser.arg_regex ; end

#arg_regex=(val) ⇒ Object



14
# File 'lib/code_web/cli.rb', line 14

def arg_regex=(val) ; code_parser.arg_regex = val ; end

#debug=(val) ⇒ Object



17
# File 'lib/code_web/cli.rb', line 17

def debug=(val) ; code_parser.debug = val ; end

#debug?Boolean

Returns:

  • (Boolean)


18
# File 'lib/code_web/cli.rb', line 18

def debug? ; code_parser.debug? ; end

#display_resultsObject



106
107
108
109
# File 'lib/code_web/cli.rb', line 106

def display_results
  STDOUT.puts "parsed #{files_parsed} files"
  report_generator.new(method_calls, class_map, arg_regex, output, :base_url => base_url).report
end

#exit_on_error=(val) ⇒ Object



15
# File 'lib/code_web/cli.rb', line 15

def exit_on_error=(val) ; code_parser.exit_on_error = val ; end

#files_parsedObject



11
# File 'lib/code_web/cli.rb', line 11

def files_parsed ; code_parser.file_count ; end

#method_callsObject



10
# File 'lib/code_web/cli.rb', line 10

def method_calls ; code_parser.method_calls ; end

#method_regex=(val) ⇒ Object



13
# File 'lib/code_web/cli.rb', line 13

def method_regex=(val) ; code_parser.method_regex = val ; end

#parse_argumentsObject



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
83
84
85
86
87
88
89
# File 'lib/code_web/cli.rb', line 58

def parse_arguments
  #defaults
  self.report_generator = ::CodeWeb::HtmlReport
  self.output = STDOUT

  #parsing the command line
  OptionParser.new do |opt|
    opt.banner = "Usage: code_web regex [file_name ...]"
#       opt.on('-n', '--requests=count',   Integer, "Number of requests (default: #{requests})")  { |v| options[:requests] = v }
    opt.on('-t', '--text',                      'Use text reports')                           { |v| self.report_generator = ::CodeWeb::TextReport }
    opt.on('-u', '--url URL',                   'Base url (e.g.: https://github.com/miq/miq/blob/master)') { |v| self.base_url = v }
    opt.on('-b', '--both',                      'Show file and url references')               { |v| self.file_and_url = v }
    opt.on('-a', '--arg ARG_REGEX',             'Only files with hash argument')              { |v| self.arg_regex = Regexp.new(v) }
    opt.on('-o', '--output FILENAME',           'Output filename')                            { |v| self.output = (v == '-') ? STDOUT : File.new(v,'w') }
    opt.on('-e', '--error-out',                 'exit on unknown tags')                       { |v| self.exit_on_error = true}
    opt.on('-V', '--verbose',                   'verbose parsing')                            { |v| self.verbose = true}
    opt.on('-D', '--debug',                     'debug parsing')                              { |v| self.debug = true}
    opt.on('-p', '--pattern FILENAME_REGEX=COLOR','color to emphasize a file')                { |v| v = v.split('=') ; self.class_map[Regexp.new(v.first)] = v.last }
    opt.on('--byebug') { require "byebug" }
    opt.on('--pry') { require "pry" }
    opt.on_tail("-h", "--help", "Show this message")                                          { puts opt ; exit }
    opt.on_tail("-v", "--version", "Show version_information")                                { puts "Code Web version #{CodeWeb::VERSION}" ; exit }
    opt.parse!(arguments)

    if arguments.length == 0
      puts opt
      exit
    end
  end
  self.method_regex = Regexp.new(arguments[0])
  self.filenames = arguments[1..-1] || "."
end

#parse_filesObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/code_web/cli.rb', line 91

def parse_files
  filenames.each do |arg|
    arg = "#{arg}/**/*.rb" if Dir.exists?(arg)
    if File.exist?(arg)
      puts arg if debug?
      code_parser.parse arg
    else
      Dir[arg].each do |file_name|
        puts arg if debug?
        code_parser.parse(file_name)
      end
    end
  end
end

#runObject



52
53
54
55
56
# File 'lib/code_web/cli.rb', line 52

def run
  parse_arguments
  parse_files
  display_results
end

#verbose=(val) ⇒ Object



16
# File 'lib/code_web/cli.rb', line 16

def verbose=(val) ; code_parser.verbose = val ; end