Skip to content

Commit b869c8d

Browse files
authored
Merge pull request #80 from jsonschema2dataclass/target-dir-prefix
2 parents daa1fb4 + 2b51316 commit b869c8d

File tree

5 files changed

+31
-25
lines changed

5 files changed

+31
-25
lines changed

README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Help wanted for this task
109109
| source | ConfigurableFileCollection | | Location of the JSON Schema file(s). Note: this may refer to a single file or a directory of files.
110110
| sourceSortOrder | String | OS | The sort order to be applied when recursively processing the source files. By default the OS can influence the processing order. Supported values: OS (Let the OS influence the order the source files are processed.) FILES_FIRST (Case sensitive sort, visit the files first. The source files are processed in a breadth first sort order.) SUBDIRS_FIRST (Case sensitive sort, visit the sub-directories before the files. The source files are processed in a depth first sort order.)
111111
| sourceType | String | jsonschema | The type of input documents that will be read Supported values: jsonschema (schema documents, containing formal rules that describe the structure of JSON data) json (documents that represent an example of the kind of JSON data that the generated Java types will be mapped to) yamlschema (JSON schema documents, represented as YAML) yaml (documents that represent an example of the kind of YAML (or JSON) data that the generated Java types will be mapped to)
112+
| targetDirecotryPrefix | String | generated/source/js2d | Directory prefix under build directory where underlying tool will generate sources |
112113
| targetPackage | String | | Package name used for generated Java classes (for types where a fully qualified name has not been supplied in the schema using the 'javaType' property).
113114
| targetVersion | String | | The target version for generated source files.
114115
| timeType | String | | What type to use instead of string when adding string type fields of format time (not date-time) to generated Java types.

