Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14762 from MikeMcQuaid/generate_api
Add generate-{cask,formula}-api commands
- Loading branch information
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -802,6 +802,8 @@ then | |
edit | ||
extract | ||
formula | ||
generate-cask-api | ||
generate-formula-api | ||
livecheck | ||
pr-pull | ||
pr-upload | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |