From bb975cb30fd169b4778482e831e987759b48dc27 Mon Sep 17 00:00:00 2001 From: Ben Bader Date: Sat, 23 Nov 2024 23:48:36 -0700 Subject: [PATCH] Simplify thrifty-gradle-plugin config DSL (#11) * Simplify thrifty-gradle-plugin config DSL * Add missing license header --- .github/workflows/pre-merge.yml | 4 +- .../kotlin/com/bendb/thrifty/Toolchain.kt | 20 ++++++ thrifty-gradle-plugin/README.md | 64 ++++++------------- .../GenerateThriftSourcesWorkAction.java | 11 +--- .../thrifty/gradle/KotlinThriftOptions.java | 45 ------------- .../gradle/SerializableThriftOptions.java | 33 ++-------- .../bendb/thrifty/gradle/ThriftOptions.java | 12 +++- .../thrifty/gradle/ThriftyExtension.java | 42 ++++++++++-- 8 files changed, 96 insertions(+), 135 deletions(-) delete mode 100644 thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/KotlinThriftOptions.java diff --git a/.github/workflows/pre-merge.yml b/.github/workflows/pre-merge.yml index eed06c74..1a8ea80f 100644 --- a/.github/workflows/pre-merge.yml +++ b/.github/workflows/pre-merge.yml @@ -14,7 +14,7 @@ jobs: fail-fast: false matrix: jvm-version: [21] - os: [ubuntu-latest, macos-13] + os: [ubuntu-latest, macos-latest] env: JDK_VERSION: ${{ matrix.jvm-version }} GRADLE_OPTS: -Dorg.gradle.daemon=false @@ -34,7 +34,7 @@ jobs: cache-gradle-${{ matrix.os }}-${{ matrix.jvm-version }} - name: Ensure all code files have license headers - if: matrix.os == 'ubuntu-latest' && matrix.jvm-version == '17' + if: matrix.os == 'ubuntu-latest' && matrix.jvm-version == '21' shell: bash run: | ./script/ensure_license_headers.sh diff --git a/build-src/src/main/kotlin/com/bendb/thrifty/Toolchain.kt b/build-src/src/main/kotlin/com/bendb/thrifty/Toolchain.kt index cef2227e..e6793c36 100644 --- a/build-src/src/main/kotlin/com/bendb/thrifty/Toolchain.kt +++ b/build-src/src/main/kotlin/com/bendb/thrifty/Toolchain.kt @@ -1,3 +1,23 @@ +/* + * Thrifty + * + * Copyright (c) Benjamin Bader + * + * All rights reserved. + * + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING + * WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, + * FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. + * + * See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. + */ package com.bendb.thrifty import org.gradle.jvm.toolchain.JavaLanguageVersion diff --git a/thrifty-gradle-plugin/README.md b/thrifty-gradle-plugin/README.md index bba154f7..e852a727 100644 --- a/thrifty-gradle-plugin/README.md +++ b/thrifty-gradle-plugin/README.md @@ -2,7 +2,7 @@ Gradle Plugin ------------------- Thrifty's Gradle plugin allows one to incorporate .thrift source files into a project -and have Thrifty generate Kotlin or Java code from them as part of regular builds. +and have Thrifty generate Kotlin code from them as part of regular builds. Incorporate it into your build like so: @@ -23,7 +23,7 @@ thrifty { // Note that if you do, 'src/main/thrift' is ignored. // By default, all .thrift files in this directory and its subdirectores // will be compiled. - sourceDir 'path/to/thrift/files', 'path/to/other/thrift/files' + sourceDir 'path/to/thrift/files' // Specify several directories containing thrift files, as above. // Note the plural name 'sourceDirs'. @@ -46,51 +46,27 @@ thrifty { // you can specify a different location: outputDir 'some/other/location' - // By default, Kotlin sources will be generated. You can provide a few options to the - // Kotlin code generator (all options shown with their default values): - kotlin { - // Thrift service implementations are generated by default. Set 'false' - // here to prevent them from being generated. - emitServiceClients true - - // By default, struct fields are named exactly as written in the .thrift IDL. - // Other choices are 'java' and 'pascal' - 'someField' and 'SomeField', respectively. - nameStyle 'default' - - // Standard Kotlin collections are used by default. If you want something custom, provide - // that here. - // For example, you might choose 'android.util.ArrayMap' for maps. - listType 'kotlin.collections.ArrayList' - setType 'kotlin.collections.LinkedHashSet' - mapType 'kotlin.collections.LinkedHashMap' - - // When true, generated structs will implement the Parcelable interface. This is optional, - // and results in larger code. - parcelable = false - - // The above options are also applicable to the 'java' block; Kotlin-specific options - // follow. - - // When true, generated structs will be include 'Builder' inner classes. This is - // for legacy compatibiltity only, and leads to larger code. - structBuilders = false - - // The Kotlin code generator supports several different service-client API styles. - // Vaid values are 'default' (the default), 'coroutine', and 'none'. - serviceClientStyle 'default' - } + // Thrift service implementations are generated by default. Set 'false' + // here to prevent them from being generated. + emitServiceClients true + // By default, struct fields are named exactly as written in the .thrift IDL. + // Other choices are 'java' and 'pascal' - 'someField' and 'SomeField', respectively. + nameStyle 'default' - // On the other hand, if you want Java sources, then _don't_ add the kotlin block. Add at - // least an empty 'java' block - all options shown here may be left out entirely. - java { - // Most of the options shown in the kotlin block above also apply here. + // Standard Kotlin collections are used by default. If you want something custom, provide + // that here. + // For example, you might choose 'android.util.ArrayMap' for maps. + listType 'kotlin.collections.ArrayList' + setType 'kotlin.collections.LinkedHashSet' + mapType 'kotlin.collections.LinkedHashMap' - // Specifies which kind of nullability annotations to add to generated code, if any. - // - // valid values are 'none' (the default), 'android-support', and 'androidx'. - nullabilityAnnotationKind 'none' - } + // When true, generated structs will implement the Parcelable interface. This is optional, + // and results in larger code. + parcelable = false + + // When true, an experimental feature will be enabled that generates a server for the service. + setGenerateServer = false } // If you want to put a TypeProcessor on the classpath, you can do it like so: diff --git a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/GenerateThriftSourcesWorkAction.java b/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/GenerateThriftSourcesWorkAction.java index 4eff550d..2d1e839a 100644 --- a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/GenerateThriftSourcesWorkAction.java +++ b/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/GenerateThriftSourcesWorkAction.java @@ -91,12 +91,7 @@ private void actuallyExecute() throws IOException { } SerializableThriftOptions opts = getParameters().getThriftOptions().get(); - if (opts.isKotlin()) { - generateKotlinThrifts(schema, opts); - } else { - // TODO: Refactor ThriftOptions, etc - throw new IllegalStateException("Only Kotlin thrift options are supported"); - } + generateKotlinThrifts(schema, opts); } private void reportThriftException(LoadFailedException e) { @@ -149,13 +144,11 @@ private void generateKotlinThrifts(Schema schema, SerializableThriftOptions opts gen.parcelize(); } - SerializableThriftOptions.Kotlin kopt = opts.getKotlinOpts(); - if (!opts.isGenerateServiceClients()) { gen.omitServiceClients(); } - if (kopt.isGenerateServer()) { + if (opts.isGenerateServer()) { gen.generateServer(); } diff --git a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/KotlinThriftOptions.java b/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/KotlinThriftOptions.java deleted file mode 100644 index 1b571af1..00000000 --- a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/KotlinThriftOptions.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Thrifty - * - * Copyright (c) Benjamin Bader - * Copyright (c) Microsoft Corporation - * - * All rights reserved. - * - * Licensed 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING - * WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, - * FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. - * - * See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. - */ -package com.bendb.thrifty.gradle; - -import org.gradle.api.tasks.Input; -import org.gradle.api.tasks.Optional; - -import java.io.Serializable; -import java.util.Locale; -import java.util.TreeMap; - -/** - * Thrift options specific to the Kotlin language. - */ -public class KotlinThriftOptions extends ThriftOptions implements Serializable { - private boolean generateServer = false; - - @Input - public boolean isGenerateServer() { - return generateServer; - } - - public void setGenerateServer(boolean generateServer) { - this.generateServer = generateServer; - } -} diff --git a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/SerializableThriftOptions.java b/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/SerializableThriftOptions.java index b4474c5b..8c7f25ae 100644 --- a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/SerializableThriftOptions.java +++ b/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/SerializableThriftOptions.java @@ -26,21 +26,6 @@ // Can't just use ThriftOptions cuz Gradle decorates them with non-serializable types, // and we need to pass these options to Worker API params that must be serializable. class SerializableThriftOptions implements Serializable { - static class Kotlin implements Serializable { - private boolean generateServer; - - // Required for Serializable - Kotlin() {} - - public Kotlin(boolean generateServer) { - this.generateServer = generateServer; - } - - public boolean isGenerateServer() { - return generateServer; - } - } - private boolean generateServiceClients = true; private FieldNameStyle nameStyle = FieldNameStyle.DEFAULT; private String listType = null; @@ -48,7 +33,7 @@ public boolean isGenerateServer() { private String mapType = null; private boolean parcelable = false; private boolean allowUnknownEnumValues = false; - private Kotlin kotlinOpts; + private boolean generateServer = false; // For Serializable SerializableThriftOptions() {} @@ -61,13 +46,7 @@ public boolean isGenerateServer() { this.mapType = options.getMapType(); this.parcelable = options.getParcelable(); this.allowUnknownEnumValues = options.getAllowUnknownEnumValues(); - - if (options instanceof KotlinThriftOptions) { - KotlinThriftOptions kto = (KotlinThriftOptions) options; - this.kotlinOpts = new Kotlin(kto.isGenerateServer()); - } else { - throw new IllegalArgumentException("Unexpected thrift-options type:" + options); - } + this.generateServer = options.isGenerateServer(); } public boolean isGenerateServiceClients() { @@ -98,11 +77,7 @@ public boolean isAllowUnknownEnumValues() { return allowUnknownEnumValues; } - public Kotlin getKotlinOpts() { - return kotlinOpts; - } - - public boolean isKotlin() { - return kotlinOpts != null; + public boolean isGenerateServer() { + return generateServer; } } diff --git a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/ThriftOptions.java b/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/ThriftOptions.java index c330efaa..f6834614 100644 --- a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/ThriftOptions.java +++ b/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/ThriftOptions.java @@ -31,7 +31,7 @@ /** * Thrift options applicable to all supported languages. */ -public abstract class ThriftOptions implements Serializable { +public class ThriftOptions implements Serializable { private boolean generateServiceClients = true; private FieldNameStyle nameStyle = FieldNameStyle.DEFAULT; private String listType = null; @@ -39,6 +39,7 @@ public abstract class ThriftOptions implements Serializable { private String mapType = null; private boolean parcelable = false; private boolean allowUnknownEnumValues = false; + private boolean generateServer = false; @Input public boolean getGenerateServiceClients() { @@ -126,4 +127,13 @@ public boolean getAllowUnknownEnumValues() { public void setAllowUnknownEnumValues(boolean allowUnknownEnumValues) { this.allowUnknownEnumValues = allowUnknownEnumValues; } + + @Input + public boolean isGenerateServer() { + return generateServer; + } + + public void setGenerateServer(boolean generateServer) { + this.generateServer = generateServer; + } } diff --git a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/ThriftyExtension.java b/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/ThriftyExtension.java index 920a5411..cd875209 100644 --- a/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/ThriftyExtension.java +++ b/thrifty-gradle-plugin/src/main/java/com/bendb/thrifty/gradle/ThriftyExtension.java @@ -69,7 +69,7 @@ public ThriftyExtension(ObjectFactory objects, ProjectLayout layout) { .convention(Collections.singletonList( new DefaultThriftSourceDirectory( getDefaultSourceDirectorySet()))); - this.thriftOptions = objects.property(ThriftOptions.class).convention(new KotlinThriftOptions()); + this.thriftOptions = objects.property(ThriftOptions.class).value(new ThriftOptions()); this.outputDirectory = objects.directoryProperty().convention(layout.getBuildDirectory().dir(DEFAULT_OUTPUT_DIR)); this.thriftyVersion = objects.property(String.class); } @@ -155,9 +155,41 @@ public void outputDir(String path) { } } - public void kotlin(Action action) { - KotlinThriftOptions opts = objects.newInstance(KotlinThriftOptions.class); - action.execute(opts); - thriftOptions.set(opts); + // ThriftOptions forwards + + public void setGenerateServiceClients(boolean generateServiceClients) { + thriftOptions.get().setGenerateServiceClients(generateServiceClients); + } + + public void setNameStyle(String styleName) { + thriftOptions.get().setNameStyle(styleName); + } + + public void setNameStyle(FieldNameStyle style) { + thriftOptions.get().setNameStyle(style); + } + + public void setListType(String listType) { + thriftOptions.get().setListType(listType); + } + + public void setSetType(String setType) { + thriftOptions.get().setSetType(setType); + } + + public void setMapType(String mapType) { + thriftOptions.get().setMapType(mapType); + } + + public void setParcelable(boolean parcelable) { + thriftOptions.get().setParcelable(parcelable); + } + + public void setAllowUnknownEnumValues(boolean allowUnknownEnumValues) { + thriftOptions.get().setAllowUnknownEnumValues(allowUnknownEnumValues); + } + + public void setGenerateServer(boolean generateServer) { + thriftOptions.get().setGenerateServer(generateServer); } }