Created
April 2, 2021 08:12
-
-
Save fjahn/1f1cbbf570318a4967234ca9edaff9db to your computer and use it in GitHub Desktop.
Statamic Move Assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
ASSET_PATH='./public/assets' | |
UPDATE_PATHS=('./content' './resources') | |
# Make sure we are in project root | |
if ! grep --quiet 'statamic/cms' composer.json 2> /dev/null | |
then | |
>&2 echo '$0: Current directory needs to be in project root of statamic project' | |
exit 1 | |
fi | |
if [[ ! -d ${ASSET_PATH} ]] | |
then | |
>&2 echo "$0: Asset path not found" | |
exit 1 | |
fi | |
for UPDATE_PATH in $UPDATE_PATHS | |
do | |
if [[ ! -d ${UPDATE_PATH} ]] | |
then | |
>&2 echo "$0: Update path '$UPDATE_PATH' not found" | |
exit 1 | |
fi | |
done | |
if [[ $# -ne 1 ]] | |
then | |
>&2 echo "Usage: $0 (before|after)" | |
exit 1 | |
fi | |
STAGE=$1 | |
if [[ $STAGE == 'before' ]] | |
then | |
echo "$0: Committing before state..." | |
git -C ${ASSET_PATH} init > /dev/null | |
git -C ${ASSET_PATH} add -A > /dev/null | |
git -C ${ASSET_PATH} commit \ | |
--message "Snapshot of before state" \ | |
--author="Move Assets Script <[email protected]>" \ | |
|| true # If there is nothing to commit, ignore error | |
echo "$0: Done! Move your assets and call '$0 after' afterwards to update the paths" | |
exit 0 | |
fi | |
if [[ $STAGE == 'after' ]] | |
then | |
# Stage all changes (so file movement is tracked) | |
git -C ${ASSET_PATH} add -A > /dev/null | |
# Check if there are any changes that are not renames | |
if git -C ${ASSET_PATH} status -s | grep -v '.meta' | grep -qv '^R' | |
then | |
>&2 echo "$0: Error: Git tracked changes that are not renames! Remove the non-rename changes and try again." | |
exit 1 | |
fi | |
CHANGES=$(git -C ${ASSET_PATH} status -s | grep -v '.meta') || { | |
echo "$0: No changes in assets" | |
exit 0 | |
} | |
echo "$0: Detected $(echo "$CHANGES" | wc -l) changes" | |
IFS_BEFORE=$IFS | |
IFS=$'\n' | |
REGEX="^R (.+) -> (.+)$" | |
for change in $CHANGES | |
do | |
if [[ $change =~ $REGEX ]] | |
then | |
before=${BASH_REMATCH[1]} | |
escaped_before=$(printf '%s\n' "$before" | sed -e 's/[]\/$*.^[]/\\&/g') | |
after=${BASH_REMATCH[2]} | |
escaped_after=$(printf '%s\n' "$after" | sed -e 's/[\/&]/\\&/g') | |
for UPDATE_PATH in $UPDATE_PATHS | |
do | |
find "${UPDATE_PATH}" -type f -not -path '*/\.*' -print0 | xargs -0 sed -i '' "s/$escaped_before/$escaped_after/g" | |
done | |
else | |
>&2 echo "$0: Error: Could not parse change: '$change'" | |
exit 1 | |
fi | |
done | |
IFS=$IFS_BEFORE | |
echo "All done! You can remove the temporary git repo in the assets folder using 'rm -rf ${ASSET_PATH}/.git'" | |
exit 0 | |
fi | |
>&2 echo "$0: Unknown stage: '$STAGE'. Use either 'before' or 'after'." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment