Port Homebrew::DevCmd::GenerateFormulaApi
This commit is contained in:
parent
afceaec076
commit
7fc20640f2
@ -5,79 +5,79 @@ require "cli/parser"
|
|||||||
require "formula"
|
require "formula"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
module DevCmd
|
||||||
|
class GenerateFormulaApi < AbstractCommand
|
||||||
sig { returns(CLI::Parser) }
|
FORMULA_JSON_TEMPLATE = <<~EOS
|
||||||
def generate_formula_api_args
|
---
|
||||||
Homebrew::CLI::Parser.new do
|
layout: formula_json
|
||||||
description <<~EOS
|
---
|
||||||
Generate `homebrew/core` API data files for <#{HOMEBREW_API_WWW}>.
|
{{ content }}
|
||||||
The generated files are written to the current directory.
|
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
switch "-n", "--dry-run", description: "Generate API data without writing it to files."
|
cmd_args do
|
||||||
|
description <<~EOS
|
||||||
|
Generate `homebrew/core` API data files for <#{HOMEBREW_API_WWW}>.
|
||||||
|
The generated files are written to the current directory.
|
||||||
|
EOS
|
||||||
|
|
||||||
named_args :none
|
switch "-n", "--dry-run", description: "Generate API data without writing it to files."
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
FORMULA_JSON_TEMPLATE = <<~EOS
|
named_args :none
|
||||||
---
|
|
||||||
layout: formula_json
|
|
||||||
---
|
|
||||||
{{ content }}
|
|
||||||
EOS
|
|
||||||
|
|
||||||
def html_template(title)
|
|
||||||
<<~EOS
|
|
||||||
---
|
|
||||||
title: #{title}
|
|
||||||
layout: formula
|
|
||||||
redirect_from: /formula-linux/#{title}
|
|
||||||
---
|
|
||||||
{{ content }}
|
|
||||||
EOS
|
|
||||||
end
|
|
||||||
|
|
||||||
def generate_formula_api
|
|
||||||
args = generate_formula_api_args.parse
|
|
||||||
|
|
||||||
tap = CoreTap.instance
|
|
||||||
raise TapUnavailableError, tap.name unless tap.installed?
|
|
||||||
|
|
||||||
unless args.dry_run?
|
|
||||||
directories = ["_data/formula", "api/formula", "formula", "api/internal/v3"]
|
|
||||||
FileUtils.rm_rf directories + ["_data/formula_canonical.json"]
|
|
||||||
FileUtils.mkdir_p directories
|
|
||||||
end
|
|
||||||
|
|
||||||
Homebrew.with_no_api_env do
|
|
||||||
tap_migrations_json = JSON.dump(tap.tap_migrations)
|
|
||||||
File.write("api/formula_tap_migrations.json", tap_migrations_json) unless args.dry_run?
|
|
||||||
|
|
||||||
Formulary.enable_factory_cache!
|
|
||||||
Formula.generating_hash!
|
|
||||||
|
|
||||||
tap.formula_names.each do |name|
|
|
||||||
formula = Formulary.factory(name)
|
|
||||||
name = formula.name
|
|
||||||
json = JSON.pretty_generate(formula.to_hash_with_variations)
|
|
||||||
html_template_name = html_template(name)
|
|
||||||
|
|
||||||
unless args.dry_run?
|
|
||||||
File.write("_data/formula/#{name.tr("+", "_")}.json", "#{json}\n")
|
|
||||||
File.write("api/formula/#{name}.json", FORMULA_JSON_TEMPLATE)
|
|
||||||
File.write("formula/#{name}.html", html_template_name)
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
onoe "Error while generating data for formula '#{name}'."
|
|
||||||
raise
|
|
||||||
end
|
end
|
||||||
|
|
||||||
homebrew_core_tap_json = JSON.generate(tap.to_internal_api_hash)
|
sig { override.void }
|
||||||
File.write("api/internal/v3/homebrew-core.json", homebrew_core_tap_json) unless args.dry_run?
|
def run
|
||||||
canonical_json = JSON.pretty_generate(tap.formula_renames.merge(tap.alias_table))
|
tap = CoreTap.instance
|
||||||
File.write("_data/formula_canonical.json", "#{canonical_json}\n") unless args.dry_run?
|
raise TapUnavailableError, tap.name unless tap.installed?
|
||||||
|
|
||||||
|
unless args.dry_run?
|
||||||
|
directories = ["_data/formula", "api/formula", "formula", "api/internal/v3"]
|
||||||
|
FileUtils.rm_rf directories + ["_data/formula_canonical.json"]
|
||||||
|
FileUtils.mkdir_p directories
|
||||||
|
end
|
||||||
|
|
||||||
|
Homebrew.with_no_api_env do
|
||||||
|
tap_migrations_json = JSON.dump(tap.tap_migrations)
|
||||||
|
File.write("api/formula_tap_migrations.json", tap_migrations_json) unless args.dry_run?
|
||||||
|
|
||||||
|
Formulary.enable_factory_cache!
|
||||||
|
Formula.generating_hash!
|
||||||
|
|
||||||
|
tap.formula_names.each do |name|
|
||||||
|
formula = Formulary.factory(name)
|
||||||
|
name = formula.name
|
||||||
|
json = JSON.pretty_generate(formula.to_hash_with_variations)
|
||||||
|
html_template_name = html_template(name)
|
||||||
|
|
||||||
|
unless args.dry_run?
|
||||||
|
File.write("_data/formula/#{name.tr("+", "_")}.json", "#{json}\n")
|
||||||
|
File.write("api/formula/#{name}.json", FORMULA_JSON_TEMPLATE)
|
||||||
|
File.write("formula/#{name}.html", html_template_name)
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
onoe "Error while generating data for formula '#{name}'."
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
|
||||||
|
homebrew_core_tap_json = JSON.generate(tap.to_internal_api_hash)
|
||||||
|
File.write("api/internal/v3/homebrew-core.json", homebrew_core_tap_json) unless args.dry_run?
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def html_template(title)
|
||||||
|
<<~EOS
|
||||||
|
---
|
||||||
|
title: #{title}
|
||||||
|
layout: formula
|
||||||
|
redirect_from: /formula-linux/#{title}
|
||||||
|
---
|
||||||
|
{{ content }}
|
||||||
|
EOS
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "cmd/shared_examples/args_parse"
|
require "cmd/shared_examples/args_parse"
|
||||||
|
require "dev-cmd/generate-formula-api"
|
||||||
|
|
||||||
RSpec.describe "brew generate-formula-api" do
|
RSpec.describe Homebrew::DevCmd::GenerateFormulaApi do
|
||||||
it_behaves_like "parseable arguments"
|
it_behaves_like "parseable arguments"
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user