Skip to content

Commit

Permalink
Added support for incremental Gradle builds
Browse files Browse the repository at this point in the history
fixes gh-1133
  • Loading branch information
marcingrzejszczak committed Jul 31, 2019
1 parent 709801c commit b9f3af3
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package org.springframework.cloud.contract.verifier.plugin
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import org.gradle.api.internal.ConventionTask
import org.gradle.api.DefaultTask
import org.gradle.api.logging.Logger
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.WorkResult

Expand All @@ -37,22 +39,30 @@ import org.springframework.cloud.contract.verifier.converter.ToYamlConverter
*/
@PackageScope
@CompileStatic
class ContractsCopyTask extends ConventionTask {
class ContractsCopyTask extends DefaultTask {
private static final String ORIGINAL_PATH = "original"

ContractVerifierExtension extension
GradleContractsDownloader downloader

@InputDirectory
File input

@OutputDirectory
File outputContractsFolder

@TaskAction
void copy() {
ContractVerifierConfigProperties props = ExtensionToProperties.fromExtension(getExtension())
File file = getDownloader().downloadAndUnpackContractsIfRequired(getExtension(), props)
file = contractsSubDirIfPresent(logger, file)
ContractVerifierConfigProperties props = props()
if (logger.isDebugEnabled()) {
logger.debug("Config props [" + props + "]")
}
File file = getInput()
String antPattern = "${props.includedRootFolderAntPattern}*.*"
String slashSeparatedGroupId = project.group.toString().replace(".", File.separator)
String slashSeparatedAntPattern = antPattern.replace(slashSeparatedGroupId, project.group.toString())
String root = root(props)
File outputContractsFolder = outputContractsFolder(root)
String root = root()
File outputContractsFolder = getOutputContractsFolder()
project.logger.info("Downloading and unpacking files from [$file] to [$outputContractsFolder]. The inclusion ant patterns are [${antPattern}] and [${slashSeparatedAntPattern}]")
copy(file, antPattern, slashSeparatedAntPattern, props, outputContractsFolder)
if (getExtension().isConvertToYaml()) {
Expand All @@ -61,15 +71,26 @@ class ContractsCopyTask extends ConventionTask {

}

private File getInput() {
File file = getDownloader().downloadAndUnpackContractsIfRequired(getExtension(), props())
return contractsSubDirIfPresent(logger, file)
}

private ContractVerifierConfigProperties props() {
return ExtensionToProperties.fromExtension(getExtension())
}

@CompileDynamic
private File outputContractsFolder(String root) {
private File getOutputContractsFolder() {
String root = root()
File outputContractsFolder = outputFolder(root, "contracts")
ext.contractsDslDir = outputContractsFolder
return outputContractsFolder
}

@CompileDynamic
private String root(ContractVerifierConfigProperties props) {
private String root() {
ContractVerifierConfigProperties props = props()
String root = OutputFolderBuilder.buildRootPath(project)
ext.contractVerifierConfigProperties = props
return root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package org.springframework.cloud.contract.verifier.plugin

import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import org.gradle.api.DefaultTask
import org.gradle.api.Task
import org.gradle.api.internal.ConventionTask
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
Expand All @@ -33,8 +35,12 @@ import org.springframework.cloud.contract.verifier.converter.RecursiveFilesConve
* @since 2.0.0
*/
@CompileStatic
class GenerateClientStubsFromDslTask extends ConventionTask {
class GenerateClientStubsFromDslTask extends DefaultTask {

@InputDirectory
File contractsDslDir

@OutputDirectory
File stubsOutputDir

ContractVerifierExtension configProperties
Expand All @@ -45,16 +51,24 @@ class GenerateClientStubsFromDslTask extends ConventionTask {
logger.info("Stubs output dir [${getStubsOutputDir()}")
Task copyContractsTask = project.getTasksByName(SpringCloudContractVerifierGradlePlugin.COPY_CONTRACTS_TASK_NAME, false).first()
ContractVerifierConfigProperties props = props(copyContractsTask)
File contractsDslDir = contractsDslDir(copyContractsTask, props)
logger.info("Spring Cloud Contract Verifier Plugin: Invoking DSL to client stubs conversion")
props.contractsDslDir = contractsDslDir
props.contractsDslDir = getContractsDslDir()
props.includedContracts = ".*"
File outMappingsDir = OutputFolderBuilder.outputMappingsDir(project, getStubsOutputDir())
logger.info("Contracts dir is [${contractsDslDir}] output stubs dir is [${outMappingsDir}]")
RecursiveFilesConverter converter = new RecursiveFilesConverter(props, outMappingsDir)
converter.processFiles()
}

private File getContractsDslDir() {
Task copyContractsTask = project.getTasksByName(SpringCloudContractVerifierGradlePlugin.COPY_CONTRACTS_TASK_NAME, false).first()
ContractVerifierConfigProperties props = props(copyContractsTask)
if (logger.isDebugEnabled()) {
logger.debug("Config props [" + props + "]")
}
return contractsDslDir(copyContractsTask, props)
}

@CompileDynamic
private ContractVerifierConfigProperties props(Task task) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ package org.springframework.cloud.contract.verifier.plugin

import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Task
import org.gradle.api.internal.ConventionTask
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.spec.ContractVerifierException
import org.springframework.cloud.contract.verifier.TestGenerator
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
import org.springframework.cloud.contract.verifier.config.TestFramework

import static org.springframework.cloud.contract.verifier.plugin.SpringCloudContractVerifierGradlePlugin.COPY_CONTRACTS_TASK_NAME

Expand All @@ -37,20 +37,22 @@ import static org.springframework.cloud.contract.verifier.plugin.SpringCloudCont
* @since 1.0.0
*/
@CompileStatic
class GenerateServerTestsTask extends ConventionTask {
class GenerateServerTestsTask extends DefaultTask {

@InputDirectory
File contractsDslDir

@OutputDirectory
File generatedTestSourcesDir

//TODO: How to deal with @Input*, @Output* and that domain object?
ContractVerifierExtension configProperties
GradleContractsDownloader downloader

@TaskAction
void generate() {
logger.info("Generated test sources dir [${getGeneratedTestSourcesDir()}]")
Task copyContractsTask = project.getTasksByName(COPY_CONTRACTS_TASK_NAME, false).first()
ContractVerifierConfigProperties props = props(copyContractsTask)
File contractsDslDir = contractsDslDir(copyContractsTask, props)
ContractVerifierConfigProperties props = getProps()
File contractsDslDir = getContractsDslDir()
if (getConfigProperties().getContractDependency()) {
project.logger.debug("Updating the stubs locations for the case where we have a JAR with contracts")
props.contractsDslDir = contractsDslDir
Expand All @@ -59,9 +61,6 @@ class GenerateServerTestsTask extends ConventionTask {
project.logger.info("Spring Cloud Contract Verifier Plugin: Invoking test sources generation")
project.logger.info("Contracts are unpacked to [${contractsDslDir}]")
project.logger.info("Included contracts are [${props.includedContracts}]")
def sourceSetType = getConfigProperties().getTestFramework() == TestFramework.SPOCK ?
"groovy" : "java"
applySourceSets(sourceSetType)
try {
props = props ?: ExtensionToProperties.fromExtension(getConfigProperties())
props.contractsDslDir = contractsDslDir
Expand All @@ -74,18 +73,9 @@ class GenerateServerTestsTask extends ConventionTask {
}
}

@CompileDynamic
private void applySourceSets(sourceSetType) {
project.sourceSets.test."${sourceSetType}" {
project.logger.
info("Registering ${getConfigProperties().generatedTestSourcesDir} as test source directory")
srcDir getConfigProperties().getGeneratedTestSourcesDir()
}
project.sourceSets.test.resources {
project.logger.
info("Registering ${getConfigProperties().generatedTestResourcesDir} as test resource directory")
srcDir getConfigProperties().getGeneratedTestResourcesDir()
}
private ContractVerifierConfigProperties getProps() {
Task copyContractsTask = project.getTasksByName(COPY_CONTRACTS_TASK_NAME, false).first()
return props(copyContractsTask)
}

@CompileDynamic
Expand All @@ -101,6 +91,12 @@ class GenerateServerTestsTask extends ConventionTask {
}
}

private File getContractsDslDir() {
Task copyContractsTask = project.getTasksByName(COPY_CONTRACTS_TASK_NAME, false).first()
ContractVerifierConfigProperties props = props(copyContractsTask)
return contractsDslDir(copyContractsTask, props)
}

@CompileDynamic
private File contractsDslDir(Task task, ContractVerifierConfigProperties props) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package org.springframework.cloud.contract.verifier.plugin

import groovy.transform.CompileStatic
import org.gradle.api.internal.ConventionTask
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

import org.springframework.cloud.contract.stubrunner.ContractProjectUpdater
Expand All @@ -33,11 +34,16 @@ import org.springframework.cloud.contract.stubrunner.StubRunnerOptions
* @since 2.0.0
*/
@CompileStatic
class PublishStubsToScmTask extends ConventionTask {
class PublishStubsToScmTask extends DefaultTask {

private final ExtensionHolderSpec closureHolder = new ClosureHolder()

@OutputDirectory
File stubsOutputDir

ContractVerifierExtension configProperties

GradleContractsDownloader downloader
private final ExtensionHolderSpec closureHolder = new ClosureHolder()

@TaskAction
void publishStubsToScm() {
Expand Down Expand Up @@ -67,6 +73,12 @@ class PublishStubsToScmTask extends ConventionTask {
return true
}

/**
* This method can be used to perform additional customization of
* the {@link ContractVerifierExtension}.
*
* @param closure to customize the {@link ContractVerifierExtension}
*/
void customize(@DelegatesTo(ContractVerifierExtension) Closure closure) {
project.logger.debug("Storing the extension closure")
this.closureHolder.extensionClosure = closure
Expand Down
Loading

0 comments on commit b9f3af3

Please sign in to comment.