Skip to content

Commit

Permalink
Update version when publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeweerd committed Aug 12, 2024
1 parent 21e214e commit fe25676
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
117 changes: 117 additions & 0 deletions .github/update_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/bin/env python3
#
# Takes --version X.Y.Z or -V X.Y.Z option and sets version in manifest.json.
# Must be launched from the root of the repository.
#
# Modified from : https://raw.githubusercontent.com/bramstroker/homeassistant-zha-toolkit/master/.github/scripts/update_hacs_manifest.py # noqa: E501
#
# MIT License
#
# Copyright (c) 2021 Bram Gerritsen
# Copyright (c) 2022-2024 Mario DE WEERD
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""Update the files with new version."""
import json
import os
import re
import sys


def update_manifest(path=None, version=None):
"""Update the manifest file."""
if path is None:
return

with open(
path,
encoding="utf_8",
) as manifestfile:
manifest = json.load(manifestfile)

manifest["version"] = version

with open(
path,
"w",
encoding="utf_8",
) as manifestfile:
manifestfile.write(json.dumps(manifest, indent=4, sort_keys=True))


def replace_version_in_file(path: str, regex: str, version: str):
# Remove any leading 'v' from the provided version
new_version = version.lstrip("v")

# Compile the regex pattern
pattern = re.compile(regex)

# Function to replace the version part in the match
def version_replacer(match):
print("YAS")
# Extract the original version from the match
original_version = match.group("version")

# Determine if the original version started with 'v'
if original_version.startswith("v"):
replacement_version = f"v{new_version}"
else:
replacement_version = new_version

# Replace the version in the matched string
replacement_match = match.group(0).replace(
original_version, replacement_version
)

return replacement_match

# Read the file content
with open(path, "r", encoding="utf_8") as file:
content = file.read()

# Replace the versions in the content
new_content = pattern.sub(version_replacer, content)

# Write the modified content back to the file
with open(path, "w", encoding="utf_8") as file:
file.write(new_content)


version = "0.0.0"
for index, value in enumerate(sys.argv):
if value in ["--version", "-V", "-v"]:
version = sys.argv[index + 1]


path = f"{os.getcwd()}/manifest.json"
update_manifest(None)

# Example:
# replace_version_in_file('file.txt', r'(?P<version>v?\d+\.\d+\.\d+)', '2.3.4')

file = f"{os.getcwd()}/configure.ac"
regex = r'AC_INIT\(\[.*?\], \[(?P<version>.*?)]'
# regex= r'(?P<version>v?\d+\.\d+\.\d+)'
replace_version_in_file(file, regex, version)

file = f"{os.getcwd()}/src/shc.c"
regex = r'"Version (?P<version>.*?)"'
# regex= r'(?P<version>v?\d+\.\d+\.\d+)'
replace_version_in_file(file, regex, version)
8 changes: 8 additions & 0 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ on:
push:
paths: [man.md, aclocal.m4, configure.ac]
workflow_dispatch:
workflow_call:
release:
types: [published]

jobs:
convert_via_pandoc:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Set version number (when publishing)
if: startsWith(github.ref, 'ref/tags/v')
run: |
python3 ${{ github.workspace }}/.github/update_version.py --version ${{ github.ref_name }}
- uses: dorny/paths-filter@v2
id: changes
with:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dynamic = ["version"]

[tool.codespell]
ignore-words-list = """
stdio,master,scrpt
stdio,master,scrpt,weerd
"""
skip = """./.*,*/.metadata,*.xml,configure,*Makefile*,config*,*.m4,man.html"""
quiet-level=2
Expand Down

0 comments on commit fe25676

Please sign in to comment.