Skip to content

Commit

Permalink
Merge pull request #12246 from grails/globalGrailClassTraitInjectTran…
Browse files Browse the repository at this point in the history
…sform

Fixes problems writing props to grails.factories files
  • Loading branch information
puneetbehl committed Dec 11, 2021
2 parents 3ab1618 + 6f7e966 commit 3b0cdbd
Showing 1 changed file with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,40 +168,18 @@ class GlobalGrailsClassInjectorTransformation implements ASTTransformation, Comp
def props = new Properties()
def superTypeName = superType.getName()

File sourceDirectory = findSourceDirectory(compilationTargetDirectory)
// generate META-INF/grails.factories
def factoriesFile = new File(compilationTargetDirectory, "META-INF/grails.factories")
factoriesFile.parentFile.mkdirs()
if (factoriesFile.exists()) {
// update
factoriesFile.withInputStream { InputStream input ->
props.load(input)
}

def existing = props.getProperty(superTypeName)
if(!existing) {
props.put(superTypeName, classNodeName)
}
else if (!existing.contains(classNodeName)) {
props.put(superTypeName, [existing, classNodeName].join(','))
}
File factoriesFile = new File(compilationTargetDirectory, "META-INF/grails.factories")
if (!factoriesFile.parentFile.exists()) {
factoriesFile.parentFile.mkdirs()
}
def sourceFactoriesFile = new File(sourceDirectory, "src/main/resources/META-INF/grails.factories")
if (sourceFactoriesFile.exists()) {
// update
sourceFactoriesFile.withInputStream { InputStream input ->
props.load(input)
}
loadFromFile(props, factoriesFile)

def existing = props.getProperty(superTypeName)
if (!existing) {
props.put(superTypeName, classNodeName)
} else if (!existing.contains(classNodeName)) {
props.put(superTypeName, [existing, classNodeName].join(','))
}
} else {
props.put(superTypeName, classNodeName)
}
File sourceDirectory = findSourceDirectory(compilationTargetDirectory)
File sourceFactoriesFile = new File(sourceDirectory, "src/main/resources/META-INF/grails.factories")
loadFromFile(props, sourceFactoriesFile)

addToProps(props, superTypeName, classNodeName)

factoriesFile.withWriter { Writer writer ->
props.store(writer, "Grails Factories File")
Expand All @@ -211,6 +189,32 @@ class GlobalGrailsClassInjectorTransformation implements ASTTransformation, Comp
return false
}

private static void loadFromFile(Properties props, File factoriesFile) {
if (factoriesFile.exists()) {
Properties fileProps = new Properties()
factoriesFile.withInputStream { InputStream input ->
fileProps.load(input)
fileProps.each { Map.Entry prop->
addToProps(props, (String) prop.key, (String) prop.value)
}
}
}4
// update
}

private static Properties addToProps(Properties props, String superTypeName, String classNodeNames) {
final List<String> classNodesNameList = classNodeNames.tokenize(',')
classNodesNameList.forEach(classNodeName -> {
String existing = props.getProperty(superTypeName)
if (!existing) {
props.put(superTypeName, classNodeName)
} else if (existing && !existing.contains(classNodeName)) {
props.put(superTypeName, [existing, classNodeName].join(','))
}
})
props
}

private static File findSourceDirectory(File compilationTargetDirectory) {
File sourceDirectory = compilationTargetDirectory
while (sourceDirectory != null && !(sourceDirectory.name in ["build", "target"])) {
Expand Down

0 comments on commit 3b0cdbd

Please sign in to comment.