Skip to content

Instantly share code, notes, and snippets.

@raylillywhite
Created February 19, 2013 09:20
Show Gist options
  • Save raylillywhite/4984291 to your computer and use it in GitHub Desktop.
Save raylillywhite/4984291 to your computer and use it in GitHub Desktop.
#!/bin/sh
ORIGINALDIR="$( pwd )"
SCRIPTDIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BASEDIR="$( cd -P $SCRIPTDIR/.. && pwd )"
COLORON='printf "`tput setaf 7`"'
COLORON2='printf "`tput setaf 5`"'
COLORONRED='printf "`tput setaf 1`"'
COLOROFF='printf "`tput sgr0`"'
pathsToPullStringsFrom=("$BASEDIR/HotelTonight")
excludedPathsRegex="HTLocalizationUtils\.h"
oldLineCount=`cat $BASEDIR/HotelTonight/en.lproj/Localizable.strings | wc -l`
#htstrRegex='s/HTStr\(@"([^"]*)"[^"]*"([^"]*)"[^"]*"([^"]*)"\)/NSLocalizedStringWithDefaultValue\(@"\1", nil, \[NSBundle mainBundle\], @"\2", @"\3"\)/g'
htstrRegexFirstPart='/HTStr.*$/{'
htstrRegexMainSubstitute='s/HTStr\(@"(([^"]|\\")*)"[^"]*"(([^"]|\\")*)"[^"]*"(([^"]|\\")*)"\)/NSLocalizedStringWithDefaultValue\(@"\1", nil, \[NSBundle mainBundle\], @"\3", @"\5"\)/g'
htstrRegexLastPart='}'
applyRegexToPath () #single argument - path
{
eval "find $1 $findFilesOptions | xargs gsed -r -i '$htstrRegexMainSubstitute'"
eval "find $1 $findFilesOptions | xargs gsed -r -i '$htstrRegexFirstPart N; $htstrRegexMainSubstitute $htstrRegexLastPart'"
eval "find $1 $findFilesOptions | xargs gsed -r -i '$htstrRegexFirstPart N; N; $htstrRegexMainSubstitute $htstrRegexLastPart'"
eval "find $1 $findFilesOptions | xargs gsed -r -i '$htstrRegexFirstPart N; N; N; $htstrRegexMainSubstitute $htstrRegexLastPart'"
eval "find $1 $findFilesOptions | xargs gsed -r -i '$htstrRegexFirstPart N; N; N; N; $htstrRegexMainSubstitute $htstrRegexLastPart'"
eval "find $1 $findFilesOptions | xargs gsed -r -i '$htstrRegexFirstPart N; N; N; N; N; $htstrRegexMainSubstitute $htstrRegexLastPart'"
}
generateStringsForPathAndGenstringsOptions ()
{
pathToPullStringsFrom=$1
genstringsOptions=$2
eval "$COLORON2"
echo "\n\nWorking in directory $1"
eval "$COLOROFF"
eval "$COLORON"
echo "\nReplacing HTStr with NSLocalizedStringWithDefaultValue..."
eval "$COLOROFF"
applyRegexToPath "$pathToPullStringsFrom"
eval "$COLORON"
echo "\nChecking for any remaining HTStr instances (if you see any below, the regex might not be working quite right)\n"
printf "This many lines did not get converted correctly: "
eval "$COLOROFF"
eval "$COLORONRED"
eval "find $pathToPullStringsFrom $findFilesOptions | xargs grep --color -n 'HTStr(' | wc -l"
eval "find $pathToPullStringsFrom $findFilesOptions | xargs grep --color -n 'HTStr('"
eval "$COLOROFF"
eval "$COLORON"
echo "\nGenerating strings file..."
eval "$COLOROFF"
eval "find $pathToPullStringsFrom $findFilesOptions | xargs genstrings $genstringsOptions -q -o $BASEDIR/HotelTonight/temp_en.lproj"
eval "$COLORON"
echo "\nChecking out (reverting) modified .m and .h files..."
eval "find $pathToPullStringsFrom $findFilesOptions | xargs git checkout"
eval "$COLOROFF"
}
for pathToPullStringsFrom in "${pathsToPullStringsFrom[@]}"
do
if [ ! -d $pathToPullStringsFrom ]; then
echo "\nAre you in the base directory? Could not find pathsToPullStringsFrom:\n$pathToPullStringsFrom\n"
exit
fi
done
findFilesOptions=" -regex \".*\.[mh]\" | grep -E -v \"$excludedPathsRegex\""
echo "\n\n"
git status
echo "\nThis will modify most .m and .h files and then revert everything back to unmodified with git. If you have any uncommitted changes (see above), you may want to reconsider running this script. Continue?"
select yn in "Yes" "No"; do
case $yn in
Yes ) break;;
No ) exit;;
esac
done
# set -o verbose #echo on
mkdir $BASEDIR/HotelTonight/temp_en.lproj
generateStringsForPathAndGenstringsOptions "${pathsToPullStringsFrom[0]}" ""
for pathToPullStringsFrom in "${pathsToPullStringsFrom[@]:1}"
do
generateStringsForPathAndGenstringsOptions "$pathToPullStringsFrom" "-a"
done
mv $BASEDIR/HotelTonight/temp_en.lproj/Localizable.strings $BASEDIR/HotelTonight/en.lproj
rm -rf $BASEDIR/HotelTonight/temp_en.lproj
# set +o verbose #echo off
newLineCount=`cat HotelTonight/en.lproj/Localizable.strings | wc -l`
echo "Old length: \t$oldLineCount\nNew length: \t$newLineCount"
eval "cd $ORIGINALDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment