Skip to content

Commit

Permalink
feat: adds version param (closes #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Dec 5, 2023
1 parent da9b372 commit a5aad15
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v0.18.0 (2023-12-05)

- Adds a `version` parameter which can override the automatically detected version of a formula with an explicit value

## v0.17.0 (2023-10-26)

- Upgrades from Python 3.11 to Python 3.12
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ jobs:
# Optional - string
custom_require: custom_download_strategy

# 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
# not pass `brew audit` due to mismatched or redundant version strings
# Optional - string
version: '1.2.0'

# Adds URL and checksum targets for different OS and architecture pairs. Using this option assumes
# a tar archive exists on your GitHub repo with the following URL pattern (this cannot be customized):
# https://github.com/{GITHUB_OWNER}/{REPO_NAME}/releases/download/{TAG}/{REPO_NAME}-{VERSION}-{OPERATING_SYSTEM}-{ARCHITECTURE}.tar.gz'
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ inputs:
custom_require:
description: "Allows you to add a custom require_relative at the top of the formula template."
required: false
version:
description: "Override the automatically detected version of a formula with an explicit value."
required: false
target_darwin_amd64:
description: "Add a custom URL/checksum target for AMD64 Darwin builds."
required: false
Expand Down
2 changes: 1 addition & 1 deletion homebrew_releaser/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.17.0'
__version__ = '0.18.0'
4 changes: 3 additions & 1 deletion homebrew_releaser/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
TARGET_DARWIN_ARM64,
TARGET_LINUX_AMD64,
TARGET_LINUX_ARM64,
VERSION,
)
from homebrew_releaser.formula import Formula
from homebrew_releaser.git import Git
Expand Down Expand Up @@ -73,7 +74,7 @@ def run_github_action():
url=f'https://api.github.com/repos/{GITHUB_OWNER}/{GITHUB_REPO}/releases/latest'
).json()
assets = latest_release['assets']
version = latest_release['tag_name']
version = VERSION or latest_release['tag_name']
version_no_v = version.lstrip('v')
logger.info(f'Latest release ({version}) successfully identified!')

Expand Down Expand Up @@ -144,6 +145,7 @@ def run_github_action():
TEST,
DOWNLOAD_STRATEGY,
CUSTOM_REQUIRE,
version_no_v if VERSION else None,
)

Utils.write_file(os.path.join(HOMEBREW_TAP, FORMULA_FOLDER, f'{repository["name"]}.rb'), template, 'w')
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')
VERSION = os.getenv('INPUT_VERSION')

# App Constants
LOGGER_NAME = 'homebrew-releaser'
Expand Down
5 changes: 5 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,
version: Optional[str] = None,
) -> str:
"""Generates the formula data for Homebrew.
Expand Down Expand Up @@ -130,6 +131,9 @@ class {{class_name}} < Formula
homepage "https://github.com/{{owner}}/{{repo_name}}"
url "{{tar_url}}"{{# download_strategy}}, using: {{download_strategy}}{{/ download_strategy}}
sha256 "{{autogenerated_tar_checksum}}"
{{# version}}
version "{{version}}"
{{/ version}}
{{# license_type}}
license "{{license_type}}"
{{/ license_type}}
Expand Down Expand Up @@ -208,6 +212,7 @@ def install
'test_instructions': test.strip() if test else None,
'download_strategy': download_strategy,
'custom_require': custom_require,
'version': version,
'darwin_amd64_url': darwin_amd64_url,
'darwin_amd64_checksum': darwin_amd64_checksum,
'darwin_arm64_url': darwin_arm64_url,
Expand Down
2 changes: 1 addition & 1 deletion homebrew_releaser/readme_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def replace_table_contents(file_content: _io.TextIOWrapper, old_table: str, new_

@staticmethod
def does_readme_exist(homebrew_tap: str) -> Optional[str]:
"""Determines the README file to open. The README file must either be:
"""Determines the README file to open. The README file must either:
1. Have the file extension of `.md`
2. Reside in the root of a project
Expand Down
16 changes: 16 additions & 0 deletions test/formulas/test_generate_formula_override_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# typed: true
# frozen_string_literal: true

# This file was generated by Homebrew Releaser. DO NOT EDIT.
class TestGenerateFormulaOverrideVersion < Formula
desc "Release scripts, binaries, and executables to github"
homepage "https://github.com/Justintime50/test-generate-formula-override-version"
url "https://github.com/Justintime50/test-generate-formula-override-version/archive/refs/tags/v0.1.0.tar.gz"
sha256 "0000000000000000000000000000000000000000000000000000000000000000"
version "9.8.7"
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 @@ -636,3 +636,39 @@ def test_generate_formula_download_strategy():

assert formula.count(', using: CustomDownloadStrategy') == 5
assert 'require_relative "../formula_imports/mock_download_strategy"' in formula


def test_generate_formula_override_version():
"""Tests that we generate the formula content correctly (when the version is overridden).
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,
version='9.8.7',
)

record_formula(formula_path, formula_filename, formula)

assert '9.8.7' 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) == 12
assert len(formulas) == 13
assert formulas[0] == {
'name': 'test-generate-formula',
'desc': 'Tool to release scripts, binaries, and executables to github',
Expand Down

0 comments on commit a5aad15

Please sign in to comment.