Code:
def tmpFile = new File("/Users/dbell/Desktop/example.txt")
def cnt = 0
def enumName = "Sports"
def enumStringBuilder = new StringBuilder()
enumStringBuilder.append("public enum ${enumName} {\n\n")
tmpFile.eachLine {
def desc = it
def code = desc.toUpperCase().replaceAll("\\s", "_")
if (cnt > 0) { enumStringBuilder.append(",\n") }
enumStringBuilder.append("\t${code}(\"${code}\",\"${desc}\")")
cnt++
}
def enumOutput = enumStringBuilder.toString() + ";\n";
println enumOutput
println "\tString code;"
println "\tString description;\n"
println "\t${enumName}(String code, String description) {"
println "\t\tthis.code = code;"
println "\t\tthis.description = description;"
println "\t}"
println "}"
example.txt
Soccer
Football
Basketball
Hockey
Hacky Sack
Corn Hole
Generated Output
public enum Sports {
SOCCER("SOCCER","Soccer"),
FOOTBALL("FOOTBALL","Football"),
BASKETBALL("BASKETBALL","Basketball"),
HOCKEY("HOCKEY","Hockey"),
HACKY_SACK("HACKY_SACK","Hacky Sack"),
CORN_HOLE("CORN_HOLE","Corn Hole");
String code;
String description;
Sports(String code, String description) {
this.code = code;
this.description = description;
}
}
Screenshot: