Skip to content

Commit

Permalink
Add a local WP environment for developing the packages
Browse files Browse the repository at this point in the history
  • Loading branch information
jdamner committed May 29, 2024
1 parent e8171a1 commit 0d0d702
Show file tree
Hide file tree
Showing 22 changed files with 334 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ jobs:
- name: Install dependencies
run: composer install
- name: Run lint
run: ./vendor/bin/phpcs -q --report=checkstyle | cs2pr
run: composer run phpcs -- -q --report=checkstyle | cs2pr
- name: Run tests
run: composer run phpunit
- name: Run static analysis
run: ./vendor/bin/phpstan --error-format=checkstyle --memory-limit=1G analyse | cs2pr
run: composer run phpstan -- --error-format=checkstyle --memory-limit=1G | cs2pr
- name: Run coverage-check
run: composer run coverage-check
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ node_modules
/packages/*/build
.turbo
/composer.lock

# Docker WP Image tends to add these plugins...
packages/akismet
packages/hello.php
packages/index.php

# MU-Plugins & Themes are managed via composer
/docker/wordpress/mu-plugins/*
/docker/wordpress/themes/*
!/docker/wordpress/mu-plugins/plugin-loader.php
7 changes: 3 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ This is the BoxUK mono-repo for our WordPress packages. The [WordPress Skeleton]

## How to use

This mono repo allows you to develop any of the packages. At the moment, there's not any way to run the code in
WordPress, so that's currently left as an open issue for someone to address. PR's welcome!
This mono repo allows you to develop any of the packages. To start a WordPress development environment to test against, just run `docker-compose up -d wordpress` and you'll be able to access WordPress at [http://localhost:8000](http://localhost:8000).

We use [Monorepo Builder](https://github.com/symplify/monorepo-builder) tools to manage the mono-repo dependancies.

Expand Down Expand Up @@ -36,8 +35,8 @@ All packages need to have 100% test coverage. During CI they will be tested for

If your package requires javascript, you can also setup a `package.json` file in the root of the package. Much like `composer.json`, this will be merged automatically at the root level.

To run `npm` commands directly in your package run `bin/npm -w pacakges/<package-name>` with your command. For example `bin/npm -w packages/iconography run test` would run tests specifically in the iconography package.
To run `npm` commands directly in your package run `bin/npm -w packages/<package-name>` with your command. For example `bin/npm -w packages/iconography run test` would run tests specifically in the iconography package.

Commands can also be run globally across all packages using `turbo`. This is setup so that if you run `bin/npm run test` it will run test in every package that has a `pacakge.json` file with a `test` script. You should try to keep naming consistent across packages to support this work. All currently supported scripts in `turbo` are listed in the `turbo.json` file at the root.
Commands can also be run globally across all packages using `turbo`. This is setup so that if you run `bin/npm run test` it will run test in every package that has a `package.json` file with a `test` script. You should try to keep naming consistent across packages to support this work. All currently supported scripts in `turbo` are listed in the `turbo.json` file at the root.

During CI, the `lint`, `test` and `build` NPM scripts are run to validate the package quality. You should ensure your package supports these.
2 changes: 1 addition & 1 deletion bin/composer
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
docker-compose run --rm php composer "$@"
./bin/php composer "$@"
95 changes: 95 additions & 0 deletions bin/create-package
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash

set -e

PACKAGE_NAME=$1

if [ -z "$PACKAGE_NAME" ]; then
echo "Usage: $0 <package-name>"
exit 1
fi

# Validate the package name is in kebab-case
if [[ ! $PACKAGE_NAME =~ ^[a-z0-9-]+$ ]]; then
echo "Package name must be in lowercase with hyphens only"
exit 1
fi

if [ -d "./packages/$PACKAGE_NAME" ]; then
echo "Package already exists at ./packages/$PACKAGE_NAME"
exit 1
fi

# Convert the package name to PascalCase
# Can't use `sed` as the `\U` flag is not supported on MacOS.
PASCAL_PACKAGE_NAME=$(echo $PACKAGE_NAME | awk -F- '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1' OFS="")

# Create the necessary directories etc...
mkdir -p ./packages/$PACKAGE_NAME
mkdir -p ./packages/$PACKAGE_NAME/src
mkdir -p ./packages/$PACKAGE_NAME/tests

cat <<EOT >> ./packages/$PACKAGE_NAME/Readme.md
Please do not submit any Pull Requests here. They will be closed.
---
Please submit your PR here instead: https://github.com/boxuk/wp-packages
This repository is what we call a "subtree split": a read-only subset of that main repository.
We're looking forward to your PR there!
EOT

touch ./packages/$PACKAGE_NAME/.deployignore

cat <<EOT >> ./packages/$PACKAGE_NAME/composer.json
{
"name": "boxuk/$PACKAGE_NAME",
"type": "wordpress-muplugin",
"autoload": {
"psr-4": {
"Boxuk\\\\${PASCAL_PACKAGE_NAME}\\\\": "src"
}
},
"authors": [
{
"name": "BoxUK",
"email": "developers@boxuk.com"
}
],
"require": {}
}
EOT

cat <<EOT >> ./packages/$PACKAGE_NAME/$PACKAGE_NAME.php
<?php
/**
* Plugin Name: $PASCAL_PACKAGE_NAME
* Description: A brief description of the plugin.
* Version: 1.0.0
* Author: BoxUK
* Author URI: https://boxuk.com
*
* @package Boxuk\\${PASCAL_PACKAGE_NAME}
*/
declare( strict_types=1 );
namespace Boxuk\\${PASCAL_PACKAGE_NAME};
// Your code here
EOT

# Add the package to the mono-repo config.
bin/composer run mono:merge

# Modify the auto-load paths (the default is /packages/$PACKAGE_NAME/src), but we need it to be /plugins/$PACKAGE_NAME/src
sed -i '' -e "s/packages\/$PACKAGE_NAME\/src/plugins\/$PACKAGE_NAME\/src/g" composer.json

bin/composer dump-autoload

echo ""
echo "Package created at ./packages/$PACKAGE_NAME"
echo "Please update the .github/workflows/packages.yml file to include the new package in the build process."
echo ""
2 changes: 2 additions & 0 deletions bin/nodejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
docker-compose run --rm nodejs "$@"
2 changes: 1 addition & 1 deletion bin/npm
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env bash
docker-compose run --rm nodejs npm "$@"
bin/nodejs npm "$@"
2 changes: 2 additions & 0 deletions bin/php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
docker-compose run --rm php "$@"
34 changes: 30 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
{
"name": "boxuk/wp-packages",
"version": "1.0.0",
"authors": [
{
"name": "BoxUK",
"email": "developers@boxuk.com"
}
],
"repositories": [
{
"only": [
"wpackagist-plugin/*",
"wpackagist-theme/*"
],
"type": "composer",
"url": "https://wpackagist.org"
}
],
"require": {
"symfony/validator": "^7.0 || ^6.0"
},
Expand All @@ -24,13 +35,15 @@
"permafrost-dev/coverage-check": "^2.0",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"symplify/monorepo-builder": "^11.2",
"szepeviktor/phpstan-wordpress": "^1.3"
"szepeviktor/phpstan-wordpress": "^1.3",
"wpackagist-plugin/sqlite-database-integration": "^2.0",
"wpackagist-theme/twentytwentyfour": "^1.0"
},
"autoload": {
"psr-4": {
"Boxuk\\BoxWpEditorTools\\": [
"packages/editor-tools/src/",
"packages/wp-editor-tools/src/"
"packages/editor-tools/src",
"plugins/editor-tools/src/"
]
}
},
Expand All @@ -39,8 +52,21 @@
"boxuk/wp-iconography": "self.version"
},
"config": {
"vendor-dir": "packages/vendor",
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/installers": true
}
},
"extra": {
"installer-paths": {
"docker/wordpress/mu-plugins/{$name}": [
"type:wordpress-muplugin",
"type:wordpress-plugin"
],
"docker/wordpress/themes/{$name}": [
"type:wordpress-theme"
]
}
}
}
15 changes: 13 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@ version: '3.1'
services:
php:
build: ./docker/php
working_dir: /usr/src/app
working_dir: /var/www/html
volumes:
- .:/usr/src/app
- .:/var/www/html
nodejs:
build: ./docker/nodejs
working_dir: /usr/src/app
volumes:
- .:/usr/src/app

wordpress:
build: ./docker/wordpress
ports:
- "8000:80"
volumes:
- ./docker/wordpress/wp-config.php:/var/www/html/wp-config.php
- ./docker/wordpress/db.php:/var/www/html/wp-content/db.php
- ./docker/wordpress/mu-plugins/:/var/www/html/wp-content/mu-plugins/
- ./docker/wordpress/themes:/var/www/html/wp-content/themes/
- ./packages/:/var/www/html/wp-content/plugins/
1 change: 1 addition & 0 deletions docker/wordpress/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM wordpress:6.5.3
43 changes: 43 additions & 0 deletions docker/wordpress/db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Plugin Name: SQLite integration (Drop-in)
* Version: 1.0.0
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
*
* This file is auto-generated and copied from the sqlite plugin.
* Please don't edit this file directly.
*
* @package wp-sqlite-integration
*/

define( 'SQLITE_DB_DROPIN_VERSION', '1.8.0' );

// Bail early if the SQLite implementation was not located in the plugin.
if ( ! file_exists( ABSPATH . 'wp-content/mu-plugins/sqlite-database-integration/wp-includes/sqlite/db.php' ) ) {
return;
}

// Define SQLite constant.
if ( ! defined( 'DATABASE_TYPE' ) ) {
define( 'DATABASE_TYPE', 'sqlite' );
}

// Require the implementation from the plugin.
require_once ABSPATH . 'wp-content/mu-plugins/sqlite-database-integration/wp-includes/sqlite/db.php';

// Activate the performance-lab plugin if it is not already activated.
add_action(
'admin_footer',
function () {
if ( defined( 'SQLITE_MAIN_FILE' ) ) {
return;
}
if ( ! function_exists( 'activate_plugin' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( is_plugin_inactive( 'sqlite-database-integration' ) ) {
activate_plugin( 'sqlite-database-integration' );
}
}
);
13 changes: 13 additions & 0 deletions docker/wordpress/mu-plugins/plugin-loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* A plugin loader for WordPress.
*
* @package BoxUK/WP
*/

// Define the path to the mu-plugins directory.
define( 'MU_PLUGINS_DIR', __DIR__ );

/**
* Nothing fancy here. Just `require_once` the necessary plugin files.
*/
46 changes: 46 additions & 0 deletions docker/wordpress/wp-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the installation.
* You don't have to use the web site, you can copy this file to "wp-config.php"
* and fill in the values.
*
* This file contains the following configurations:
*
* * Database settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* @link https://wordpress.org/support/article/editing-wp-config-php/
*
* @package WordPress
*/

define( 'DATABASE_TYPE', 'sqlite' );
define( 'DB_DIR', '/var/www/html/wp-content/database' );
define( 'DB_FILE', 'wordpress.db' );

define( 'AUTH_KEY', 'uDh8?//l(9c$Qu`NRD4H3k426U3V}x!I#{pbFV7Z!1eT)T$[+6Go)Gpg?K5Z|R{D' );
define( 'SECURE_AUTH_KEY', 'nGAeRQI%DhdGhMoT.{Ba(&V5D[Z7FDO@NHRX_d3;q~S+jko-tsA; `+~6!<r_X>b' );
define( 'LOGGED_IN_KEY', 'cqiEHUYDtHxsqW=w|_.g{uCL2cifNJMi_WL2yf?sn)=UF+6nhO>a-qKLH75bZ)n1' );
define( 'NONCE_KEY', ';H#]ee;nLS~h l~Oy(p|1f<@-;yG1%Y{ h(P8-T<(9X+M(?uUkD)obyL+R[)<Z4z' );
define( 'AUTH_SALT', 'pjVYn-F]kG%^)##=3^Y@_bH@OW~3#fp:D;B-+#skTM{W+|MtedR|qVn%hQ3Iv2 &' );
define( 'SECURE_AUTH_SALT', ')C4@PV(d:DU0}i{,P[r.mrjfwyX!>kZvvhRW$|QZQ;S&8&Rep.N$*E938l9bxi`m' );
define( 'LOGGED_IN_SALT', '%V}O=GU*_{),8)-n-DXKhZXhqeu}lR1U0oA,#f+=7br+&g2TAE_>ZL-,ogsWuedO' );
define( 'NONCE_SALT', 'dm[=a6+2b1rHuGW=xE`#-<}<w_ -dnmdvM%:ZcxLCCPtq2>o{}NlqT>4<kL6nX-#' );

/**#@-*/
$table_prefix = 'wp_'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
define( 'WP_DEBUG', true );
define( 'WP_SCRIPT_DEBUG', true );

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-content/plugins/vendor/autoload.php';
require_once ABSPATH . 'wp-settings.php';
Loading

0 comments on commit 0d0d702

Please sign in to comment.