Skip to content

Commit

Permalink
DX: automatically update hook definition (#1)
Browse files Browse the repository at this point in the history
* DOC: update `README.md`
* DX: write current tag into `README.md`
  • Loading branch information
redeboer authored Sep 5, 2024
1 parent f62a70a commit 3c727fe
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 45 deletions.
107 changes: 107 additions & 0 deletions .github/workflows/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""Run with https://docs.astral.sh/uv/guides/scripts:
.. code-block:: shell
uv run --no-project .github/workflows/main.py
"""
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "pyyaml",
# ]
# ///

import json
import subprocess
from pathlib import Path

import yaml


REPO_DIR = Path(__file__).parent.parent.parent.absolute()


def main() -> int:
tags = set(get_existing_tags())
versions = set(get_prettier_versions())
expected_tags = {f"v{t}" for t in versions}
missing_tags = expected_tags - tags
if not missing_tags:
print("No new versions found")
return 0
for tag in sorted(missing_tags):
print(f"Updating to {tag}")
version = _to_version(tag)
update_files(version)
stage_commit_and_tag(tag)
return 0


def _to_version(tag: str) -> str:
return tag[1:]


def get_existing_tags() -> list[str]:
tags = git("tag", "--list").splitlines()
return sorted(t for t in tags if t.startswith("v"))


def get_prettier_versions() -> list[str]:
prettier_versions = _get_node_package_versions("prettier")
prettier_versions = [
v for v in prettier_versions if "alpha" not in v if "beta" not in v
]
return sorted(prettier_versions)


def _get_node_package_versions(package_name: str) -> list[str]:
cmd = ("npm", "view", package_name, "--json")
output = json.loads(subprocess.check_output(cmd))
return output["versions"]


def update_files(new_version: str) -> None:
_replace_in_pre_commit_hooks(new_version)
_replace_in_readme(new_version)
_update_version_file(new_version)


def _replace_in_pre_commit_hooks(new_version: str) -> None:
with open(REPO_DIR / ".pre-commit-hooks.yaml") as stream:
pre_commit_hooks = yaml.safe_load(stream)
pre_commit_hooks[0]["additional_dependencies"] = [f"prettier@{new_version}"]
with open(REPO_DIR / ".pre-commit-hooks.yaml", "w") as stream:
yaml.dump(pre_commit_hooks, stream, sort_keys=False)


def _replace_in_readme(new_version: str) -> None:
with open(REPO_DIR / "README.md") as f:
readme = f.read()
current_version = __get_current_version()
new_readme = readme.replace(current_version, new_version)
with open(REPO_DIR / "README.md", "w") as f:
f.write(new_readme)


def __get_current_version() -> str:
with open(REPO_DIR / ".version") as stream:
return stream.readline().strip()


def _update_version_file(new_version: str) -> None:
with open(REPO_DIR / ".version", "w") as stream:
stream.write(new_version + "\n")


def stage_commit_and_tag(tag: str) -> None:
git("add", ".pre-commit-hooks.yaml", ".version", "README.md")
git("commit", "-m", f"MAINT: upgrade to Prettier {tag}")
git("tag", tag)


def git(*cmd: str) -> str:
output = subprocess.check_output(("git", "-C", REPO_DIR) + cmd)
return output.decode()


if __name__ == "__main__":
raise SystemExit(main())
38 changes: 22 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
name: main
on:
push:
branches: [main]
schedule:
- cron: '15 8 * * *'
- cron: "50 1 * * 1"
workflow_dispatch:

jobs:
build:
name: main
update:
name: Upgrade hook
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- run: pip install pre-commit-mirror-maker
- run: git config --global user.name 'Github Actions'
- run: git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
- run: pre-commit-mirror . --language=node --package-name=prettier --types=text --entry='prettier --write --ignore-unknown' --id=prettier
- run: |
git remote set-url origin https://x-access-token:[email protected]/$GITHUB_REPOSITORY
git push origin HEAD:refs/heads/main --tags
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v5
- name: Set up uv
run: curl -LsSf https://astral.sh/uv/install.sh | sh
- run: git config user.name 'Github Actions'
- run: git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
- run: uv run --no-project .github/workflows/main.py
- run: |
git remote set-url origin https://x-access-token:${{ secrets.PAT }}@github.com/${{ github.repository }}
for tag in $(git tag); do
git push origin $tag
done
git push origin HEAD:refs/heads/main
# for-loop is required to trigger CI on the tags
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44 changes: 15 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,36 @@
# archived
# Pre-commit mirror for Prettier

prettier made some changes that breaks plugins entirely

___


prettier mirror
===============

Mirror of prettier package for pre-commit.

For pre-commit: see https://github.com/pre-commit/pre-commit

For prettier: see https://github.com/prettier/prettier
> [!NOTE]
> This fork is a continuation of [github.com/pre-commit/mirrors-prettier](https://github.com/pre-commit/mirrors-prettier).
Mirror of the [Prettier](https://github.com/prettier/prettier) formatter for [pre-commit](https://github.com/pre-commit/pre-commit).

### Using prettier with pre-commit

Add this to your `.pre-commit-config.yaml`:

```yaml
- repo: https://github.com/pre-commit/mirrors-prettier
rev: '' # Use the sha / tag you want to point at
- repo: https://github.com/ComPWA/prettier-pre-commit
rev: v4.0.0-alpha.8
hooks:
- id: prettier
- id: prettier
```
*note*: only prettier versions >= 2.1.0 are supported
When using plugins with `prettier` you'll need to declare them under
`additional_dependencies`. For example:
When using [Prettier plugins](https://prettier.io/docs/en/plugins), you'll need to declare them under [`additional_dependencies`](https://pre-commit.com/#config-additional_dependencies). For example:

```yaml
- repo: https://github.com/pre-commit/mirrors-prettier
rev: '' # Use the sha / tag you want to point at
- repo: https://github.com/ComPWA/prettier-pre-commit
rev: v4.0.0-alpha.8
hooks:
- id: prettier
- id: prettier
additional_dependencies:
- prettier@2.1.2
- '@prettier/plugin-xml@0.12.0'
- prettier@4.0.0-alpha.8
- '@prettier/plugin-xml@3.4.1'
```

By default, all files are passed to `prettier`, if you want to limit the
file list, adjust `types` / `types_or` / `files`:
By default, all files are passed to `prettier`, if you want to limit the file list, adjust `types` / `types_or` / `files`:

```yaml
- id: prettier
- id: prettier
types_or: [css, javascript]
```

0 comments on commit 3c727fe

Please sign in to comment.