Skip to content

Instantly share code, notes, and snippets.

@davidalger
Last active June 8, 2017 18:36
Show Gist options
  • Save davidalger/fc88849cb1451589856a to your computer and use it in GitHub Desktop.
Save davidalger/fc88849cb1451589856a to your computer and use it in GitHub Desktop.
Converts long-form echo tags to short-echo tags
#!/usr/bin/env bash
#
# note: using *html vs *.phtml because one .html file has a use of <?php echo in it
#
function replace_tags {
find "$1" -type f -name "$2" -print0 | xargs -0 perl -pi -w -e "$3"
}
echo "==> Convert single-line long-tags to uniform short-echo tag format"
replace_tags . '*html' 's#<\?php\s*(/\*\s*\@[a-zA-Z]+\s*\*/)\s*echo\s*(.*?);?\s*\?>#<?= $1 $2 ?>#g'
echo "==> Replace multi-line long-tags with short-echo opening "
replace_tags . '*html' 's#<\?php\s*echo\s*#<?= #g'
echo "==> Unify padding on pre-existing short-echo tags"
replace_tags . '*html' 's#<\?=\s*([^\?]*?);?\s*\?>#<?= $1 ?>#g'
echo "==> Convert long-tags to short-echo tags in non-template files"
replace_tags app/code/ '*.php' 's#<\?php\s*echo\s*#<?= #g'
echo "==> Revert changes made to unit tests before committing"
git status -s | grep 'Test/Unit' | awk '{print $2}' | xargs git checkout
echo "==> Verify syntax of all modified files"
git status -s | grep -E "(.php|.phtml|.html)$" | awk '{print $2}' | xargs -n1 php -l | grep -v "No syntax errors";
echo "==> Commit changes for review"
git commit -a;
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment