diff --git a/README.rst b/README.rst index 14dcf2078..bfbe18364 100644 --- a/README.rst +++ b/README.rst @@ -132,6 +132,7 @@ Answer the prompts with your own desired options_. For example:: use_heroku [n]: n use_grunt [n]: y use_angular [n]: n + use_precommit_hooks [n]: n Select open_source_license: 1 - MIT 2 - BSD @@ -162,6 +163,10 @@ For development, see the following for local development: .. _`Developing locally`: http://cookiecutter-django.readthedocs.io/en/latest/developing-locally.html .. _`Developing locally using docker`: http://cookiecutter-django.readthedocs.io/en/latest/developing-locally-docker.html +If you chose to install pre-commit hooks, make sure to also run this after git init:: + + $ pre-commit install + Community ----------- diff --git a/cookiecutter.json b/cookiecutter.json index 8fe7010a2..10f8a8d52 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -22,5 +22,6 @@ "use_heroku": "n", "use_grunt": "n", "use_angular": "n", + "use_precommit_hooks": "n", "open_source_license": ["MIT", "BSD", "Not open source"] } diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index cffb052e8..e33f57c8c 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -106,6 +106,13 @@ def remove_pycharm_dir(project_directory): shutil.rmtree(docs_dir_location) +def remove_pre_commit_file(): + """ + Removes pre-commit hook yaml file + """ + os.remove(os.path.join(PROJECT_DIRECTORY, '.pre-commit-config.yaml')) + + def remove_heroku_files(): """ Removes files needed for heroku if it isn't going to be used @@ -180,7 +187,6 @@ if '{{ cookiecutter.use_docker }}'.lower() != 'y': if '{{ cookiecutter.use_grunt }}'.lower() != 'y': remove_grunt_files() - # 7. Display a warning if use_docker and use_grunt are selected. Grunt isn't supported by our # docker config atm. if '{{ cookiecutter.use_grunt }}'.lower() == 'y' and '{{ cookiecutter.use_docker }}'.lower() == 'y': @@ -199,5 +205,9 @@ if '{{ cookiecutter.use_mailhog }}'.lower() == 'y' and '{{ cookiecutter.use_dock " mailhog service to your docker configuration manually." ) +# 8. If pre-commit hooks aren't used, remove pre-commit yaml file +if '{{ cookiecutter.use_precommit_hooks }}'.lower() != 'y': + remove_pre_commit_file() + # 4. Copy files from /docs/ to {{ cookiecutter.project_slug }}/docs/ # copy_doc_files(PROJECT_DIRECTORY) diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml new file mode 100644 index 000000000..65bc20e24 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -0,0 +1,20 @@ +- repo: git://github.com/pre-commit/pre-commit-hooks + sha: "6b1aa19425b35f8c34f43ada8f1045b028dc19a5" + hooks: + # keep ids alphabetical! see http://pre-commit.com/hooks.html + - id: debug-statements + files: \.py$ + - id: end-of-file-fixer + files: \.(py|sh)$ + - id: flake8 + files: \.(py|sh)$ + - id: name-tests-test + files: tests/.+\.py$ + args: ['--django'] # Use args: ['--django'] to match test*.py instead. + - id: trailing-whitespace + files: \.(py|sh|yaml|txt)$ +- repo: git://github.com/FalconSocial/pre-commit-python-sorter + sha: 1.0.4 + hooks: + - id: python-import-sorter + args: ['--diff'] diff --git a/{{cookiecutter.project_slug}}/requirements/local.txt b/{{cookiecutter.project_slug}}/requirements/local.txt index c88093fef..9264998a7 100644 --- a/{{cookiecutter.project_slug}}/requirements/local.txt +++ b/{{cookiecutter.project_slug}}/requirements/local.txt @@ -14,3 +14,7 @@ ipdb==0.10.0 pytest-django==2.9.1 pytest-sugar==0.7.1 + +{% if cookiecutter.use_precommit_hooks == 'y' -%} +pre-commit==0.8.2 +{%- endif %} \ No newline at end of file