Created
February 10, 2023 12:19
-
-
Save leffsu/c596197eb231f746303fa216a0a81486 to your computer and use it in GitHub Desktop.
Fixing AppSync generated Java classes
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
task fixGeneratedClassesUsingEnums { | |
doLast { | |
def list = [] | |
fileTree("build/generated/source/appsync/com/amazonaws/amplify/generated/graphql").matching { | |
include "*.java" | |
}.each { file -> | |
def lines = [] | |
def foundTierToReplace = false | |
new File(file.path).eachLine { line -> | |
if (line.contains('.valueOf') && !line.contains('Boolean') && !line.contains('Double')) { | |
foundTierToReplace = true | |
lines << line.replaceAll(".valueOf", '.safeValueOf') + System.getProperty("line.separator") | |
} else { | |
lines << line + System.getProperty("line.separator") | |
} | |
} | |
if (foundTierToReplace) { | |
new File(file.path).withWriter { w -> | |
lines.forEach { line -> | |
w << line | |
} | |
} | |
list << file | |
} | |
} | |
} | |
} | |
task fixEnums { | |
doLast { | |
def list = [] | |
fileTree("build/generated/source/appsync/com/amazonaws/amplify/type").matching { | |
include "*.java" | |
}.each { file -> | |
def lines = [] | |
def isEnum = false | |
def enumName = "" | |
def isScalar = false | |
def shouldReplace = true | |
new File(file.path).eachLine { | |
if(it.contains("UNKNOWN")) { | |
shouldReplace = false | |
} | |
} | |
new File(file.path).eachLine { line -> | |
if (!isScalar) { | |
isScalar = line.contains('ScalarType') | |
} | |
if (line.contains('public enum')) { | |
isEnum = true | |
def signature = line.split(' ') | |
enumName = signature[2] | |
lines << line + System.getProperty("line.separator") | |
} else { | |
lines << line + System.getProperty("line.separator") | |
} | |
} | |
if (isEnum && shouldReplace) { | |
lines.removeLast() | |
lines << ',' + System.getProperty("line.separator") + System.getProperty("line.separator") | |
if (isScalar) { | |
lines << 'UNKNOWN {\n' + | |
' @Override\n' + | |
' public String typeName() {\n' + | |
' return "UNKNOWN";\n' + | |
' }\n' + | |
'\n' + | |
' @Override\n' + | |
' public Class javaType() {\n' + | |
' return String.class;\n' + | |
' }\n' + | |
' };' + System.getProperty("line.separator") + System.getProperty("line.separator") | |
} else { | |
lines << 'UNKNOWN;' + System.getProperty("line.separator") + System.getProperty("line.separator") | |
} | |
lines << 'public static ' + enumName + ' safeValueOf(String text) {' + System.getProperty("line.separator") | |
lines << ' try {' + System.getProperty("line.separator") | |
lines << ' return valueOf(text);' + System.getProperty("line.separator") | |
lines << ' } catch (Exception e) {' + System.getProperty("line.separator") | |
lines << ' return ' + enumName + '.UNKNOWN;' + System.getProperty("line.separator") | |
lines << ' }' + System.getProperty("line.separator") | |
lines << ' }' | |
lines << '}' | |
new File(file.path).withWriter { w -> | |
lines.forEach { line -> | |
w << line | |
} | |
} | |
list << file | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment