Skip to content

Instantly share code, notes, and snippets.

@illabo
Created May 14, 2020 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save illabo/a3cf7577f2f3b1ca809dcb80f807a857 to your computer and use it in GitHub Desktop.
Save illabo/a3cf7577f2f3b1ca809dcb80f807a857 to your computer and use it in GitHub Desktop.
Pre- and post-commit hooks to autogenerate docs and create tag from Podspec version label when committing on release branch
#!/usr/local/bin/python3
import semver_compare
import subprocess
podspec = subprocess.check_output("ls -1 *.podspec | head -1", shell=True).decode().strip()
new_tag = semver_compare.check_file(podspec, branches=["release"], verbose=True)
if new_tag:
print(f"Creating tag {new_tag}")
subprocess.run(["git", "tag", new_tag])
#!/usr/local/bin/python3
import semver_compare
import subprocess
podspec = subprocess.check_output("ls -1 docs/index.html | head -1", shell=True).decode().strip()
new_tag = semver_compare.check_file(podspec, template='IIDadata\s+%s\s+Docs', verbose=False)
if not new_tag:
subprocess.run(["bundle", "exec", "jazzy --min-acl public"])
subprocess.run(["git", "add", "docs/"])
#!/usr/local/bin/python3
import re
import sys
import subprocess
branch = subprocess.check_output("git rev-parse --abbrev-ref HEAD", shell=True).decode().strip()
tag = subprocess.check_output("git tag -l --sort='-v:refname' | head -1", shell=True).decode().strip()
semver_regex = r"(?P<full_semver>(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)"
def check_file(filename, branches=None, template='version\s+=\s+\'%s\'\s*$', verbose=False):
if branches and branch not in branches:
return
git_tag_matches = re.search(f"^{semver_regex}$", tag, re.MULTILINE)
with open(filename,"r") as file:
new_regex = template % semver_regex
for l in file:
podspec_matches = re.search(new_regex, l, re.MULTILINE)
if podspec_matches and len(podspec_matches.groups()) > 0:
if _should_retag(git_tag_matches, podspec_matches, verbose=verbose):
return podspec_matches.group('full_semver')
def _should_retag(git_match, spec_match, verbose=False):
if git_match.group('full_semver') == spec_match.group('full_semver'):
print(f"Pod version: {git_match.group('full_semver')}")
return False
if verbose:
print(f"Have {git_match.group('full_semver')} in git tag")
print(f"Have {spec_match.group('full_semver')} in podspec ver")
if _part_newer(git_match.group('major'), spec_match.group('major')):
if verbose:
print("Major version is newer")
return True
if _part_older(git_match.group('major'), spec_match.group('major')):
if verbose:
print("Major version is older")
print('Please check correctness of podspec version label!')
return False
if _part_newer(git_match.group('minor'), spec_match.group('minor')):
if verbose:
print("Minor version is newer")
return True
if _part_older(git_match.group('minor'), spec_match.group('minor')):
if verbose:
print("Minor version is older")
print('Please check correctness of podspec version label!')
return False
if _part_newer(git_match.group('patch'), spec_match.group('patch')):
if verbose:
print("Patch version is newer")
return True
if _part_older(git_match.group('patch'), spec_match.group('patch')):
if verbose:
print("Patch version is older")
print('Please check correctness of podspec version label!')
return False
if git_match.group('prerelease') != spec_match.group('prerelease'):
if git_match.group('prerelease') == None:
if verbose:
print("Pre-release version couldn't be set after release")
print('Consider setting build metadata instead')
return False
if spec_match.group('prerelease') == None:
if verbose:
print("Altered to release version")
return True
if verbose:
print("Pre-release version altered")
return True
if git_match.group('buildmetadata') != spec_match.group('buildmetadata'):
if verbose:
print("Build metadata altered")
return True
if verbose:
print('Please check correctness of podspec version label!')
return False
def _part_newer(gitp, specp):
if specp > gitp:
return True
return False
def _part_older(gitp, specp):
if specp < gitp:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment