Skip to content
Snippets Groups Projects
Commit cabba663 authored by Josha Bartsch's avatar Josha Bartsch
Browse files

init

parents
Branches
Tags v1.0.0 v1.0.1
No related merge requests found
- id: bump-version
name: Bump moodle component version
description: This hook bumps moodle components version string (commonly found in version.php).
entry: bump-version
language: python
files: ^version.php
always_run: true
[build-system]
requires = ["setuptools>=41", "wheel", "setuptools-git-versioning<2"]
build-backend = "setuptools.build_meta"
[project]
name = "moodle-pre-commit"
description = ''
readme = ""
requires-python = ">=3.10"
keywords = []
authors = [
{ name = "Josha Bartsch", email = "bartsch@itc.rwth-aachen.de" },
]
dependencies = [
"gitpython"
]
dynamic = ["version"]
[project.scripts]
bump-version="moodle_pre_commit.bumpversion:run"
[tool.setuptools-git-versioning]
enabled = true
#!/usr/bin/env python3
import re
from git import Repo
from argparse import ArgumentParser
from datetime import datetime
from pathlib import Path
from os import environ
def bump(match: str) -> str:
today = datetime.now().strftime("%Y%m%d")
version = match.group("ver")
date = version[:8]
it = int(version[8:])
it = 0 if date != today else it + 1
environ["bumped"] = "1"
return f"{match.group('pre')}{today}{it:02};"
def run():
parser = ArgumentParser()
parser.add_argument("file", help="Versionfile path")
args = parser.parse_args()
if not Path(args.file).exists():
print("version.php not found")
exit(1)
repo = Repo()
index = [diff.a_blob.name for diff in repo.index.diff("HEAD")]
if "version.php" in index:
exit(0)
with open(args.file, "r") as reader:
lines = reader.readlines()
content = map(
lambda line: re.sub(
r"(?P<pre>\s*\$plugin->version\s*=\s*)(?P<ver>\d{10});", bump, line
),
lines,
)
with open(args.file, "w") as writer:
for line in content:
writer.write(f"{line}")
if "bumped" not in environ:
raise Exception("Invalid version number")
repo.index.add("version.php")
if __name__ == "__main__":
run()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment