Class: Homebrew::DevCmd::GenerateFormulaApi

Inherits:
AbstractCommand show all
Defined in:
dev-cmd/generate-formula-api.rb

Constant Summary collapse

FORMULA_JSON_TEMPLATE =
<<~EOS
  ---
  layout: formula_json
  ---
  {{ content }}
EOS

Instance Attribute Summary

Attributes inherited from AbstractCommand

#args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, #initialize, parser

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#runvoid

This method returns an undefined value.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'dev-cmd/generate-formula-api.rb', line 30

def run
  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

    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