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


##########
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()

Review Comment:
   I've used the same as in the Maven plugin: 
   
   
https://github.com/apache/avro/blob/4e376735ebbd14cc17e53116183039c8c4ced8ab/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java#L391
   
   and:
   
   
https://github.com/apache/avro/blob/4e376735ebbd14cc17e53116183039c8c4ced8ab/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java#L462



-- 
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