Skip to content

Instantly share code, notes, and snippets.

@dulao5
Last active February 23, 2018 05:49
Show Gist options
  • Save dulao5/2c1d41a15c0bd71904bab68fe8a878af to your computer and use it in GitHub Desktop.
Save dulao5/2c1d41a15c0bd71904bab68fe8a878af to your computer and use it in GitHub Desktop.
convert_underscore_case_to_camelCase_variables.sh
#!/bin/sh
#===============================================================================
#
# USAGE: convert_underscore_case_to_camelCase_variables.sh <file名>
#
# DESCRIPTION: PHPファイルの underscore_case 変数名を camelCase に自動に変換する
#
#===============================================================================
set -o nounset
target_file=$1
if [ ! -f "$target_file" ]; then
echo "USAGE: $0 <file名>"
exit 1
fi
underscore_var_list=$(egrep '\$[_a-z0-9A-Z]+' -o $target_file |sort -u -r | grep _ | egrep -v '\$_[_A-Z]+$')
underscore_instance_var_list=$(grep -Po '\->[_a-z0-9A-Z]+(\s*\()?' $target_file | grep -v '(' | sort -u -r | grep _)
camelcase_var_name=''
for underscore_var_name in $underscore_var_list $underscore_instance_var_list
do
camelcase_var_name=$(echo $underscore_var_name | sed -e 's/\([a-z0-9]\)_\([a-z]\)/\1\U\2/g')
if [ 1 == $(echo -en "$camelcase_var_name\n$underscore_var_name\n" | sort -u | wc -l) ]; then
echo -e "skip convert \t $underscore_var_name \t to \t $camelcase_var_name"
continue
fi
message="now convert \t $underscore_var_name \t to \t $camelcase_var_name"
sed -e 's/'$underscore_var_name'/'$camelcase_var_name'/g' -i $target_file
if [ $(which git) ]; then
commit_message="auto-convert : '$underscore_var_name' → '$camelcase_var_name'"
git add $target_file > /dev/null 2>&1 \
&& git commit -m "$commit_message" $target_file > /dev/null 2>&1 \
&& message="$message\t"$(git rev-parse --short HEAD)
fi
echo -e "$message"
done
@dulao5
Copy link
Author

dulao5 commented Feb 23, 2018

tools/convert_underscore_case_to_camelCase_variables.sh lib/AndroidDrm/DrmCheck.php |tee -a /tmp/convert.log

# 変更表を生成
awk -F"\t" 'BEGIN { print "|old|new|commit hash|"; print "|:----|:-----|:----|"}  /^now  convert/{print "|"$2"|"$4"|"$5"|"}' /tmp/convert.log

git rebase 后 hash 被重新整理

git hist |head -200 |grep auto-convert | awk '{print "| "$7" \t| " $9" \t| "$2" \t|"}'|sed -e "s/'//g" |less

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment