Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new input to allow include statements at the top of formula class #46

Merged
merged 12 commits into from
Jul 11, 2024
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ jobs:
# Optional - string
custom_require: custom_download_strategy

# Allows you to add custom includes inside the formula class, before dependencies and install blocks.
# Optional - string
formula_includes: 'include Language::Python::Virtualenv'

# Override the automatically detected version of a formula with an explicit value.
# This option should only be used if Homebrew cannot automatically detect the version when generating
# the Homebrew formula. Including this when not necessary could lead to uninstallable formula that may
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
- INPUT_TEST='assert_match("my script output", shell_output("my-script-command"))'
- INPUT_DOWNLOAD_STRATEGY=
- INPUT_CUSTOM_REQUIRE=
- INPUT_FORMULA_INCLUDES=
- INPUT_TARGET_DARWIN_AMD64=
- INPUT_TARGET_DARWIN_ARM64=
- INPUT_TARGET_LINUX_AMD64=
Expand Down
2 changes: 2 additions & 0 deletions homebrew_releaser/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
CUSTOM_REQUIRE,
DOWNLOAD_STRATEGY,
FORMULA_FOLDER,
FORMULA_INCLUDES,
GITHUB_OWNER,
GITHUB_REPO,
GITHUB_TOKEN,
Expand Down Expand Up @@ -156,6 +157,7 @@ def run_github_action():
TEST,
DOWNLOAD_STRATEGY,
CUSTOM_REQUIRE,
FORMULA_INCLUDES,
version_no_v if VERSION else None,
)

Expand Down
1 change: 1 addition & 0 deletions homebrew_releaser/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
) # Must check for string `false` since GitHub Actions passes the bool as a string
DOWNLOAD_STRATEGY = os.getenv('INPUT_DOWNLOAD_STRATEGY')
CUSTOM_REQUIRE = os.getenv('INPUT_CUSTOM_REQUIRE')
FORMULA_INCLUDES = os.getenv('INPUT_FORMULA_INCLUDES')
VERSION = os.getenv('INPUT_VERSION')

# App Constants
Expand Down
6 changes: 6 additions & 0 deletions homebrew_releaser/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def generate_formula_data(
test: Optional[str] = None,
download_strategy: Optional[str] = None,
custom_require: Optional[str] = None,
formula_includes: Optional[str] = None,
version: Optional[str] = None,
) -> str:
"""Generates the formula data for Homebrew.
Expand Down Expand Up @@ -127,6 +128,10 @@ def generate_formula_data(

# This file was generated by Homebrew Releaser. DO NOT EDIT.
class {{class_name}} < Formula
{{# formula_includes}}
{{{formula_includes}}}

{{/ formula_includes}}
desc "{{description}}"
homepage "https://github.com/{{owner}}/{{repo_name}}"
url "{{tar_url}}"{{# download_strategy}}, using: {{download_strategy}}{{/ download_strategy}}
Expand Down Expand Up @@ -212,6 +217,7 @@ def install
'test_instructions': test.strip() if test else None,
'download_strategy': download_strategy,
'custom_require': custom_require,
'formula_includes': formula_includes.strip() if formula_includes else None,
'version': version,
'darwin_amd64_url': darwin_amd64_url,
'darwin_amd64_checksum': darwin_amd64_checksum,
Expand Down
17 changes: 17 additions & 0 deletions test/formulas/test_generate_formula_formula_includes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# typed: true
# frozen_string_literal: true

# This file was generated by Homebrew Releaser. DO NOT EDIT.
class TestGenerateFormulaFormulaIncludes < Formula
include Language::Python::Virtualenv

desc "Release scripts, binaries, and executables to github"
homepage "https://github.com/Justintime50/test-generate-formula-formula-includes"
url "https://github.com/Justintime50/test-generate-formula-formula-includes/archive/refs/tags/v0.1.0.tar.gz"
sha256 "0000000000000000000000000000000000000000000000000000000000000000"
license "MIT"

def install
bin.install "src/secure-browser-kiosk.sh" => "secure-browser-kiosk"
end
end
36 changes: 36 additions & 0 deletions test/unit/test_formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,39 @@ def test_generate_formula_override_version():
record_formula(formula_path, formula_filename, formula)

assert '9.8.7' in formula


def test_generate_formula_formula_includes():
"""Tests that we generate the formula content correctly when using the formula_includes param.

NOTE: See docstring in `record_formula` for more details on how recording formulas works.
"""
formula_filename = f'{inspect.stack()[0][3]}.rb'
mock_repo_name = formula_filename.replace('_', '-').replace('.rb', '')
mock_tar_url = f'https://github.com/{USERNAME}/{mock_repo_name}/archive/refs/tags/v0.1.0.tar.gz'

repository = {
'description': DESCRIPTION,
'license': LICENSE,
}

formula = Formula.generate_formula_data(
owner=USERNAME,
repo_name=mock_repo_name,
repository=repository,
checksums=[
{
f'{mock_repo_name}.tar.gz': {
'checksum': CHECKSUM,
'url': f'https://github.com/justintime50/{mock_repo_name}/releases/download/{VERSION}/{mock_repo_name}-{VERSION}.tar.gz', # noqa
},
}
],
install=INSTALL,
tar_url=mock_tar_url,
formula_includes='include Language::Python::Virtualenv',
)

record_formula(formula_path, formula_filename, formula)

assert 'include Language::Python::Virtualenv' in formula
2 changes: 1 addition & 1 deletion test/unit/test_readme_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_format_formula_data():
"""
formulas = ReadmeUpdater.format_formula_data('./test')

assert len(formulas) == 13
assert len(formulas) == 14
assert formulas[0] == {
'name': 'test-generate-formula',
'desc': 'Tool to release scripts, binaries, and executables to github',
Expand Down