Skip to content

Commit

Permalink
Improve project generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ericof committed May 16, 2024
1 parent e2ea47e commit 6861170
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 135 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ test: bin/cookieplone ## Test all cookiecutters
$(foreach project,$(TOP_LEVEL_TEMPLATES),$(MAKE) -C "./$(project)/" test ;)
$(foreach project,$(SUB_TEMPLATES),$(MAKE) -C "./sub/$(project)/" test ;)

.PHONY: test-pdb
test-pdb: bin/cookieplone ## Test all cookiecutters (and stop on error)
@echo "$(GREEN)==> Test all cookiecutters (and stop on error)$(RESET)"
bin/python3 -m pytest tests -x --pdb
$(foreach project,$(TOP_LEVEL_TEMPLATES),$(MAKE) -C "./$(project)/" test-pdb ;)
$(foreach project,$(SUB_TEMPLATES),$(MAKE) -C "./sub/$(project)/" test-pdb ;)


.PHONY: report-context
report-context: bin/cookieplone ## Generate a report of all context options
@echo "$(GREEN)==> Generate a report of all context options$(RESET)"
Expand Down
1 change: 1 addition & 0 deletions backend_addon/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"__generator_date_short": "{% now 'utc', '%Y-%m-%d' %}",
"__generator_date_long": "{% now 'utc', '%Y-%m-%d %H:%M:%S' %}",
"__generator_signature": "This was generated by [cookiecutter-plone](https://github.com/plone/cookieplone-templates/backend_addon) on {{ cookiecutter.__generator_date_long }}",
"__backend_addon_git_initialize": "1",
"__prompts__": {
"title": "Addon Title",
"description": "A short description of your addon",
Expand Down
8 changes: 6 additions & 2 deletions backend_addon/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import subprocess
import sys
from collections import OrderedDict
from pathlib import Path

from cookieplone.utils import console, files

context: OrderedDict = {{cookiecutter}}

# PATH OF CONTENT TO BE REMOVED
TO_REMOVE_PATHS = {
"feature_headless": [
Expand Down Expand Up @@ -48,10 +51,11 @@ def initialize_git():

def main():
"""Final fixes."""
keep_headless = int("{{ cookiecutter.feature_headless }}")
keep_headless = int(context.get("feature_headless"))
if not keep_headless:
remove_files("feature_headless")
initialize_git()
if int(context.get("__backend_addon_git_initialize")):
initialize_git()
msg = """
[bold blue]{{ cookiecutter.title }}[/bold blue]
Expand Down
2 changes: 1 addition & 1 deletion backend_addon/{{ cookiecutter.__folder_name }}/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
graft src/{{ cookiecutter.github_organization }}
graft src/{{ cookiecutter.__package_namespace }}
graft docs
graft news
graft tests
Expand Down
4 changes: 0 additions & 4 deletions backend_addon/{{ cookiecutter.__folder_name }}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ GREEN=`tput setaf 2`
RESET=`tput sgr0`
YELLOW=`tput setaf 3`

# Set distributions still in development
DISTRIBUTIONS="{{ cookiecutter.__package_name }}"
ALLOWED_DISTRIBUTIONS="{{ cookiecutter.__package_name }}"

PLONE6=6.0-latest

# Python checks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/pre-commit/mirrors-eslint
rev: 'v9.2.0'
hooks:
- id: eslint
files: '^packages/.*/src/.*/*.(js,jsx,ts,tsx)$'
types: [file]
1 change: 1 addition & 0 deletions project/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"__generator_date_short": "{% now 'utc', '%Y-%m-%d' %}",
"__generator_date_long": "{% now 'utc', '%Y-%m-%d %H:%M:%S' %}",
"__generator_signature": "This was generated by [cookieplone-templates](https://github.com/plone/cookieplone-templates) on {{ cookiecutter.__generator_date_long }}",
"__pre_commit_version": "3.7.1",
"__gha_enable": true,
"__gha_version_checkout": "v4",
"__gha_version_docker_stack": "v1.2.0",
Expand Down
17 changes: 12 additions & 5 deletions project/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
"""Post generation hook."""

from collections import OrderedDict # noQA
from collections import OrderedDict
from copy import deepcopy
from pathlib import Path

from cookieplone import generator
from cookieplone.utils import console, files

context = {{cookiecutter}}
context: OrderedDict = {{cookiecutter}}


SUB_PROJECT_REMOVE = [".github"]
BACKEND_ADDON_REMOVE = [
".github",
".git"
]

FRONTEND_ADDON_REMOVE = [".github"]

DEVOPS_TO_REMOVE = {
"ansible": [
Expand Down Expand Up @@ -51,15 +55,18 @@ def generate_backend_addon(context, output_dir):
# Go to backend/src/
output_dir = output_dir / "backend" / "src"
folder_name = context.get("python_package_name")
# Do not initialize the repository
context["__backend_addon_git_initialize"] = "0"
generator.generate_subtemplate(
"backend_addon", output_dir, folder_name, context, SUB_PROJECT_REMOVE
"backend_addon", output_dir, folder_name, context, BACKEND_ADDON_REMOVE
)
files.remove_files(output_dir / folder_name, BACKEND_ADDON_REMOVE)


def generate_frontend_addon(context, output_dir):
"""Run volto generator."""
generator.generate_subtemplate(
"frontend_addon", output_dir, "frontend", context, SUB_PROJECT_REMOVE
"frontend_addon", output_dir, "frontend", context, FRONTEND_ADDON_REMOVE
)


Expand Down
4 changes: 2 additions & 2 deletions project/hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Pre generation hook."""

import sys
from collections import OrderedDict # noQA
from collections import OrderedDict
from pathlib import Path
from textwrap import dedent

Expand All @@ -10,7 +10,7 @@

output_path = Path().resolve()

context = {{cookiecutter}}
context: OrderedDict = {{cookiecutter}}


def check_errors(context: dict) -> data.ContextValidatorResult:
Expand Down
13 changes: 0 additions & 13 deletions project/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ def context(cookieplone_root) -> dict:
}


@pytest.fixture(scope="session")
def context_unittest(context) -> dict:
"""Cookiecutter context."""
new_context = deepcopy(context)
new_context["python_test_framework"] = "unittest"
return new_context


@pytest.fixture(scope="session")
def context_devops_cache(context) -> dict:
Expand Down Expand Up @@ -104,12 +97,6 @@ def cutter_result(cookies_session, context):
return cookies_session.bake(extra_context=context)


@pytest.fixture(scope="session")
def cutter_result_unittest(cookies_session, context_unittest):
"""Cookiecutter result."""
return cookies_session.bake(extra_context=context_unittest)


@pytest.fixture(scope="session")
def cutter_result_devops_no_ansible(cookies_session, context_devops_no_ansible):
"""Cookiecutter result."""
Expand Down
34 changes: 14 additions & 20 deletions project/tests/test_project_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def test_backend_top_level_files(cutter_result, filename: str):

BACKEND_PACKAGE_FILES_PYTEST = [
"src/ploneorgbr/setup.py",
"src/ploneorgbr/src/ploneorgbr/configure.zcml",
"src/ploneorgbr/src/ploneorgbr/dependencies.zcml",
"src/ploneorgbr/src/ploneorgbr/permissions.zcml",
"src/ploneorgbr/src/ploneorgbr/profiles.zcml",
"src/ploneorgbr/src/ploneorgbr/testing.py",
"src/plonegov.ploneorgbr/src/plonegov/ploneorgbr/configure.zcml",
"src/plonegov.ploneorgbr/src/plonegov/ploneorgbr/dependencies.zcml",
"src/plonegov.ploneorgbr/src/plonegov/ploneorgbr/permissions.zcml",
"src/plonegov.ploneorgbr/src/plonegov/ploneorgbr/profiles.zcml",
"src/plonegov.ploneorgbr/src/plonegov/ploneorgbr/testing.py",
"src/ploneorgbr/tests/conftest.py",
"src/ploneorgbr/tests/setup/test_setup_install.py",
"src/ploneorgbr/tests/setup/test_setup_uninstall.py",
Expand All @@ -47,21 +47,15 @@ def test_backend_package_files_pytest(cutter_result, filename: str):
assert path.is_file()


BACKEND_PACKAGE_FILES_PYTEST = [
"src/ploneorgbr/setup.py",
"src/ploneorgbr/src/ploneorgbr/configure.zcml",
"src/ploneorgbr/src/ploneorgbr/dependencies.zcml",
"src/ploneorgbr/src/ploneorgbr/permissions.zcml",
"src/ploneorgbr/src/ploneorgbr/profiles.zcml",
"src/ploneorgbr/src/ploneorgbr/testing.py",
"src/ploneorgbr/src/ploneorgbr/tests/__init__.py",
"src/ploneorgbr/src/ploneorgbr/tests/test_setup.py",
FILES_TO_BE_REMOVED = [
"src/plonegov.ploneorgbr/.github",
"src/plonegov.ploneorgbr/.git",
]


@pytest.mark.parametrize("filename", BACKEND_FILES)
def test_backend_package_files_unittest(cutter_result_unittest, filename: str):
"""Test backend package files."""
backend_folder = cutter_result_unittest.project_path / "backend"
@pytest.mark.parametrize("filename", FILES_TO_BE_REMOVED)
def test_backend_package_files_removed(cutter_result, filename: str):
"""Test backend package files are removed."""
backend_folder = cutter_result.project_path / "backend"
path = backend_folder / filename
assert path.is_file()
assert path.exists() is False
assert path.parent.exists()
16 changes: 16 additions & 0 deletions project/{{ cookiecutter.__folder_name }}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/ddanier/sub-pre-commit.git
rev: v{{ cookiecutter.__pre_commit_version }} # MUST match your pre-commit version
hooks:
- id: sub-pre-commit
alias: backend
name: "pre-commit for backend/"
args: ["-p", "backend/src/ploneorg.brasil"]
files: "^backend/src/ploneorg.brasil/.*"
stages: ["commit"]
- id: sub-pre-commit
alias: frontend
name: "pre-commit for frontend"
args: ["-p", "frontend"]
files: "^frontend/.*"
stages: ["commit"]
87 changes: 44 additions & 43 deletions project/{{ cookiecutter.__folder_name }}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ MAKEFLAGS+=--warn-undefined-variables
MAKEFLAGS+=--no-builtin-rules

CURRENT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
GIT_FOLDER=$(BACKEND_FOLDER)/.git

PROJECT_NAME={{ cookiecutter.project_slug }}
STACK_NAME={{ cookiecutter.__devops_stack_name }}

PRE_COMMIT = pipx run --spec "pre-commit=={{ cookiecutter.__pre_commit_version }}"

# We like colors
# From: https://coderwall.com/p/izxssa/colored-makefile-for-golang-projects
RED=`tput setaf 1`
Expand All @@ -21,58 +24,66 @@ RESET=`tput sgr0`
YELLOW=`tput setaf 3`

.PHONY: all
all: build
all: install

# Add the following 'help' target to your Makefile
# And add help text after each target name starting with '\#\#'
.PHONY: help
help: ## This help message
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: install-frontend
install-frontend: ## Install React Frontend
###########################################
# Frontend
###########################################
.PHONY: frontend-install
frontend-install: ## Install React Frontend
$(MAKE) -C "./frontend/" install

.PHONY: build-frontend
build-frontend: ## Build React Frontend
.PHONY: frontend-build
frontend-build: ## Build React Frontend
$(MAKE) -C "./frontend/" build

.PHONY: start-frontend
start-frontend: ## Start React Frontend
.PHONY: frontend-start
frontend-start: ## Start React Frontend
$(MAKE) -C "./frontend/" start

.PHONY: install-backend
install-backend: ## Create virtualenv and install Plone
$(MAKE) -C "./backend/" build-dev
$(MAKE) create-site
.PHONY: frontend-test
frontend-test: ## Test frontend codebase
@echo "Test frontend"
$(MAKE) -C "./frontend/" test

.PHONY: build-backend
build-backend: ## Build Backend
$(MAKE) -C "./backend/" build-dev
###########################################
# Backend
###########################################
.PHONY: backend-install
backend-install: ## Create virtualenv and install Plone
$(MAKE) -C "./backend/" install
$(MAKE) backend-create-site

.PHONY: create-site
create-site: ## Create a Plone site with default content
.PHONY: backend-build
backend-build: ## Build Backend
$(MAKE) -C "./backend/" install

.PHONY: backend-create-site
backend-create-site: ## Create a Plone site with default content
$(MAKE) -C "./backend/" create-site

.PHONY: start-backend
start-backend: ## Start Plone Backend
.PHONY: backend-start
backend-start: ## Start Plone Backend
$(MAKE) -C "./backend/" start

.PHONY: backend-test
backend-test: ## Test backend codebase
@echo "Test backend"
$(MAKE) -C "./backend/" test

.PHONY: install
install: ## Install
@echo "Install Backend & Frontend"
if [ -d $(GIT_FOLDER) ]; then $(PRE_COMMIT) install; else echo "$(RED) Not installing pre-commit$(RESET)";fi
$(MAKE) install-backend
$(MAKE) install-frontend

# TODO production build

.PHONY: build
build: ## Build in development mode
@echo "Build"
$(MAKE) build-backend
$(MAKE) install-frontend


.PHONY: start
start: ## Start
@echo "Starting application"
Expand All @@ -85,30 +96,20 @@ clean: ## Clean installation
$(MAKE) -C "./backend/" clean
$(MAKE) -C "./frontend/" clean

.PHONY: format
format: ## Format codebase
@echo "Format codebase"
$(MAKE) -C "./backend/" format
$(MAKE) -C "./frontend/" format
.PHONY: check
check: ## Lint and Format codebase
@echo "Lint and Format codebase"
$(MAKE) -C "./backend/" check
$(MAKE) -C "./frontend/" check

.PHONY: i18n
i18n: ## Update locales
@echo "Update locales"
$(MAKE) -C "./backend/" i18n
$(MAKE) -C "./frontend/" i18n

.PHONY: test-backend
test-backend: ## Test backend codebase
@echo "Test backend"
$(MAKE) -C "./backend/" test

.PHONY: test-frontend
test-frontend: ## Test frontend codebase
@echo "Test frontend"
$(MAKE) -C "./frontend/" test

.PHONY: test
test: test-backend test-frontend ## Test codebase
test: backend-test frontend-test ## Test codebase

.PHONY: build-images
build-images: ## Build docker images
Expand Down
Loading

0 comments on commit 6861170

Please sign in to comment.