Skip to content

Commit

Permalink
Slightly refactor GitHub workflows & tools
Browse files Browse the repository at this point in the history
* Remove unused code
* Future-proof version regexes
* Fixup wraps
* Make tests run on tags—again
  • Loading branch information
oakkitten committed May 23, 2022
1 parent 9003820 commit a0b83f4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/fast-forward.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
fast_forward:
name: Fast-forward
name: \#${{ github.event.number }}
runs-on: ubuntu-latest
if: github.event.label.name == 'fast-forward'

Expand Down
16 changes: 11 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
branches-ignore:
- update-test-environment/*
tags:
- '**'
pull_request:
workflow_dispatch:

Expand Down Expand Up @@ -64,18 +66,22 @@ jobs:
- name: Check out repository
uses: actions/checkout@v2

# https://github.com/actions/checkout/issues/290
- name: Fetch tag annotations
run: git fetch --tags --force

- name: Assemble the add-on
run: |
run: |
export VERSION="$(git describe --long)"
cd anki_wallpaper
sed -i -r "s/# Wallpaper/\0 <version>$VERSION<\/version>/" config.md
zip -r ../anki_wallpaper.ankiaddon *
echo ASSET_NAME="anki_wallpaper-$VERSION.ankiaddon" >> "$GITHUB_ENV"
zip -r ../anki-wallpaper.ankiaddon *
echo ASSET_NAME="anki-wallpaper-$VERSION.ankiaddon" >> "$GITHUB_ENV"
- name: Store artifact
uses: svenstaro/upload-release-action@1.0.1
uses: svenstaro/upload-release-action@v2
with:
tag: ${{ github.ref }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: anki_wallpaper.ankiaddon
file: anki-wallpaper.ankiaddon
asset_name: ${{ env.ASSET_NAME }}
21 changes: 4 additions & 17 deletions .github/workflows/update.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import subprocess
import shlex
import re
import sys
from contextlib import contextmanager
Expand All @@ -12,14 +10,6 @@ def log(*args):
print(*args, file=sys.stderr)


def run(command: str) -> str:
return (
subprocess.run(shlex.split(command), capture_output=True, check=True)
.stdout
.decode()
)


def remove_two_edge_newlines(text: str) -> str:
if text.startswith("\n"):
text = text[1:]
Expand All @@ -39,12 +29,9 @@ def insert_before_line(self, line_re: str, addition: str):
lines = []
for line in self.contents.split("\n"):
if re.search(line_re, line):
leading_spaces = len(line) - len(line.lstrip())
leading_spaces = " " * (len(line) - len(line.lstrip()))
for new_line in remove_two_edge_newlines(dedent(addition)).split("\n"):
if new_line.strip():
lines.append(" " * leading_spaces + new_line)
else:
lines.append("")
lines.append(leading_spaces + new_line if new_line.strip() else "")
lines.append(line)
self.contents = "\n".join(lines)

Expand All @@ -67,13 +54,13 @@ def update_prerelease(tox_ini: File, tests_yml: File, version):
log(f":: updating pre-release test environment with Anki {version}")

tox_ini.contents = re.sub(
r"ankipre: (anki|aqt\[qt6])==\d+\.\d+\.[a-z0-9]+",
r"ankipre: (anki|aqt\[qt6])==[\d.a-z]+",
fr"ankipre: \1=={version}",
tox_ini.contents
)

tests_yml.contents = re.sub(
r"Pre-release \(\d+\.\d+\.[a-z0-9]+\)",
r"Pre-release \([\d.a-z]+\)",
fr"Pre-release ({version})",
tests_yml.contents
)
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ jobs:
if: matrix.environment == 'pre-release'
continue-on-error: true
run: |
export PRERELEASE=$(pip index --pre versions aqt | perl -ne 'm/aqt \((2.1.[a-z0-9]+)\)/ && print "$1"')
export STABLE=$(pip index versions aqt | perl -ne 'm/aqt \((2.1.[a-z0-9]+)\)/ && print "$1"')
export PRERELEASE=$(pip index --pre versions aqt | perl -ne 'm/aqt \(([\d.a-z]+)\)/ && print "$1"')
export STABLE=$(pip index versions aqt | perl -ne 'm/aqt \(([\d.a-z]+)\)/ && print "$1"')
if [ "$PRERELEASE" = "$STABLE" ]; then exit 3; fi
python .github/workflows/update.py update-prerelease $PRERELEASE \
&& git add tox.ini .github/workflows/tests.yml \
Expand All @@ -56,7 +56,7 @@ jobs:
if: matrix.environment == 'stable'
continue-on-error: true
run: |
export STABLE=$(pip index versions aqt | perl -ne 'm/aqt \((2.1.[a-z0-9]+)\)/ && print "$1"')
export STABLE=$(pip index versions aqt | perl -ne 'm/aqt \(([\d.a-z]+)\)/ && print "$1"')
python .github/workflows/update.py add-stable $STABLE \
&& git add tox.ini .github/workflows/tests.yml \
&& git commit -am "Tests: add new environment for Anki $STABLE" \
Expand Down
9 changes: 3 additions & 6 deletions anki_wallpaper/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ def patch_method(obj, method_name, action):
original_method = getattr(obj, method_name)

def decorator(function):
if not hasattr(obj, method_name):
raise Exception(f"Object {obj} has no method with the name {method_name}")

if action == "replace":
@wraps(function)
@wraps(original_method)
def patched_method(*args, **kwargs):
return function(*args, **kwargs)

elif action == "prepend":
@wraps(function)
@wraps(original_method)
def patched_method(*args, **kwargs):
function(*args, **kwargs)
return original_method(*args, **kwargs)

elif action == "append":
@wraps(function)
@wraps(original_method)
def patched_method(*args, **kwargs):
result = original_method(*args, **kwargs)
function(*args, **kwargs)
Expand Down

0 comments on commit a0b83f4

Please sign in to comment.