VS Code Dev Container (#4198)

* decontainer setup

* update

* fix typo

* keep .envs

* add info

* typo

* minor fixes

* keep compose/production

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add isort and lint

* choose editor and configure devcontainer

* choose editor and configure devcontainer

* add ssh client

* Remove isort arguments duplicated with base config

* End of file

* Only keep black as code formatter

* Add note about black not being accepted as formatter in devcontainer

* Avoid empty lines in devcontainer.json

* Move bash_history into .devcontainer folder

* Remove .prettierignore file

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bruno Alla <alla.brunoo@gmail.com>
Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>
This commit is contained in:
masavini 2023-07-03 11:55:33 +02:00 committed by GitHub
parent 72a11fdf68
commit 1ed6d6e03b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 153 additions and 22 deletions

View File

@ -126,7 +126,11 @@ Answer the prompts with your own desired [options](http://cookiecutter-django.re
Choose from 1, 2 [1]: 1 Choose from 1, 2 [1]: 1
timezone [UTC]: America/Los_Angeles timezone [UTC]: America/Los_Angeles
windows [n]: n windows [n]: n
use_pycharm [n]: y Select an editor to use. The choices are:
1 - None
2 - PyCharm
3 - VS Code
Choose from 1, 2, 3 [1]: 1
use_docker [n]: n use_docker [n]: n
Select postgresql_version: Select postgresql_version:
1 - 14 1 - 14

View File

@ -16,7 +16,7 @@
"username_type": ["username", "email"], "username_type": ["username", "email"],
"timezone": "UTC", "timezone": "UTC",
"windows": "n", "windows": "n",
"use_pycharm": "n", "editor": ["None", "PyCharm", "VS Code"],
"use_docker": "n", "use_docker": "n",
"postgresql_version": ["14", "13", "12", "11", "10"], "postgresql_version": ["14", "13", "12", "11", "10"],
"cloud_provider": ["AWS", "GCP", "Azure", "None"], "cloud_provider": ["AWS", "GCP", "Azure", "None"],

View File

@ -53,11 +53,15 @@ timezone:
windows: windows:
Indicates whether the project should be configured for development on Windows. Indicates whether the project should be configured for development on Windows.
use_pycharm: editor:
Indicates whether the project should be configured for development with PyCharm_. Select an editor to use. The choices are:
1. None
2. PyCharm_
3. `VS Code`_
use_docker: use_docker:
Indicates whether the project should be configured to use Docker_ and `Docker Compose`_. Indicates whether the project should be configured to use Docker_, `Docker Compose`_ and `devcontainer`_.
postgresql_version: postgresql_version:
Select a PostgreSQL_ version to use. The choices are: Select a PostgreSQL_ version to use. The choices are:
@ -148,9 +152,11 @@ debug:
.. _Apache Software License 2.0: http://www.apache.org/licenses/LICENSE-2.0 .. _Apache Software License 2.0: http://www.apache.org/licenses/LICENSE-2.0
.. _PyCharm: https://www.jetbrains.com/pycharm/ .. _PyCharm: https://www.jetbrains.com/pycharm/
.. _VS Code: https://github.com/microsoft/vscode
.. _Docker: https://github.com/docker/docker .. _Docker: https://github.com/docker/docker
.. _Docker Compose: https://docs.docker.com/compose/ .. _Docker Compose: https://docs.docker.com/compose/
.. _devcontainer: https://containers.dev/
.. _PostgreSQL: https://www.postgresql.org/docs/ .. _PostgreSQL: https://www.postgresql.org/docs/

View File

@ -74,12 +74,13 @@ def remove_pycharm_files():
def remove_docker_files(): def remove_docker_files():
shutil.rmtree(".devcontainer")
shutil.rmtree("compose") shutil.rmtree("compose")
file_names = ["local.yml", "production.yml", ".dockerignore"] file_names = ["local.yml", "production.yml", ".dockerignore"]
for file_name in file_names: for file_name in file_names:
os.remove(file_name) os.remove(file_name)
if "{{ cookiecutter.use_pycharm }}".lower() == "y": if "{{ cookiecutter.editor }}".lower() == "PyCharm":
file_names = ["docker_compose_up_django.xml", "docker_compose_up_docs.xml"] file_names = ["docker_compose_up_django.xml", "docker_compose_up_docs.xml"]
for file_name in file_names: for file_name in file_names:
os.remove(os.path.join(".idea", "runConfigurations", file_name)) os.remove(os.path.join(".idea", "runConfigurations", file_name))
@ -427,7 +428,7 @@ def main():
if "{{ cookiecutter.username_type }}" == "username": if "{{ cookiecutter.username_type }}" == "username":
remove_custom_user_manager_files() remove_custom_user_manager_files()
if "{{ cookiecutter.use_pycharm }}".lower() == "n": if "{{ cookiecutter.editor }}".lower() != "PyCharm":
remove_pycharm_files() remove_pycharm_files()
if "{{ cookiecutter.use_docker }}".lower() == "y": if "{{ cookiecutter.use_docker }}".lower() == "y":
@ -445,8 +446,8 @@ def main():
if "{{ cookiecutter.keep_local_envs_in_vcs }}".lower() == "y": if "{{ cookiecutter.keep_local_envs_in_vcs }}".lower() == "y":
print( print(
INFO + ".env(s) are only utilized when Docker Compose and/or " INFO + ".env(s) are only utilized when Docker Compose and/or "
"Heroku support is enabled so keeping them does not " "Heroku support is enabled so keeping them does not make sense "
"make sense given your current setup." + TERMINATOR "given your current setup." + TERMINATOR
) )
remove_envs_and_associated_files() remove_envs_and_associated_files()
else: else:

View File

@ -52,8 +52,9 @@ SUPPORTED_COMBINATIONS = [
{"open_source_license": "Not open source"}, {"open_source_license": "Not open source"},
{"windows": "y"}, {"windows": "y"},
{"windows": "n"}, {"windows": "n"},
{"use_pycharm": "y"}, {"editor": "None"},
{"use_pycharm": "n"}, {"editor": "PyCharm"},
{"editor": "VS Code"},
{"use_docker": "y"}, {"use_docker": "y"},
{"use_docker": "n"}, {"use_docker": "n"},
{"postgresql_version": "14"}, {"postgresql_version": "14"},
@ -373,14 +374,15 @@ def test_error_if_incompatible(cookies, context, invalid_context):
@pytest.mark.parametrize( @pytest.mark.parametrize(
["use_pycharm", "pycharm_docs_exist"], ["editor", "pycharm_docs_exist"],
[ [
("n", False), ("None", False),
("y", True), ("PyCharm", True),
("VS Code", False),
], ],
) )
def test_pycharm_docs_removed(cookies, context, use_pycharm, pycharm_docs_exist): def test_pycharm_docs_removed(cookies, context, editor, pycharm_docs_exist):
context.update({"use_pycharm": use_pycharm}) context.update({"editor": editor})
result = cookies.bake(extra_context=context) result = cookies.bake(extra_context=context)
with open(f"{result.project_path}/docs/index.rst") as f: with open(f"{result.project_path}/docs/index.rst") as f:

View File

@ -0,0 +1,20 @@
#
# .bashrc.override.sh
#
# persistent bash history
HISTFILE=~/.bash_history
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
# set some django env vars
source /entrypoint
# restore default shell options
set +o errexit
set +o pipefail
set +o nounset
# start ssh-agent
# https://code.visualstudio.com/docs/remote/troubleshooting
eval "$(ssh-agent -s)"

View File

@ -0,0 +1,87 @@
// For format details, see https://containers.dev/implementors/json_reference/
{
"name": "{{cookiecutter.project_slug}}_dev",
"dockerComposeFile": [
"../local.yml"
],
"init": true,
"mounts": [
{
"source": "./.devcontainer/bash_history",
"target": "/home/dev-user/.bash_history",
"type": "bind"
},
{
"source": "~/.ssh",
"target": "/tmp",
"type": "bind"
},
{
"source": "~/.ssh",
"target": "/home/dev-user/.ssh",
"type": "bind"
}
],
// Tells devcontainer.json supporting services / tools whether they should run
// /bin/sh -c "while sleep 1000; do :; done" when starting the container instead of the containers default command
"overrideCommand": true,
"service": "django",
// "remoteEnv": {"PATH": "/home/dev-user/.local/bin:${containerEnv:PATH}"},
"remoteUser": "dev-user",
"workspaceFolder": "/app",
// Set *default* container specific settings.json values on container create.
"customizations": {
{%- if cookiecutter.editor == "VS Code" %}
"vscode": {
"settings": {
"editor.formatOnSave": true,
"[python]": {
"analysis.autoImportCompletions": true,
"analysis.typeCheckingMode": "basic",
"defaultInterpreterPath": "/usr/local/bin/python",
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
// Uncomment when fixed
// https://github.com/microsoft/vscode-remote-release/issues/8474
// "editor.defaultFormatter": "ms-python.black-formatter",
"formatting.blackPath": "/usr/local/bin/black",
"formatting.provider": "black",
"languageServer": "Pylance",
// "linting.banditPath": "/usr/local/py-utils/bin/bandit",
"linting.enabled": true,
"linting.flake8Enabled": true,
"linting.flake8Path": "/usr/local/bin/flake8",
"linting.mypyEnabled": true,
"linting.mypyPath": "/usr/local/bin/mypy",
"linting.pycodestylePath": "/usr/local/bin/pycodestyle",
// "linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"linting.pylintEnabled": true,
"linting.pylintPath": "/usr/local/bin/pylint"
}
},
// https://code.visualstudio.com/docs/remote/devcontainerjson-reference#_vs-code-specific-properties
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"davidanson.vscode-markdownlint",
"mrmlnc.vscode-duplicate",
"visualstudioexptteam.vscodeintellicode",
"visualstudioexptteam.intellicode-api-usage-examples",
// python
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.isort",
"ms-python.black-formatter",
// django
"batisteo.vscode-django"
]
}
{%- endif %}
},
// Uncomment the next line if you want start specific services in your Docker Compose config.
// "runServices": [],
// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
// "shutdownAction": "none",
// Uncomment the next line to run commands after the container is created.
"postCreateCommand": "cat .devcontainer/bashrc.override.sh >> ~/.bashrc"
}

View File

@ -161,11 +161,10 @@ typings/
!.vscode/extensions.json !.vscode/extensions.json
*.code-workspace *.code-workspace
# Local History for Visual Studio Code # Local History for devcontainer
.history/ .devcontainer/bash_history
{% if cookiecutter.editor == 'PyCharm' -%}
{% if cookiecutter.use_pycharm == 'y' -%}
# Provided default Pycharm Run/Debug Configurations should be tracked by git # Provided default Pycharm Run/Debug Configurations should be tracked by git
# In case of local modifications made by Pycharm, use update-index command # In case of local modifications made by Pycharm, use update-index command
# for each changed file, like this: # for each changed file, like this:

View File

@ -33,6 +33,18 @@ ENV BUILD_ENV ${BUILD_ENVIRONMENT}
WORKDIR ${APP_HOME} WORKDIR ${APP_HOME}
{% if cookiecutter.use_docker == "y" %}
# devcontainer dependencies and utils
RUN apt-get update && apt-get install --no-install-recommends -y \
sudo git bash-completion nano ssh
# Create devcontainer user and add it to sudoers
RUN groupadd --gid 1000 dev-user \
&& useradd --uid 1000 --gid dev-user --shell /bin/bash --create-home dev-user \
&& echo dev-user ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/dev-user \
&& chmod 0440 /etc/sudoers.d/dev-user
{% endif %}
# Install required system dependencies # Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \ RUN apt-get update && apt-get install --no-install-recommends -y \
# psycopg2 dependencies # psycopg2 dependencies
@ -49,7 +61,7 @@ COPY --from=python-build-stage /usr/src/app/wheels /wheels/
# use wheels to install python dependencies # use wheels to install python dependencies
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \ RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \
&& rm -rf /wheels/ && rm -rf /wheels/
COPY ./compose/production/django/entrypoint /entrypoint COPY ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint RUN sed -i 's/\r$//g' /entrypoint

View File

@ -10,7 +10,7 @@ Welcome to {{ cookiecutter.project_name }}'s documentation!
:maxdepth: 2 :maxdepth: 2
:caption: Contents: :caption: Contents:
howto{% if cookiecutter.use_pycharm == 'y' %} howto{% if cookiecutter.editor == 'PyCharm' %}
pycharm/configuration{% endif %} pycharm/configuration{% endif %}
users users