Skip to content

Instantly share code, notes, and snippets.

@seanr
Created May 16, 2017 14:00
Show Gist options
  • Save seanr/6c8cfeca0a9f55790a91515ab064e449 to your computer and use it in GitHub Desktop.
Save seanr/6c8cfeca0a9f55790a91515ab064e449 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
set -e
# Load variables.
source "$(dirname $0)/_vars"
usage() {
echo "Build the application."
echo " "
echo "./scripts/docker/build [options]"
echo " "
echo "[options]"
echo " -c copy local files instead of linking"
echo " -g keep .git folder (used by multidev-update"
echo " -h display this help text"
echo " -o build output directory (default: /var/www/html)"
echo " -s code source directory (default: /code)"
}
# Figure out the real path to a directory.
realpath() {
TARGET_FILE=$1
cd `dirname ${TARGET_FILE}`
TARGET_FILE=`basename ${TARGET_FILE}`
while [ -L "${TARGET_FILE}" ]; do
TARGET_FILE=`readlink ${TARGET_FILE}`
cd `dirname ${TARGET_FILE}`
TARGET_FILE=`basename ${TARGET_FILE}`
done
echo `pwd -P`/${TARGET_FILE}
}
COMMAND_COPY="cp -rf"
COMMAND_LINK="ln -s"
COMMAND_GIT="! -name .git"
# Command to use with custom files.
COMMAND_CUSTOM_FILES=${COMMAND_LINK}
while getopts 'cgho:s:' FLAG; do
case "${FLAG}" in
c) COMMAND_CUSTOM_FILES=${COMMAND_COPY}; shift ;;
g) OPTION_KEEP_GIT=${COMMAND_GIT}; shift ;;
h) usage; exit 1 ;;
o) BUILD_DIRECTORY="${OPTARG}"; shift 2 ;;
s) REPOSITORY_DIRECTORY="${OPTARG}"; shift 2 ;;
# v) verbose=true; shift ;;
*) echo "Unexpected option"; exit 1 ;;
esac
done
# Output colors.
HILIGHT="\033[34m"
SUCCESS="\033[32m"
ERROR="\033[31m"
RESET="\033[0m"
# Drush make files.
DRUSH_MAKE_INSTALL_DIRECTORY="_drushmake"
DRUSH_MAKEFILE_CORE="drupal-org-core.make"
DRUSH_MAKEFILE_CONTRIB="drupal-org-contrib.make"
if [ ! -f "${REPOSITORY_DIRECTORY}/${DRUSH_MAKEFILE_CORE}" ] || [ ! -f "${REPOSITORY_DIRECTORY}/${DRUSH_MAKEFILE_CONTRIB}" ]; then
echo -e "${COLOR_ERROR}Missing required drush make files.${COLOR_RESET}"
exit 1
fi
# Remove old build files.
echo -e "${COLOR_HILIGHT}Removing old build files...${COLOR_RESET}"
find "${BUILD_DIRECTORY}" ! -name ".gitkeep" ${OPTION_KEEP_GIT} -mindepth 1 -maxdepth 1 -exec rm -rf {} +
echo -e "Previous build files removed from ${BUILD_DIRECTORY}"
# Ensure drush works in the container.
echo -e "${COLOR_HILIGHT}Drush status:${COLOR_RESET}"
drush status
# Create temporary build directory.
# The application isn't built here initially because drush-make deletes the destination
# directory during the build process, which would break the docker volume mount.
mkdir -p "${BUILD_DIRECTORY}/${DRUSH_MAKE_INSTALL_DIRECTORY}"
# Build Drupal contributed modules.
echo -e "${COLOR_HILIGHT}Building Drupal contributed modules...${COLOR_RESET}"
drush make --no-core --contrib-destination="." "${REPOSITORY_DIRECTORY}/${DRUSH_MAKEFILE_CONTRIB}" "${BUILD_DIRECTORY}/${DRUSH_MAKE_INSTALL_DIRECTORY}/contrib"
echo -e "Built Drupal contributed modules."
# Build Drupal core distribution.
echo -e "${COLOR_HILIGHT}Building Drupal core distribution...${COLOR_RESET}"
drush make "${REPOSITORY_DIRECTORY}/${DRUSH_MAKEFILE_CORE}" "${BUILD_DIRECTORY}/${DRUSH_MAKE_INSTALL_DIRECTORY}/core"
echo -e "Built Drupal core distribution."
# Move application to the final destination.
echo -e "${COLOR_HILIGHT}Compiling application...${COLOR_RESET}"
# Move Drupal core files to final destination.
${COMMAND_COPY} "${BUILD_DIRECTORY}/${DRUSH_MAKE_INSTALL_DIRECTORY}/core/." "${BUILD_DIRECTORY}"
echo -e "Copied Drupal core files."
# Move Drupal contrib modules to final destination.
${COMMAND_COPY} "${BUILD_DIRECTORY}/${DRUSH_MAKE_INSTALL_DIRECTORY}/contrib" "${BUILD_DIRECTORY}/profiles/attn"
echo -e "Copied Drupal contrib modules."
# Remove temporary build directory.
rm -rf "${BUILD_DIRECTORY}/${DRUSH_MAKE_INSTALL_DIRECTORY}"
# Move custom modules to final destination.
echo -e "${COLOR_HILIGHT}Linking custom code...${COLOR_RESET}"
# Change the logging text depending on command behavior.
if [ -z "${COMMAND_CUSTOM_FILES##*cp*}" ]; then
COMMAND_CUSTOM_FILES_TEXT="Copied"
else
COMMAND_CUSTOM_FILES_TEXT="Linked"
fi
# Link custom profile.
${COMMAND_CUSTOM_FILES} "${REPOSITORY_DIRECTORY}/attn.info" "${BUILD_DIRECTORY}/profiles/attn/attn.info"
${COMMAND_CUSTOM_FILES} "${REPOSITORY_DIRECTORY}/attn.install" "${BUILD_DIRECTORY}/profiles/attn/attn.install"
${COMMAND_CUSTOM_FILES} "${REPOSITORY_DIRECTORY}/attn.profile" "${BUILD_DIRECTORY}/profiles/attn/attn.profile"
${COMMAND_CUSTOM_FILES} "${REPOSITORY_DIRECTORY}/attn.test" "${BUILD_DIRECTORY}/profiles/attn/attn.test"
echo -e "${COMMAND_CUSTOM_FILES_TEXT} custom profile."
# Link custom modules.
${COMMAND_CUSTOM_FILES} "${REPOSITORY_DIRECTORY}/lib/modules/attn" "${BUILD_DIRECTORY}/profiles/attn/modules/attn"
echo -e "${COMMAND_CUSTOM_FILES_TEXT} custom modules."
# Link custom ckeditor plugins.
${COMMAND_CUSTOM_FILES} "${REPOSITORY_DIRECTORY}/lib/libraries/ckeditor-plugins/attn_image" "${BUILD_DIRECTORY}/profiles/attn/libraries/ckeditor-plugins/attn_image"
${COMMAND_CUSTOM_FILES} "${REPOSITORY_DIRECTORY}/lib/libraries/ckeditor-plugins/htmlwriter_nosort" "${BUILD_DIRECTORY}/profiles/attn/libraries/ckeditor-plugins/htmlwriter_nosort"
${COMMAND_CUSTOM_FILES} "${REPOSITORY_DIRECTORY}/lib/libraries/ckeditor-plugins/survey" "${BUILD_DIRECTORY}/profiles/attn/libraries/ckeditor-plugins/survey"
echo -e "${COMMAND_CUSTOM_FILES_TEXT} custom ckeditor plugins."
# Link custom settings files.
${COMMAND_COPY} "${BUILD_DIRECTORY}/sites/default/default.settings.php" "${BUILD_DIRECTORY}/sites/default/settings.php"
echo -e "Copied default settings file."
if [ -f "${REPOSITORY_DIRECTORY}/config/settings.local.php" ]; then
# Link local settings file.
${COMMAND_CUSTOM_FILES} "${REPOSITORY_DIRECTORY}/config/settings.local.php" "${BUILD_DIRECTORY}/sites/default/settings.local.php"
# Include the local settings file in PHP.
# Use "sh -c" here because that's what docker requires.
sh -c 'echo "if (file_exists(dirname(__FILE__) . \"/settings.local.php\")) { include_once \"settings.local.php\"; }" >> '"${BUILD_DIRECTORY}/sites/default/settings.php"
echo -e "${COMMAND_CUSTOM_FILES_TEXT} custom settings file."
fi
# Add pantheon config to build.
if [ -f "${REPOSITORY_DIRECTORY}/config/pantheon.yml" ]; then
${COMMAND_COPY} "${REPOSITORY_DIRECTORY}/config/pantheon.yml" "${BUILD_DIRECTORY}/pantheon.yml"
echo -e "Added Pantheon config."
fi
echo -e "${COLOR_SUCCESS}Application build complete.${COLOR_RESET}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment