forked from apache/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
128 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/commercehub/gradle/plugin/avro/ResolveAvroDependenciesTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.commercehub.gradle.plugin.avro; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import org.apache.avro.Schema; | ||
import org.gradle.api.GradleException; | ||
import org.gradle.api.file.FileCollection; | ||
import org.gradle.api.specs.NotSpec; | ||
import org.gradle.api.tasks.CacheableTask; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
import static com.commercehub.gradle.plugin.avro.Constants.SCHEMA_EXTENSION; | ||
|
||
/** | ||
* Task to read Avro schema files, resolve their dependencies, and write out dependency-free Avro schema files. | ||
*/ | ||
@CacheableTask | ||
public class ResolveAvroDependenciesTask extends OutputDirTask { | ||
private final SchemaResolver resolver = new SchemaResolver(getProject(), getLogger()); | ||
|
||
@TaskAction | ||
protected void process() { | ||
getLogger().info("Found {} files", getInputs().getSourceFiles().getFiles().size()); | ||
failOnUnsupportedFiles(); | ||
processFiles(); | ||
} | ||
|
||
private void failOnUnsupportedFiles() { | ||
FileCollection unsupportedFiles = filterSources(new NotSpec<>(new FileExtensionSpec(SCHEMA_EXTENSION))); | ||
if (!unsupportedFiles.isEmpty()) { | ||
throw new GradleException( | ||
String.format("Unsupported file extension for the following files: %s", unsupportedFiles)); | ||
} | ||
} | ||
|
||
private void processFiles() { | ||
int processedFileCount = processSchemaFiles(); | ||
setDidWork(processedFileCount > 0); | ||
} | ||
|
||
private int processSchemaFiles() { | ||
Set<File> inputFiles = filterSources(new FileExtensionSpec(SCHEMA_EXTENSION)).getFiles(); | ||
ProcessingState processingState = resolver.resolve(inputFiles); | ||
for (Schema schema : processingState.getSchemas()) { | ||
try { | ||
String outputPath = schema.getNamespace().replaceAll(Pattern.quote("."), "/") | ||
+ "/" + schema.getName() + "." + SCHEMA_EXTENSION; | ||
File outputFile = new File(getOutputDir().get().getAsFile(), outputPath); | ||
String schemaJson = schema.toString(true); | ||
FileUtils.writeJsonFile(outputFile, schemaJson); | ||
getLogger().debug("Wrote {}", outputFile.getPath()); | ||
} catch (IOException ex) { | ||
throw new GradleException(String.format("Failed to write resolved schema definition for %s", schema.getFullName()), ex); | ||
} | ||
} | ||
return processingState.getProcessedTotal(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...roovy/com/commercehub/gradle/plugin/avro/ResolveAvroDependenciesTaskFunctionalSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.commercehub.gradle.plugin.avro | ||
|
||
import org.hamcrest.MatcherAssert | ||
import spock.lang.Subject | ||
import uk.co.datumedge.hamcrest.json.SameJSONAs | ||
|
||
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS | ||
|
||
@Subject(ResolveAvroDependenciesTask) | ||
class ResolveAvroDependenciesTaskFunctionalSpec extends FunctionalSpec { | ||
def "resolves dependencies"() { | ||
def srcDir = testProjectDir.newFolder("src", "avro", "normalized") | ||
|
||
given: "a build with the task declared" | ||
applyAvroBasePlugin() | ||
buildFile << """ | ||
|tasks.register("resolveAvroDependencies", com.commercehub.gradle.plugin.avro.ResolveAvroDependenciesTask) { | ||
| source file("src/avro/normalized") | ||
| outputDir = file("build/avro/resolved") | ||
|} | ||
|""".stripMargin() | ||
|
||
and: "some normalized schema files" | ||
copyResource("/examples/separate/Breed.avsc", srcDir) | ||
copyResource("/examples/separate/Cat.avsc", srcDir) | ||
|
||
when: "running the task" | ||
def result = run("resolveAvroDependencies") | ||
|
||
then: "the resolved schema files are generated" | ||
result.task(":resolveAvroDependencies").outcome == SUCCESS | ||
MatcherAssert.assertThat( | ||
projectFile("build/avro/resolved/example/Cat.avsc").text, | ||
SameJSONAs.sameJSONAs(getClass().getResourceAsStream("/examples/inline/Cat.avsc").text)) | ||
MatcherAssert.assertThat( | ||
projectFile("build/avro/resolved/example/Breed.avsc").text, | ||
SameJSONAs.sameJSONAs(getClass().getResourceAsStream("/examples/separate/Breed.avsc").text)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters