Skip to content

Instantly share code, notes, and snippets.

@g000m
Last active October 5, 2021 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g000m/f9bdfffb92310761d8afc5f38e3ebc26 to your computer and use it in GitHub Desktop.
Save g000m/f9bdfffb92310761d8afc5f38e3ebc26 to your computer and use it in GitHub Desktop.
For converting a standard WordPress install to composer-based WP
#!/bin/bash
### 1. check for requirements & set variables
# check for directory name as input
if [ -z $1 ]; then
echo "bedrock directory name must be passed in"
exit 1
fi
# make sure composer is installed
which composer
if [ $? -eq 127 ]; then
echo "Please make sure composer is installed. /"which composer/" was not successful."
exit 1
fi
# @TODO make sure we're in a WP root.
# @TODO check for wpcli
# Check for WordPress
wp core version >/dev/null 2>&1
if [ $? -eq 1 ]; then
echo "Not in a working WordPress install directory"
exit 1
fi
# set variables
WPDIR=$(pwd)
WORKINGDIR=$(cd ../ && pwd)
BEDROCKDIR=$WORKINGDIR/$1
BEDROCKENV=$BEDROCKDIR/.env
# Check for wp-config
if [ ! -f "$WPDIR/wp-config.php" ]; then
echo "wp-config.php not found in current directory."
exit 1
fi
# exit if bedrock directory exists
[ -d $BEDROCKDIR ] && echo "Directory exists." && exit 1
### 2. Clone bedrock
git clone --depth 1 git@github.com:roots/bedrock.git $BEDROCKDIR
### 3. Copy wp-content, excluding uploads
echo "copying wp-content directory to web/app"
# @TODO make uploads conditional on user choice
rsync -vaq $WPDIR/wp-content/ $BEDROCKDIR/web/app --exclude uploads
### 4. Composer require plugins available in repo/wpackagist
# get list of plugins
PLUGINS=$(wp plugin list --fields=name,status --path=$WPDIR | grep -ve "must-use" -e "dropin" | sed -n '1!p' | awk '{print cat $1}' | tr '\n' ' ')
PLUGINSARRAY=($PLUGINS) # convert to array. unnecessary?
# Require plugins from repo
echo "installing ${#PLUGINSARRAY[@]} plugins"
echo ""
echo "# non-repo plugins" >> $BEDROCKDIR/.gitignore #adds comment to end of .gitignore. @TODO don't add this if no non-repo plugins
failedArr=()
for PLUGIN in ${PLUGINSARRAY[@]}; do
# update composer.json without uddating composer.lock or installing files
cd $BEDROCKDIR && composer require --no-install wpackagist-plugin/$PLUGIN >/dev/null 2>&1
EXITCODE=$?
if [ $EXITCODE -eq 1 ]; then
failedArr+=($PLUGIN)
echo "!web/app/plugins/$PLUGIN" >> $BEDROCKDIR/.gitignore
echo $PLUGIN is not in repo - commit file manually.
elif [ $EXITCODE -eq 0 ]; then
rm -rf $BEDROCKDIR/web/app/plugins/$PLUGIN
echo required $PLUGIN
fi
done
echo plugins not available: ${failedArr[*]} # what to do with this?
### 5. Set up .env values
cp $BEDROCKDIR/.env.example $BEDROCKENV
if [ ! -f "$BEDROCKENV" ]; then
echo "Cannot find bedrock .env file. You can configure this yourself."
else
# @TODO refactor this into a loop
WP_HOME=$(wp config get WP_HOME --path=$WPDIR)
DB_HOST=$(wp config get DB_HOST --path=$WPDIR)
DB_NAME=$(wp config get DB_NAME --path=$WPDIR)
DB_USER=$(wp config get DB_USER --path=$WPDIR)
DB_PASSWORD=$(wp config get DB_PASSWORD --path=$WPDIR)
DB_PREFIX=$(wp db prefix --path=$WPDIR)
echo $DB_HOST
sed -i -e "s~http://example.com~$WP_HOME~" $BEDROCKENV # breaks due to /: bad flag in substitute command: '/'
sed -i -e "s/database_host/$DB_HOST/" $BEDROCKENV
sed -i -e "s/database_name/$DB_NAME/" $BEDROCKENV
sed -i -e "s/database_user/$DB_USER/" $BEDROCKENV
sed -i -e "s/database_password/$DB_PASSWORD/" $BEDROCKENV
#if [ ! $DB_PREFIX -eq 'wp_' ]; then
sed -i -e "s/wp_/$DB_PREFIX/" $BEDROCKENV
sed -i -e "s/# DB_PREFIX/DB_PREFIX/" $BEDROCKENV
#fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment