| 
									
										
										
										
											2017-05-03 11:33:00 +05:30
										 |  |  | #:  * `style` [`--fix`] [`--display-cop-names`] [`--only-cops=`[COP1,COP2..]|`--except-cops=`[COP1,COP2..]] [<files>|<taps>|<formulae>]: | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  | #:    Check formulae or files for conformance to Homebrew style guidelines. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    <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. | 
					
						
							|  |  |  | #: | 
					
						
							| 
									
										
										
										
											2016-09-18 14:28:19 +01:00
										 |  |  | #:    If `--fix` is passed, style violations will be automatically fixed using | 
					
						
							|  |  |  | #:    RuboCop's `--auto-correct` feature. | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  | #: | 
					
						
							|  |  |  | #:    If `--display-cop-names` is passed, the RuboCop cop name for each violation | 
					
						
							|  |  |  | #:    is included in the output. | 
					
						
							|  |  |  | #: | 
					
						
							| 
									
										
										
										
											2017-05-03 11:33:00 +05:30
										 |  |  | #:    If `--only-cops` is passed, only the given Rubocop cop(s)' violations would be checked. | 
					
						
							|  |  |  | #: | 
					
						
							|  |  |  | #:    If `--except-cops` is passed, the given Rubocop cop(s)' checks would be skipped. | 
					
						
							|  |  |  | #: | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  | #:    Exits with a non-zero status if any style violations are found. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "utils" | 
					
						
							| 
									
										
										
										
											2016-11-20 13:00:01 -05:00
										 |  |  | require "json" | 
					
						
							| 
									
										
										
										
											2017-05-29 01:21:36 +02:00
										 |  |  | require "open3" | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-02 13:36:26 +00:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-02 13:36:26 +00:00
										 |  |  |   def style | 
					
						
							|  |  |  |     target = if ARGV.named.empty? | 
					
						
							| 
									
										
										
										
											2016-09-27 18:48:06 +02:00
										 |  |  |       nil | 
					
						
							| 
									
										
										
										
											2015-08-06 15:39:08 +08:00
										 |  |  |     elsif ARGV.named.any? { |file| File.exist? file } | 
					
						
							|  |  |  |       ARGV.named | 
					
						
							| 
									
										
										
										
											2016-09-18 14:28:03 +01:00
										 |  |  |     elsif ARGV.named.any? { |tap| tap.count("/") == 1 } | 
					
						
							|  |  |  |       ARGV.named.map { |tap| Tap.fetch(tap).path } | 
					
						
							| 
									
										
										
										
											2015-01-02 13:36:26 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       ARGV.formulae.map(&:path) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 11:33:00 +05:30
										 |  |  |     only_cops = ARGV.value("only-cops").to_s.split(",") | 
					
						
							|  |  |  |     except_cops = ARGV.value("except-cops").to_s.split(",") | 
					
						
							|  |  |  |     if !only_cops.empty? && !except_cops.empty? | 
					
						
							|  |  |  |       odie "--only-cops and --except-cops cannot be used simultaneously!" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     options = { fix: ARGV.flag?("--fix") } | 
					
						
							|  |  |  |     if !only_cops.empty? | 
					
						
							|  |  |  |       options[:only_cops] = only_cops | 
					
						
							|  |  |  |     elsif !except_cops.empty? | 
					
						
							|  |  |  |       options[:except_cops] = except_cops | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Homebrew.failed = check_style_and_print(target, options) | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |   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] | 
					
						
							| 
									
										
										
										
											2017-05-03 12:09:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-07 17:28:39 +01:00
										 |  |  |     Homebrew.install_gem_setup_path! "rubocop", HOMEBREW_RUBOCOP_VERSION | 
					
						
							| 
									
										
										
										
											2017-05-03 12:09:18 +01:00
										 |  |  |     require "rubocop" | 
					
						
							|  |  |  |     require_relative "../rubocops" | 
					
						
							| 
									
										
										
										
											2015-01-02 13:36:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 13:32:46 +01:00
										 |  |  |     args = %w[
 | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |       --force-exclusion | 
					
						
							| 
									
										
										
										
											2015-03-13 11:09:32 +01:00
										 |  |  |     ] | 
					
						
							| 
									
										
										
										
											2017-05-28 16:59:53 +01:00
										 |  |  |     if fix | 
					
						
							|  |  |  |       args << "--auto-correct" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       args << "--parallel" | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-09-27 18:48:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 11:33:00 +05:30
										 |  |  |     if options[:except_cops] | 
					
						
							| 
									
										
										
										
											2017-05-03 12:58:08 +01:00
										 |  |  |       options[:except_cops].map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop.to_s, "") } | 
					
						
							| 
									
										
										
										
											2017-05-03 11:33:00 +05:30
										 |  |  |       cops_to_exclude = options[:except_cops].select do |cop| | 
					
						
							|  |  |  |         RuboCop::Cop::Cop.registry.names.include?(cop) || | 
					
						
							|  |  |  |           RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-04-23 04:09:13 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 11:33:00 +05:30
										 |  |  |       args << "--except" << cops_to_exclude.join(",") unless cops_to_exclude.empty? | 
					
						
							|  |  |  |     elsif options[:only_cops] | 
					
						
							| 
									
										
										
										
											2017-05-03 12:58:08 +01:00
										 |  |  |       options[:only_cops].map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop.to_s, "") } | 
					
						
							| 
									
										
										
										
											2017-05-03 11:33:00 +05:30
										 |  |  |       cops_to_include = options[:only_cops].select do |cop| | 
					
						
							|  |  |  |         RuboCop::Cop::Cop.registry.names.include?(cop) || | 
					
						
							|  |  |  |           RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-04-23 04:09:13 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 11:33:00 +05:30
										 |  |  |       args << "--only" << cops_to_include.join(",") unless cops_to_include.empty? | 
					
						
							| 
									
										
										
										
											2017-03-29 02:07:53 +05:30
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 18:48:06 +02:00
										 |  |  |     if files.nil? | 
					
						
							| 
									
										
										
										
											2016-09-28 22:34:09 +02:00
										 |  |  |       args << "--config" << HOMEBREW_LIBRARY_PATH/".rubocop.yml" | 
					
						
							| 
									
										
										
										
											2017-04-27 20:17:06 +02:00
										 |  |  |       args << HOMEBREW_LIBRARY_PATH | 
					
						
							| 
									
										
										
										
											2016-09-27 18:48:06 +02:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2016-10-01 12:13:09 +01:00
										 |  |  |       args << "--config" << HOMEBREW_LIBRARY/".rubocop.yml" | 
					
						
							| 
									
										
										
										
											2016-09-27 18:48:06 +02:00
										 |  |  |       args += files | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-03-13 11:09:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-28 16:59:53 +01:00
										 |  |  |     cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-28 22:47:14 +02:00
										 |  |  |     case output_type | 
					
						
							|  |  |  |     when :print | 
					
						
							|  |  |  |       args << "--display-cop-names" if ARGV.include? "--display-cop-names" | 
					
						
							| 
									
										
										
										
											2016-10-01 17:04:44 +01:00
										 |  |  |       args << "--format" << "simple" if files | 
					
						
							| 
									
										
										
										
											2017-05-28 16:59:53 +01:00
										 |  |  |       system(cache_env, "rubocop", *args) | 
					
						
							| 
									
										
										
										
											2016-09-28 22:47:14 +02:00
										 |  |  |       !$?.success? | 
					
						
							|  |  |  |     when :json | 
					
						
							| 
									
										
										
										
											2017-05-28 16:59:53 +01:00
										 |  |  |       json, _, status = Open3.capture3(cache_env, "rubocop", "--format", "json", *args) | 
					
						
							| 
									
										
										
										
											2017-03-08 09:26:07 +00:00
										 |  |  |       # exit status of 1 just means violations were found; other numbers mean | 
					
						
							|  |  |  |       # execution errors. | 
					
						
							| 
									
										
										
										
											2016-09-28 22:47:14 +02:00
										 |  |  |       # exitstatus can also be nil if RuboCop process crashes, e.g. due to | 
					
						
							| 
									
										
										
										
											2017-03-08 09:26:07 +00:00
										 |  |  |       # native extension problems. | 
					
						
							|  |  |  |       # JSON needs to be at least 2 characters. | 
					
						
							| 
									
										
										
										
											2017-05-29 01:21:36 +02:00
										 |  |  |       if !status.success? || json.to_s.length < 2
 | 
					
						
							| 
									
										
										
										
											2017-03-08 09:26:07 +00:00
										 |  |  |         raise "Error running `rubocop --format json #{args.join " "}`" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-11-20 13:00:01 -05:00
										 |  |  |       RubocopResults.new(JSON.parse(json)) | 
					
						
							| 
									
										
										
										
											2016-09-28 22:47:14 +02:00
										 |  |  |     else | 
					
						
							|  |  |  |       raise "Invalid output_type for check_style_impl: #{output_type}" | 
					
						
							| 
									
										
										
										
											2016-04-18 17:39:21 -04:00
										 |  |  |     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 |