| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  | # typed: true | 
					
						
							|  |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "cli/parser" | 
					
						
							|  |  |  | require "formula" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							|  |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sig { returns(CLI::Parser) } | 
					
						
							|  |  |  |   def generate_formula_api_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							|  |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2023-09-09 12:04:54 -04:00
										 |  |  |         Generate `homebrew/core` API data files for <#{HOMEBREW_API_WWW}>. | 
					
						
							| 
									
										
										
										
											2023-02-23 08:45:56 +00:00
										 |  |  |         The generated files are written to the current directory. | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  |       EOS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-13 14:49:33 -07:00
										 |  |  |       switch "-n", "--dry-run", description: "Generate API data without writing it to files." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  |       named_args :none | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-23 09:09:54 +00:00
										 |  |  |   FORMULA_JSON_TEMPLATE = <<~EOS | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  |     --- | 
					
						
							|  |  |  |     layout: formula_json | 
					
						
							|  |  |  |     --- | 
					
						
							|  |  |  |     {{ content }} | 
					
						
							|  |  |  |   EOS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def html_template(title) | 
					
						
							|  |  |  |     <<~EOS | 
					
						
							|  |  |  |       --- | 
					
						
							|  |  |  |       title: #{title} | 
					
						
							|  |  |  |       layout: formula | 
					
						
							| 
									
										
										
										
											2023-02-23 08:45:56 +00:00
										 |  |  |       redirect_from: /formula-linux/#{title} | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  |       --- | 
					
						
							|  |  |  |       {{ content }} | 
					
						
							|  |  |  |     EOS | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def generate_formula_api | 
					
						
							| 
									
										
										
										
											2023-05-13 14:49:33 -07:00
										 |  |  |     args = generate_formula_api_args.parse | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-23 08:45:56 +00:00
										 |  |  |     tap = CoreTap.instance | 
					
						
							| 
									
										
										
										
											2023-06-22 16:53:46 +01:00
										 |  |  |     raise TapUnavailableError, tap.name unless tap.installed? | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-13 14:49:33 -07:00
										 |  |  |     unless args.dry_run? | 
					
						
							|  |  |  |       directories = ["_data/formula", "api/formula", "formula"] | 
					
						
							|  |  |  |       FileUtils.rm_rf directories + ["_data/formula_canonical.json"] | 
					
						
							|  |  |  |       FileUtils.mkdir_p directories | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 16:53:46 +01:00
										 |  |  |     Homebrew.with_no_api_env do | 
					
						
							| 
									
										
										
										
											2023-07-18 12:37:32 +01:00
										 |  |  |       tap_migrations_json = JSON.dump(tap.tap_migrations) | 
					
						
							|  |  |  |       File.write("api/formula_tap_migrations.json", tap_migrations_json) unless args.dry_run? | 
					
						
							| 
									
										
										
										
											2023-07-14 02:04:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 16:53:46 +01:00
										 |  |  |       Formulary.enable_factory_cache! | 
					
						
							|  |  |  |       Formula.generating_hash! | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 16:53:46 +01:00
										 |  |  |       tap.formula_names.each do |name| | 
					
						
							|  |  |  |         formula = Formulary.factory(name) | 
					
						
							|  |  |  |         name = formula.name | 
					
						
							|  |  |  |         json = JSON.pretty_generate(formula.to_hash_with_variations) | 
					
						
							| 
									
										
										
										
											2023-07-18 12:37:32 +01:00
										 |  |  |         html_template_name = html_template(name) | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 16:53:46 +01:00
										 |  |  |         unless args.dry_run? | 
					
						
							|  |  |  |           File.write("_data/formula/#{name.tr("+", "_")}.json", "#{json}\n") | 
					
						
							|  |  |  |           File.write("api/formula/#{name}.json", FORMULA_JSON_TEMPLATE) | 
					
						
							| 
									
										
										
										
											2023-07-18 12:37:32 +01:00
										 |  |  |           File.write("formula/#{name}.html", html_template_name) | 
					
						
							| 
									
										
										
										
											2023-06-22 16:53:46 +01:00
										 |  |  |         end | 
					
						
							|  |  |  |       rescue | 
					
						
							|  |  |  |         onoe "Error while generating data for formula '#{name}'." | 
					
						
							|  |  |  |         raise | 
					
						
							| 
									
										
										
										
											2023-05-13 14:49:33 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 16:53:46 +01:00
										 |  |  |       canonical_json = JSON.pretty_generate(tap.formula_renames.merge(tap.alias_table)) | 
					
						
							|  |  |  |       File.write("_data/formula_canonical.json", "#{canonical_json}\n") unless args.dry_run? | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-02-22 15:33:16 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |