| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | require "English" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  | module Hbc | 
					
						
							|  |  |  |   class CLI | 
					
						
							|  |  |  |     class Style < Base | 
					
						
							|  |  |  |       def self.help | 
					
						
							|  |  |  |         "checks Cask style using RuboCop" | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def self.run(*args) | 
					
						
							|  |  |  |         retval = new(args).run | 
					
						
							|  |  |  |         raise CaskError, "style check failed" unless retval | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       attr_reader :args | 
					
						
							|  |  |  |       def initialize(args) | 
					
						
							|  |  |  |         @args = args | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def run | 
					
						
							|  |  |  |         install_rubocop | 
					
						
							|  |  |  |         system "rubocop", *rubocop_args, "--", *cask_paths | 
					
						
							|  |  |  |         $CHILD_STATUS.success? | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-14 20:57:00 -04:00
										 |  |  |       RUBOCOP_CASK_VERSION = "~> 0.10.4".freeze | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def install_rubocop | 
					
						
							|  |  |  |         Utils.capture_stderr do | 
					
						
							|  |  |  |           begin | 
					
						
							|  |  |  |             Homebrew.install_gem_setup_path! "rubocop-cask", RUBOCOP_CASK_VERSION, "rubocop" | 
					
						
							|  |  |  |           rescue SystemExit | 
					
						
							| 
									
										
										
										
											2016-08-26 16:04:47 +02:00
										 |  |  |             raise CaskError, Tty.strip_ansi($stderr.string).chomp.sub(/\AError: /, "") | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def cask_paths | 
					
						
							|  |  |  |         @cask_paths ||= if cask_tokens.empty? | 
					
						
							| 
									
										
										
										
											2016-10-14 20:11:33 +02:00
										 |  |  |           Hbc.all_tapped_cask_dirs | 
					
						
							|  |  |  |         elsif cask_tokens.any? { |file| File.exist?(file) } | 
					
						
							|  |  |  |           cask_tokens | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           cask_tokens.map { |token| Hbc.path(token) } | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def cask_tokens | 
					
						
							|  |  |  |         @cask_tokens ||= self.class.cask_tokens_from(args) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def rubocop_args | 
					
						
							|  |  |  |         fix? ? autocorrect_args : default_args | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def default_args | 
					
						
							| 
									
										
										
										
											2016-10-18 16:10:36 +02:00
										 |  |  |         [ | 
					
						
							|  |  |  |           "--require", "rubocop-cask", | 
					
						
							| 
									
										
										
										
											2016-10-18 16:39:25 +02:00
										 |  |  |           "--config", "/dev/null", # always use `rubocop-cask` default config | 
					
						
							| 
									
										
										
										
											2016-10-18 16:10:36 +02:00
										 |  |  |           "--format", "simple", | 
					
						
							|  |  |  |           "--force-exclusion" | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def autocorrect_args | 
					
						
							|  |  |  |         default_args + ["--auto-correct"] | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       def fix? | 
					
						
							| 
									
										
										
										
											2016-10-14 20:03:34 +02:00
										 |  |  |         args.any? { |arg| arg =~ /--(fix|(auto-?)?correct)/ } | 
					
						
							| 
									
										
										
										
											2016-09-24 13:52:43 +02:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | end |