frevib commented on code in PR #3614:
URL: https://github.com/apache/avro/pull/3614#discussion_r2679360061
##########
lang/java/gradle-plugin/src/main/kotlin/eu/eventloopsoftware/avro/gradle/plugin/tasks/CompileAvroSchemaTask.kt:
##########
@@ -0,0 +1,136 @@
+package org.apache.avro.gradle.plugin.tasks
+
+import org.apache.avro.SchemaParseException
+import org.apache.avro.SchemaParser
+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.Project
+import org.gradle.api.file.FileTree
+import org.gradle.api.provider.Property
+import org.gradle.api.tasks.TaskAction
+import java.io.File
+import java.io.IOException
+import java.util.*
+
+abstract class CompileSchemaTask : AbstractCompileTask() {
+
+ /**
+ * A set of Glob patterns used to select files from the source
+ * directory for processing. By default, the pattern "**/*.avsc"
+ * is used to select avsc files.
+ *
+ * @parameter
+ */
+ //@get:Input
+ //val includes: Set<String> = setOf("**/*.avsc")
+
+ @TaskAction
+ fun compileSchema() {
+ project.logger.info("Generating Java files from Avro schemas...")
+
+ if (!source.isEmpty) {
+ val sourceDirectoryFullPath =
getSourceDirectoryFullPath(sourceDirectory)
+ val outputDirectoryFullPath =
getBuildDirectoryFullPath(outputDirectory)
+ compileSchemas(source, sourceDirectoryFullPath,
outputDirectoryFullPath)
+ } else {
+ logger.warn("No Avro files found in $sourceDirectory. Nothing to
compile")
+ }
+
+ project.logger.info("Done generating Java files from Avro schemas...")
+ }
+
+
+ private fun getSourceDirectoryFullPath(directoryProperty:
Property<String>): File =
+ project.layout.projectDirectory.dir(directoryProperty.get()).asFile
+
+ private fun getBuildDirectoryFullPath(directoryProperty:
Property<String>): File =
+ project.layout.buildDirectory.dir(directoryProperty).get().asFile
+
+ private fun compileSchemas(fileTree: FileTree, sourceDirectory: File,
outputDirectory: File) {
+ val sourceFileForModificationDetection: File? =
+ fileTree
+ .files
+ .filter { file: File -> file.lastModified() > 0 }
+ .maxBy { it.lastModified() }
+
+ try {
+ val parser = SchemaParser()
+ for (sourceFile in fileTree.files) {
+ parser.parse(sourceFile)
+ }
+ val schemas = parser.parsedNamedSchemas
+
+ doCompile(sourceFileForModificationDetection,
SpecificCompiler(schemas), outputDirectory)
+ } catch (ex: IOException) {
+ // TODO: more concrete exceptions
+ throw RuntimeException("IO ex: Error compiling a file in " +
sourceDirectory + " to " + outputDirectory, ex)
+ } catch (ex: SchemaParseException) {
+ throw RuntimeException(
+ "SchemaParse ex Error compiling a file in " + sourceDirectory
+ " to " + outputDirectory,
+ ex
+ )
+ }
+ }
+
+ private fun doCompile(
+ sourceFileForModificationDetection: File?,
+ compiler: SpecificCompiler,
+ outputDirectory: File
+ ) {
+ setCompilerProperties(compiler)
+ // TODO:
+ // * customLogicalTypeFactories
+
+ try {
+ for (customConversion in customConversions.get()) {
+
compiler.addCustomConversion(Thread.currentThread().getContextClassLoader().loadClass(customConversion))
+ }
+ } catch (e: ClassNotFoundException) {
+ throw IOException(e)
Review Comment:
We are only catching `ClassNotFoundException` and not `IOException`. For all
error handling I've mostly copied how it's done in the Maven plugin.
--
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]