Skip to content

Commit

Permalink
pre-commit: Always run qmlformat and require to skip the hook if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed May 29, 2021
1 parent 63c1855 commit fe41560
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
7 changes: 1 addition & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,11 @@ repos:
- manual
- id: qmlformat
name: qmlformat
entry: qmlformat -i
entry: tools/qmlformat.py
pass_filenames: true
require_serial: true
language: system
types: [text]
files: ^.*\.qml$
# Not enabled in commit stage by default, because qmlformat requires Qt
# 5.15 to be installed
stages:
- manual
- id: qmllint
name: qmllint
entry: qmllint
Expand Down
37 changes: 37 additions & 0 deletions tools/qmlformat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
"""
Small qmlformat wrapper that warns the user if the tool is not installed.
"""
import argparse
import shutil
import subprocess
import pathlib
import sys

QMLFORMAT_MISSING_MESSAGE = """
qmlformat is not installed. It is included in Qt 5.15 and later. If that Qt
version is not available on your system, please use the SKIP environment
variable when committing:
$ SKIP=qmlformat git commit
"""


def main(argv=None):
qmlformat_executable = shutil.which("qmlformat")
if not qmlformat_executable:
print(QMLFORMAT_MISSING_MESSAGE.strip(), file=sys.stderr)
return 1

parser = argparse.ArgumentParser()
parser.add_argument("file", nargs="+", type=pathlib.Path)
args = parser.parse_args(argv)

for filename in args.file:
subprocess.call((qmlformat_executable, "-i", filename))

return 0


if __name__ == "__main__":
sys.exit(main())

0 comments on commit fe41560

Please sign in to comment.