frevib commented on code in PR #3614:
URL: https://github.com/apache/avro/pull/3614#discussion_r2969995405


##########
lang/java/gradle-plugin/src/main/kotlin/eu/eventloopsoftware/avro/gradle/plugin/tasks/AbstractCompileTask.kt:
##########
@@ -0,0 +1,166 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License.  You may obtain a copy of the License at
+*
+*     https://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package eu.eventloopsoftware.avro.gradle.plugin.tasks
+
+import java.io.File
+import java.io.IOException
+import java.net.URL
+import java.net.URLClassLoader
+import org.apache.avro.LogicalTypes
+import org.apache.avro.Protocol
+import org.apache.avro.compiler.specific.SpecificCompiler
+import org.apache.avro.compiler.specific.SpecificCompiler.FieldVisibility
+import org.apache.avro.generic.GenericData
+import org.gradle.api.DefaultTask
+import org.gradle.api.GradleException
+import org.gradle.api.file.ConfigurableFileCollection
+import org.gradle.api.file.DirectoryProperty
+import org.gradle.api.provider.ListProperty
+import org.gradle.api.provider.Property
+import org.gradle.api.tasks.Classpath
+import org.gradle.api.tasks.Input
+import org.gradle.api.tasks.InputFiles
+import org.gradle.api.tasks.OutputDirectory
+
+abstract class AbstractCompileTask : DefaultTask() {
+
+  @get:OutputDirectory abstract val outputDirectory: DirectoryProperty
+
+  @get:Input abstract val fieldVisibility: Property<String>
+
+  @get:Input abstract val testExcludes: ListProperty<String>
+
+  @get:Input abstract val stringType: Property<String>
+
+  @get:Input abstract val velocityToolsClassesNames: ListProperty<String>
+
+  @get:Input abstract val templateDirectory: Property<String>
+
+  @get:Input abstract val recordSpecificClass: Property<String>
+
+  @get:Input abstract val errorSpecificClass: Property<String>
+
+  @get:Input abstract val createOptionalGetters: Property<Boolean>
+
+  @get:Input abstract val gettersReturnOptional: Property<Boolean>
+
+  @get:Input abstract val optionalGettersForNullableFieldsOnly: 
Property<Boolean>
+
+  @get:Input abstract val createSetters: Property<Boolean>
+
+  @get:Input abstract val createNullSafeAnnotations: Property<Boolean>
+
+  @get:Input abstract val nullSafeAnnotationNullable: Property<String>
+
+  @get:Input abstract val nullSafeAnnotationNotNull: Property<String>
+
+  @get:Input abstract val customConversions: ListProperty<String>
+
+  @get:Input abstract val customLogicalTypeFactories: ListProperty<String>
+
+  @get:Input abstract val enableDecimalLogicalType: Property<Boolean>
+
+  @get:InputFiles @get:Classpath abstract val runtimeClassPathFileCollection: 
ConfigurableFileCollection
+
+  protected fun doCompile(
+      sourceFileForModificationDetection: File?,
+      protocol: Protocol,
+      outputDirectory: File?,
+  ) {
+    doCompile(sourceFileForModificationDetection, SpecificCompiler(protocol), 
outputDirectory!!)
+  }
+
+  protected fun doCompile(
+      sourceFileForModificationDetection: File?,
+      compiler: SpecificCompiler,
+      outputDirectory: File,
+  ) {
+    setCompilerProperties(compiler)
+    try {
+      for (customConversion in customConversions.get()) {
+        
compiler.addCustomConversion(Thread.currentThread().getContextClassLoader().loadClass(customConversion))
+      }
+    } catch (e: ClassNotFoundException) {
+      throw IOException(e)
+    }
+    compiler.compileToDestination(sourceFileForModificationDetection, 
outputDirectory)
+  }
+
+  private fun setCompilerProperties(compiler: SpecificCompiler) {
+    compiler.setTemplateDir(templateDirectory.get())
+    compiler.setStringType(GenericData.StringType.valueOf(stringType.get()))
+    compiler.setFieldVisibility(getFieldV())
+    compiler.setCreateOptionalGetters(createOptionalGetters.get())
+    compiler.setGettersReturnOptional(gettersReturnOptional.get())
+    
compiler.setOptionalGettersForNullableFieldsOnly(optionalGettersForNullableFieldsOnly.get())
+    compiler.setCreateSetters(createSetters.get())
+    compiler.setCreateNullSafeAnnotations(createNullSafeAnnotations.get())
+    compiler.setNullSafeAnnotationNullable(nullSafeAnnotationNullable.get())
+    compiler.setNullSafeAnnotationNotNull(nullSafeAnnotationNotNull.get())
+    compiler.setEnableDecimalLogicalType(enableDecimalLogicalType.get())
+    // TODO: likely not needed
+    //
+    // 
compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding"))
+    
compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools(velocityToolsClassesNames.get()))
+    compiler.setRecordSpecificClass(recordSpecificClass.get())
+    compiler.setErrorSpecificClass(errorSpecificClass.get())
+  }
+
+  private fun getFieldV(): FieldVisibility {
+    try {
+      val upperCaseFieldVisibility = fieldVisibility.get().trim().uppercase()
+      return FieldVisibility.valueOf(upperCaseFieldVisibility)
+    } catch (_: IllegalArgumentException) {
+      logger.warn("Could not parse field visibility: ${fieldVisibility.get()}, 
using PRIVATE")
+      return FieldVisibility.PRIVATE
+    }
+  }
+
+  private fun instantiateAdditionalVelocityTools(velocityToolsClassesNames: 
List<String>): List<Any> {
+    return velocityToolsClassesNames.map { velocityToolClassName ->
+      try {
+        
Class.forName(velocityToolClassName).getDeclaredConstructor().newInstance()
+      } catch (e: Exception) {
+        throw RuntimeException(e)

Review Comment:
   Much better 



##########
lang/java/gradle-plugin/src/main/kotlin/eu/eventloopsoftware/avro/gradle/plugin/tasks/CompileAvroSchemaTask.kt:
##########
@@ -0,0 +1,81 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License.  You may obtain a copy of the License at
+*
+*     https://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package eu.eventloopsoftware.avro.gradle.plugin.tasks
+
+import java.io.File
+import java.io.IOException
+import org.apache.avro.Protocol
+import org.apache.avro.SchemaParseException
+import org.apache.avro.SchemaParser
+import org.apache.avro.compiler.specific.SpecificCompiler
+import org.gradle.api.file.ConfigurableFileCollection
+import org.gradle.api.tasks.InputFiles
+import org.gradle.api.tasks.SkipWhenEmpty
+import org.gradle.api.tasks.TaskAction
+
+abstract class CompileAvroSchemaTask : AbstractCompileTask() {
+
+  @get:InputFiles @get:SkipWhenEmpty abstract val schemaFiles: 
ConfigurableFileCollection
+
+  @get:InputFiles @get:SkipWhenEmpty abstract val protocolFiles: 
ConfigurableFileCollection
+
+  @TaskAction
+  fun compileSchema() {
+    logger.info("Generating Java files from ${schemaFiles.files.size} Avro 
schemas...")
+
+    compileSchemas(schemaFiles, outputDirectory.get().asFile)
+
+    logger.info("Done generating Java files from Avro schemas...")
+  }
+
+  private fun compileSchemas(schemaFileTree: ConfigurableFileCollection, 
outputDirectory: File) {
+    val sourceFileForModificationDetection: File? =
+        schemaFileTree.asFileTree.files.filter { file: File -> 
file.lastModified() > 0 }.maxBy { it.lastModified() }
+
+    // Need to register custom logical type factories before schema 
compilation.
+    try {
+      loadLogicalTypesFactories()
+    } catch (e: IOException) {
+      throw RuntimeException("Error while loading logical types factories ", e)
+    }
+
+    try {
+      val parser = SchemaParser()
+      for (sourceFile in schemaFileTree.files) {
+        parser.parse(sourceFile)
+      }
+      val schemas = parser.parsedNamedSchemas
+      doCompile(sourceFileForModificationDetection, SpecificCompiler(schemas), 
outputDirectory)
+
+      for (sourceFile in protocolFiles.files) {
+        val protocol = Protocol.parse(sourceFile)
+        doCompile(sourceFile, protocol, outputDirectory)
+      }
+    } catch (ex: IOException) {
+      throw RuntimeException(
+          "IO ex: Error compiling a file in " + schemaFileTree.asPath + " to " 
+ outputDirectory,
+          ex,
+      )
+    } catch (ex: SchemaParseException) {
+      throw RuntimeException(

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to