All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.codearte.accurest.plugin.AccurestGradlePlugin.groovy Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package io.codearte.accurest.plugin

import io.codearte.accurest.config.AccurestConfigProperties
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
/**
 * @author Jakub Kubrynski
 */
class AccurestGradlePlugin implements Plugin {

	private static final String GENERATE_SERVER_TESTS_TASK_NAME = 'generateAccurest'
	private static final String DSL_TO_WIREMOCK_CLIENT_TASK_NAME = 'generateWireMockClientStubs'

	private static final Class IDEA_PLUGIN_CLASS = org.gradle.plugins.ide.idea.IdeaPlugin
	private static final String GROUP_NAME = "Verification"

	private Project project

	@Override
	void apply(Project project) {
		this.project = project
		AccurestConfigProperties extension = project.extensions.create('accurest', AccurestConfigProperties)

		project.check.dependsOn(GENERATE_SERVER_TESTS_TASK_NAME)

		setConfigurationDefaults(extension)
		createGenerateTestsTask(extension)
		createAndConfigureGenerateWireMockClientStubsFromDslTask(extension)
		deprecatedCreateAndConfigureGenerateWiremockClientStubsFromDslTask()
		project.dependencies.add("testCompile", "com.github.tomakehurst:wiremock:2.0.10-beta")
		project.dependencies.add("testCompile", "com.toomuchcoding.jsonassert:jsonassert:${extension.getJsonAssertVersion()}")
		project.dependencies.add("testCompile", "org.assertj:assertj-core:2.3.0")

		project.afterEvaluate {
			def hasIdea = project.plugins.findPlugin(IDEA_PLUGIN_CLASS)
			if (hasIdea) {
				project.idea {
					module {
						testSourceDirs += extension.generatedTestSourcesDir
						testSourceDirs += extension.contractsDslDir
					}
				}
			}
		}
	}

	void setConfigurationDefaults(AccurestConfigProperties extension) {
		extension.with {
			generatedTestSourcesDir = project.file("${project.buildDir}/generated-test-sources/accurest")
			contractsDslDir = defaultAccurestContractsDir() //TODO: Use sourceset
			basePackageForTests = 'io.codearte.accurest.tests'
		}
	}

	private File defaultAccurestContractsDir() {
		project.file("${project.rootDir}/src/test/accurest")
	}

	private void createGenerateTestsTask(AccurestConfigProperties extension) {
		Task task = project.tasks.create(GENERATE_SERVER_TESTS_TASK_NAME, GenerateServerTestsTask)
		task.description = "Generate server tests from GroovyDSL"
		task.group = GROUP_NAME
		task.conventionMapping.with {
			contractsDslDir = { extension.contractsDslDir }
			generatedTestSourcesDir = { extension.generatedTestSourcesDir }
			configProperties = { extension }
		}
	}

	private void createAndConfigureGenerateWireMockClientStubsFromDslTask(AccurestConfigProperties extension) {
		Task task = project.tasks.create(DSL_TO_WIREMOCK_CLIENT_TASK_NAME, GenerateWireMockClientStubsFromDslTask)
		task.description = "Generate WireMock client stubs from GroovyDSL"
		task.group = GROUP_NAME
		task.conventionMapping.with {
			contractsDslDir = { extension.contractsDslDir }
			stubsOutputDir = { extension.stubsOutputDir }
			configProperties = { extension }
		}
	}

	private void deprecatedCreateAndConfigureGenerateWiremockClientStubsFromDslTask() {
		Task task = project.tasks.create('generateWiremockClientStubs')
		task.dependsOn('generateWireMockClientStubs')
		task.description = "DEPRECATED - Generates WireMock client stubs. - DEPRECATED - use 'generateWireMockClientStubs' task"
		task.group = GROUP_NAME
		task.doFirst {logger.warn("DEPRECATION WARNING. Task 'generateWiremockClientStubs' is deprecated. Use 'generateWireMockClientStubs' task instead.")}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy