Skip to content

Instantly share code, notes, and snippets.

@markcummins
Created September 18, 2022 18:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markcummins/561c76c8d1dc2dc33907d630b1da472f to your computer and use it in GitHub Desktop.
Save markcummins/561c76c8d1dc2dc33907d630b1da472f to your computer and use it in GitHub Desktop.
WP Scaffold Plugin
#!/bin/bash
echo "Plugin Name:"
read plugin_name;
# Make Sure Directory doesn't exist
if test -d "./${plugin_name}"; then
echo "Exiting, this directory already exists, try using a different name";
exit 0
fi
# Make Sure NPM is Installed
npm=`which npm 2>&1`
if [ $? -ne 0 ]; then
echo "Exiting, npm was not found"
exit 0
fi
# Make the Directory and Install WP-Env
mkdir "${plugin_name}"
chown $USER:www-data -R "${plugin_name}"
cd "./${plugin_name}"
npm init && npm install --save-dev @wordpress/env
# Update Package.Json and Create .wp-env config file
sed -i '/"test":/d' ./package.json
sed -i '/"scripts":/ s/$/\n "wp-env": "wp-env"/' ./package.json
sed -i '/"wp-env":/ s/$/,\n "wp": "wp-env run cli"/' ./package.json
sed -i "/\"wp\":/ s/$/,\n \"preunit:php\": \"wp-env start \&\& wp-env run composer 'install --no-interaction'\"/" ./package.json
sed -i "/\"preunit:php\":/ s/$/,\n \"unit:php\": \"wp-env run phpunit 'phpunit -c \/var\/www\/html\/wp-content\/plugins\/${plugin_name}\/phpunit.xml.dist --verbose'\"/" ./package.json
# Create the default .wp-env file
echo '{ "plugins": [ "." ], "config": { "WP_DEBUG": true, "SCRIPT_DEBUG": true } }' > .wp-env.json
# Start the Dev Environment
npm run wp-env start
# Create the Plugin (Skip Creating package.json when prompted)
echo -e "s" | npm run wp scaffold plugin \'${plugin_name}\' --dir=.
# Remove stuff we don't need (Optional)
rm Gruntfile.js .editorconfig .travis.yml
# Set the name in `./phpunit.xml.dist` to `<testsuite name="default">`
sed -i 's/<testsuite>/<testsuite name="default">/g' ./phpunit.xml.dist
# Include Yoast Polyfill
echo '{"require-dev": {"yoast/phpunit-polyfills": "^1.0"}}' > composer.json
# Install Composer Dependencies && Include Yoast in tests/bootstrap.php
sed -i "s/<?php/<?php\n\nrequire dirname(dirname(__FILE__)) . '\/vendor\/yoast\/phpunit-polyfills\/phpunitpolyfills-autoload.php';/g" ./tests/bootstrap.php
cp ./tests/test-sample.php ./tests/test-plugin.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment