Add pre-commit scripts for file formatting

This commit is contained in:
Aaron Franke
2024-10-27 18:34:06 -07:00
parent 156aa63183
commit eff4d44f06
6 changed files with 153 additions and 1 deletions

48
.github/workflows/file_format.py vendored Executable file
View File

@@ -0,0 +1,48 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
if len(sys.argv) < 2:
print("Invalid usage of file_format.py, it should be called with a path to one or multiple files.")
sys.exit(1)
BOM = b"\xef\xbb\xbf"
changed = []
invalid = []
for file in sys.argv[1:]:
try:
with open(file, "rt", encoding="utf-8") as f:
original = f.read()
except UnicodeDecodeError:
invalid.append(file)
continue
if original == "":
continue
EOL = "\n"
revamp = EOL.join([line.rstrip("\n\r\t ") for line in original.splitlines(True)]).rstrip(EOL) + EOL
new_raw = revamp.encode(encoding="utf-8")
if new_raw.startswith(BOM):
new_raw = new_raw[len(BOM) :]
with open(file, "rb") as f:
old_raw = f.read()
if old_raw != new_raw:
changed.append(file)
with open(file, "wb") as f:
f.write(new_raw)
if changed:
for file in changed:
print(f"FIXED: {file}")
if invalid:
for file in invalid:
print(f"REQUIRES MANUAL CHANGES: {file}")
sys.exit(1)

33
.github/workflows/static_checks.yml vendored Normal file
View File

@@ -0,0 +1,33 @@
name: 📊 Static Checks
on: [push, pull_request]
concurrency:
group: ci-${{ github.actor }}-${{ github.head_ref || github.run_number }}-${{ github.ref }}-static
jobs:
static-checks:
name: Code style and file formatting
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Install APT dependencies
uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: libxml2-utils
- name: Install Python dependencies and general setup
run: |
git config diff.wsErrorHighlight all
- name: Style checks via pre-commit
uses: pre-commit/action@v3.0.1
with:
extra_args: --all-files
- name: XML validation via xmllint
run: |
xmllint --noout warehouse.doap data/io.github.flattool.Warehouse.gschema.xml data/io.github.flattool.Warehouse.metainfo.xml.in src/warehouse.gresource.xml

4
.gitignore vendored
View File

@@ -1,5 +1,7 @@
/subprojects/blueprint-compiler /subprojects/blueprint-compiler
.DS_Store
.flatpak .flatpak
.flatpak-builder .flatpak-builder
.idea/
.ruff_cache/
.vscode .vscode
.DS_Store

33
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,33 @@
default_language_version:
python: python3
exclude: |
(?x)^(
CODE_OF_CONDUCT.md
)
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.6
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
- id: codespell
exclude: |
(?x)^(
.*\.po$
)
additional_dependencies: [tomli]
- repo: local
hooks:
- id: file-format
name: file-format
language: python
entry: python .github/workflows/file_format.py
types_or: [text]

View File

@@ -56,3 +56,18 @@ You're all set! Launch the application by clicking its icon in your app menu or
```shell ```shell
flatpak run io.github.flattool.Warehouse flatpak run io.github.flattool.Warehouse
``` ```
## 👥 Contributing
### Compiling from Source
TODO
### Formatting
Warehouse uses [pre-commit](https://pre-commit.com/) for code formatting.
- Install using `pip install pre-commit`
- Run `pre-commit install` in the Warehouse repository root to set up pre-commit for this repo.
- Run `pre-commit run --all-files` to format all files in the repository.
If you run into a situation where pre-commit is broken, you can use `git commit --no-verfiy` to skip the pre-commit checks.

21
pyproject.toml Normal file
View File

@@ -0,0 +1,21 @@
[tool.ruff]
line-length = 160
[tool.ruff.format]
indent-style = "tab"
[tool.ruff.lint]
ignore = [
"E401", # Allow multiple imports on one line.
"E402", # Module level import not at top of file (invalid for warehouse because we need gi.require_version above some imports).
"E713", # Test for membership does not have to be "not in".
"E714", # Test for object identity does not have to be "is not".
"F401", # Don't delete unused imports.
"F821", # Undefined name (underscore for translations).
]
[tool.codespell]
skip = """\
CODE_OF_CONDUCT.md,
*.po
"""