Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Kotlin trailing lambda syntax for custom output formatters (#690
) * This is done by offering a new API in which an output formatter can be specified as org.gradle.api.Action<Result>. Gradle automatically maps Groovy closures and Kotlin lambdas to arguments of that type (https://docs.gradle.org/current/userguide/kotlin_dsl.html#groovy_closures_from_kotlin). * Internally, there have been some changes done to support this: - The type of the property that is used to set an output formatter is `Any`, and this isn't the nicer because tooling can't really hint users of the API with the correct types, and we also have to deal manually with the types of input. - The new type we wanted to support (Action<Result>) would run into type erasure problems if we used this existing property and we would need to have code casting the types in an unsafe way (as we can't know if a provided argument is Action<Result> or Action of something else). - To sort this out, we have kept the existing property for compatibility reasons (it can still be used to set String, Reporter or Closure output formatters) but we have added a new function to set Action<Result> output formatters. With this we achieve maintaining public API compatibility whilst adding support for the new Action<Result> output formatters with improved type support (internally, safe use of types, and externally, type hinting in tooling and clear public API signature).
- Loading branch information
1 parent
682a0ac
commit cc714f8
Showing
8 changed files
with
203 additions
and
33 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
32 changes: 32 additions & 0 deletions
32
...in/src/main/kotlin/com/github/benmanes/gradle/versions/updates/OutputFormatterArgument.kt
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,32 @@ | ||
package com.github.benmanes.gradle.versions.updates | ||
|
||
import com.github.benmanes.gradle.versions.reporter.Reporter | ||
import com.github.benmanes.gradle.versions.reporter.result.Result | ||
import org.gradle.api.Action | ||
|
||
/** | ||
* Represents all the types of arguments for output formatting supported in [DependencyUpdatesTask]. | ||
*/ | ||
sealed interface OutputFormatterArgument { | ||
|
||
/** | ||
* A string representing one of the built-in output formatters (i.e. "json", "text", "html" or | ||
* "xml"), or a comma-separated list with a combination of them (e.g. "json,text"). | ||
*/ | ||
class BuiltIn(val formatterNames: String) : OutputFormatterArgument | ||
|
||
/** | ||
* An implementation of the [Reporter] interface to provide a custom output formatting. | ||
*/ | ||
class CustomReporter(val reporter: Reporter) : OutputFormatterArgument | ||
|
||
/** | ||
* An [Action] to provide a custom output formatting. Enables the use of the trailing closure/lambda | ||
* syntax for output formatting definition. | ||
*/ | ||
class CustomAction(val action: Action<Result>) : OutputFormatterArgument | ||
|
||
companion object { | ||
val DEFAULT = BuiltIn(formatterNames = "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
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