Skip to content

Commit

Permalink
Fix project generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ericof committed May 17, 2024
1 parent 910a048 commit bd30e5a
Show file tree
Hide file tree
Showing 24 changed files with 359 additions and 108 deletions.
34 changes: 22 additions & 12 deletions backend_addon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ YELLOW=`tput setaf 3`
.PHONY: all
all: build

TEMPLATE = $(shell basename $(CURRENT_DIR))
ADDON_FOLDER_NAME = collective.addon
BASE_FOLDER = ../
BIN_FOLDER = ${BASE_FOLDER}/bin


# Add the following 'help' target to your Makefile
# And add help text after each target name starting with '\#\#'
Expand All @@ -21,24 +26,29 @@ help: ## This help message

.PHONY: clean
clean: ## Clean
rm -rf collective.addon
rm -rf volto-addon

../bin/cookieplone: ## cookieplone installation
$(MAKE) -C ".." bin/cookieplone
$(BIN_FOLDER)/cookieplone: ## cookieplone installation
$(MAKE) -C $(BASE_FOLDER) bin/cookieplone

.PHONY: format
format: ../bin/cookieplone ## Format code
format: $(BIN_FOLDER)/cookieplone ## Format code
@echo "$(GREEN)==> Formatting codebase $(RESET)"
../bin/black hooks tests
../bin/isort hooks tests
$(BIN_FOLDER)/black hooks tests
$(BIN_FOLDER)/isort hooks tests

.PHONY: generate
generate: ../bin/cookieplone ## Create a sample package
generate: $(BIN_FOLDER)/cookieplone ## Create a sample package
@echo "$(GREEN)==> Creating new test package$(RESET)"
rm -rf collective.addon
../bin/cookieplone . --no-input
rm -rf $(ADDON_FOLDER_NAME)
COOKIEPLONE_REPOSITORY=$(BASE_FOLDER) $(BIN_FOLDER)/cookieplone $(TEMPLATE) --no_input

.PHONY: test
test: ../bin/cookieplone ## Create a sample package and tests it
@echo "$(GREEN)==> Creating new test package$(RESET)"
../bin/python -m pytest tests
test: $(BIN_FOLDER)/cookieplone ## Create a sample package and tests it
@echo "$(GREEN)==> Test template$(RESET)"
$(BIN_FOLDER)/python -m pytest tests

.PHONY: test-pdb
test-pdb: $(BIN_FOLDER)/cookieplone ## Stop on the first failed test
@echo "$(GREEN)==> Test template, stop on first error$(RESET)"
$(BIN_FOLDER)/python -m pytest tests -x --pdb
1 change: 1 addition & 0 deletions backend_addon/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"__package_namespace": "{{ cookiecutter.python_package_name | package_namespace }}",
"__folder_name": "{{ cookiecutter.python_package_name }}",
"__python_package_name_upper": "{{ cookiecutter.python_package_name | pascal_case }}",
"__profile_language": "en",
"__version_package": "1.0.0a0",
"__profile_version": "1000",
"__gha_enable": true,
Expand Down
76 changes: 37 additions & 39 deletions backend_addon/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,63 @@
"""Post generation hook."""

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

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


context: OrderedDict = {{cookiecutter}}