demo/androidDev/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ jsonSchema2Pojo {
6565
//source = files("${sourceSets.main.output.resourcesDir}/json")
6666
source.setFrom files("${project.rootDir}/schema")
6767

68+
targetDirectoryPrefix = 'generated/source/jsonschema2dataclass'
6869
// Target directory for generated Java source files. The plugin will ignore this value for Android projects and will generate the classes into a generated source folder for each build variant and add this directory to the
6970
// java source set so the compiler will find and compile the newly generated source files.
7071
// targetDirectory = file("${project.rootDir}/build/generated-sources/js2p")

src/main/groovy/com/github/eirnym/js2p/JsonSchemaExtension.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ package com.github.eirnym.js2p
1818
import groovy.transform.ToString
1919
import org.gradle.api.NamedDomainObjectContainer
2020
import org.gradle.api.model.ObjectFactory
21+
import org.gradle.api.provider.Property
2122

2223
import javax.inject.Inject
2324

25+
import static com.github.eirnym.js2p.JsonSchemaPlugin.TARGET_FOLDER_BASE
26+
2427
@ToString
2528
class JsonSchemaExtension extends JsonSchema2PojoPluginConfiguration {
2629
final NamedDomainObjectContainer<JsonSchema2PojoPluginConfiguration> executions
30+
final Property<String> targetDirectoryPrefix
2731

2832
@Inject
2933
JsonSchemaExtension(ObjectFactory objectFactory) {
3034
super("main", objectFactory)
35+
targetDirectoryPrefix = objectFactory.property(String.class).convention(TARGET_FOLDER_BASE)
3136
executions = objectFactory.domainObjectContainer(JsonSchema2PojoPluginConfiguration)
3237
}
3338
}

src/main/groovy/com/github/eirnym/js2p/JsonSchemaPlugin.groovy

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ class JsonSchemaPlugin implements Plugin<Project> {
119119
private static Path getAndroidJsonPath(Project project) {
120120
def sets
121121
if (project.android.hasProperty('sourceSets')) {
122-
sets = project.android.sourceSets;
122+
sets = project.android.sourceSets
123123
} else {
124-
sets = project.android.sourceSetsContainer;
124+
sets = project.android.sourceSetsContainer
125125
}
126126

127127
def main = sets.find { it.name.startsWith("main") }
@@ -160,6 +160,7 @@ class JsonSchemaPlugin implements Plugin<Project> {
160160
taskNameSuffix,
161161
execId,
162162
execution.source,
163+
extension.targetDirectoryPrefix.get(),
163164
targetDirectorySuffix
164165
)
165166
postConfigure(task)
@@ -173,6 +174,7 @@ class JsonSchemaPlugin implements Plugin<Project> {
173174
String taskNameSuffix,
174175
int executionId,
175176
ConfigurableFileCollection source,
177+
String targetDirectoryPrefix,
176178
String targetDirectorySuffix
177179
) {
178180

@@ -187,7 +189,7 @@ class JsonSchemaPlugin implements Plugin<Project> {
187189
task.execution = executionId
188190
task.sourceFiles.setFrom(source)
189191
task.targetDirectory.set(
190-
project.layout.buildDirectory.dir("${TARGET_FOLDER_BASE}${executionId}${targetDirectorySuffix}")
192+
project.layout.buildDirectory.dir("${targetDirectoryPrefix}${executionId}${targetDirectorySuffix}")
191193
)
192194
task.inputs.files({ task.sourceFiles.filter({ it.exists() }) })
193195
.skipWhenEmpty()
@@ -214,7 +216,7 @@ class JsonSchemaPlugin implements Plugin<Project> {
214216
}
215217
}
216218
}
217-
private void verifyGradleVersion() {
219+
private static void verifyGradleVersion() {
218220
if (GradleVersion.current() < GradleVersion.version(MINIMUM_GRADLE_VERSION)) {
219221
throw new GradleException("Plugin ${PLUGIN_ID} requires at least Gradle $MINIMUM_GRADLE_VERSION, but you are using ${GradleVersion.current().version}")
220222
}

src/test/groovy/com/github/eirnym/js2p/JavaTaskFunctionalTest.groovy

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ class JavaTaskFunctionalTest {
2626
private static final String COLON_TASK_NAME = ':' + TASK_NAME
2727
private static final String COLON_TASK_NAME0 = COLON_TASK_NAME + '0'
2828
private static final String COLON_TASK_NAME1 = COLON_TASK_NAME + '1'
29+
2930
private static final String TARGET_FOLDER_BASE0 = TARGET_FOLDER_BASE + '0'
30-
private static final String TARGET_FOLDER_BASE1 = TARGET_FOLDER_BASE + '1'
31+
private static final String TARGET_FOLDER_BASE_CUSTOM = TARGET_FOLDER_BASE + 's'
32+
private static final String TARGET_FOLDER_BASE_CUSTOM0 = TARGET_FOLDER_BASE_CUSTOM + '0'
33+
private static final String TARGET_FOLDER_BASE_CUSTOM1 = TARGET_FOLDER_BASE_CUSTOM + '1'
3134
private static final List<String> GRADLE_RELEASES = [
3235
'6.8.3', '6.8', '6.7.1', '6.6.1', '6.5.1', '6.4.1', '6.3', '6.2.2', '6.2.1', '6.1.1', '6.0.1', // 6.x
3336
]
@@ -52,9 +55,7 @@ class JavaTaskFunctionalTest {
5255
def result = executeRunner(gradleVersion, testProjectDir)
5356

5457
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
55-
def js2pDir = testProjectDir.resolve("build/${TARGET_FOLDER_BASE0}")
56-
assertExists(js2pDir.toFile())
57-
assertExists(js2pDir.resolve("Address.java").toFile())
58+
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE0}")
5859
}
5960

6061
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@@ -68,9 +69,7 @@ class JavaTaskFunctionalTest {
6869
def result = executeRunner(gradleVersion, testProjectDir)
6970

7071
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
71-
def js2pDir = testProjectDir.resolve("build/${TARGET_FOLDER_BASE0}/com/example")
72-
assertExists(js2pDir.toFile())
73-
assertExists(js2pDir.resolve("Address.java").toFile())
72+
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE0}/com/example")
7473
}
7574
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
7675
@NullSource
@@ -83,9 +82,7 @@ class JavaTaskFunctionalTest {
8382
def result = executeRunner(gradleVersion, testProjectDir)
8483

8584
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
86-
def js2pDir = testProjectDir.resolve("build/${TARGET_FOLDER_BASE0}/com/example")
87-
assertExists(js2pDir.toFile())
88-
assertExists(js2pDir.resolve("Address.java").toFile())
85+
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE0}/com/example")
8986
}
9087

9188
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@@ -99,9 +96,7 @@ class JavaTaskFunctionalTest {
9996
def result = executeRunner(gradleVersion, testProjectDir)
10097

10198
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
102-
def js2pDir = testProjectDir.resolve("build/${TARGET_FOLDER_BASE0}/com/example")
103-
assertExists(js2pDir.toFile())
104-
assertExists(js2pDir.resolve("Address.java").toFile())
99+
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE0}/com/example")
105100
}
106101

107102
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@@ -114,16 +109,11 @@ class JavaTaskFunctionalTest {
114109
copyAddressJSON()
115110

116111
def result = executeRunner(gradleVersion, testProjectDir, COLON_TASK_NAME)
117-
118112
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME0).outcome)
119-
def js2pDir0 = testProjectDir.resolve("build/${TARGET_FOLDER_BASE0}/com/example")
120-
assertExists(js2pDir0.toFile())
121-
assertExists(js2pDir0.resolve("Address.java").toFile())
122-
123113
assertEquals(TaskOutcome.SUCCESS, result.task(COLON_TASK_NAME1).outcome)
124-
def js2pDir1 = testProjectDir.resolve("build/${TARGET_FOLDER_BASE1}/org/example")
125-
assertExists(js2pDir1.toFile())
126-
assertExists(js2pDir1.resolve("Address.java").toFile())
114+
115+
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE_CUSTOM0}/com/example")
116+
addressJavaExists(testProjectDir, "build/${TARGET_FOLDER_BASE_CUSTOM1}/org/example")
127117
}
128118

129119
@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@@ -198,6 +188,7 @@ class JavaTaskFunctionalTest {
198188
private void createBuildFilesMultiple() {
199189
Files.write(buildFile, (BUILD_FILE_HEADER + """
200190
|jsonSchema2Pojo{
191+
| targetDirectoryPrefix = "${TARGET_FOLDER_BASE_CUSTOM}"
201192
| executions {
202193
| com{
203194
| targetPackage = 'com.example'
@@ -256,6 +247,12 @@ class JavaTaskFunctionalTest {
256247
assertTrue(file.exists())
257248
}
258249

250+
private static void addressJavaExists(Path testProjectDir, String targetDirectory) {
251+
def js2pDir = testProjectDir.resolve(targetDirectory)
252+
assertExists(js2pDir.toFile())
253+
assertExists(js2pDir.resolve("Address.java").toFile())
254+
}
255+
259256
private static BuildResult executeRunner(
260257
String gradleVersion, Path testProjectDir, String task = COLON_TASK_NAME, boolean shouldFail = false) {
261258
def arguments = GradleRunner.create()

0 commit comments

Comments
 (0)