Skip to content

Instantly share code, notes, and snippets.

@chetbox
Created December 7, 2017 21:43
Show Gist options
  • Save chetbox/5460daa3f5036d786ca0c58e84bc7cf1 to your computer and use it in GitHub Desktop.
Save chetbox/5460daa3f5036d786ca0c58e84bc7cf1 to your computer and use it in GitHub Desktop.
Script to automate the majority of the Java to Kotlin conversion fixes for the Beeline Android app
#!/bin/bash
set -e
rm -f $(find app/src -iname "Triple*.java")
JAVA_FILES=$(find app/src -iname "*.java")
EXISTING_KOTLIN_FILES=$(find app/src -iname "*.kt")
for FILE in $JAVA_FILES ; do
echo "$FILE"
sed -i '' -E -e 's/@Inject/@Inject @android.support.annotation.NonNull/g' "$FILE"
sed -i '' -E -e 's/@Provides/@Provides @android.support.annotation.NonNull/g' "$FILE"
sed -i '' -E -e 's/(@BindView\([^)]*\))/\1 @android.support.annotation.NonNull/g' "$FILE"
sed -i '' -E -e 's/(public static BeelineComponent)/@android.support.annotation.NonNull \1/g' "$FILE"
sed -i '' -E -e 's/import +android\.util\.Pair;//g' "$FILE"
sed -i '' -E -e 's/import +android\.support\.v4\.util\.Pair;//g' "$FILE"
sed -i '' -E -e 's/Pair\.create\(/new Pair<>\(/g' "$FILE"
sed -i '' -E -e 's/Pair::create/Pair::new/g' "$FILE"
sed -i '' -E -e 's/([^\.])Pair</\1Pair</g' "$FILE"
sed -i '' -E -e 's/import +co\.beeline\.util\.Triple;//g' "$FILE"
sed -i '' -E -e 's/Triple::create/kotlin.Triple::new/g' "$FILE"
sed -i '' -E -e 's/\.first([^\(]|$)/.getFirst()\1/g' "$FILE"
sed -i '' -E -e 's/\.second([^\(]|$)/.getSecond()\1/g' "$FILE"
sed -i '' -E -e 's/\.third([^\(]|$)/.getThird()\1/g' "$FILE"
done
for FILE in $EXISTING_KOTLIN_FILES ; do
echo "$FILE"
sed -i '' -E -e 's/import +android.util.Pair//g' "$FILE"
sed -i '' -E -e 's/android\.util\.Pair</Pair</g' "$FILE"
sed -i '' -E -e 's/Pair\.create/Pair/g' "$FILE"
done
echo ""
echo "Now covert all Java files to Kotlin in Android Studio"
echo "Press return when ready"
read
NEW_KOTLIN_FILES="$(echo "$JAVA_FILES" | sed -E 's/\.java/\.kt/g')"
for FILE in $NEW_KOTLIN_FILES ; do
echo "$FILE"
sed -i '' -E -e 's/@Inject/@Inject lateinit/g' "$FILE"
sed -i '' -E -e 's/(Observable.combineLatest)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/Func[1-9]<[^\({]*> *{/{/g' "$FILE"
sed -i '' -E -e 's/Action[0-9](<[^\({]*>)? *{/{/g' "$FILE"
sed -i '' -E -e 's/(Errors.logErrorAndComplete)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/(\.compose)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/(\.map)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/(\.flatMap)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/(\.switchMap)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/(\.subscribe)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/(\.subscribe)\(<[^\({]*>(.*)\)/\1\2/g' "$FILE"
sed -i '' -E -e 's/(\.bindToLifecycle)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/(\.bindUntilEvent)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/(\.bindUntilRecycled)<[^\({]*>/\1/g' "$FILE"
sed -i '' -E -e 's/(sendRequest)<[^>]*>\(/\1(/g' "$FILE"
sed -i '' -E -e 's/@(Observable.combineLatest)/ \1/g' "$FILE"
sed -i '' -E -e 's/ *__ -> */ /g' "$FILE"
sed -i '' -E -e 's/@(kotlin\.Pair)/Pair/g' "$FILE"
sed -i '' -E -e 's/@(kotlin\.Triple)/Triple/g' "$FILE"
sed -i '' -E -e 's/(@BindView\([^)]*\))( +internal) +/\1\2 lateinit /g' "$FILE"
done
echo ""
echo "Now you'll most likely have some errors to fix"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment