Created
March 2, 2014 21:35
-
-
Save romanodesouza/9314324 to your computer and use it in GitHub Desktop.
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 | |
# | |
# Pretty basic script for php psr0 refactoring | |
# USAGE: | |
# Don't touch the php file! Just use "git mv" and then run this script | |
FILES="" | |
function set_files_by_git_status() { | |
FILES=`git status -s | grep ^R | awk '{print $4}'` | |
} | |
function set_files() { | |
set_files_by_git_status | |
} | |
function main() { | |
set_files | |
local files=`egrep -o "^(((abstract|final) )?class (\w+)|interface (\w)+)" $FILES` | |
IFS=$'\n' | |
for file in $files; do | |
local file_path=`echo $file | cut -f1 -d:` | |
local pattern=`echo $file | cut -f2- -d:` | |
local old_namespace=`egrep -o "namespace (\w+\\\\\\\\[^;]+);" $file_path | cut -f2 -d' ' | cut -f1 -d';'` | |
local old_name=`echo $pattern | awk '{print $NF}'` | |
local new_name=`echo $file_path | awk -F/ '{print $NF}' | cut -f1 -d.` | |
local new_namespace=`echo $file_path | awk -F/ '{$NF=""; print $0}' | sed 's,src ,,' | sed 's, ,\\\\\\\\,g'` | |
new_namespace=`echo ${new_namespace%?}` | |
new_namespace=`echo ${new_namespace%?}` | |
local pattern_replace=`echo $pattern | awk '{$NF=""; print $0}'`"$new_name" | |
local namespace_replace="namespace $new_namespace;" | |
# Refactor the file | |
echo "Refactoring the file $file_path..." | |
sed -e "s,$pattern,$pattern_replace," -e "s,namespace .*;,$namespace_replace," -i $file_path | |
# Refactor the project | |
old_namespace=`echo $old_namespace | sed 's,\\\\,\\\\\\\\,g'` | |
echo "Refactoring the project..." | |
sed -r \ | |
-e "s,($new_namespace\\\\$new_name|$old_namespace\\\\$old_name),$new_namespace\\\\$new_name,g" \ | |
-e "s,(extends|implements|new) ($new_name|$old_name),\\1 $new_name,g" \ | |
-i `find . -type f -name "*.php" -not -wholename "$file_path"` | |
done | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment