refactor(ci/scripts): Convert bump-versions.sh to python3 (#3027)

This commit is contained in:
Claudemirovsky 2024-03-07 20:12:01 -03:00 committed by GitHub
parent f2fecd65c0
commit 231aa88ab3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 76 additions and 65 deletions

75
.github/scripts/bump-versions.py vendored Executable file
View File

@ -0,0 +1,75 @@
#!/usr/bin/env python3
from concurrent.futures import Future, ThreadPoolExecutor, as_completed
import itertools
from pathlib import Path
import re
import subprocess
import sys
VERSION_STR = "VersionCode ="
VERSION_REGEX = re.compile(f"{VERSION_STR} (\\d+)")
BUMPED_FILES: list[Path] = []
BOT_EMAIL = "aniyomi-bot@aniyomi.org"
BOT_NAME = "aniyomi-bot[bot]"
def has_match(query: str, file: Path) -> tuple[Path, bool]:
return (file, query in file.read_text())
def find_files_with_match(query: str, include_multisrc: bool = True) -> list[Path]:
files = Path("src").glob("*/*/build.gradle")
if include_multisrc:
files = itertools.chain(files, Path("lib-multisrc").glob("*/build.gradle.kts"))
# Prevent bumping files twice.
files = filter(lambda file: file not in BUMPED_FILES, files)
# Use multiple threads to find matches.
with ThreadPoolExecutor() as executor:
futures = [executor.submit(has_match, query, file) for file in files]
results = map(Future.result, as_completed(futures))
return [path for path, result in results if result]
def replace_version(match: re.Match) -> str:
version = int(match.group(1))
print(f"{version} -> {version + 1}")
return f"{VERSION_STR} {version + 1}"
def bump_version(file: Path):
BUMPED_FILES.append(file)
with file.open("r+") as f:
print(f"\n{file}: ", end="")
text = VERSION_REGEX.sub(replace_version, f.read())
# Move the cursor to the start again, to prevent writing at the end
f.seek(0)
f.write(text)
def bump_lib_multisrc(theme: str):
for file in find_files_with_match(f"themePkg = '{theme}'", include_multisrc=False):
bump_version(file)
def commit_changes():
paths = [str(path.resolve()) for path in BUMPED_FILES]
subprocess.check_call(["git", "config", "--local", "user.email", BOT_EMAIL])
subprocess.check_call(["git", "config", "--local", "user.name", BOT_NAME])
subprocess.check_call(["git", "add"] + paths)
subprocess.check_call(["git", "commit", "-S", "-m", "[skip ci] chore: Mass-bump on extensions"])
subprocess.check_call(["git", "push"])
if __name__ == "__main__":
if len(sys.argv) > 1:
# Regex to match the lib name in the path, like "unpacker" or "dood-extractor".
lib_regex = re.compile(r"lib/([a-z0-9-]+)/")
# Find matches and remove None results.
matches = filter(None, map(lib_regex.search, sys.argv[1:]))
for match in matches:
project_path = ":lib:" + match.group(1)
for file in find_files_with_match(project_path):
if file.parent.parent.name == "lib-multisrc":
bump_lib_multisrc(file.parent.name)
else:
bump_version(file)
if len(BUMPED_FILES) > 0:
commit_changes()

View File

@ -1,64 +0,0 @@
#!/bin/bash
versionStr="VersionCode ="
bumpedFiles=""
# cut -d "=" -f 2 -> string.split("=")[1]
# "extVersionCode = 6" -> ["extVersionCode ", " 6"] -> " 6" -> "6"
getValue() { cut -d "=" -f 2 | cut -d " " -f 2;}
getVersion() {
grep "$versionStr" "$1" | getValue
}
bumpVersion() {
local file=$1
local oldVersion=$(getVersion $file)
local newVersion=$((oldVersion + 1))
echo -e "\n$file: $oldVersion -> $newVersion\n"
sed -i "s/$versionStr $oldVersion/$versionStr $newVersion/" $file
}
bumpLibMultisrcVersion() {
local themeName=$(echo $1 | grep -Eo "lib-multisrc/\w+" | cut -c 14-)
for file in $(grep -l -R "themePkg = '$themeName'" --include build.gradle src/); do
# prevent bumping the same extension multiple times
if [[ ! $bumpedFiles =~ ( |^)$file( |$) ]]; then
bumpedFiles+="$file "
bumpVersion $file
fi
done
}
findAndBump() {
for lib in $@; do
for file in $(grep -l -R ":lib:$lib" --include "build.gradle" --include "build.gradle.kts" src/ lib-multisrc/); do
# prevent bumping the same extension multiple times
if [[ ! $bumpedFiles =~ ( |^)$file( |$) ]]; then
if [[ $file =~ ^lib-multisrc ]]; then
bumpLibMultisrcVersion ${file/build.gradle.kts/}
else
bumpedFiles+="$file "
bumpVersion $file
fi
fi
done
done
commitChanges $bumpedFiles
}
commitChanges() {
if [[ -n "$@" ]]; then
git config --global user.email "aniyomi-bot@aniyomi.org"
git config --global user.name "aniyomi-bot[bot]"
git add $@
git commit -S -m "[skip ci] chore: Mass-bump on extensions"
git push
fi
}
# lib/cryptoaes/build.gradle.kts -> lib/cryptoaes -> cryptoaes
modified=$(echo $@ | tr " " "\n" | grep -Eo "^lib/\w+" | sort | uniq | cut -c 5-)
if [[ -n "$modified" ]]; then
findAndBump $modified
fi

View File

@ -52,7 +52,7 @@ jobs:
- name: Bump extensions that uses a modified lib - name: Bump extensions that uses a modified lib
if: steps.modified-libs.outputs.any_changed == 'true' if: steps.modified-libs.outputs.any_changed == 'true'
run: | run: |
./.github/scripts/bump-versions.sh ${{ steps.modified-libs.outputs.all_changed_files }} ./.github/scripts/bump-versions.py ${{ steps.modified-libs.outputs.all_changed_files }}
- name: Validate Gradle Wrapper - name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@a494d935f4b56874c4a5a87d19af7afcf3a163d0 # v2 uses: gradle/wrapper-validation-action@a494d935f4b56874c4a5a87d19af7afcf3a163d0 # v2