maximus 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +1,30 @@
1
1
  require 'json'
2
2
 
3
3
  module Maximus
4
+ # @since 0.1.0
4
5
  class Lint
5
6
  attr_accessor :output
6
7
 
7
8
  include Helper
8
9
 
9
- # opts - Lint options (default: {})
10
- # :is_dev
11
- # :git_files
12
- # :root_dir
13
- # :path
14
- # all lints should start with the following defined:
15
- # def result method to handle the actual parsing
16
- # TODO - should this be def output to be more consistent?
17
- # Didn't want to trip over the instance variable @output
18
- # `@task = 'name'` (__method__.to_s works fine)
19
- # `@path ||= 'path/or/**/glob/to/files'` (string)
20
- # they should contain the data output from the linter in JSON or
21
- # JSON.parse format with the styles defined in README.md
22
- # they should end with something similar to
23
- # `@output[:files_inspected] ||= files_inspected(extension, delimiter, base_path_replacement)`
24
- # `refine data_from_output`
10
+ # Perform a lint of relevant code
11
+ #
12
+ # All defined lints require a "result" method
13
+ # @example the result method in the child class
14
+ # def result(opts = {})
15
+ # @task = __method__.to_s
16
+ # @path ||= 'path/or/**/glob/to/files''
17
+ # lint_data = JSON.parse(`some-command-line-linter`)
18
+ # @output[:files_inspected] ||= files_inspected(extension, delimiter, base_path_replacement)
19
+ # refine data_from_output
20
+ # end
21
+ #
22
+ # @param opts [Hash] the options to create a lint with
23
+ # @option opts [Boolean] :is_dev (false) whether or not the class was initialized from the command line
24
+ # @option opts [Array<String, Symbol>] :git_files files returned from the commit
25
+ # @option opts [String] :root_dir base directory
26
+ # @option opts [String, Array] :path ('') path to files. Accepts glob notation
27
+ # @return @output [Hash] combined and refined data from lint
25
28
  def initialize(opts = {})
26
29
  opts[:is_dev] ||= false
27
30
  opts[:root_dir] ||= root_dir
@@ -35,7 +38,9 @@ module Maximus
35
38
  end
36
39
 
37
40
  # Convert raw data into warnings, errors, conventions or refactors. Use this wisely.
38
- # Returns complete @output as Hash
41
+ #
42
+ # @param data [Hash] unfiltered lint data
43
+ # @return [Hash] refined lint data and all the other bells and whistles
39
44
  def refine(data)
40
45
  # Prevent abortive empty JSON.parse error
41
46
  data = '{}' if data.blank?
@@ -77,7 +82,7 @@ module Maximus
77
82
  @output[:lint_refactors] = lint_refactors
78
83
  lint_count = (lint_errors.length + lint_warnings.length + lint_conventions.length + lint_refactors.length)
79
84
  if @@is_dev
80
- lint_dev_format data unless data.blank?
85
+ puts lint_dev_format(data) unless data.blank?
81
86
  puts lint_summarize
82
87
  lint_ceiling lint_count
83
88
  else
@@ -92,13 +97,18 @@ module Maximus
92
97
  protected
93
98
 
94
99
  # List all files inspected
95
- # Returns Array
96
- def files_inspected(ext, delimiter = ',', replace = @opts[:root_dir])
97
- @path.is_a?(Array) ? @path.split(delimiter) : file_list(@path, ext, replace)
100
+ #
101
+ # @param ext [String] extension to search for
102
+ # @param delimiter [String] comma or space separated
103
+ # @param remove [String] remove from all file names
104
+ # @return all_files [Array<string>] list of file names
105
+ def files_inspected(ext, delimiter = ',', remove = @opts[:root_dir])
106
+ @path.is_a?(Array) ? @path.split(delimiter) : file_list(@path, ext, remove)
98
107
  end
99
108
 
100
109
  # Compare lint output with lines changed in commit
101
- # Returns Array of lints that match the lines in commit
110
+ #
111
+ # @return [Array] lints that match the lines in commit
102
112
  def relevant_lints(lint, files)
