Skip to content

Commit

Permalink
Self-review
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Jan 16, 2024
1 parent 0290026 commit 748ad18
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions scripts/version-patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
import subprocess
import sys

def replace_version(filename, new_text):
def replace_version(filename, build_info):
pattern = r'(Current : constant String := "[^+]+\+)([^"]*)(";)'
with open(filename, 'rb') as file:
content = file.read()

# Depending on the context in which this is run, there may be mix-ups with
# line terminators between the environment detected, github runner, etc...
# So just keep them as they are and that should always work.
content = content.decode()

new_content = re.sub(pattern, r'\g<1>' + new_text + r'\3', content)
with open(filename, 'rb') as file:
content = file.read().decode()

# The pattern captures the part between '+' and '";', replacing it with our
# new build information

new_content = re.sub(pattern, r'\g<1>' + build_info + r'\3', content)

# A few sanity checks and write if needed

if new_content == content:
if new_text in content:
if build_info in content:
print(f"Note: version in {filename} already up to date")
else:
print(f"WARNING: failed to update version in {filename}")
Expand All @@ -32,11 +37,11 @@ def replace_version(filename, new_text):
sys.exit(0)

# If there is an argument to the script, retrieve it here and use it as the new
# dirty flag
# dirty flag after the commit
if len(sys.argv) > 1:
dirty = sys.argv[1]
else:
# Detect the current directory contains changes
# Detect whether the current directory contains changes
if subprocess.call(['git', 'diff-index', '--quiet', 'HEAD', '--']) != 0:
dirty = "_dirty"
else:
Expand All @@ -45,6 +50,6 @@ def replace_version(filename, new_text):
# Find the short git commit of the repository in the current directory
commit = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip()

# Replace the build version part with the short commit hash
# Replace the build version part with the short commit hash plus any extra info
print(f"Updating version in src/alire/alire-version.ads to commit {commit}{dirty}...")
replace_version('src/alire/alire-version.ads', commit+dirty)

0 comments on commit 748ad18

Please sign in to comment.