Skip to content

Commit

Permalink
feat: adds version param (closes #39) (#40)
Browse files Browse the repository at this point in the history
* feat: adds version param (closes #39)

* fix: lint and remove unused build deps

* fix: changelog date

* fix: lint
  • Loading branch information
Justintime50 committed Dec 12, 2023
1 parent da9b372 commit f21d26b
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 101 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-12)

- 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
14 changes: 6 additions & 8 deletions homebrew_releaser/readme_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@ def generate_table(formulas: List) -> str:
rows = []

for formula in formulas:
rows.append(
[
f'[{formula["name"]}]({formula.get("homepage")})',
formula.get('desc'),
f'`brew install {formula["name"]}`',
]
)
rows.append([
f'[{formula["name"]}]({formula.get("homepage")})',
formula.get('desc'),
f'`brew install {formula["name"]}`',
])

table = pretty_tables.create(
headers=headers,
Expand Down Expand Up @@ -181,7 +179,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
4 changes: 0 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ audit:
bandit:
{{VIRTUAL_BIN}}/bandit -r {{PROJECT_NAME}}/

# Builds the project in preparation for release
build:
{{VIRTUAL_BIN}}/python -m build

# Runs the Black Python formatter against the project
black:
{{VIRTUAL_BIN}}/black {{PROJECT_NAME}}/ {{TEST_DIR}}/
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
DEV_REQUIREMENTS = [
'bandit == 1.7.*',
'black == 23.*',
'build == 0.10.*',
'flake8 == 6.*',
'isort == 5.*',
'mypy == 1.5.*',
'mypy == 1.7.*',
'pytest == 7.*',
'pytest-cov == 4.*',
'twine == 4.*',
'types-requests',
]

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
132 changes: 76 additions & 56 deletions test/unit/test_formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ def test_generate_formula():
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
},
}
],
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,
depends_on=DEPENDS_ON,
Expand Down Expand Up @@ -123,14 +121,12 @@ def test_generate_formula_no_article_description():
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
},
}
],
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,
depends_on=None,
Expand Down Expand Up @@ -162,14 +158,12 @@ def test_generate_formula_formula_name_starts_description():
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
},
}
],
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,
depends_on=None,
Expand Down Expand Up @@ -199,14 +193,12 @@ def test_generate_formula_no_depends_on():
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
},
}
],
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,
depends_on=None,
Expand Down Expand Up @@ -236,14 +228,12 @@ def test_generate_formula_no_test():
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
},
}
],
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,
depends_on=DEPENDS_ON,
Expand Down Expand Up @@ -511,14 +501,12 @@ def test_generate_formula_string_false_configs():
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
},
}
],
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,
depends_on=None,
Expand Down Expand Up @@ -550,14 +538,12 @@ def test_generate_formula_empty_fields():
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
},
}
],
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,
depends_on=None,
Expand Down Expand Up @@ -636,3 +622,37 @@ 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
Loading

0 comments on commit f21d26b

Please sign in to comment.