diff --git a/README.md b/README.md index 9b4250f..a03790c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index a1a7ebf..34a8f0a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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= diff --git a/homebrew_releaser/app.py b/homebrew_releaser/app.py index 6fca2dc..5b93ebb 100644 --- a/homebrew_releaser/app.py +++ b/homebrew_releaser/app.py @@ -10,6 +10,7 @@ CUSTOM_REQUIRE, DOWNLOAD_STRATEGY, FORMULA_FOLDER, + FORMULA_INCLUDES, GITHUB_OWNER, GITHUB_REPO, GITHUB_TOKEN, @@ -156,6 +157,7 @@ def run_github_action(): TEST, DOWNLOAD_STRATEGY, CUSTOM_REQUIRE, + FORMULA_INCLUDES, version_no_v if VERSION else None, ) diff --git a/homebrew_releaser/constants.py b/homebrew_releaser/constants.py index 3cec292..ce0dd5b 100644 --- a/homebrew_releaser/constants.py +++ b/homebrew_releaser/constants.py @@ -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 diff --git a/homebrew_releaser/formula.py b/homebrew_releaser/formula.py index 98427b9..6235a37 100644 --- a/homebrew_releaser/formula.py +++ b/homebrew_releaser/formula.py @@ -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. @@ -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}} @@ -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, diff --git a/test/formulas/test_generate_formula_formula_includes.rb b/test/formulas/test_generate_formula_formula_includes.rb new file mode 100644 index 0000000..d9f8ee0 --- /dev/null +++ b/test/formulas/test_generate_formula_formula_includes.rb @@ -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 diff --git a/test/unit/test_formula.py b/test/unit/test_formula.py index bfdc8d4..771131d 100644 --- a/test/unit/test_formula.py +++ b/test/unit/test_formula.py @@ -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 diff --git a/test/unit/test_readme_updater.py b/test/unit/test_readme_updater.py index 9c72494..8d91211 100644 --- a/test/unit/test_readme_updater.py +++ b/test/unit/test_readme_updater.py @@ -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',