103
113
  all_files = {}
104
114
  files.each do |file|
@@ -112,7 +122,7 @@ module Maximus
112
122
  unless lint_file.blank?
113
123
  all_files[revert_name] = []
114
124
 
115
- # TODO - originally I tried .map and delete_if, but this works,
125
+ # @todo originally I tried .map and delete_if, but this works,
116
126
  # and the other method didn't cover all bases.
117
127
  # Gotta be a better way to write this though
118
128
  lint_file.each do |l|
@@ -136,7 +146,8 @@ module Maximus
136
146
  private
137
147
 
138
148
  # Send abbreviated results to console or to the log
139
- # Returns console message
149
+ #
150
+ # @return [String] console message to display
140
151
  def lint_summarize
141
152
  puts "\n" if @@is_dev
142
153
 
@@ -154,9 +165,12 @@ module Maximus
154
165
  success
155
166
  end
156
167
 
157
- # If there's just too much to handle, through a warning. MySQL may not store everything and throw an abortive error if the blob is too large
158
- # Returns prompt if lint_length is greater than 100
168
+ # If there's just too much to handle, through a warning.
169
+ # MySQL may not store all the data and throw an abortive error if the blob is too large
159
170
  # If prompt returns truthy, execution continues
171
+ #
172
+ # @param lint_length [Integer] count of how many lints
173
+ # @return [String] console message to display
160
174
  def lint_ceiling(lint_length)
161
175
  if lint_length > 100
162
176
  lint_dev_format
@@ -170,9 +184,12 @@ module Maximus
170
184
  end
171
185
  end
172
186
 
173
- # Dev display, used for the rake task
174
- # Returns console message
187
+ # Dev display, executed only when called from command line
188
+ #
189
+ # @param errors [Hash] data from lint
190
+ # @return [String] console message to display
175
191
  def lint_dev_format(errors = @output[:raw_data])
192
+ return if errors.blank?
176
193
  pretty_output = ''
177
194
  errors.each do |filename, error_list|
178
195
  pretty_output += "\n"
@@ -194,7 +211,7 @@ module Maximus
194
211
  pretty_output += "\n"
195
212
  end
196
213
  end
197
- puts pretty_output
214
+ pretty_output
198
215
  end
199
216
 
200
217
  end
@@ -1,7 +1,10 @@
1
1
  module Maximus
2
+ # @since 0.1.0
2
3
  class Brakeman < Maximus::Lint
3
4
 
4
5
  # Brakeman (requires Rails)
6
+ #
7
+ # @see Lint#initialize
5
8
  def result
6
9
 
7
10
  return unless @@is_rails
@@ -35,7 +38,10 @@ module Maximus
35
38
  end
36
39
  end
37
40
  end
38
- brakeman = JSON.parse(brakeman.to_json) #don't event ask
41
+ # The output of brakeman is a mix of strings and symbols
42
+ # but resetting the JSON like this standardizes everything.
43
+ # @todo Better way to get around this?
44
+ brakeman = JSON.parse(brakeman.to_json)
39
45
  end
40
46
 
41
47
  @output[:files_inspected] ||= files_inspected('rb', ' ')
@@ -45,7 +51,10 @@ module Maximus
45
51
 
46
52
  private
47
53
 
48
- # Convert to maximus format
54
+ # Convert to {file:README.md Maximus format}
55
+ #
56
+ # @param error [Hash] lint error
57
+ # @return [Hash]
49
58
  def hash_for_brakeman(error, type)
