Skip to content

Commit

Permalink
feat: Run our own hooks on this repo (#67)
Browse files Browse the repository at this point in the history
* Run `pre-commit try-repo . generate-renovate-annotations --all`

* Add dev section and script to generate table via cog

* Run `pre-commit try-repo . cog --file README.md`

* Remove custom class definition

* Add renovate annotation for cog

* We don't need the string replace since we're importing

* Small refactors for readability
  • Loading branch information
mattkram authored May 10, 2024
1 parent 6d0e2bd commit f3cff52
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,27 @@ If this is a new template, `pre-commit` will not fail and you may need to do an
```shell
git commit --amend path/to/file.html
```

## Dev setup

We have a dev setup that uses `conda` for environment management.
We also use `make` to automate common tasks.
The targets are documented below:


<!-- [[[cog
import os, sys; sys.path.insert(0, os.path.join(os.getcwd(), "dev"))
from generate_makefile_targets_table import main; main()
]]] -->
<!-- THE FOLLOWING CODE IS GENERATED BY COG VIA PRE-COMMIT. ANY MANUAL CHANGES WILL BE LOST. -->
| Target | Description |
|-----------------|-----------------------------------------------|
| `help` | Display help on all Makefile targets |
| `setup` | Setup local conda environment for development |
| `install-hooks` | Download + install all pre-commit hooks |
| `pre-commit` | Run pre-commit against all files |
| `type-check` | Run static type checks |
| `test` | Run all the unit tests |
<!-- [[[end]]] -->

> **Note:** Interestingly, the table above is generated by the `cog` hook defined in this repo :smile:
46 changes: 46 additions & 0 deletions dev/generate_makefile_targets_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import subprocess
from textwrap import dedent
from typing import NamedTuple

import cog


class MakefileTarget(NamedTuple):
target: str
description: str


def main():
raw_text = dedent(subprocess.check_output("make", text=True))

makefile_targets = []
for line in raw_text.splitlines():
target, _, description = line.partition(" ")
makefile_targets.append(
MakefileTarget(target=target.strip(), description=description.strip())
)

# Add two since we will surround with backticks
max_target_len = max(len(t.target) for t in makefile_targets) + 2
max_description_len = max(len(t.description) for t in makefile_targets)

cog.outl(
"<!-- THE FOLLOWING CODE IS GENERATED BY COG VIA PRE-COMMIT. ANY MANUAL CHANGES WILL BE LOST. -->"
)
cog.outl(
f"| {'Target':{max_target_len}s} | {'Description':{max_description_len}s} |"
)
cog.outl(
f"|{'-'*(max_target_len + 2)}|{'-'*(max_description_len + 2):{max_description_len}s}|"
)
for t in makefile_targets:
target_str = f"`{t.target}`"
cog.outl(
f"| {target_str:{max_target_len}s} | {t.description:{max_description_len}s} |"
)


if __name__ == "__main__":
# If we run this file as a script, we just make `cog.outl` emit print statements for debugging purposes.
cog.outl = print
main()
20 changes: 14 additions & 6 deletions environment-dev.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
channels:
- defaults
dependencies:
- python=3.10
- pytest
- pytest-cov
- pyyaml
- pip
- mypy
# renovate: datasource=conda depName=main/python
- python=3.10.14
# renovate: datasource=conda depName=main/pip
- pip=24.0
# renovate: datasource=conda depName=main/pyyaml
- pyyaml=6.0.1
# renovate: datasource=conda depName=main/mypy
- mypy=1.8.0
# renovate: datasource=conda depName=main/pytest
- pytest=7.4.0
# renovate: datasource=conda depName=main/pytest-cov
- pytest-cov=4.1.0
- pip:
# renovate: datasource=pypi
- cog==0.9.7
- -e .

1 comment on commit f3cff52

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/anaconda_pre_commit_hooks
   __init__.py00100% 
   add_renovate_annotations.py1151884%46–49, 60–62, 107, 141, 150, 165, 218, 224–229
tests
   test_generate_renovate_annotations.py610100% 
TOTAL1761889% 

Please sign in to comment.