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
|
|
|
|
Generates Formula API data files for formulae.brew.sh.
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
generate_formula_api_args.parse
|
|
|
|
|
2023-02-23 08:45:56 +00:00
|
|
|
tap = CoreTap.instance
|
2023-02-22 15:33:16 +00:00
|
|
|
|
|
|
|
directories = ["_data/formula", "api/formula", "formula"]
|
|
|
|
FileUtils.rm_rf directories + ["_data/formula_canonical.json"]
|
|
|
|
FileUtils.mkdir_p directories
|
|
|
|
|
|
|
|
Formulary.enable_factory_cache!
|
2023-03-21 19:05:41 -07:00
|
|
|
Formula.generating_hash!
|
2023-02-22 15:33:16 +00:00
|
|
|
|
|
|
|
tap.formula_names.each do |name|
|
|
|
|
formula = Formulary.factory(name)
|
|
|
|
name = formula.name
|
|
|
|
json = JSON.pretty_generate(formula.to_hash_with_variations)
|
|
|
|
|
|
|
|
File.write("_data/formula/#{name.tr("+", "_")}.json", "#{json}\n")
|
2023-02-23 09:09:54 +00:00
|
|
|
File.write("api/formula/#{name}.json", FORMULA_JSON_TEMPLATE)
|
2023-02-22 15:33:16 +00:00
|
|
|
File.write("formula/#{name}.html", html_template(name))
|
2023-02-24 16:57:20 +00:00
|
|
|
rescue
|
|
|
|
onoe "Error while generating data for formula '#{name}'."
|
|
|
|
raise
|
2023-02-22 15:33:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
canonical_json = JSON.pretty_generate(tap.formula_renames.merge(tap.alias_table))
|
|
|
|
File.write("_data/formula_canonical.json", "#{canonical_json}\n")
|
|
|
|
end
|
|
|
|
end
|