diff --git a/CHANGES.md b/CHANGES.md index 23ea5c5..85ccdbb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Change Log +# 0.3.1 + * Fix extension support for configuring encoding + * Make default encoding UTF-8 + * 0.3.0 * IntelliJ: register generated source directories even if they don't already exist. * Add avro-base plugin, which exposes tasks and the extension without creating tasks, defaults, etc. diff --git a/src/main/java/com/commercehub/gradle/plugin/avro/AvroBasePlugin.java b/src/main/java/com/commercehub/gradle/plugin/avro/AvroBasePlugin.java index c05d6cd..b116e9f 100644 --- a/src/main/java/com/commercehub/gradle/plugin/avro/AvroBasePlugin.java +++ b/src/main/java/com/commercehub/gradle/plugin/avro/AvroBasePlugin.java @@ -19,7 +19,14 @@ public void apply(final Project project) { private static void configureExtension(final Project project) { final AvroExtension avroExtension = project.getExtensions().create(AVRO_EXTENSION_NAME, DefaultAvroExtension.class); - conventionMapping(avroExtension).map("stringType", new Callable() { + ConventionMapping extensionMapping = conventionMapping(avroExtension); + extensionMapping.map("encoding", new Callable() { + @Override + public String call() throws Exception { + return Constants.UTF8_ENCONDING; + } + }); + extensionMapping.map("stringType", new Callable() { @Override public String call() throws Exception { return GenericData.StringType.String.name(); @@ -28,7 +35,14 @@ public String call() throws Exception { project.getTasks().withType(GenerateAvroJavaTask.class).all(new Action() { @Override public void execute(GenerateAvroJavaTask task) { - conventionMapping(task).map("stringType", new Callable() { + ConventionMapping taskMapping = conventionMapping(task); + taskMapping.map("encoding", new Callable() { + @Override + public String call() throws Exception { + return avroExtension.getEncoding(); + } + }); + taskMapping.map("stringType", new Callable() { @Override public String call() throws Exception { return avroExtension.getStringType(); diff --git a/src/main/java/com/commercehub/gradle/plugin/avro/AvroExtension.java b/src/main/java/com/commercehub/gradle/plugin/avro/AvroExtension.java index 1c5c25e..c6bc904 100644 --- a/src/main/java/com/commercehub/gradle/plugin/avro/AvroExtension.java +++ b/src/main/java/com/commercehub/gradle/plugin/avro/AvroExtension.java @@ -1,5 +1,6 @@ package com.commercehub.gradle.plugin.avro; public interface AvroExtension { + String getEncoding(); String getStringType(); } diff --git a/src/main/java/com/commercehub/gradle/plugin/avro/DefaultAvroExtension.java b/src/main/java/com/commercehub/gradle/plugin/avro/DefaultAvroExtension.java index e848f55..8787de2 100644 --- a/src/main/java/com/commercehub/gradle/plugin/avro/DefaultAvroExtension.java +++ b/src/main/java/com/commercehub/gradle/plugin/avro/DefaultAvroExtension.java @@ -1,8 +1,18 @@ package com.commercehub.gradle.plugin.avro; public class DefaultAvroExtension implements AvroExtension { + private String encoding; private String stringType; + @Override + public String getEncoding() { + return encoding; + } + + public void setEncoding(String encoding) { + this.encoding = encoding; + } + @Override public String getStringType() { return stringType; diff --git a/src/main/java/com/commercehub/gradle/plugin/avro/GenerateAvroJavaTask.java b/src/main/java/com/commercehub/gradle/plugin/avro/GenerateAvroJavaTask.java index a2eb870..db0bdde 100644 --- a/src/main/java/com/commercehub/gradle/plugin/avro/GenerateAvroJavaTask.java +++ b/src/main/java/com/commercehub/gradle/plugin/avro/GenerateAvroJavaTask.java @@ -25,7 +25,7 @@ public class GenerateAvroJavaTask extends OutputDirTask { private static Set SUPPORTED_EXTENSIONS = SetBuilder.build(PROTOCOL_EXTENSION, SCHEMA_EXTENSION); - private String encoding; + private String encoding = Constants.UTF8_ENCONDING; private String stringType; @@ -61,6 +61,7 @@ private GenericData.StringType parseStringType() { @TaskAction protected void process() { + getLogger().debug("Using encoding {}", getEncoding()); getLogger().info("Found {} files", getInputs().getSourceFiles().getFiles().size()); failOnUnsupportedFiles(); preClean(); @@ -109,9 +110,7 @@ private void processProtoFile(File sourceFile) { Protocol protocol = Protocol.parse(sourceFile); SpecificCompiler compiler = new SpecificCompiler(protocol); compiler.setStringType(parseStringType()); - if (encoding != null) { - compiler.setOutputCharacterEncoding(encoding); - } + compiler.setOutputCharacterEncoding(getEncoding()); compiler.compileToDestination(sourceFile, getOutputDir()); } catch (IOException ex) { throw new GradleException(String.format("Failed to compile protocol definition file %s", sourceFile), ex); @@ -140,9 +139,7 @@ private int processSchemaFiles() { Schema schema = parser.parse(sourceFile); SpecificCompiler compiler = new SpecificCompiler(schema); compiler.setStringType(parseStringType()); - if (encoding != null) { - compiler.setOutputCharacterEncoding(encoding); - } + compiler.setOutputCharacterEncoding(getEncoding()); compiler.compileToDestination(sourceFile, getOutputDir()); types = parser.getTypes(); getLogger().info("Processed {}", sourceFile);