Skip to content

Commit

Permalink
Merge pull request #14762 from MikeMcQuaid/generate_api
Browse files Browse the repository at this point in the history
Add generate-{cask,formula}-api commands
  • Loading branch information
MikeMcQuaid committed Feb 23, 2023
2 parents 43209a1 + 9c9213a commit f6d1fa6
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Library/Homebrew/brew.sh
Expand Up @@ -802,6 +802,8 @@ then
edit
extract
formula
generate-cask-api
generate-formula-api
livecheck
pr-pull
pr-upload
Expand Down
64 changes: 64 additions & 0 deletions Library/Homebrew/dev-cmd/generate-cask-api.rb
@@ -0,0 +1,64 @@
# typed: true
# frozen_string_literal: true

require "cli/parser"
require "cask/cask"

module Homebrew
extend T::Sig

module_function

sig { returns(CLI::Parser) }
def generate_cask_api_args
Homebrew::CLI::Parser.new do
description <<~EOS
Generates Cask API data files for formulae.brew.sh.
The generated files are written to the current directory.
EOS

named_args :none
end
end

JSON_TEMPLATE = <<~EOS
---
layout: cask_json
---
{{ content }}
EOS

def html_template(title)
<<~EOS
---
title: #{title}
layout: cask
---
{{ content }}
EOS
end

def generate_cask_api
generate_cask_api_args.parse

tap = Tap.default_cask_tap

directories = ["_data/cask", "api/cask", "api/cask-source", "cask"].freeze
FileUtils.rm_rf directories
FileUtils.mkdir_p directories

Cask::Cask.generating_hash!

tap.cask_files.each do |path|
cask = Cask::CaskLoader.load(path)
name = cask.token
json = JSON.pretty_generate(cask.to_hash_with_variations)

File.write("_data/cask/#{name}.json", "#{json}\n")
File.write("api/cask/#{name}.json", JSON_TEMPLATE)
File.write("api/cask-source/#{name}.rb", path.read)
File.write("cask/#{name}.html", html_template(name))
end
end
end
67 changes: 67 additions & 0 deletions Library/Homebrew/dev-cmd/generate-formula-api.rb
@@ -0,0 +1,67 @@
# typed: true
# frozen_string_literal: true

require "cli/parser"
require "formula"

module Homebrew
extend T::Sig

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.
The generated files are written to the current directory.
EOS

named_args :none
end
end

JSON_TEMPLATE = <<~EOS
---
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
generate_formula_api_args.parse

tap = CoreTap.instance

directories = ["_data/formula", "api/formula", "formula"]
FileUtils.rm_rf directories + ["_data/formula_canonical.json"]
FileUtils.mkdir_p directories

Formulary.enable_factory_cache!

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")
File.write("api/formula/#{name}.json", JSON_TEMPLATE)
File.write("formula/#{name}.html", html_template(name))
end

canonical_json = JSON.pretty_generate(tap.formula_renames.merge(tap.alias_table))
File.write("_data/formula_canonical.json", "#{canonical_json}\n")
end
end
8 changes: 8 additions & 0 deletions Library/Homebrew/test/dev-cmd/generate-cask-api_spec.rb
@@ -0,0 +1,8 @@
# typed: false
# frozen_string_literal: true

require "cmd/shared_examples/args_parse"

describe "brew generate-cask-api" do
it_behaves_like "parseable arguments"
end
8 changes: 8 additions & 0 deletions Library/Homebrew/test/dev-cmd/generate-formula-api_spec.rb
@@ -0,0 +1,8 @@
# typed: false
# frozen_string_literal: true

require "cmd/shared_examples/args_parse"

describe "brew generate-formula-api" do
it_behaves_like "parseable arguments"
end

0 comments on commit f6d1fa6

Please sign in to comment.