Skip to content

Instantly share code, notes, and snippets.

@ianmjones
Last active August 28, 2024 03:52
Show Gist options
  • Save ianmjones/0b853c415eafe9792ba04b6a9e83822f to your computer and use it in GitHub Desktop.
Save ianmjones/0b853c415eafe9792ba04b6a9e83822f to your computer and use it in GitHub Desktop.
A script for downloading the AWS PHP SDK v2, stripping down to S3 functionality and then applying a custom namespace.
#!/usr/bin/env bash
set -e
if [ ! -d src/amazon-s3-and-cloudfront ]; then
echo 'This script must be run from the repository root.'
exit 1
fi
for PROG in composer find sed
do
which ${PROG}
if [ 0 -ne $? ]
then
echo "${PROG} not found in path."
exit 1
fi
done
REPO_ROOT=${PWD}
TMP_ROOT="${REPO_ROOT}/aws2-build"
TARGET_DIR="${REPO_ROOT}/src/amazon-s3-and-cloudfront/vendor/Aws2"
if [ -d ${TMP_ROOT} ]; then
rm -rf ${TMP_ROOT}
fi;
mkdir ${TMP_ROOT}
cd ${TMP_ROOT}
function log_step() {
echo
echo
echo ${1}
echo
echo
}
log_step "Install the latest v2 of the AWS SDK"
mkdir sdk
(
cd sdk
composer require aws/aws-sdk-php:^2.8 --no-update --no-interaction
composer install --no-autoloader --prefer-dist --no-interaction --no-suggest
# Delete everything from the SDK except for S3 and Common files.
find vendor/aws/aws-sdk-php/src/Aws/ -type d -mindepth 1 -maxdepth 1 ! -name S3 ! -name Common -exec rm -rf {} +
# Remove tests & docs
find vendor -type d -iname tests -exec rm -rf {} +
find vendor -type d -iname docs -exec rm -rf {} +
)
log_step "Install php-scoper for code prefixing"
mkdir scoper
(
cd scoper
composer require humbug/php-scoper:^0.5 --update-no-dev --no-interaction
)
log_step "Run the prefixer, adding our namespace prefix" # Prefixed files are written into the ./sdk_prefixed directory.
scoper/vendor/bin/php-scoper add-prefix --prefix="DeliciousBrains\\WP_Offload_S3\\Aws2\\" --output-dir=sdk_prefixed sdk/vendor/
(
cd sdk_prefixed
rm -rf composer
# Set the locale to prevent sed errors from characters with different encoding.
export LC_ALL=C
# Perform regex search replace to clean up any missed replacements in string literals (1 literal backslash = 4 in the command)
OS_NAME=`uname -s`
if [ "Darwin" = "${OS_NAME}" ]
then
find . -name "*.php" -print0 | xargs -0 sed -i '' -E "s:'(Aws|Guzzle|Symfony)\\\\\\\\:'DeliciousBrains\\\\\\\\WP_Offload_S3\\\\\\\\Aws2\\\\\\\\\1\\\\\\\\:g"
else
find . -name "*.php" -print0 | xargs -0 sed -i'' -E "s:'(Aws|Guzzle|Symfony)\\\\\\\\:'DeliciousBrains\\\\\\\\WP_Offload_S3\\\\\\\\Aws2\\\\\\\\\1\\\\\\\\:g"
fi
# Generate a classmap-only Composer autoloader for the SDK files.
echo '{ "autoload": { "classmap": [""] } }' > composer.json
composer dump-autoload --classmap-authoritative --no-interaction
rm composer.json
if [ "Darwin" = "${OS_NAME}" ]
then
find . -name "*.php" -print0 | xargs -0 sed -i '' -E "s:(Composer\\\\Autoload):DeliciousBrains\\\\WP_Offload_S3\\\\Aws2\\\\\1:g"
else
find . -name "*.php" -print0 | xargs -0 sed -i'' -E "s:(Composer\\\\Autoload):DeliciousBrains\\\\WP_Offload_S3\\\\Aws2\\\\\1:g"
fi
)
# Delete the target directory if it exists.
if [ -d ${TARGET_DIR} ]; then
rm -rf ${TARGET_DIR}
fi
# Move the prefixed SDK files to the plugin's vendor directory where they are referenced.
mv sdk_prefixed ${TARGET_DIR}
# Clean up the temporary working directory.
rm -rf ${TMP_ROOT}
log_step "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment