| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  | require "formula" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Readall | 
					
						
							|  |  |  |   class << self | 
					
						
							|  |  |  |     def valid_ruby_syntax?(ruby_files) | 
					
						
							|  |  |  |       ruby_files_queue = Queue.new | 
					
						
							|  |  |  |       ruby_files.each { |f| ruby_files_queue << f } | 
					
						
							|  |  |  |       failed = false | 
					
						
							|  |  |  |       workers = (0...Hardware::CPU.cores).map do | 
					
						
							|  |  |  |         Thread.new do | 
					
						
							| 
									
										
										
										
											2016-08-22 08:52:42 +02:00
										 |  |  |           Kernel.loop do | 
					
						
							|  |  |  |             begin | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |               # As a side effect, print syntax errors/warnings to `$stderr`. | 
					
						
							| 
									
										
										
										
											2016-08-22 08:52:42 +02:00
										 |  |  |               failed = true if syntax_errors_or_warnings?(ruby_files_queue.deq(true)) | 
					
						
							|  |  |  |             rescue ThreadError | 
					
						
							|  |  |  |               break | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       workers.each(&:join) | 
					
						
							|  |  |  |       !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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |       failed = false | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  |       failed = false | 
					
						
							|  |  |  |       formulae.each do |file| | 
					
						
							|  |  |  |         begin | 
					
						
							|  |  |  |           Formulary.factory(file) | 
					
						
							|  |  |  |         rescue Interrupt | 
					
						
							|  |  |  |           raise | 
					
						
							| 
									
										
										
										
											2017-10-07 00:31:28 +02:00
										 |  |  |         rescue Exception => e # rubocop:disable Lint/RescueException | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |           onoe "Invalid formula: #{file}" | 
					
						
							|  |  |  |           puts e | 
					
						
							|  |  |  |           failed = true | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       !failed | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def valid_tap?(tap, options = {}) | 
					
						
							|  |  |  |       failed = false | 
					
						
							|  |  |  |       if options[:aliases] | 
					
						
							| 
									
										
										
										
											2017-04-22 17:08:36 +01:00
										 |  |  |         valid_aliases = valid_aliases?(tap.alias_dir, tap.formula_dir) | 
					
						
							| 
									
										
										
										
											2016-07-09 13:51:19 +01:00
										 |  |  |         failed = true unless valid_aliases | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       valid_formulae = valid_formulae?(tap.formula_files) | 
					
						
							|  |  |  |       failed = true unless valid_formulae | 
					
						
							|  |  |  |       !failed | 
					
						
							|  |  |  |     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 |