| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2020-06-16 20:35:40 +02:00
										 |  |  | require "cask/cask_loader" | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-18 02:39:52 +02:00
										 |  |  | # Helper module for validating syntax in taps. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # @api private | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  | module Readall | 
					
						
							|  |  |  |   class << self | 
					
						
							|  |  |  |     def valid_ruby_syntax?(ruby_files) | 
					
						
							| 
									
										
										
										
											2020-11-23 18:15:48 +01:00
										 |  |  |       failed = T.let(false, T::Boolean) | 
					
						
							| 
									
										
										
										
											2019-04-18 14:46:40 +09:00
										 |  |  |       ruby_files.each do |ruby_file| | 
					
						
							|  |  |  |         # As a side effect, print syntax errors/warnings to `$stderr`. | 
					
						
							|  |  |  |         failed = true if syntax_errors_or_warnings?(ruby_file) | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |       !failed | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-22 17:08:36 +01:00
										 |  |  |     def valid_aliases?(alias_dir, formula_dir) | 
					
						
							| 
									
										
										
										
											2017-04-22 21:43:45 +01:00
										 |  |  |       return true unless alias_dir.directory? | 
					
						
							| 
									
										
										
										
											2017-04-22 17:08:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 18:15:48 +01:00
										 |  |  |       failed = T.let(false, T::Boolean) | 
					
						
							| 
									
										
										
										
											2017-04-22 17:08:36 +01:00
										 |  |  |       alias_dir.each_child do |f| | 
					
						
							|  |  |  |         if !f.symlink? | 
					
						
							|  |  |  |           onoe "Non-symlink alias: #{f}" | 
					
						
							|  |  |  |           failed = true | 
					
						
							|  |  |  |         elsif !f.file? | 
					
						
							|  |  |  |           onoe "Non-file alias: #{f}" | 
					
						
							|  |  |  |           failed = true | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (formula_dir/"#{f.basename}.rb").exist? | 
					
						
							|  |  |  |           onoe "Formula duplicating alias: #{f}" | 
					
						
							| 
									
										
										
										
											2017-02-21 11:31:18 -08:00
										 |  |  |           failed = true | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |       !failed | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def valid_formulae?(formulae) | 
					
						
							| 
									
										
										
										
											2020-11-23 18:15:48 +01:00
										 |  |  |       success = T.let(true, T::Boolean) | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |       formulae.each do |file| | 
					
						
							| 
									
										
										
										
											2019-10-13 10:03:26 +01:00
										 |  |  |         Formulary.factory(file) | 
					
						
							|  |  |  |       rescue Interrupt | 
					
						
							|  |  |  |         raise | 
					
						
							|  |  |  |       rescue Exception => e # rubocop:disable Lint/RescueException | 
					
						
							|  |  |  |         onoe "Invalid formula: #{file}" | 
					
						
							| 
									
										
										
										
											2020-06-16 20:35:40 +02:00
										 |  |  |         $stderr.puts e | 
					
						
							|  |  |  |         success = false | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-06-16 20:35:40 +02:00
										 |  |  |       success | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def valid_casks?(casks) | 
					
						
							| 
									
										
										
										
											2020-11-23 18:15:48 +01:00
										 |  |  |       success = T.let(true, T::Boolean) | 
					
						
							| 
									
										
										
										
											2020-06-16 20:35:40 +02:00
										 |  |  |       casks.each do |file| | 
					
						
							|  |  |  |         Cask::CaskLoader.load(file) | 
					
						
							|  |  |  |       rescue Interrupt | 
					
						
							|  |  |  |         raise | 
					
						
							|  |  |  |       rescue Exception => e # rubocop:disable Lint/RescueException | 
					
						
							|  |  |  |         onoe "Invalid cask: #{file}" | 
					
						
							|  |  |  |         $stderr.puts e | 
					
						
							|  |  |  |         success = false | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       success | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def valid_tap?(tap, options = {}) | 
					
						
							| 
									
										
										
										
											2020-06-16 20:35:40 +02:00
										 |  |  |       success = true | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |       if options[:aliases] | 
					
						
							| 
									
										
										
										
											2017-04-22 17:08:36 +01:00
										 |  |  |         valid_aliases = valid_aliases?(tap.alias_dir, tap.formula_dir) | 
					
						
							| 
									
										
										
										
											2020-06-16 20:35:40 +02:00
										 |  |  |         success = false unless valid_aliases | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |       end | 
					
						
							|  |  |  |       valid_formulae = valid_formulae?(tap.formula_files) | 
					
						
							| 
									
										
										
										
											2020-06-16 20:35:40 +02:00
										 |  |  |       valid_casks = valid_casks?(tap.cask_files) | 
					
						
							|  |  |  |       success = false if !valid_formulae || !valid_casks | 
					
						
							|  |  |  |       success | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def syntax_errors_or_warnings?(rb) | 
					
						
							| 
									
										
										
										
											2018-08-29 19:56:32 +02:00
										 |  |  |       # Retrieve messages about syntax errors/warnings printed to `$stderr`. | 
					
						
							|  |  |  |       _, err, status = system_command(RUBY_PATH, args: ["-c", "-w", rb], print_stderr: false) | 
					
						
							| 
									
										
										
										
											2018-07-19 23:56:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # Ignore unnecessary warning about named capture conflicts. | 
					
						
							|  |  |  |       # See https://bugs.ruby-lang.org/issues/12359. | 
					
						
							| 
									
										
										
										
											2018-08-29 19:56:32 +02:00
										 |  |  |       messages = err.lines | 
					
						
							|  |  |  |                     .grep_v(/named capture conflicts a local variable/) | 
					
						
							|  |  |  |                     .join | 
					
						
							| 
									
										
										
										
											2018-07-19 23:56:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |       $stderr.print messages | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Only syntax errors result in a non-zero status code. To detect syntax | 
					
						
							|  |  |  |       # warnings we also need to inspect the output to `$stderr`. | 
					
						
							| 
									
										
										
										
											2018-08-29 19:56:32 +02:00
										 |  |  |       !status.success? || !messages.chomp.empty? | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2020-06-16 20:35:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | require "extend/os/readall" |