From ab4a32d5583ca05fc985d16fc1ffc894dab5db6a Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Sun, 20 Mar 2022 15:00:40 +0000 Subject: [PATCH] Unify compressor, gulp and custom bootstrap options (#3535) --- .github/workflows/ci.yml | 6 +-- README.md | 12 +++--- cookiecutter.json | 11 +++-- docs/deployment-on-heroku.rst | 2 +- docs/developing-locally.rst | 2 +- docs/project-generation-options.rst | 17 +++----- hooks/post_gen_project.py | 32 ++++++--------- tests/test_cookiecutter_generation.py | 9 ++--- .../.github/dependabot.yml | 2 +- {{cookiecutter.project_slug}}/.gitignore | 2 +- .../docker_compose_up_django.xml | 2 +- .../.idea/{{cookiecutter.project_slug}}.iml | 2 +- {{cookiecutter.project_slug}}/README.md | 5 +-- .../compose/production/django/Dockerfile | 4 +- .../compose/production/django/start | 2 +- .../config/settings/base.py | 2 +- .../config/settings/local.py | 2 +- .../config/settings/production.py | 2 +- {{cookiecutter.project_slug}}/gulpfile.js | 10 +---- {{cookiecutter.project_slug}}/local.yml | 2 +- {{cookiecutter.project_slug}}/pytest.ini | 2 +- .../requirements/base.txt | 6 +-- .../static/sass/project.scss | 3 -- .../templates/base.html | 40 ++++++++++++------- 24 files changed, 78 insertions(+), 101 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c502b130..121657e2f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,7 +50,7 @@ jobs: - name: Basic args: "" - name: Extended - args: "use_celery=y use_drf=y js_task_runner=Gulp" + args: "use_celery=y use_drf=y frontend_pipeline=Gulp" name: "${{ matrix.script.name }} Docker" runs-on: ubuntu-latest @@ -75,9 +75,9 @@ jobs: matrix: script: - name: With Celery - args: "use_celery=y use_compressor=y" + args: "use_celery=y frontend_pipeline='Django Compressor'" - name: With Gulp - args: "js_task_runner=Gulp custom_bootstrap_compilation=y" + args: "frontend_pipeline='Gulp'" name: "${{ matrix.script.name }} Bare metal" runs-on: ubuntu-latest diff --git a/README.md b/README.md index fcffd75ca..c96d8cc92 100644 --- a/README.md +++ b/README.md @@ -127,10 +127,6 @@ Answer the prompts with your own desired [options](http://cookiecutter-django.re 4 - 11.14 5 - 10.19 Choose from 1, 2, 3, 4, 5 [1]: 1 - Select js_task_runner: - 1 - None - 2 - Gulp - Choose from 1, 2 [1]: 1 Select cloud_provider: 1 - AWS 2 - GCP @@ -149,8 +145,12 @@ Answer the prompts with your own desired [options](http://cookiecutter-django.re Choose from 1, 2, 3, 4, 5, 6, 7, 8, 9 [1]: 1 use_async [n]: n use_drf [n]: y - custom_bootstrap_compilation [n]: n - use_compressor [n]: n + Select frontend_pipeline: + 1 - None + 2 - Django Compressor + 3 - Gulp + 4 - Gulp with custom Bootstrap + Choose from 1, 2, 3, 4 [1]: 1 use_celery [n]: y use_mailhog [n]: n use_sentry [n]: y diff --git a/cookiecutter.json b/cookiecutter.json index b24a3846d..4a43bae7e 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -24,10 +24,6 @@ "11.14", "10.19" ], - "js_task_runner": [ - "None", - "Gulp" - ], "cloud_provider": [ "AWS", "GCP", @@ -46,8 +42,11 @@ ], "use_async": "n", "use_drf": "n", - "custom_bootstrap_compilation": "n", - "use_compressor": "n", + "frontend_pipeline": [ + "None", + "Django Compressor", + "Gulp" + ], "use_celery": "n", "use_mailhog": "n", "use_sentry": "n", diff --git a/docs/deployment-on-heroku.rst b/docs/deployment-on-heroku.rst index bab5881cd..e239b6593 100644 --- a/docs/deployment-on-heroku.rst +++ b/docs/deployment-on-heroku.rst @@ -112,7 +112,7 @@ Or add the DSN for your account, if you already have one: Gulp & Bootstrap compilation ++++++++++++++++++++++++++++ -If you've opted for a custom bootstrap build, you'll most likely need to setup +If you've opted for Gulp, you'll most likely need to setup your app to use `multiple buildpacks`_: one for Python & one for Node.js: .. code-block:: bash diff --git a/docs/developing-locally.rst b/docs/developing-locally.rst index e9c1234c2..23bb7b9a8 100644 --- a/docs/developing-locally.rst +++ b/docs/developing-locally.rst @@ -155,7 +155,7 @@ To run Celery locally, make sure redis-server is installed (instructions are ava Sass Compilation & Live Reloading --------------------------------- -If you've opted for Gulp as JS task runner, the project comes configured with `Sass`_ compilation and `live reloading`_. As you change you Sass/JS source files, the task runner will automatically rebuild the corresponding CSS and JS assets and reload them in your browser without refreshing the page. +If you've opted for Gulp as front-end pipeline, the project comes configured with `Sass`_ compilation and `live reloading`_. As you change you Sass/JS source files, the task runner will automatically rebuild the corresponding CSS and JS assets and reload them in your browser without refreshing the page. #. Make sure that `Node.js`_ v16 is installed on your machine. #. In the project root, install the JS dependencies with:: diff --git a/docs/project-generation-options.rst b/docs/project-generation-options.rst index 26fb79d2f..3cc719779 100644 --- a/docs/project-generation-options.rst +++ b/docs/project-generation-options.rst @@ -61,12 +61,6 @@ postgresql_version: 4. 11.14 5. 10.19 -js_task_runner: - Select a JavaScript task runner. The choices are: - - 1. None - 2. Gulp_ - cloud_provider: Select a cloud provider for static & media files. The choices are: @@ -95,13 +89,12 @@ use_async: use_drf: Indicates whether the project should be configured to use `Django Rest Framework`_. -custom_bootstrap_compilation: - Indicates whether the project should support Bootstrap recompilation - via the selected JavaScript task runner's task. This can be useful - for real-time Bootstrap variable alteration. +frontend_pipeline: + Select a pipeline to compile and optimise frontend assets (JS, CSS, ...): -use_compressor: - Indicates whether the project should be configured to use `Django Compressor`_. + 1. None + 2. `Django Compressor`_ + 3. `Gulp`_: support Bootstrap recompilation with real-time variables alteration. use_celery: Indicates whether the project should be configured to use Celery_. diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index d784ba62b..9c3d946c1 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -10,7 +10,6 @@ TODO: restrict Cookiecutter Django project initialization to """ from __future__ import print_function -import json import os import random import shutil @@ -92,6 +91,11 @@ def remove_gulp_files(): file_names = ["gulpfile.js"] for file_name in file_names: os.remove(file_name) + remove_sass_files() + + +def remove_sass_files(): + shutil.rmtree(os.path.join("{{cookiecutter.project_slug}}", "static", "sass")) def remove_packagejson_file(): @@ -100,16 +104,6 @@ def remove_packagejson_file(): os.remove(file_name) -def remove_bootstrap_packages(): - with open("package.json", mode="r") as fd: - content = json.load(fd) - for package_name in ["bootstrap", "gulp-concat", "@popperjs/core"]: - content["devDependencies"].pop(package_name) - with open("package.json", mode="w") as fd: - json.dump(content, fd, ensure_ascii=False, indent=2) - fd.write("\n") - - def remove_celery_files(): file_names = [ os.path.join("config", "celery_app.py"), @@ -363,13 +357,13 @@ def main(): if ( "{{ cookiecutter.use_docker }}".lower() == "y" - and "{{ cookiecutter.cloud_provider}}".lower() != "aws" + and "{{ cookiecutter.cloud_provider}}" != "AWS" ): remove_aws_dockerfile() if "{{ cookiecutter.use_heroku }}".lower() == "n": remove_heroku_files() - elif "{{ cookiecutter.use_compressor }}".lower() == "n": + elif "{{ cookiecutter.frontend_pipeline }}" != "Django Compressor": remove_heroku_build_hooks() if ( @@ -389,15 +383,13 @@ def main(): if "{{ cookiecutter.keep_local_envs_in_vcs }}".lower() == "y": append_to_gitignore_file("!.envs/.local/") - if "{{ cookiecutter.js_task_runner}}".lower() == "none": + if "{{ cookiecutter.frontend_pipeline }}" != "Gulp": remove_gulp_files() remove_packagejson_file() if "{{ cookiecutter.use_docker }}".lower() == "y": remove_node_dockerfile() - elif "{{ cookiecutter.custom_bootstrap_compilation }}" == "n": - remove_bootstrap_packages() - if "{{ cookiecutter.cloud_provider}}".lower() == "none": + if "{{ cookiecutter.cloud_provider}}" == "None": print( WARNING + "You chose not to use a cloud provider, " "media files won't be served in production." + TERMINATOR @@ -409,13 +401,13 @@ def main(): if "{{ cookiecutter.use_docker }}".lower() == "y": remove_celery_compose_dirs() - if "{{ cookiecutter.ci_tool }}".lower() != "travis": + if "{{ cookiecutter.ci_tool }}" != "Travis": remove_dottravisyml_file() - if "{{ cookiecutter.ci_tool }}".lower() != "gitlab": + if "{{ cookiecutter.ci_tool }}" != "Gitlab": remove_dotgitlabciyml_file() - if "{{ cookiecutter.ci_tool }}".lower() != "github": + if "{{ cookiecutter.ci_tool }}" != "Github": remove_dotgithub_folder() if "{{ cookiecutter.use_drf }}".lower() == "n": diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index fc3ecf811..9c30aeedd 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -87,12 +87,9 @@ SUPPORTED_COMBINATIONS = [ {"use_async": "n"}, {"use_drf": "y"}, {"use_drf": "n"}, - {"js_task_runner": "None"}, - {"js_task_runner": "Gulp"}, - {"custom_bootstrap_compilation": "y"}, - {"custom_bootstrap_compilation": "n"}, - {"use_compressor": "y"}, - {"use_compressor": "n"}, + {"frontend_pipeline": "None"}, + {"frontend_pipeline": "django-compressor"}, + {"frontend_pipeline": "Gulp"}, {"use_celery": "y"}, {"use_celery": "n"}, {"use_mailhog": "y"}, diff --git a/{{cookiecutter.project_slug}}/.github/dependabot.yml b/{{cookiecutter.project_slug}}/.github/dependabot.yml index cf88cf33b..420a63cdc 100644 --- a/{{cookiecutter.project_slug}}/.github/dependabot.yml +++ b/{{cookiecutter.project_slug}}/.github/dependabot.yml @@ -82,7 +82,7 @@ updates: schedule: interval: "daily" -{%- if cookiecutter.js_task_runner != "None" %} +{%- if cookiecutter.frontend_pipeline == 'Gulp' %} # Enable version updates for javascript/npm - package-ecosystem: "npm" diff --git a/{{cookiecutter.project_slug}}/.gitignore b/{{cookiecutter.project_slug}}/.gitignore index d48356fa1..ede26ef72 100644 --- a/{{cookiecutter.project_slug}}/.gitignore +++ b/{{cookiecutter.project_slug}}/.gitignore @@ -338,7 +338,7 @@ MailHog .ipython/ {%- endif %} -{%- if cookiecutter.js_task_runner == 'Gulp' %} +{%- if cookiecutter.frontend_pipeline == 'Gulp' %} project.css project.min.css vendors.js diff --git a/{{cookiecutter.project_slug}}/.idea/runConfigurations/docker_compose_up_django.xml b/{{cookiecutter.project_slug}}/.idea/runConfigurations/docker_compose_up_django.xml index 67f369a3c..ad3b6a35a 100644 --- a/{{cookiecutter.project_slug}}/.idea/runConfigurations/docker_compose_up_django.xml +++ b/{{cookiecutter.project_slug}}/.idea/runConfigurations/docker_compose_up_django.xml @@ -10,7 +10,7 @@