# PATH OF CONTENT TO BE REMOVED
TO_REMOVE_PATHS = {
FEATURES_TO_REMOVE = {
"feature_headless": [
"serializers",
]
}

def handle_feature_headless(context: OrderedDict, output_dir: Path):
package_namespace = context.get("__package_namespace")
package_name = context.get("__package_name")
output_dir = output_dir / "src" / package_namespace / package_name
files.remove_files(output_dir, FEATURES_TO_REMOVE["feature_headless"])

def run_cmd(command: str, shell: bool, cwd: str) -> bool:
proc = subprocess.run(command, shell=shell, cwd=cwd, capture_output=True)
if proc.returncode:
# Write errors to the main process stderr
console.error(f"Error while running {command}")
return False if proc.returncode else True


def remove_files(category: str):
to_remove = TO_REMOVE_PATHS.get(category, [])
package_namespace = "{{ cookiecutter.__package_namespace }}"
package_name = "{{ cookiecutter.__package_name }}"
base_path = Path("src") / package_namespace / package_name
# Remove all files
files.remove_files(base_path, to_remove)


def initialize_git():
"""Apply black and isort to the generated codebase."""
console.info("Git repository")
steps = [
["Initialize", ["git", "init", "."], False, "."],
["Add files", ["git", "add", "."], False, "."],
]
for step in steps:
msg, command, shell, cwd = step
console.info(f" - {msg}")
result = run_cmd(command, shell=shell, cwd=cwd)
if not result:
sys.exit(1)
def handle_git_initialization(context: OrderedDict, output_dir: Path):
"""Initialize a GIT repository for the project codebase."""
git.initialize_repository(output_dir)


def main():
"""Final fixes."""
keep_headless = int(context.get("feature_headless"))
if not keep_headless:
remove_files("feature_headless")
if int(context.get("__backend_addon_git_initialize")):
initialize_git()
output_dir = Path().cwd()
remove_headless = not int(context.get("feature_headless"))
initialize_git = bool(int(context.get("__backend_addon_git_initialize")))
# Cleanup / Git
actions = [
[
handle_feature_headless,
"Remove files used in headless setup",
remove_headless,
],
[
handle_git_initialization,
"Initialize Git repository",
initialize_git,
],
]
for func, title, enabled in actions:
if not int(enabled):
continue
new_context = deepcopy(context)
console.print(f" -> {title}")
func(new_context, output_dir)

msg = """
[bold blue]{{ cookiecutter.title }}[/bold blue]
Now, enter the repositorym run the code formatter with:
Now, enter the repository run the code formatter with:
make format
Expand Down
10 changes: 10 additions & 0 deletions backend_addon/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"pyproject.toml",
"README.md",
"requirements.txt",
"scripts/create_site.py",
"setup.py",
"tox.ini",
]
Expand Down Expand Up @@ -88,6 +89,7 @@ def context(cookieplone_root) -> dict:
"author": "Plone Collective",
"email": "[email protected]",
"feature_headless": "1",
"__backend_addon_git_initialize": "1",
"__cookieplone_repository_path": f"{cookieplone_root}",
}

Expand All @@ -100,6 +102,14 @@ def context_no_headless(context) -> dict:
new_context["feature_headless"] = "0"
return new_context

@pytest.fixture(scope="session")
def context_no_git(context) -> dict:
"""Cookiecutter context without Git repository."""
new_context = deepcopy(context)
new_context["python_package_name"] = "collective.addonnogit"
new_context["__backend_addon_git_initialize"] = "0"
return new_context


@pytest.fixture(scope="session")
def bad_context() -> dict:
Expand Down
32 changes: 32 additions & 0 deletions backend_addon/tests/test_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from pathlib import Path
from .conftest import PKG_SRC_FEATURE_HEADLESS, PKG_SRC_FILES, ROOT_FILES


Expand Down Expand Up @@ -56,3 +57,34 @@ def test_pkg_src_feature_files_generated(cutter_result, file_path: str):
path = src_path / file_path
assert path.exists()
assert path.is_file()


@pytest.mark.parametrize(
"file_path,schema_name",
[
[".github/workflows/meta.yml", "github-workflow"],
[".pre-commit-config.yaml", "pre-commit-config"],
["pyproject.toml", "pyproject"],
],
)
def test_json_schema(
cutter_result, schema_validate_file, file_path: str, schema_name: str
):
path = cutter_result.project_path / file_path
assert schema_validate_file(path, schema_name)


def test_git_initialization(cutter_result):
from cookieplone.utils import git

path = cutter_result.project_path
repo = git.repo_from_path(path)
assert Path(repo.working_dir) == path


def test_git_initialization_not_set(cookies, context_no_git):
from cookieplone.utils import git

cutter_result = cookies.bake(extra_context=context_no_git)
path = cutter_result.project_path
assert git.check_path_is_repository(path) is False
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated from:
# https://github.com/plone/meta/tree/master/config/default
# https://github.com/plone/meta/tree/main/config/default
# See the inline comments on how to expand/tweak this configuration file
name: Meta
on:
Expand Down Expand Up @@ -28,14 +28,15 @@ jobs:
uses: plone/meta/.github/workflows/qa.yml@main
test:
uses: plone/meta/.github/workflows/test.yml@main
with:

py-versions: '["3.12", "3.11", "3.10"]'
coverage:
uses: plone/meta/.github/workflows/coverage.yml@main
dependencies:
uses: plone/meta/.github/workflows/dependencies.yml@main
release_ready:
uses: plone/meta/.github/workflows/release_ready.yml@main
circular:
uses: plone/meta/.github/workflows/circular.yml@main

##
# To modify the list of default jobs being created add in .meta.toml:
Expand All @@ -57,6 +58,13 @@ jobs:
# os_dependencies = "git libxml2 libxslt"
##

##
# To test against a specific matrix of python versions
# when running tests jobs, add in .meta.toml:
# [github]
# py_versions = "['3.12', '3.11']"
##


##
# Specify additional jobs in .meta.toml:
Expand Down
23 changes: 17 additions & 6 deletions backend_addon/{{ cookiecutter.__folder_name }}/.meta.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
# Generated from:
# https://github.com/plone/meta/tree/master/config/default
# https://github.com/plone/meta/tree/main/config/default
# See the inline comments on how to expand/tweak this configuration file
[meta]
template = "default"
commit-id = "25d2fa7f"
commit-id = "71d0218b"

[pyproject]
codespell_skip = "*.min.js"
codespell_ignores = "vew"
dependencies_ignores = "['plone.volto', 'zestreleaser.towncrier', 'zest.releaser', 'pytest', 'pytest-cov', 'pytest-plone']"
dependencies_mappings = [
"Plone = ['Products.CMFPlone', 'Products.CMFCore', 'Products.GenericSetup']",
]
"pytest-plone = ['pytest', 'plone.testing', 'plone.app.testing']",
]
dependencies_ignores = "['plone.app.iterate', 'plone.app.upgrade', 'plone.volto', 'zestreleaser.towncrier', 'zest.releaser', 'pytest-cov']"

[tox]
use_mxdev = true
test_runner = "pytest"
test_path = "/tests"
use_mxdev = true
test_deps_additional = ""

[github]
py_versions = "[\"3.12\", \"3.11\", \"3.10\"]"
jobs = [
"qa",
"test",
"coverage",
"dependencies",
"release_ready",
]
2 changes: 2 additions & 0 deletions backend_addon/{{ cookiecutter.__folder_name }}/MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ graft src/{{ cookiecutter.__package_namespace }}
graft docs
graft news
graft tests
graft scripts
include *.acceptance
include .coveragerc
include .dockerignore
include .editorconfig
Expand Down
16 changes: 10 additions & 6 deletions backend_addon/{{ cookiecutter.__folder_name }}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ bin/pip bin/tox bin/mxdev:
bin/pip install -U "pip" "wheel" "cookiecutter" "mxdev" "tox" "pre-commit"
if [ -d $(GIT_FOLDER) ]; then bin/pre-commit install; else echo "$(RED) Not installing pre-commit$(RESET)";fi

.PHONY: config
config: bin/pip ## Create instance configuration
instance/etc/zope.ini: bin/pip ## Create instance configuration
@echo "$(GREEN)==> Create instance configuration$(RESET)"
bin/cookiecutter -f --no-input --config-file instance.yaml gh:plone/cookiecutter-zope-instance

.PHONY: config
config: instance/etc/zope.ini

.PHONY: build-dev
build-dev: config ## pip install Plone packages
@echo "$(GREEN)==> Setup Build$(RESET)"
Expand All @@ -65,11 +67,9 @@ build-dev: config ## pip install Plone packages
.PHONY: install
install: build-dev ## Install Plone 6.0


.PHONY: build
build: build-dev ## Install Plone 6.0


.PHONY: clean
clean: ## Remove old virtualenv and creates a new one
@echo "$(RED)==> Cleaning environment and build$(RESET)"
Expand All @@ -80,9 +80,13 @@ start: ## Start a Plone instance on localhost:8080
PYTHONWARNINGS=ignore ./bin/runwsgi instance/etc/zope.ini

.PHONY: console
console: ## Start a zope console
console: instance/etc/zope.ini ## Start a zope console
PYTHONWARNINGS=ignore ./bin/zconsole debug instance/etc/zope.conf

.PHONY: create-site
create-site: instance/etc/zope.ini ## Create a new site from scratch
PYTHONWARNINGS=ignore ./bin/zconsole run instance/etc/zope.conf ./scripts/create_site.py

.PHONY: format
format: bin/tox ## Format the codebase according to our standards
@echo "$(GREEN)==> Format codebase$(RESET)"
Expand All @@ -100,7 +104,7 @@ bin/i18ndude: bin/pip
.PHONY: i18n
i18n: bin/i18ndude ## Update locales
@echo "$(GREEN)==> Updating locales$(RESET)"
bin/update_dist_locale
bin/update_locale

# Tests
.PHONY: test
Expand Down
Loading

0 comments on commit bd30e5a

Please sign in to comment.