Class: SaikuroCMDLineRunner

Inherits:
Object
  • Object
show all
Includes:
ResultIndexGenerator
Defined in:
lib/saikuro/saikuro_cmd_line_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResultIndexGenerator

#list_analyzed_files, #print_summary_table_rows, #summarize_errors_and_warnings, #write_cyclo_index, #write_index, #write_token_index

Constructor Details

#initializeSaikuroCMDLineRunner

Returns a new instance of SaikuroCMDLineRunner.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 11

def initialize
  @opt = GetoptLong.new(
                       ["-o","--output_directory", GetoptLong::REQUIRED_ARGUMENT],
                       ["-h","--help", GetoptLong::NO_ARGUMENT],
                       ["-f","--formater", GetoptLong::REQUIRED_ARGUMENT],
                       ["-c","--cyclo", GetoptLong::NO_ARGUMENT],
                       ["-t","--token", GetoptLong::NO_ARGUMENT],
                       ["-y","--filter_cyclo", GetoptLong::REQUIRED_ARGUMENT],
                       ["-k","--filter_token", GetoptLong::REQUIRED_ARGUMENT],
                       ["-w","--warn_cyclo", GetoptLong::REQUIRED_ARGUMENT],
                       ["-s","--warn_token", GetoptLong::REQUIRED_ARGUMENT],
                       ["-e","--error_cyclo", GetoptLong::REQUIRED_ARGUMENT],
                       ["-d","--error_token", GetoptLong::REQUIRED_ARGUMENT],
                       ["-p","--parse_file", GetoptLong::REQUIRED_ARGUMENT],
                       ["-i","--input_directory", GetoptLong::REQUIRED_ARGUMENT],
                       ["-v","--verbose", GetoptLong::NO_ARGUMENT]
                       )
  self.output_dir = "./"
  self.formater = "html"
  self.comp_state = self.comp_token = false
end

Instance Attribute Details

#comp_stateObject

Returns the value of attribute comp_state.



9
10
11
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 9

def comp_state
  @comp_state
end

#comp_tokenObject

Returns the value of attribute comp_token.



9
10
11
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 9

def comp_token
  @comp_token
end

#formaterObject

Returns the value of attribute formater.



9
10
11
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 9

def formater
  @formater
end

#output_dirObject

Returns the value of attribute output_dir.



9
10
11
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 9

def output_dir
  @output_dir
end

#state_formaterObject

Returns the value of attribute state_formater.



9
10
11
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 9

def state_formater
  @state_formater
end

#token_count_formaterObject

Returns the value of attribute token_count_formater.



9
10
11
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 9

def token_count_formater
  @token_count_formater
end

Instance Method Details

#analyze(files) ⇒ Object



57
58
59
60
61
62
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 57

def analyze(files)
  Saikuro.analyze(files,
    state_formater,
    token_count_formater,
    output_dir)
end

#get_ruby_files(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 33

def get_ruby_files(path)
  files = Array.new
  Find.find(path) do |f|
    if !FileTest.directory?(f)
  if f =~ /rb$/
 files<< f
  end
    end
  end
  files
end

#no_complexity_token_or_state?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 149

def no_complexity_token_or_state?
  !comp_state && !comp_token
end

#parse_opts(state_filter, token_filter, files) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 69

def parse_opts(state_filter, token_filter, files)
  @opt.each do |arg,val|
    case arg
    when "-o"  then self.output_dir = val
    when "-h"  then usage('help')
    when "-f"  then self.formater = val
    when "-c"  then self.comp_state = true
    when "-t"  then self.comp_token = true
    when "-k"  then token_filter.limit = val.to_i
    when "-s"  then token_filter.warn = val.to_i
    when "-d"  then token_filter.error = val.to_i
    when "-y"  then state_filter.limit = val.to_i
    when "-w"  then state_filter.warn = val.to_i
    when "-e"  then state_filter.error = val.to_i
    when "-p"  then files<< val
    when "-i"  then files.concat(get_ruby_files(val))
    when "-v"
      STDOUT.puts "Verbose mode on"
      $VERBOSE = true
    end
  end
  usage('no complexity token or state set') if no_complexity_token_or_state?
rescue => err
  usage([err.class,err.message,err.backtrace[0..15]].join(', '))
end

#runObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 45

def run
  files = Array.new
  state_filter = Filter.new(5)
  token_filter = Filter.new(10, 25, 50)

  parse_opts(state_filter, token_filter, files)
  set_formatters(state_filter, token_filter)

  idx_states, idx_tokens = analyze(files)
  write_results(idx_states, idx_tokens)
end

#set_formatters(state_filter, token_filter) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 153

def set_formatters(state_filter, token_filter)
  if formater =~ /html/i
    self.state_formater = StateHTMLComplexityFormater.new(STDOUT,state_filter)
    self.token_count_formater = HTMLTokenCounterFormater.new(STDOUT,token_filter)
  else
    self.state_formater = ParseStateFormater.new(STDOUT,state_filter)
    self.token_count_formater = TokenCounterFormater.new(STDOUT,token_filter)
  end

  self.state_formater = nil if !comp_state
  self.token_count_formater = nil if !comp_token
end

#usage(message) ⇒ Object



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
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 95

def usage(message)
usage = "== Usage\n\nsaikuro [ -h ] [-o output_directory] [-f type] [ -c, -t ]\n[ -y, -w, -e, -k, -s, -d - number ] ( -p file | -i directory )\n\n== Help\n\n-o, --output_directory (directory) : A directory to ouput the results in.\nThe current directory is used if this option is not passed.\n\n-h, --help : This help message.\n\n-f, --formater (html | text) : The format to output the results in.\nThe default is html\n\n-c, --cyclo : Compute the cyclomatic complexity of the input.\n\n-t, --token : Count the number of tokens per line of the input.\n\n-y, --filter_cyclo (number) : Filter the output to only include methods\nwhose cyclomatic complexity are greater than the passed number.\n\n-w, --warn_cyclo (number) : Highlight with a warning methods whose\ncyclomatic complexity are greather than or equal to the passed number.\n\n\n-e, --error_cyclo (number) : Highligh with an error methods whose\ncyclomatic complexity are greather than or equal to the passed number.\n\n\n-k, --filter_token (number) : Filter the output to only include lines\nwhose token count are greater than the passed number.\n\n\n-s, --warn_token (number) : Highlight with a warning lines whose\ntoken count are greater than or equal to the passed number.\n\n\n-d, --error_token (number) : Highlight with an error lines whose\ntoken count are greater than or equal to the passed number.\n\n\n-p, --parse_file (file) : A file to use as input.\n\n-i, --input_directory (directory) : All ruby files found recursively\ninside the directory are passed as input.\n"
STDOUT.puts usage
STDOUT.puts
STDOUT.puts message
end

#write_results(idx_states, idx_tokens) ⇒ Object



64
65
66
67
# File 'lib/saikuro/saikuro_cmd_line_runner.rb', line 64

def write_results(idx_states, idx_tokens)
  write_cyclo_index(idx_states, output_dir)
  write_token_index(idx_tokens, output_dir)
end