Skip to content

Instantly share code, notes, and snippets.

@renventura
Last active April 17, 2020 12:34
Show Gist options
  • Save renventura/1991c080e432ba86504183e8d313a38a to your computer and use it in GitHub Desktop.
Save renventura/1991c080e432ba86504183e8d313a38a to your computer and use it in GitHub Desktop.
BASH command for generating a boilerplate WordPress plugin from https://github.com/renventura/boilerplate-plugin.
#!/bin/bash
##############################
###### Get plugin data #######
##############################
# Main plugin key
echo "Plugin key (lowercase with dashes; e.g. 'my-plugin'): "
read PLUGIN_KEY
if test -z "$PLUGIN_KEY"
then
echo "You must specify a plugin key."
exit
else
PLUGIN_KEY=$(echo $PLUGIN_KEY | awk '{print tolower($0)}')
DEFAULT_NAME=$(echo $PLUGIN_KEY | sed -e 's/-/ /' | awk '{for (i=1; i<=NF; ++i) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)); } print }')
fi
# Plugin name
echo "Plugin name (default: $DEFAULT_NAME): "
read PLUGIN_NAME
if test -z "$PLUGIN_NAME"
then
PLUGIN_NAME=$DEFAULT_NAME
fi
# Plugin description
echo "Plugin description (default: empty): "
read PLUGIN_DESCRIPTION
# Plugin URL
echo "Plugin URI (default: #): "
read PLUGIN_URI
if test -z "$PLUGIN_URI"
then
PLUGIN_URI='#'
fi
# Git Repository URL
echo "Git Repository URI (no .git extension; default: #): "
read GIT_REPO_URI
if test -z "$GIT_REPO_URI"
then
GIT_REPO_URI='#'
fi
# Git origin URL
echo "Git Origin URI (used for git remote add origin; uses .git extension): "
read GIT_ORIGIN_URI
# Author Name
echo "Author name (default: Ren Ventura): "
read PLUGIN_AUTHOR
if test -z "$PLUGIN_AUTHOR"
then
PLUGIN_AUTHOR="Ren Ventura"
fi
# Author email
echo "Author email address (default: rv@renventura.com): "
read AUTHOR_EMAIL
if test -z "$AUTHOR_EMAIL"
then
AUTHOR_EMAIL="rv@renventura.com"
fi
# Author URI
echo "Author URI (default: https://renventura.com): "
read AUTHOR_URI
if test -z "$AUTHOR_URI"
then
AUTHOR_URI="https://renventura.com"
fi
echo "Author's vendor name (default: renventura): "
read AUTHOR_NAMESPACE
if test -z "$AUTHOR_NAMESPACE"
then
AUTHOR_NAMESPACE="renventura"
fi
##############################
######### Other data #########
##############################
# Format PLUGIN_KEY for various uses
PLUGIN_KEY_CLEAN=$(echo $PLUGIN_KEY | sed 's/\./\_/g')
PLUGIN_KEY_CLEAN_US=$(echo $PLUGIN_KEY_CLEAN | sed 's/\-/\_/g')
PLUGIN_KEY_CLEAN_US_UPPER=$(echo "$PLUGIN_KEY_CLEAN_US" | awk '{print toupper($0)}')
# Escape URIs for regexp use within sed
PLUGIN_URI=$(echo $PLUGIN_URI | sed -e 's/\//\\\//g')
AUTHOR_URI=$(echo $AUTHOR_URI | sed -e 's/\//\\\//g')
GIT_REPO_URI=$(echo $GIT_REPO_URI | sed -e 's/\//\\\//g')
# Current year
YEAR=$(date +%Y)
##############################
### Pull down boilerplate ####
##############################
# Fetch the current base code from GitHub
git clone https://github.com/renventura/boilerplate-plugin $PLUGIN_KEY
# Cleanup project directory
rm -rf $PLUGIN_KEY/.git $PLUGIN_KEY/README.md
mv $PLUGIN_KEY/boilerplate-plugin.php $PLUGIN_KEY/$PLUGIN_KEY.php
mv $PLUGIN_KEY/assets/css/sass/boilerplate-plugin.scss $PLUGIN_KEY/assets/css/sass/$PLUGIN_KEY.scss
mv $PLUGIN_KEY/gitignore.txt $PLUGIN_KEY/.gitignore
mv $PLUGIN_KEY/gitattributes.txt $PLUGIN_KEY/.gitattributes
mv $PLUGIN_KEY/README_BLANK.md $PLUGIN_KEY/README.md
rm $PLUGIN_KEY/assets/css/sass/partials/.keep
rm $PLUGIN_KEY/assets/images/raw/.keep
rm $PLUGIN_KEY/assets/js/custom/.keep
rm $PLUGIN_KEY/assets/js/vendors/.keep
rm $PLUGIN_KEY/languages/.keep
# Add the origin if provided
if test ! -z "$GIT_ORIGIN_URI"
then
cd $PLUGIN_KEY
git init
git remote add origin "$GIT_ORIGIN_URI"
cd ..
fi
############################################
# Search and replace boilerplate variables #
############################################
# %YEAR% - Year
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%YEAR%/$YEAR/g" {} +
# %PLUGIN_KEY% - Plugin key
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%PLUGIN_KEY%/$PLUGIN_KEY_CLEAN/g" {} +
# %PLUGIN_PREFIX% - Plugin prefix (for non-namespace functionality; uses underscores)
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%PLUGIN_PREFIX%/$PLUGIN_KEY_CLEAN_US/g" {} +
# %TEXT_DOMAIN% - Text domain
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%TEXT_DOMAIN%/$PLUGIN_KEY_CLEAN/g" {} +
# %PLUGIN_CONSTANT_PREFIX% - Constant prefix
find $PLUGIN_KEY -type f -name "*.php" -exec sed -i '' "s/%PLUGIN_CONSTANT_PREFIX%/$PLUGIN_KEY_CLEAN_US_UPPER/g" {} +
# %PLUGIN_DOMAIN% - Plugin domain; all lowercase (e.g. renventura/my-plugin)
# Have to use an alternate delimiter because the PLUGIN_DOMAIN variable has a "/"
PLUGIN_DOMAIN=$(echo "$AUTHOR_NAMESPACE/$PLUGIN_KEY_CLEAN" | awk '{print tolower($0)}')
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s~%PLUGIN_DOMAIN%~$PLUGIN_DOMAIN~g" {} +
# %PLUGIN_NAMESPACE% - Apply plugin key as pascal case namespace
PLUGIN_NAMESPACE=$(echo $PLUGIN_KEY_CLEAN | sed -e 's/-/ /' | awk '{for (i=1; i<=NF; ++i) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)); } print }' | sed -e 's/ //')
find $PLUGIN_KEY -type f -name "*.php" -exec sed -i '' "s/%PLUGIN_NAMESPACE%/$PLUGIN_NAMESPACE/g" {} +
# %PLUGIN_NAME% - Plugin name
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%PLUGIN_NAME%/$PLUGIN_NAME/g" {} +
# %PLUGIN_DESCRIPTION% - Plugin description
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%PLUGIN_DESCRIPTION%/$PLUGIN_DESCRIPTION/g" {} +
# %PLUGIN_URI% - Plugin's own website
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%PLUGIN_URI%/$PLUGIN_URI/g" {} +
# %GIT_REPO_URI% - Plugin's Git repository
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%GIT_REPO_URI%/$GIT_REPO_URI/g" {} +
# %PLUGIN_AUTHOR% - Plugin author name
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%PLUGIN_AUTHOR%/$PLUGIN_AUTHOR/g" {} +
# %AUTHOR_EMAIL% - Plugin author email address
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%AUTHOR_EMAIL%/$AUTHOR_EMAIL/g" {} +
# %AUTHOR_URI% - Plugin author website
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%AUTHOR_URI%/$AUTHOR_URI/g" {} +
# %AUTHOR_NAMESPACE% - Plugin author namespace (Usually a company name; used as the top-level PHP namespace)
find $PLUGIN_KEY -type f -name "*.*" -exec sed -i '' "s/%AUTHOR_NAMESPACE%/$AUTHOR_NAMESPACE/g" {} +
#################
### All done ####
#################
echo "Ready to roll..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment