Skip to content

Instantly share code, notes, and snippets.

@eporama
Last active May 18, 2021 14:52
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 eporama/45aaaf338ffbb83401e6b0318f37e165 to your computer and use it in GitHub Desktop.
Save eporama/45aaaf338ffbb83401e6b0318f37e165 to your computer and use it in GitHub Desktop.
A script to mimic my setting up process for a test site
#!/usr/bin/env bash
if [[ -z $1 ]]; then
echo "Usage: local.sh <projectName> <(optional) localPort>"
exit
fi
localPort=8888
if [[ ! -z $2 ]]; then
localPort=$2
fi
projectName=$1
# Use --no-install because we want to change "web/" to "docroot/" in composer.json
# before running composer install
composer create-project --no-install drupal/recommended-project $projectName
cd $projectName
# Replace all the "web/" instances to "docroot/" via an in-place sed
sed -i -e 's[web/[docroot/[' composer.json
composer install
# Start this off as a git repository to be able to see history
git init
git add -A
git commit -m "Initial: composer create-project drupal/recommended-project $projectName"
# To set up local settings, we'll need drush to create a hash_salt
composer require drush/drush
git add -A
git commit -m "composer require drush/drush"
# Copy the default settings.php file in place
cp docroot/sites/default/default.settings.php docroot/sites/default/settings.php
# And patch it to allow the settings.local.php file to be included.
cat <<EOF | git apply -
diff --git a/docroot/sites/default/settings.php b/docroot/sites/default/settings.php
index 0bb18b0d..ba37bc6c 100644
--- a/docroot/sites/default/settings.php
+++ b/docroot/sites/default/settings.php
@@ -775,7 +775,7 @@
*
* Keep this code block at the end of this file to take full effect.
*/
-#
-# if (file_exists(\$app_root . '/' . \$site_path . '/settings.local.php')) {
-# include \$app_root . '/' . \$site_path . '/settings.local.php';
-# }
+
+if (file_exists(\$app_root . '/' . \$site_path . '/settings.local.php')) {
+ include \$app_root . '/' . \$site_path . '/settings.local.php';
+}
EOF
# Commit these changes to the repo
git add docroot/sites/default/settings.php
git commit -m "Uncommenting settings.local.php logic"
# Make sure we don't commit this local file to the repository
echo "*.local.php" > .gitignore
git add .gitignore
git commit -m "Adding *.local.php to gitignore"
# Now go ahead and create the settings.local.php
# Start by generating a new hash for this site
# Drush is in PATH because I include `./vendor/bin/`
hashSalt=$(drush php-eval 'echo \Drupal\Component\Utility\Crypt::randomBytesBase64(55) . "\n";')
currrentUser=$(whoami)
cat <<EOF > docroot/sites/default/settings.local.php
<?php
\$settings['hash_salt'] = '$hashSalt';
\$settings['config_sync_directory'] = '../config/default';
\$databases = [
'default' =>
[
'default' =>
[
'database' => '$projectName',
'username' => '$currentUser',
'password' => '',
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'collation' => 'utf8mb4_general_ci',
'prefix' => '',
],
],
];
\$settings['trusted_host_patterns'] = [
'^localhost$',
'^127\.0\.0\.1$',
];
\$settings['skip_permissions_hardening'] = TRUE;
EOF
# Since we're skipping permission hardening, we set this permission manually
# simply removing w on the files, but not the directory will allow
# rm -rf to complete without issue
chmod u-w docroot/sites/default/settings.local.php docroot/sites/default/settings.php
# Before we install, we'll need a database created
mysqladmin create $projectName
# Now a full drush site:install turning off the emails to notify on update checks
drush site:install \
--yes \
--site-name="$projectName on localhost" \
--account-name=user1 \
--account-mail=${currrentUser}@acquia.com \
--site-mail=${currrentUser}@acquia.com \
standard \
install_configure_form.enable_update_status_emails=NULL
# Now that "config" and public files are created, deal with them
echo "docroot/sites/default/files" >> .gitignore
git add config .gitignore
git commit -m "Adding config and gitignore for excluding sites/default/files"
# Easier to create Drush alias for @local at this point
mkdir -p drush/sites/
cat <<EOF > drush/sites/self.site.yml
#
# A minimalist Drush site alias file.
# See https://www.drush.org/latest/site-aliases/ for lots more documentation.
#
# File: self.site.yml
local:
root: docroot
uri: 127.0.0.1:${localPort}
command:
runserver:
options:
default-server: '127.0.0.1:${localPort}/'
user:
login:
arguments: 'admin/reports/status'
EOF
# And to make sure it is in the repository
git add drush/sites/self.site.yml
git commit -m "Adding minimal drush/sites/self.site.yml"
# Take the existing site and make sure we have the initial config committed to the repository
drush @local config:export
git add config/
git commit -m "Adding base config files to repo"
# Using Applescript to tell iTerm to run a new tab with "drush runserver"
# Also officially cd into pwd in first tab so that we end up there out of process
pwd=$(pwd)
osascript<<EOF
tell application "iTerm"
activate
tell current session of current window
split horizontally with default profile
end tell
tell second session of current tab of current window
write text "cd $pwd"
write text "drush @local rs"
end tell
tell first session of current tab of current window
write text "cd $pwd"
end tell
end tell
EOF
# Log into the new site and see how the site status responds
drush @local uli admin/reports/status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment