Skip to content

Commit

Permalink
Merge pull request #12 from jxhnx/feat/add-wordpress-template
Browse files Browse the repository at this point in the history
feat: add wordpress template
  • Loading branch information
jxhnx authored Apr 10, 2024
2 parents 4500d59 + 8293abe commit 15813c9
Show file tree
Hide file tree
Showing 25 changed files with 1,648 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/generate_templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
python: ${{ steps.filter.outputs.python }}
python_datascience: ${{ steps.filter.outputs.python_datascience }}
terraform: ${{ steps.filter.outputs.terraform }}
wordpress: ${{ steps.filter.outputs.wordpress }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
Expand All @@ -28,6 +29,8 @@ jobs:
- 'templates-single/python_datascience/**'
terraform:
- 'templates-single/terraform/**'
wordpress:
- 'templates-stack/wordpress/**'
default_template:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -73,3 +76,20 @@ jobs:
- uses: ./.github/actions/composite_setup_tests
- uses: hashicorp/setup-terraform@v3
- run: python -m pytest templates-single/terraform tests

wordpress:
runs-on: ubuntu-latest
needs: changes
if: ${{ needs.changes.outputs.wordpress == 'true' }}
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/composite_setup_tests
- name: Install PHP, composer and dependencies
run: |
sudo apt-get update
sudo apt-get install -y php-cli php-xml php-curl composer
cp templates-stack/wordpress/{{cookiecutter.repo_name}}/composer.json .
composer update
- uses: actions/setup-node@v4
- run: npm install @wordpress/prettier-config @wordpress/eslint-plugin --save-dev
- run: python -m pytest templates-stack/wordpress tests
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude: "{{cookiecutter.repo_name}}(?:/.+\\.yaml$)"
exclude: "{{cookiecutter.repo_name}}(/.*)?"

default_language_version:
python: python3.10
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ Templates are divided into two categories: single and stack. Single templates ar
Tests are written with `pytest`. To run tests locally, use `python -m pytest default_template tests` and replace `default_template` with the template you want to test. Tests just using cookiecutter are marked with `@pytest.mark.default`, tests using additional system requirements and installs are marked with `@pytest.mark.system`.

The Github CI uses pytest in the same way and runs tests for all templates changes are detected in. This is the recommended approach since it installs template dependencies in a system independent container environment. Use [act](https://github.com/nektos/act) to run Github workflows locally, e.g., with `act -W .github/workflows/generate-templates.yaml --container-architecture linux/amd64` (you may have to remove/adjust the --container-architecture).

## Tags

- Major: Breaking changes
- Minor: New template or feature in multiple templates
- Service: Feature or fix in single template
3 changes: 2 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"default (./default_template)",
"python (./templates-single/python)",
"python_datascience (./templates-single/python_datascience)",
"terraform (./templates-single/terraform)"
"terraform (./templates-single/terraform)",
"wordpress (./templates-stack/wordpress)"
]
}
21 changes: 21 additions & 0 deletions templates-stack/wordpress/cookiecutter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"project_name": "My Awesome Project",
"repo_name": "{{ cookiecutter.project_name.lower()|replace(' ', '-')|replace('_', '-')|replace('.', '-')|trim() }}",
"description": "Awesome project description.",
"author_name": "John Hoffmann",
"open_source_license": [
"MIT",
"BSD",
"GPLv3",
"Apache Software License 2.0",
"Not open source"
],
"ci_tool": ["None", "Gitlab", "Github"],
"__default_code_formatter_print_width": "140",
"code_formatter_print_width": "{{ cookiecutter.__default_code_formatter_print_width }}",
"wordpress_coding_standards": ["strict", "relaxed"],
"git_init": false,
"__prompts__": {
"code_formatter_print_width": "code_formatter_print_width (recommended: 0/120/140)"
}
}
134 changes: 134 additions & 0 deletions templates-stack/wordpress/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import os
import shutil
import subprocess

import requests

TERMINATOR = "\x1b[0m"
WARNING = "\x1b[1;33m [WARNING]: "
INFO = "\x1b[1;33m [INFO]: "
HINT = "\x1b[3;33m"
SUCCESS = "\x1b[1;32m [SUCCESS]: "


def yes(value):
return str(value).lower() not in ["n", "0", "false", "f", "no", "off"]


def remove_open_source_files():
file_names = ["LICENSE"]
for file_name in file_names:
os.remove(file_name)


def remove_gplv3_files():
file_names = ["COPYING"]
for file_name in file_names:
os.remove(file_name)


def remove_dotgitlabciyml_file():
os.remove(".gitlab-ci.yml")


def remove_bitbucketpipelinesyml_file():
os.remove("bitbucket-pipelines.yml")


def remove_dotgithub_folder():
shutil.rmtree(".github")


def generate_gitignore():
print(INFO + "Fetching recent .gitignore rules..." + TERMINATOR)

rules = ["dotenv", "windows", "macos", "linux", "wordpress"]
url = "https://www.toptal.com/developers/gitignore/api/" + ",".join(rules)

try:
response = requests.get(url)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(WARNING + f"Error: {e}, using cached .gitignore" + TERMINATOR)
return None

custom_header = f"""######################################################
# Custom / project-specific .gitignore
######################################################
# Wordpress
/wordpress/*
!/wordpress/wp-content/
/wordpress/wp-content/*
!/wordpress/wp-content/themes
/wordpress/wp-content/themes/*
!/wordpress/wp-content/themes/my-theme
# CI
/vendor
/node_modules
# Moving data
/data
######################################################
# Generated .gitignore for {", ".join(rules)}
######################################################
"""

