|
| 1 | +/* |
| 2 | + * Copyright 2023. assertj-generator-gradle-plugin contributors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package org.assertj.generator.gradle |
| 14 | + |
| 15 | +import org.assertj.generator.gradle.tasks.AssertJGenerationTask |
| 16 | +import org.assertj.generator.gradle.tasks.config.AssertJGeneratorExtension |
| 17 | +import org.gradle.api.Plugin |
| 18 | +import org.gradle.api.Project |
| 19 | +import org.gradle.api.logging.Logging |
| 20 | +import org.gradle.api.model.ObjectFactory |
| 21 | +import org.gradle.api.plugins.JavaPlugin |
| 22 | +import org.gradle.api.plugins.JavaPluginExtension |
| 23 | +import org.gradle.api.tasks.SourceSet |
| 24 | +import org.gradle.kotlin.dsl.create |
| 25 | +import org.gradle.kotlin.dsl.getByType |
| 26 | +import org.gradle.kotlin.dsl.register |
| 27 | +import javax.inject.Inject |
| 28 | + |
| 29 | +/** |
| 30 | + * Defines the entry point for applying the AssertJGeneration plugin |
| 31 | + */ |
| 32 | +open class AssertJGeneratorGradlePlugin @Inject internal constructor( |
| 33 | + private val objects: ObjectFactory, |
| 34 | +) : Plugin<Project> { |
| 35 | + |
| 36 | + override fun apply(project: Project) { |
| 37 | + project.pluginManager.withPlugin("java") { |
| 38 | + val assertJGeneratorConfiguration = project.configurations.create(ASSERTJ_GEN_CONFIGURATION_NAME) |
| 39 | + .setVisible(false) |
| 40 | + .setDescription("AssertJ Generator configuration") |
| 41 | + assertJGeneratorConfiguration.defaultDependencies { |
| 42 | + it.add(project.dependencies.create("org.assertj:assertj-assertions-generator:2.0.0")) |
| 43 | + } |
| 44 | + |
| 45 | + project.configurations.named(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME) { |
| 46 | + assertJGeneratorConfiguration.extendsFrom(it) |
| 47 | + } |
| 48 | + |
| 49 | + val javaPluginExtension = project.extensions.getByType<JavaPluginExtension>() |
| 50 | + |
| 51 | + // So now we have to go through and add the properties that we want |
| 52 | + javaPluginExtension.sourceSets.all { sourceSet -> |
| 53 | + if (sourceSet.name == "test") return@all |
| 54 | + |
| 55 | + // For each sourceSet we're enacting an action on each one that adds an assertJ generation task to it |
| 56 | + logger.info("sourceSet: $sourceSet creating tasks") |
| 57 | + |
| 58 | + sourceSet.extensions.create<AssertJGeneratorExtension>( |
| 59 | + "assertJ", |
| 60 | + objects, |
| 61 | + project, |
| 62 | + sourceSet, |
| 63 | + ) |
| 64 | + |
| 65 | + addAndConfigureAssertJGenerate(project, javaPluginExtension, sourceSet) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private fun addAndConfigureAssertJGenerate( |
| 71 | + project: Project, |
| 72 | + javaPlugin: JavaPluginExtension, |
| 73 | + sourceSet: SourceSet |
| 74 | + ) { |
| 75 | + val generateTaskName = sourceSet.getTaskName("generate", "assertJ") |
| 76 | + |
| 77 | + logger.info("generationTask: $generateTaskName, sourceSet: $sourceSet") |
| 78 | + |
| 79 | + // Create a new task for the source set |
| 80 | + val generationTask = project.tasks.register<AssertJGenerationTask>(generateTaskName, objects, sourceSet) |
| 81 | + |
| 82 | + javaPlugin.sourceSets.named("test").configure { sourceSet -> |
| 83 | + sourceSet.java.srcDir(generationTask.flatMap { it.outputDir }) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + companion object { |
| 88 | + const val ASSERTJ_GEN_CONFIGURATION_NAME = "assertJ" |
| 89 | + |
| 90 | + private val logger = Logging.getLogger(AssertJGeneratorGradlePlugin::class.java) |
| 91 | + } |
| 92 | +} |
0 commit comments