ebAtUelzener commented on code in PR #3614: URL: https://github.com/apache/avro/pull/3614#discussion_r3085768251
########## lang/java/gradle-plugin/src/main/kotlin/eu/eventloopsoftware/avro/gradle/plugin/tasks/CompileAvroSchemaTask.kt: ########## @@ -0,0 +1,86 @@ +/* +* 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.GradleException +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 } + .maxByOrNull { it.lastModified() } + + // Need to register custom logical type factories before schema compilation. + try { + loadLogicalTypesFactories() + } catch (e: IOException) { + throw GradleException("Error while loading logical types factories ", e) + } + + try { + val parser = SchemaParser() + for (sourceFile in schemaFileTree.files) { + parser.parse(sourceFile) + } Review Comment: This loop assumes that a single pass of all files is sufficient to parse all input files, however in the case of an unresolved schema exception multiple passes may be required. The old plugin solves this inside its [SchemaResolver](https://github.com/davidmc24/gradle-avro-plugin/blob/master/src/main/java/com/github/davidmc24/gradle/plugin/avro/SchemaResolver.java) by delaying the parsing of these unresolved schemas and then requeueing them after each successfully parsed file. This can easily happen for instance in the default value validation step for record fields when using an enum that has not been parsed yet due to how the filetree walk has ordered the schema files. -- 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]
