Skip to content

Instantly share code, notes, and snippets.

@OJezu
Last active September 26, 2015 18:56
Show Gist options
  • Save OJezu/ac68284ff5191e9df365 to your computer and use it in GitHub Desktop.
Save OJezu/ac68284ff5191e9df365 to your computer and use it in GitHub Desktop.
Clean up some php code in repository
#convert leading 4-spaces to tabs
for i in $(seq 15); do
find . -! -path "*/.git/*" -! -path "*/bower_components/*" -type f -! -path "./img/*" -! -path "./_log/*" -! -path "./download/*" -! -path "./fonts/*" -exec sed -i "s/^\(\( \)*\)\( \)/\1\t/" \{\} \;
done
#remove trailing whitespace
find . -! -path "*/.git/*" -! -path "*/bower_components/*" -type f -! -path "./img/*" -! -path "./_log/*" -! -path "./download/*" -! -path "./fonts/*" -exec \
sed -i "s/\(\t\| \)\+\$//" \{\} \;
#remove multiple empty lines
for i in (find . -! -path "*/.git/*" -! -path "*/bower_components/*" -type f -! -path "./img/*" -! -path "./_log/*" -! -path "./download/*" -! -path "./fonts/*" -! -name "*.removed-lines")
do
cat -s "$i" > "$i.removed-lines"
mv -f "$i.removed-lines" "$i"
done
#remove short tags
find . -! -path "*/.git/*" -! -path "*/bower_components/*" -type f -! -path "./img/*" -! -path "./_log/*" -! -path "./download/*" -! -path "./fonts/*" -name "*.php" -exec sed -i 's/<?\([^=px]\|$\)/<?php \1/g' \{\} \;
find . -! -path "*/.git/*" -! -path "*/bower_components/*" -type f -! -path "./img/*" -! -path "./_log/*" -! -path "./download/*" -! -path "./fonts/*" -name "*.php" -exec sed -i 's/<?php \( \|$\)/<?php\1/g' \{\} \;
#fix execution rights of deploy scripts
find deploy -name "*.sh" -exec chmod +x \{\} \;
#remove closing php tag on end of file
find . -! -path "*/.git/*" -! -path "*/bower_components/*" -type f -! -path "./img/*" -! -path "./_log/*" -! -path "./download/*" -! -path "./fonts/*" -name "*.php" -exec perl -0777 -i -pe 's/[\s]*\?>([\s]*)\Z//' \{\} \;
#Make sure there is an empty line at end of file
find . -! -path "*/.git/*" -! -path "*/bower_components/*" -type f -! -path "./img/*" -! -path "./_log/*" -! -path "./download/*" -! -path "./fonts/*" -name "*.php" -exec sed -i -e '$a\' \{\} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment