From 7bc906de6e882bcad2b89b322879cf67e41c5117 Mon Sep 17 00:00:00 2001 From: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com> Date: Thu, 3 Jan 2019 13:02:00 -0800 Subject: [PATCH 1/2] Add yapf --- README.md | 1 + hooks/yapf.sh | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100755 hooks/yapf.sh diff --git a/README.md b/README.md index c5191541..58dbc2b9 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ supported hooks are: * **shellcheck**: Run [`shellcheck`](https://www.shellcheck.net/) to lint files that contain a bash [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) * **gofmt**: Automatically run `gofmt` on all Golang code (`*.go` files). * **golint**: Automatically run `golint` on all Golang code (`*.go` files) +* **yapf**: Automatically run [`yapf`](https://github.com/google/yapf) on all python code (`*.py` files). diff --git a/hooks/yapf.sh b/hooks/yapf.sh new file mode 100755 index 00000000..01121fb5 --- /dev/null +++ b/hooks/yapf.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +set -e + +# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions, +# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a +# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here. +export PATH=$PATH:/usr/local/bin + +readonly STYLE="{BASED_ON_STYLE: google, ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT: true, COLUMN_LIMIT: 120, BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF: true, COALESCE_BRACKETS: false, DEDENT_CLOSING_BRACKETS: true, SPLIT_BEFORE_DOT: true, SPLIT_COMPLEX_COMPREHENSION: true}" + +for file in "$@"; do + if [[ "$file" =~ \.py$ ]]; then + yapf -ri --style="$STYLE" "$(dirname "$file")" + fi +done From 3557e5bb9cf6faf08a0411e2d936fe03d2ec4572 Mon Sep 17 00:00:00 2001 From: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com> Date: Thu, 3 Jan 2019 13:14:01 -0800 Subject: [PATCH 2/2] Add yapf to list of hooks --- .pre-commit-hooks.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index d47c2fcd..ea12f85d 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -37,3 +37,11 @@ language: script files: \.go$ exclude: vendor\/.*$ + +- id: yapf + name: yapf + description: yapf (Yet Another Python Formatter) is a python formatter from Google. + entry: hooks/yapf.sh + language: script + files: \.py$ + exclude: __pycache__\/.*$