Skip to content

Commit dd8116f

Browse files
committed
#19, kotlin dsl support
1 parent 910bd89 commit dd8116f

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
lines changed

src/main/groovy/com/github/hauner/openapi/gradle/OpenApiProcessorExtension.groovy

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.github.hauner.openapi.gradle
1818

19+
import org.gradle.api.Action
1920
import org.gradle.api.Project
2021
import org.gradle.api.model.ObjectFactory
2122
import org.gradle.api.provider.MapProperty
@@ -82,6 +83,46 @@ class OpenApiProcessorExtension {
8283
processors = objectFactory.mapProperty (String, Processor)
8384
}
8485

86+
/**
87+
* groovy/kotlin dsl. create a new processor configuration.
88+
*
89+
* <pre>
90+
* openapiProcessor {
91+
* process("newProcessor") { ... }
92+
* }
93+
* </pre>
94+
*
95+
* @param name unique name of the processor
96+
* @param args {@link Processor} action
97+
* @return the new processor
98+
*/
99+
Processor process(String name, Action<Processor> action) {
100+
def processor = new Processor (name)
101+
action.execute (processor)
102+
processors.put (name, processor)
103+
}
104+
105+
/**
106+
* groovy dsl only. create a new processor configuration.
107+
*
108+
* <pre>
109+
* openapiProcessor {
110+
* newProcessor { ... }
111+
* }
112+
* </pre>
113+
*
114+
* gradle will never call this from a kotlin build script unless explicitly calling it, i.e.
115+
*
116+
* <pre>
117+
* openapiProcessor {
118+
* methodMissing "newProcessor" { ... }
119+
* }
120+
* </pre>
121+
*
122+
* @param name unique name of the processor
123+
* @param args arg array. arg[0] must be a {@link Processor} configuration block
124+
* @return the new processor
125+
*/
85126
def methodMissing (String name, def args) {
86127
def arg = args[0]
87128

src/main/groovy/com/github/hauner/openapi/gradle/Processor.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ class Processor {
6363
other.put (API_PATH, path)
6464
}
6565

66+
void prop (Map<String, Object> props) {
67+
other.putAll (props)
68+
}
69+
70+
void prop (String key, Object value) {
71+
other.put (key, value)
72+
}
73+
74+
void prop (GString key, Object value) {
75+
other.put (key.toString (), value)
76+
}
77+
6678
def methodMissing (String name, def args) {
6779
if (args[0] instanceof Closure) {
6880
def builder = new MapBuilder()

src/test/groovy/com/github/hauner/openapi/gradle/OpenApiProcessorExtensionSpec.groovy

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class OpenApiProcessorExtensionSpec extends Specification {
4242
ex.apiPath.get () == "openapi.yaml"
4343
}
4444

45-
void "converts processor closure to map" () {
45+
void "converts processor closure to map via methodMissing" () {
4646
project.configure (_, _) >> { args ->
4747
Closure c = args[1]
4848
c.delegate = args[0]
@@ -67,6 +67,25 @@ class OpenApiProcessorExtensionSpec extends Specification {
6767
ex.processors.get ().test2.other.two == "b2"
6868
}
6969

70+
void "converts processor properties to map via process()/prop() methods" () {
71+
when:
72+
def ex = new OpenApiProcessorExtension (project, objectFactory)
73+
ex.process ("test") {
74+
it.prop ("one", "a")
75+
it.prop ("two", "b")
76+
}
77+
ex.process ("test2") {
78+
it.prop ("one", "a2")
79+
it.prop ("two", "b2")
80+
}
81+
82+
then:
83+
ex.processors.get ().test.other.one == "a"
84+
ex.processors.get ().test.other.two == "b"
85+
ex.processors.get ().test2.other.one == "a2"
86+
ex.processors.get ().test2.other.two == "b2"
87+
}
88+
7089
void "handle apiPath when given as GString" () {
7190
def projectDir = "projectDir"
7291

src/test/groovy/com/github/hauner/openapi/gradle/ProcessorSpec.groovy

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package com.github.hauner.openapi.gradle
1919
import spock.lang.Specification
2020

2121
class ProcessorSpec extends Specification {
22-
22+
2323
void "gets & sets named props" () {
2424
def processor = new Processor('test')
25-
25+
2626
when:
2727
processor."${prop}" (value)
28-
28+
2929
then:
3030
processor."get${prop.capitalize ()}"() == value
3131

@@ -35,7 +35,7 @@ class ProcessorSpec extends Specification {
3535
'targetDir' | 'target folder'
3636
}
3737

38-
void "maps unknown properties" () {
38+
void "maps unknown properties via methodMissing" () {
3939
def processor = new Processor('test')
4040

4141
when:
@@ -45,7 +45,7 @@ class ProcessorSpec extends Specification {
4545
processor.other.unknown == 'foo'
4646
}
4747

48-
void "maps nested properties" () {
48+
void "maps nested properties via methodMissing" () {
4949
def processor = new Processor('test')
5050

5151
when:
@@ -65,4 +65,36 @@ class ProcessorSpec extends Specification {
6565
processor.other.test.three.five == 'd'
6666
}
6767

68+
void "maps unknown properties via prop()" () {
69+
def processor = new Processor('test')
70+
71+
when:
72+
processor.prop ('foo', "any")
73+
processor.prop ("foo2", "any2")
74+
75+
then:
76+
processor.other.foo == 'any'
77+
processor.other.foo2 == 'any2'
78+
}
79+
80+
void "maps nested properties via prop()" () {
81+
def processor = new Processor('test')
82+
83+
when:
84+
processor.prop("test", [
85+
one: "a",
86+
two: "b",
87+
three: [
88+
four: "c",
89+
five: "d"
90+
]
91+
])
92+
93+
then:
94+
processor.other.test.one == 'a'
95+
processor.other.test.two == 'b'
96+
processor.other.test.three.four == 'c'
97+
processor.other.test.three.five == 'd'
98+
}
99+
68100
}

0 commit comments

Comments
 (0)