combined_gitignore = custom_header + "\n" + response.text

with open(".gitignore", "w") as gitignore_file:
gitignore_file.write(combined_gitignore)


def run_pre_commit_subprocess():
print(INFO + "Run pre-commit..." + TERMINATOR)
subprocess.call(["git", "init", "-b", "main"])
subprocess.call(["git", "add", "*"])
subprocess.call(["pre-commit", "run", "--all-files"])
subprocess.call(["rm", "-rf", ".git"])


def run_initial_git_commit_subprocess():
print(INFO + "Initializing git repository..." + TERMINATOR)
subprocess.call(["git", "init", "-b", "main"])
subprocess.call(["git", "add", "*"])
subprocess.call(["git", "commit", "-m", "Initial commit"])


def main():
if "{{ cookiecutter.open_source_license }}" == "Not open source":
remove_open_source_files()

if "{{ cookiecutter.open_source_license }}" != "GPLv3":
remove_gplv3_files()

if "{{ cookiecutter.ci_tool }}" != "Gitlab":
remove_dotgitlabciyml_file()

if "{{ cookiecutter.ci_tool }}" != "Github":
remove_dotgithub_folder()

generate_gitignore()

if (
"{{ cookiecutter.code_formatter_print_width }}"
!= "{{ cookiecutter.__default_code_formatter_print_width }}"
):
run_pre_commit_subprocess()

if yes("{{ cookiecutter.git_init | default('false') }}"):
run_initial_git_commit_subprocess()

print(SUCCESS + "The cookie is cut!" + TERMINATOR)


if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions templates-stack/wordpress/hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
assert "\\" not in "{{ cookiecutter.author_name }}", "Don't include backslashes in author name."

code_formatter_print_width = "{{ cookiecutter.code_formatter_print_width }}"
assert (
code_formatter_print_width.isdigit() and float(code_formatter_print_width).is_integer()
), "Invalid value for code_formatter_print_width. Please provide an integer value."
24 changes: 24 additions & 0 deletions templates-stack/wordpress/hooks/pre_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json
import shutil

COOKIEJSON_PATH = "cookiecutter.json"


def remove_cookiecutterjson_key(key: str):
with open(COOKIEJSON_PATH, "r") as file:
cookiecutter_dict = json.load(file)

if key in cookiecutter_dict:
del cookiecutter_dict[key]

with open(COOKIEJSON_PATH, "w") as file:
json.dump(cookiecutter_dict, file, indent=2)


def main():
if shutil.which("git") is None:
remove_cookiecutterjson_key("git_init")


if __name__ == "__main__":
main()
19 changes: 19 additions & 0 deletions templates-stack/wordpress/test_bake_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pathlib import Path

import pytest

from tests.common import generate_cookiecutter_template

template_dir = Path(__file__).resolve().parent


@pytest.mark.default
def test_default():
git_init = {"git_init": "true"}
lint_non_default_print_width = {"code_formatter_print_width": "30", "git_init": "true"}
no_print_width_limit = {"code_formatter_print_width": "0"}
relaxed_rulset = {"wordpress_coding_standards": "relaxed"}
generate_cookiecutter_template(template_dir, git_init)
generate_cookiecutter_template(template_dir, lint_non_default_print_width)
generate_cookiecutter_template(template_dir, no_print_width_limit)
generate_cookiecutter_template(template_dir, relaxed_rulset)
29 changes: 29 additions & 0 deletions templates-stack/wordpress/{{cookiecutter.repo_name}}/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Get live data
FTP_READ_USERNAME=fe15ee4
FTP_READ_PASSWORD=password
FTP_HOST=subdomain.example.com
PROD_WP_ROOT_DIR=example.com
LOCAL_WP_ROOT_DIR=wordpress
PROD_BACKUP_DIR=backup/example.com

# Live website (WordPress)
DOMAIN=example.com
PROD_DB_NAME=fe45224d
WP_DB_PREFIX=dk583 # randomly generated by WordPress
PROD_WP_BASE_URL=https://www.example.com

# Local website
WP_PORT=8080 # adjust init.sql accordingly
STAGING_WP_BASE_URL=http://localhost:$WP_PORT
STAGING_DB_NAME=$PROD_DB_NAME
STAGING_DB_USER=john
STAGING_DB_PASSWORD="Neuter#Scoop5#Velcro"
STAGING_DB_ROOT_PASSWORD=$STAGING_DB_PASSWORD

WP_HOST_DB_NAME=wp_database
WP_HOST_DB_PORT=3306

# Import and db setup
WP_CONFIG_PATH="./wordpress/wp-config.php"
PROD_DB_DUMP_PATH="./data/$PROD_DB_NAME.sql"
PROD_DB_INIT_PATH="./local-init.sql"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
env:
browser: true
es2021: true
extends: ['eslint:recommended', 'plugin:@wordpress/eslint-plugin/recommended']
parserOptions:
ecmaVersion: latest
sourceType: module
rules: { 'prettier/prettier': 0, 'no-unused-vars': 'off' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: pre-commit

on:
pull_request:
push:
branches: [main]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install PHP, composer and dependencies
run: |
sudo apt-get update
sudo apt-get install -y php-cli php-xml php-curl composer
composer update
- uses: actions/setup-node@v4
- run: npm install @wordpress/prettier-config @wordpress/eslint-plugin --save-dev
- uses: pre-commit/[email protected]
Loading

0 comments on commit 15813c9

Please sign in to comment.