From 57a78211588c1a7dc0073377633bfa67ef9f1edf Mon Sep 17 00:00:00 2001 From: leollon Date: Fri, 19 Jul 2019 14:45:20 +0800 Subject: [PATCH] Add pre-commit for generated Django project, not for Cookiecutter Django project --- CONTRIBUTORS.rst | 2 ++ README.rst | 2 ++ cookiecutter.json | 2 +- docs/developing-locally.rst | 2 ++ docs/project-generation-options.rst | 8 +++++ hooks/post_gen_project.py | 7 ++++ tests/test_cookiecutter_generation.py | 3 ++ .../.pre-commit-config.yaml | 32 +++++++++++++++++++ .../requirements/local.txt | 3 ++ 9 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 {{cookiecutter.project_slug}}/.pre-commit-config.yaml diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 4c3ad8a04..307318a76 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -136,6 +136,7 @@ Listed in alphabetical order. Keyvan Mosharraf `@keyvanm`_ Krzysztof Szumny `@noisy`_ Krzysztof Żuraw `@krzysztofzuraw`_ + Leo Wong `@leollon`_ Leo Zhou `@glasslion`_ Leonardo Jimenez `@xpostudio4`_ Lin Xianyi `@iynaix`_ @@ -286,6 +287,7 @@ Listed in alphabetical order. .. _@kevgathuku: https://github.com/kevgathuku .. _@keyvanm: https://github.com/keyvanm .. _@knitatoms: https://github.com/knitatoms +.. _@leollon: https://github.com/leollon .. _@krzysztofzuraw: https://github.com/krzysztofzuraw .. _@MathijsHoogland: https://github.com/MathijsHoogland .. _@mattayes: https://github.com/mattayes diff --git a/README.rst b/README.rst index 30a3a2db4..460e16362 100644 --- a/README.rst +++ b/README.rst @@ -84,6 +84,7 @@ Optional Integrations .. _PythonAnywhere: https://www.pythonanywhere.com/ .. _Traefik: https://traefik.io/ .. _LetsEncrypt: https://letsencrypt.org/ +.. _pre-commit: https://github.com/pre-commit/pre-commit Constraints ----------- @@ -193,6 +194,7 @@ Answer the prompts with your own desired options_. For example:: 5 - Not open source Choose from 1, 2, 3, 4, 5 [1]: 1 keep_local_envs_in_vcs [y]: y + use_pre_commit [n]: n debug[n]: n Enter the project and take a look around:: diff --git a/cookiecutter.json b/cookiecutter.json index d6d217ca4..0472e7708 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -42,6 +42,6 @@ "use_heroku": "n", "use_travisci": "n", "keep_local_envs_in_vcs": "y", - + "use_pre_commit": "n", "debug": "n" } diff --git a/docs/developing-locally.rst b/docs/developing-locally.rst index 8694dde7a..71d5ee1be 100644 --- a/docs/developing-locally.rst +++ b/docs/developing-locally.rst @@ -26,6 +26,8 @@ First things first. #. Install development requirements: :: $ pip install -r requirements/local.txt + # Optional: install git hook if using pre-commit + $ pre-commit install #. Create a new PostgreSQL database using createdb_: :: diff --git a/docs/project-generation-options.rst b/docs/project-generation-options.rst index afa9b8aff..b70077352 100644 --- a/docs/project-generation-options.rst +++ b/docs/project-generation-options.rst @@ -103,6 +103,14 @@ keep_local_envs_in_vcs: is strongly encouraged). Note: .env(s) are only utilized when Docker Compose and/or Heroku support is enabled. +use_pre_commit: + Indicates that pointing out issues in code on every commit with git, + such as missing semicolons, trailing whitespace, sort import statements + and debug statements. It is also good for code review while not wasting time with + trivial style nitpicks. + This option is just relevant for Django project developers, + but not for Cookiecutter Django developers. + debug: Indicates whether the project should be configured for debugging. This option is relevant for Cookiecutter Django developers only. diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index ff84f1806..c701183a0 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -279,6 +279,10 @@ def remove_node_dockerfile(): shutil.rmtree(os.path.join("compose", "local", "node")) +def remove_dotpre_commit_config_yaml(): + os.remove(".pre-commit-config.yaml") + + def main(): debug = "{{ cookiecutter.debug }}".lower() == "y" @@ -342,6 +346,9 @@ def main(): if "{{ cookiecutter.use_travisci }}".lower() == "n": remove_dottravisyml_file() + if "{{ cookiecutter.use_pre_commit }}".lower() == "n": + remove_dotpre_commit_config_yaml() + print(SUCCESS + "Project initialized, keep up the good work!" + TERMINATOR) diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index 77f71df5b..cfc32268c 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -37,6 +37,7 @@ def context(): @pytest.mark.parametrize("use_sentry", YN_CHOICES, ids=lambda yn: f"sentry:{yn}") @pytest.mark.parametrize("use_compressor", YN_CHOICES, ids=lambda yn: f"cmpr:{yn}") @pytest.mark.parametrize("use_whitenoise", YN_CHOICES, ids=lambda yn: f"wnoise:{yn}") +@pytest.mark.parametrize("use_pre_commit", YN_CHOICES, ids=lambda yn: f"precommit:{yn}") @pytest.mark.parametrize("cloud_provider", CLOUD_CHOICES, ids=lambda yn: f"cloud:{yn}") def context_combination( windows, @@ -47,6 +48,7 @@ def context_combination( use_compressor, use_whitenoise, cloud_provider, + use_pre_commit, ): """Fixture that parametrize the function where it's used.""" return { @@ -58,6 +60,7 @@ def context_combination( "use_sentry": use_sentry, "use_whitenoise": use_whitenoise, "cloud_provider": cloud_provider, + "use_pre_commit": use_pre_commit, } diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml new file mode 100644 index 000000000..bceb28754 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -0,0 +1,32 @@ +exclude: .+/migrations/.+\.py +default_stages: [commit] +fail_fast: true + +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: master + hooks: + - id: trailing-whitespace + files: (^|/){{cookiecutter.project_slug}}/.+\.(py|html|sh|css|js)$ + +- repo: local + hooks: + - id: black + name: black + entry: black + args: [--line-length=80] + language: python + types: [python] + files: (^|/){{cookiecutter.project_slug}}/.+\.py$ + verbose: true + +- repo: local + hooks: + - id: flake8 + name: flake8 + entry: flake8 + args: [--max-line-length=120] + language: python + types: [python] + files: (^|/){{cookiecutter.project_slug}}/.+\.py$ + verbose: true diff --git a/{{cookiecutter.project_slug}}/requirements/local.txt b/{{cookiecutter.project_slug}}/requirements/local.txt index cfb2d28f7..8b655496f 100644 --- a/{{cookiecutter.project_slug}}/requirements/local.txt +++ b/{{cookiecutter.project_slug}}/requirements/local.txt @@ -21,6 +21,9 @@ flake8==3.7.8 # https://github.com/PyCQA/flake8 coverage==4.5.3 # https://github.com/nedbat/coveragepy black==19.3b0 # https://github.com/ambv/black pylint-django==2.0.11 # https://github.com/PyCQA/pylint-django +{%- if cookiecutter.use_pre_commit == 'y' %} +pre-commit==1.17.0 # https://github.com/pre-commit/pre-commit +{%- endif %} {%- if cookiecutter.use_celery == 'y' %} pylint-celery==0.3 # https://github.com/PyCQA/pylint-celery {%- endif %}