| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  | #:  * `style` [`--fix`] [`--display-cop-names`] [<formulae>|<files>]: | 
					
						
							|  |  |  | #:    Check formulae or files for conformance to Homebrew style guidelines. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    <formulae> is a list of formula names. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    <files> is a list of file names. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    <formulae> and <files> may not be combined. If both are omitted, style will run | 
					
						
							|  |  |  | #:    style checks on the whole Homebrew `Library`, including core code and all | 
					
						
							|  |  |  | #:    formulae. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    If `--fix` is passed and `HOMEBREW_DEVELOPER` is set, style violations | 
					
						
							|  |  |  | #:    will be automatically fixed using RuboCop's `--auto-correct` feature. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    If `--display-cop-names` is passed, the RuboCop cop name for each violation | 
					
						
							|  |  |  | #:    is included in the output. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    Exits with a non-zero status if any style violations are found. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "utils" | 
					
						
							|  |  |  | require "utils/json" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-02 13:36:26 +00:00
										 |  |  | module Homebrew | 
					
						
							|  |  |  |   def style | 
					
						
							|  |  |  |     target = if ARGV.named.empty? | 
					
						
							|  |  |  |       [HOMEBREW_LIBRARY] | 
					
						
							| 
									
										
										
										
											2015-08-06 15:39:08 +08:00
										 |  |  |     elsif ARGV.named.any? { |file| File.exist? file } | 
					
						
							|  |  |  |       ARGV.named | 
					
						
							| 
									
										
										
										
											2015-01-02 13:36:26 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       ARGV.formulae.map(&:path) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |     Homebrew.failed = check_style_and_print(target, :fix => ARGV.flag?("--fix")) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Checks style for a list of files, printing simple RuboCop output. | 
					
						
							|  |  |  |   # Returns true if violations were found, false otherwise. | 
					
						
							|  |  |  |   def check_style_and_print(files, options = {}) | 
					
						
							|  |  |  |     check_style_impl(files, :print, options) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Checks style for a list of files, returning results as a RubocopResults | 
					
						
							|  |  |  |   # object parsed from its JSON output. | 
					
						
							|  |  |  |   def check_style_json(files, options = {}) | 
					
						
							|  |  |  |     check_style_impl(files, :json, options) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def check_style_impl(files, output_type, options = {}) | 
					
						
							|  |  |  |     fix = options[:fix] | 
					
						
							| 
									
										
										
										
											2016-07-12 07:49:55 +02:00
										 |  |  |     Homebrew.install_gem_setup_path! "rubocop", "0.41.2" | 
					
						
							| 
									
										
										
										
											2015-01-02 13:36:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |     args = %W[
 | 
					
						
							|  |  |  |       --force-exclusion | 
					
						
							|  |  |  |       --config #{HOMEBREW_LIBRARY}/.rubocop.yml | 
					
						
							| 
									
										
										
										
											2015-03-13 11:09:32 +01:00
										 |  |  |     ] | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |     args << "--auto-correct" if ARGV.homebrew_developer? && fix | 
					
						
							|  |  |  |     args += files | 
					
						
							| 
									
										
										
										
											2015-03-13 11:09:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |     case output_type | 
					
						
							|  |  |  |     when :print | 
					
						
							|  |  |  |       args << "--display-cop-names" if ARGV.include? "--display-cop-names" | 
					
						
							|  |  |  |       system "rubocop", "--format", "simple", *args | 
					
						
							|  |  |  |       !$?.success? | 
					
						
							|  |  |  |     when :json | 
					
						
							|  |  |  |       json = Utils.popen_read_text("rubocop", "--format", "json", *args) | 
					
						
							| 
									
										
										
										
											2016-07-16 21:58:47 +01:00
										 |  |  |       # exit status of 1 just means violations were found; other numbers mean execution errors | 
					
						
							|  |  |  |       # exitstatus can also be nil if RuboCop process crashes, e.g. due to | 
					
						
							|  |  |  |       # native extension problems | 
					
						
							|  |  |  |       raise "Error while running RuboCop" if $?.exitstatus.nil? || $?.exitstatus > 1
 | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |       RubocopResults.new(Utils::JSON.load(json)) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       raise "Invalid output_type for check_style_impl: #{output_type}" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-03-13 11:09:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |   class RubocopResults | 
					
						
							|  |  |  |     def initialize(json) | 
					
						
							|  |  |  |       @metadata = json["metadata"] | 
					
						
							|  |  |  |       @file_offenses = {} | 
					
						
							|  |  |  |       json["files"].each do |f| | 
					
						
							|  |  |  |         next if f["offenses"].empty? | 
					
						
							|  |  |  |         file = File.realpath(f["path"]) | 
					
						
							|  |  |  |         @file_offenses[file] = f["offenses"].map { |x| RubocopOffense.new(x) } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def file_offenses(path) | 
					
						
							|  |  |  |       @file_offenses[path.to_s] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-03-13 11:09:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |   class RubocopOffense | 
					
						
							|  |  |  |     attr_reader :severity, :message, :corrected, :location, :cop_name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def initialize(json) | 
					
						
							|  |  |  |       @severity = json["severity"] | 
					
						
							|  |  |  |       @message = json["message"] | 
					
						
							|  |  |  |       @cop_name = json["cop_name"] | 
					
						
							|  |  |  |       @corrected = json["corrected"] | 
					
						
							|  |  |  |       @location = RubocopLineLocation.new(json["location"]) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def severity_code | 
					
						
							|  |  |  |       @severity[0].upcase | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def to_s(options = {}) | 
					
						
							|  |  |  |       if options[:display_cop_name] | 
					
						
							|  |  |  |         "#{severity_code}: #{location.to_short_s}: #{cop_name}: #{message}" | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         "#{severity_code}: #{location.to_short_s}: #{message}" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   class RubocopLineLocation | 
					
						
							|  |  |  |     attr_reader :line, :column, :length | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def initialize(json) | 
					
						
							|  |  |  |       @line = json["line"] | 
					
						
							|  |  |  |       @column = json["column"] | 
					
						
							|  |  |  |       @length = json["length"] | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def to_s | 
					
						
							|  |  |  |       "#{line}: col #{column} (#{length} chars)" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def to_short_s | 
					
						
							|  |  |  |       "#{line}: col #{column}" | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-01-02 13:36:26 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |