Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fazer prompt do cookiecutter condicional #117

Open
huogerac opened this issue Oct 23, 2024 · 3 comments
Open

Fazer prompt do cookiecutter condicional #117

huogerac opened this issue Oct 23, 2024 · 3 comments

Comments

@huogerac
Copy link
Contributor

No description provided.

@huogerac
Copy link
Contributor Author

https://stackoverflow.com/questions/78735387/i-want-to-make-cookicutter-conditional-propmting-cookiecutter-json-file

#cookiecutter.pre.json
{
  "description": "The Ultimate Django and Vue Template",
  "app_name": "core",
  "model": "Tasks",
  "python_version": [
    "3.11",
    "3.10",
    "3.9"
  ],
  "package_manager": [
    "requirements.txt",
    "poetry"
  ],
  "python_linter": [
    "flake8",
    "pylint",
    "ruff"
  ],
  "django_api": [
    "django_ninja",
    "django_only"
  ],
  "database_version": [
    "postgres:15-alpine",
    "postgres:14-alpine",
    "postgres:13.3-alpine",
    "postgis/postgis:14-3.2-alpine"
  ],
  "use_sqlite_local_env": "no",
  "create_frontend": "yes",
  "node_version": [
    "18.18",
    "16.17",
    "14.14"
  ],
  "pages_folder_name": [
    "views",
    "pages"
  ],
  "api_mock": [
    "mirageJS",
    "express"
  ],
  "use_github_actions_CI": "yes",
  "keep_vscode_settings": "yes",
  "keep_vscode_devcontainer": "no",
  "docker_usage": [
    "use docker by default",
    "use venv npm by default"
  ],
  "deploy_to": [
    "None",
    "fly.io"
  ],
  "deploy_domain": "abacate.com",
  "author_name": "Roger Camargo",
  "email": "{{ cookiecutter.author_name.lower()|replace(' ', '-') }}@example.com",
  "version": "0.1.0",
  "__prompts__": {
    "description": "Enter a description for your project",
    "app_name": "Enter the 'core-business' app name",
    "model": "Enter the model name (plural)",
    "model_lower": "Confirm the 'model' in the lowercase form",
    "model_singular": "Confirm the 'model' in the singular form",
    "model_singular_lower": "Confirm the 'model_lower' in the singular form",
    "python_version": "Select the Python version",
    "package_manager": "Select the package manager",
    "python_linter": "Select the Python linter (code formatter)",
    "django_api": "Select the Django API library",
    "database_version": "Select the database version",
    "use_sqlite_local_env": "Would you like to start with SQLite?",
    "node_version": "Select the Node version for the frontend",
    "pages_folder_name": "Select pages folder name for the frontend",
    "api_mock": "Select the frotend library to simulate the backend (api mock)",
    "use_github_actions_CI": "Would you like to keep the Github Actions (CI-CD)?",
    "keep_vscode_settings": "Would you like to keep the VSCode Settings?",
    "keep_vscode_devcontainer": "Would you like to keep the VSCode DevContainer?",
    "docker_usage": "Would you like to use Docker?",
    "deploy_to": "Where would you like to target your deployment?",
    "deploy_domain": "Enter the domain name",
    "author_name": "Enter the author's name",
    "email": "Enter author's e-mail",
    "version": "Enter the project version"
  },
  "__conditions__": {
    "node_version": "{{ cookiecutter.create_frontend == 'yes' }}",
    "pages_folder_name": "{{ cookiecutter.create_frontend == 'yes' }}",
    "api_mock": "{{ cookiecutter.create_frontend == 'yes' }}",
    "deploy_domain": "{{ cookiecutter.deploy_to != 'None' }}"
  }
}
#cookicuter.json
{
  "project_name": "My Django Project",
  "project_slug": "{{ cookiecutter.project_name.lower()|replace(' ', '_')|replace('-', '_')|replace('.', '_')|trim() }}",
  "_extensions": [
    "extensions.StemKeysExtension"
  ]
}

@saymoncoppi
Copy link

@huogerac Uma ideia que eu tive para o cookiecutter é a de traduzir os prompts.
Quando comecei a pesquisar sobre o assunto também encontrei esse tópico no stackoverflow.

Descrevendo um pouco da ideia, daria para usar o pre hook para:

  • Detectar o idioma
  • Ler o arquivo json com as traduções pra que estas não fiquem dentro da função
  • Apresentar os prompts a partir do idioma detectado

Com o arquivo de prompts separado da função fica facil de atualizar e incluir outros idiomas.

hooks/prompts.json

{
  "en": {
    "project_name": "Enter a user-friendly project name",
    "app_name": "Enter the 'core-business' app name",
    "model": "Enter the model name (plural)",
    "python_version": "Select the Python version"
    // Continue adicionando os outros prompts...
  },
  "pt": {
    "project_name": "Digite um nome amigável para o projeto",
    "app_name": "Digite o nome do app 'core-business'",
    "model": "Digite o nome do modelo (plural)",
    "python_version": "Selecione a versão do Python"
    // Continue adicionando os outros prompts...
  }
}

hooks/pre_gen_project.py

import os
import locale
import json

def get_system_language():
    # Detecta o idioma do sistema
    lang, _ = locale.getdefaultlocale()
    return lang

def load_translated_prompts(language_code):
    # Caminho para o arquivo de prompts
    prompts_file = os.path.join(os.path.dirname(__file__), 'prompts.json')

    # Carrega o arquivo JSON de prompts
    with open(prompts_file, 'r', encoding='utf-8') as file:
        all_prompts = json.load(file)

    # Retorna os prompts no idioma do sistema (ou inglês como fallback)
    return all_prompts.get(language_code[:2], all_prompts['en'])

# Detecta o idioma do sistema
system_language = get_system_language()

# Carrega os prompts traduzidos para o idioma detectado
translated_prompts = load_translated_prompts(system_language)

# Modifique o contexto do cookiecutter com os prompts traduzidos
cookiecutter_context = {
    "project_name": translated_prompts['project_name'],
    "app_name": translated_prompts['app_name'],
    "model": translated_prompts['model'],
    "python_version": translated_prompts['python_version']
    # Continue preenchendo o restante dos prompts traduzidos
}

cookiecutter.json

{
  "__prompts__": {
    "project_name": "{{ cookiecutter_context['project_name'] }}",
    "app_name": "{{ cookiecutter_context['app_name'] }}",
    "model": "{{ cookiecutter_context['model'] }}",
    "python_version": "{{ cookiecutter_context['python_version'] }}"
    // Continue referenciando os outros prompts
  }
}

O que você acha?
Um abraço!

@huogerac
Copy link
Contributor Author

Legal!
Vale testar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants