Skip to content

Commit

Permalink
Simplify thrifty-gradle-plugin config DSL (#11)
Browse files Browse the repository at this point in the history
* Simplify thrifty-gradle-plugin config DSL

* Add missing license header
  • Loading branch information
benjamin-bader authored Nov 24, 2024
1 parent 7f034eb commit bb975cb
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 135 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pre-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions build-src/src/main/kotlin/com/bendb/thrifty/Toolchain.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
64 changes: 20 additions & 44 deletions thrifty-gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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'.
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,14 @@
// 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;
private String setType = null;
private String mapType = null;
private boolean parcelable = false;
private boolean allowUnknownEnumValues = false;
private Kotlin kotlinOpts;
private boolean generateServer = false;

// For Serializable
SerializableThriftOptions() {}
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
/**
* 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;
private String setType = null;
private String mapType = null;
private boolean parcelable = false;
private boolean allowUnknownEnumValues = false;
private boolean generateServer = false;

@Input
public boolean getGenerateServiceClients() {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -155,9 +155,41 @@ public void outputDir(String path) {
}
}

public void kotlin(Action<KotlinThriftOptions> 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);
}
}

0 comments on commit bb975cb

Please sign in to comment.