50
59
  {
51
60
  linter: error['warning_type'],
@@ -1,7 +1,10 @@
1
1
  module Maximus
2
+ # @since 0.1.0
2
3
  class Jshint < Maximus::Lint
3
4
 
4
5
  # JSHint (requires node module)
6
+ #
7
+ # @see Lint#initialize
5
8
  def result
6
9
  @task = 'jshint'
7
10
  @path ||= @@is_rails ? "#{@opts[:root_dir]}/app/assets" : "#{@opts[:root_dir]}source/assets"
@@ -1,7 +1,10 @@
1
1
  module Maximus
2
+ # @since 0.1.0
2
3
  class Railsbp < Maximus::Lint
3
4
 
4
5
  # rails_best_practice (requires Rails)
6
+ #
7
+ # @see Lint#initialize
5
8
  def result
6
9
 
7
10
  return unless @@is_rails
@@ -26,7 +29,10 @@ module Maximus
26
29
  railsbp[file.gsub(Rails.root.to_s, '')[1..-1].to_sym] = errors.map { |o| hash_for_railsbp(o) }
27
30
  end
28
31
  end
29
- railsbp = JSON.parse(railsbp.to_json) #don't event ask
32
+ # The output of railsbp is a mix of strings and symbols
33
+ # but resetting the JSON like this standardizes everything.
34
+ # @todo Better way to get around this?
35
+ railsbp = JSON.parse(railsbp.to_json)
30
36
  end
31
37
 
32
38
  @output[:files_inspected] ||= files_inspected('rb', ' ')
@@ -36,7 +42,10 @@ module Maximus
36
42
 
37
43
  private
38
44
 
39
- # Convert to maximus format
45
+ # Convert to {file:README.md Maximus format}
46
+ #
47
+ # @param error [Hash] lint error
48
+ # @return [Hash]
40
49
  def hash_for_railsbp(error)
41
50
  {
42
51
  linter: error['message'].gsub(/\((.*)\)/, '').strip.parameterize('_').camelize,
@@ -1,7 +1,10 @@
1
1
  module Maximus
2
+ # @since 0.1.0
2
3
  class Rubocop < Maximus::Lint
3
4
 
4
5
  # RuboCop
6
+ #
7
+ # @see Lint#initialize
5
8
  def result
6
9
  @task = 'rubocop'
7
10
  @path ||= @@is_rails ? "#{@opts[:root_dir]}/app" : "#{@opts[:root_dir]}/*.rb"
@@ -1,7 +1,10 @@
1
1
  module Maximus
2
+ # @since 0.1.0
2
3
  class Scsslint < Maximus::Lint
3
4
 
4
5
  # SCSS-Lint
6
+ #
7
+ # @see Lint#initialize
5
8
  def result
6
9
  @task = 'scsslint'
7
10
  @path ||= @@is_rails ? "#{@opts[:root_dir]}/app/assets/stylesheets" : "#{@opts[:root_dir]}/source/assets/stylesheets"
@@ -1,7 +1,7 @@
1
1
  #!/bin/bash
2
2
 
3
3
  # http://stackoverflow.com/questions/8259851/using-git-diff-how-can-i-get-added-and-modified-lines-numbers
4
- # TODO - this function is tricky because I don't fully understand regex in BASH. Also, getting line hunks from git is tricky. This will likely need to be refactored
4
+ # @todo this function is tricky because I don't fully understand regex in BASH. Also, getting line hunks from git is tricky. This will likely need to be refactored
5
5
  function lines-added(){
6
6
  local path=
7
7
  local line=
@@ -1,20 +1,28 @@
1
-
2
1
  module Maximus
2
+ # @since 0.1.0
3
3
  class Statistic
4
4
  attr_accessor :output
5
5
 
6
6
  include Helper
7
7
 
8
- # opts - Lint options (default: {})
9
- # :is_dev - wether or not this is being called by a rake task
10
- # :root_dir - absolute path to working directory (optional)
11
- # :port - 4-digit port number (optional)
12
- # :base_url - standard domain with http:// (default: http://localhost:3000) (optional)
13
- # :path - default set in methods (optional)
14
- # All statistics should have the following:
15
- # def result method to handle the actual parsing
16
- # TODO - should this be def output to be more consistent?
17
- # Didn't want to trip over the instance variable @output
8
+ # Gather info about how the code performs
9
+ #
10
+ # All defined statistics require a "result" method
11
+ # @example the result method in the child class
12
+ # def result(opts = {})
13
+ # @path ||= 'path/or/**/glob/to/files''
14
+ # stat_data = JSON.parse(`some-command-line-stat-runner`)
15
+ # @output
16
+ # end
17
+ #
18
+ # @param opts [Hash] the options to create a lint with.
19
+ # @option opts [Boolean] :is_dev (false) whether or not the class was initialized from the command line
20
+ # @option opts [String] :root_dir base directory
21
+ # @option opts [String] :base_url ('http://localhost:3000/') the host
22
+ # @option opts [String, Integer] :port port number
23
+ # @option opts [String, Array] :path ('') path to files. Accepts glob notation
24
+ #
25
+ # @return output [Hash] combined and refined data from statistic
18
26
  def initialize(opts = {})
19
27
  opts[:is_dev] ||= false
20
28
  opts[:root_dir] ||= root_dir
@@ -38,6 +46,7 @@ module Maximus
38
46
  protected
39
47
 
40
48
  # Organize stat output on the @output variable
49
+ #
41
50
  # Adds @output[:statistics][:filepath] with all statistic data
42
51
  def refine_stats(stats_cli, file_path)
43
52
 
@@ -50,10 +59,10 @@ module Maximus
50
59
  stats = JSON.parse(stats_cli)
51
60
  @output[:statistics][file_path.to_sym] ||= {}
52
61
 
53
- # TODO - is there a better way to do this?
62
+ # @todo is there a better way to do this?
54
63
  fp = @output[:statistics][file_path.to_s.to_sym]
55
64
 
56
- # TODO - Can I do like a self << thing here?
65
+ # @todo Can I do like a self << thing here?
57
66
  stats.each do |stat, value|
58
67
  fp[stat.to_sym] = value
59
68
  end
@@ -1,8 +1,10 @@
1
1
  module Maximus
2
+ # @since 0.1.0
2
3
  class Phantomas < Maximus::Statistic
3
4
 
4
- # path can be array or string of URLS. Include http://
5
- # By default, checks homepage
5
+ # Phantomas evaluates page performance with phantomjs and node
6
+ #
7
+ # @see Statistic#initialize
6
8
  def result
7
9
 
8
10
  node_module_exists('phantomjs', 'brew install')
@@ -22,6 +24,7 @@ module Maximus
22
24
 
23
25
  # Organize stat output on the @output variable
24
26
  # Adds @output[:statistics][:filepath] with all statistic data
27
+ # @return [void] goes to refine statistics
25
28
  def phantomas_by_url(url, phantomas_cli)
26
29
  puts "Phantomas on #{@opts[:base_url] + url}".color(:green)
27
30
  phantomas = `#{phantomas_cli} #{@opts[:base_url] + url}`
@@ -1,8 +1,14 @@
1
1
  module Maximus
2
+ # @since 0.1.0
2
3
  class Stylestats < Maximus::Statistic
3
4
 
4
5
  # @path array preferrably absolute paths, but relative should work
5
- # If stylestatting one file, pass that as an array, i.e. ['/absolute/to/public/assets/application.css']
6
+ # If stylestatting one file, pass that as an array
7
+ #
8
+ # @example stylestat one file
9
+ # Maximus::Stylestats.new({path: ['/absolute/to/public/assets/application.css']}).result
10
+ #
11
+ # @see Statistic#initialize
6
12
  def result
7
13
 
8
14
  node_module_exists('stylestats')
@@ -27,10 +33,10 @@ module Maximus
27
33
  end
28
34
 
29
35
  if @@is_rails
30
- # TODO - I'd rather Rake::Task but it's not working in different directories
36
+ # @todo I'd rather Rake::Task but it's not working in different directories
31
37
  Dir.chdir(@opts[:root_dir]) do
32
38
  if @@is_dev
33
- # TODO - review that this may not be best practice, but it's really noisy in the console
39
+ # @todo review that this may not be best practice, but it's really noisy in the console
34
40
  quietly { `rake assets:clobber` }
35
41
  else
36
42
  `rake assets:clobber`
@@ -45,11 +51,11 @@ module Maximus
45
51
 
46
52
  private
47
53
 
48
- # Find all CSS files
49
- # Will compile using sprockets if Rails
50
- # Will compile using built-in Sass engine otherwise
51
- # Compass friendly
52
- # Returns Array of CSS files
54
+ # Find all CSS files or compile.
55
+ # Uses sprockets if Rails; Sass engine otherwise.
56
+ # Compass is supported
57
+ #
58
+ # @return [Array] CSS files
53
59
  def find_css_files
54
60
  searched_files = []
55
61
 
@@ -60,10 +66,10 @@ module Maximus
60
66
  puts "\n"
61
67
  puts 'Compiling assets for stylestats...'.color(:blue)
62
68
 
63
- # TODO - I'd rather Rake::Task but it's not working in different directories
69
+ # @todo I'd rather Rake::Task but it's not working in different directories
64
70
  Dir.chdir(@opts[:root_dir]) do
65
71
  if @@is_dev
66
- # TODO - review that this may not be best practice, but it's really noisy in the console
72
+ # @todo review that this may not be best practice, but it's really noisy in the console
67
73
  quietly { `rake assets:precompile` }
68
74
  else
69
75
  `rake assets:precompile`
@@ -93,7 +99,7 @@ module Maximus
93
99
  @path += ".scss"
94
100
 
95
101
  Dir[@path].select { |f| File.file? f }.each do |file|
96
- # TODO - don't compile file if it starts with an underscore
102
+ # @todo don't compile file if it starts with an underscore
97
103
  scss_file = File.open(file, 'rb') { |f| f.read }
98
104
 
99
105
  output_file = File.open( file.split('.').reverse.drop(1).reverse.join('.'), "w" )
@@ -1,11 +1,12 @@
1
1
  module Maximus
2
+ # @since 0.1.0
2
3
  class Wraith < Maximus::Statistic
3
4
 
4
5
  # By default checks homepage
5
6
  # Requires config to be in config/wraith/history.yaml
6
7
  # Adds a new config/wraith/history.yaml if not present
7
8
  # Path should be an Array defined as [{ label: url }]
8
- # Returns Hash as defined in the wraith_parse method
9
+ # @see Statistic#initialize
9
10
  def result
10
11
 
11
12
  node_module_exists('phantomjs', 'brew install')
@@ -29,7 +30,7 @@ module Maximus
29
30
  wraith_yaml_reset
30
31
 
31
32
  # If the paths have been updated, call a timeout and run history again
32
- # TODO - this doesn't work very well. It puts the new shots in the history folder,
33
+ # @todo this doesn't work very well. It puts the new shots in the history folder,
33
34
  # even with absolute paths. Could be a bug in wraith
34
35
  YAML.load_file(@wraith_config_file)['paths'].each do |label, url|
35
36
  edit_yaml(@wraith_config_file) do |file|
@@ -53,7 +54,7 @@ module Maximus
53
54
  # Get a diff percentage of all changes by label and screensize
54
55
  # { path: { percent_changed: [{ size: percent_diff }] } }
55
56
  # Example {:statistics=>{:/=>{:percent_changed=>[{1024=>0.0}, {767=>0.0}, {1024=>0.0}, {767=>0.0}, {1024=>0.0}, {767=>0.0}, {1024=>0.0}, {767=>0.0}] } }}
56
- # Returns Hash
57
+ # @return [Hash]
57
58
  def wraith_parse(wraith_config_file = @wraith_config_file)
58
59
  paths = YAML.load_file(wraith_config_file)['paths']
59
60
  Dir.glob("#{@opts[:root_dir]}/maximus_wraith/**/*.txt").select { |f| File.file? f }.each do |file|
@@ -69,7 +70,10 @@ module Maximus
69
70
  @output
70
71
  end
71
72
 
72
- # Update the root domain (docker ports and addresses may change) and set paths as defined in @path
73
+ # Update the root domain (docker ports and addresses may change)
74
+ # and set paths as defined in @path
75
+ #
76
+ # @return [void] but updates instance variables
73
77
  def wraith_yaml_reset(wraith_config_file = @wraith_config_file)
74
78
  edit_yaml(wraith_config_file) do |file|
75
79
  unless @@is_dev