@@ -26,16 +26,39 @@ import com.squareup.wire.schema.ProtoTarget
2626import com.squareup.wire.schema.SchemaHandler
2727import com.squareup.wire.schema.Target
2828import com.squareup.wire.schema.newSchemaHandler
29+ import java.io.File
2930import javax.inject.Inject
31+ import kotlin.LazyThreadSafetyMode.NONE
32+ import org.gradle.api.model.ObjectFactory
33+ import org.gradle.api.provider.ListProperty
34+ import org.gradle.api.provider.MapProperty
35+ import org.gradle.api.provider.Property
36+ import org.gradle.api.provider.Provider
3037
3138/* *
3239 * Specifies Wire's outputs (expressed as a list of [Target] objects) using Gradle's DSL (expressed
3340 * as destination directories and configuration options). This includes registering output
3441 * directories with the project so they can be compiled after they are generated.
3542 */
3643abstract class WireOutput {
44+ // Gradle decorates this getter for instances created with ObjectFactory.newInstance().
45+ @get:Inject
46+ protected open val objectFactory: ObjectFactory
47+ get() = throw UnsupportedOperationException (" Injected by Gradle" )
48+
49+ val out : Property <String > by lazy(NONE ) {
50+ objectFactory.property(String ::class .java)
51+ }
52+
3753 /* * Set this to override the default output directory for this [WireOutput]. */
38- var out : String? = null
54+ fun setOut (value : String? ) {
55+ out .set(value)
56+ }
57+
58+ internal fun outputDirectory (
59+ projectDir : File ,
60+ defaultOutputDirectory : Provider <String >,
61+ ): Provider <String > = out .map { relativizeOutputDirectory(it, projectDir) }.orElse(defaultOutputDirectory)
3962
4063 /* *
4164 * Transforms this [WireOutput] into a [Target] for which Wire will generate code. The [Target]
@@ -44,6 +67,16 @@ abstract class WireOutput {
4467 abstract fun toTarget (outputDirectory : String ): Target
4568}
4669
70+ internal fun relativizeOutputDirectory (
71+ outputDirectory : String ,
72+ projectDir : File ,
73+ ): String {
74+ val file = File (outputDirectory)
75+ if (! file.isAbsolute) return outputDirectory
76+ return runCatching { file.relativeTo(projectDir).path }
77+ .getOrElse { outputDirectory }
78+ }
79+
4780open class JavaOutput @Inject constructor() : WireOutput() {
4881 /* * See [com.squareup.wire.schema.Target.includes] */
4982 var includes: List <String >? = null
@@ -239,40 +272,78 @@ open class ProtoOutput @Inject constructor() : WireOutput() {
239272}
240273
241274open class CustomOutput @Inject constructor() : WireOutput() {
275+ val includes: ListProperty <String > by lazy(NONE ) {
276+ objectFactory.listProperty(String ::class .java)
277+ }
278+
279+ val excludes: ListProperty <String > by lazy(NONE ) {
280+ objectFactory.listProperty(String ::class .java)
281+ }
282+
283+ val exclusive: Property <Boolean > by lazy(NONE ) {
284+ objectFactory.property(Boolean ::class .java).convention(true )
285+ }
286+
287+ val options: MapProperty <String , String > by lazy(NONE ) {
288+ objectFactory.mapProperty(String ::class .java, String ::class .java)
289+ }
290+
291+ val schemaHandlerFactory: Property <SchemaHandler .Factory > by lazy(NONE ) {
292+ objectFactory.property(SchemaHandler .Factory ::class .java)
293+ }
294+
295+ val schemaHandlerFactoryClass: Property <String > by lazy(NONE ) {
296+ objectFactory.property(String ::class .java)
297+ }
298+
242299 /* * See [com.squareup.wire.schema.Target.includes] */
243- var includes: List <String >? = null
300+ fun setIncludes (value : List <String >? ) {
301+ includes.set(value)
302+ }
244303
245304 /* * See [com.squareup.wire.schema.Target.excludes] */
246- var excludes: List <String >? = null
305+ fun setExcludes (value : List <String >? ) {
306+ excludes.set(value)
307+ }
247308
248309 /* * See [com.squareup.wire.schema.Target.exclusive] */
249- var exclusive: Boolean = true
310+ fun setExclusive (value : Boolean ) {
311+ exclusive.set(value)
312+ }
250313
251- /* *
252- * Black boxed payload which a caller can set for the custom [SchemaHandler.Factory] to receive.
253- */
254- var options : Map < String , String > ? = null
314+ /* * Black boxed payload which a caller can set for the custom [SchemaHandler.Factory] to receive. */
315+ fun setOptions ( value : Map < String , String > ? ) {
316+ options.set(value)
317+ }
255318
256319 /* * Assign the schema handler factory instance. */
257- var schemaHandlerFactory: SchemaHandler .Factory ? = null
320+ fun setSchemaHandlerFactory (value : SchemaHandler .Factory ? ) {
321+ schemaHandlerFactory.set(value)
322+ }
258323
259324 /* *
260325 * Assign the schema handler factory by name. If you use a class name, that class must have a
261326 * no-arguments constructor.
262327 */
263- var schemaHandlerFactoryClass: String? = null
328+ fun setSchemaHandlerFactoryClass (value : String? ) {
329+ schemaHandlerFactoryClass.set(value)
330+ }
264331
265332 override fun toTarget (outputDirectory : String ): CustomTarget {
266- check((schemaHandlerFactory != null ) || (schemaHandlerFactoryClass != null )) {
333+ val configuredSchemaHandlerFactory = schemaHandlerFactory.orNull
334+ val configuredSchemaHandlerFactoryClass = schemaHandlerFactoryClass.orNull
335+
336+ check(configuredSchemaHandlerFactory != null || configuredSchemaHandlerFactoryClass != null ) {
267337 " schemaHandlerFactory or schemaHandlerFactoryClass required"
268338 }
339+
269340 return CustomTarget (
270- includes = includes ? : listOf (" *" ),
271- excludes = excludes ? : listOf (),
272- exclusive = exclusive,
341+ includes = includes.orNull ? : listOf (" *" ),
342+ excludes = excludes.orNull ? : listOf (),
343+ exclusive = exclusive.orElse( true ).get() ,
273344 outDirectory = outputDirectory,
274- options = options ? : mapOf (),
275- schemaHandlerFactory = schemaHandlerFactory ? : newSchemaHandler(schemaHandlerFactoryClass !! ),
345+ options = options.orNull ? : mapOf (),
346+ schemaHandlerFactory = configuredSchemaHandlerFactory ? : newSchemaHandler(configuredSchemaHandlerFactoryClass !! ),
276347 )
277348 }
278349}
0 commit comments