Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add incremental build support to gradle plugin #1165

Merged
merged 1 commit into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,52 @@ public ContractDownloader(StubDownloader stubDownloader,
* pattern
* @return location of the unpacked downloaded stubs
*/
// Use unpackAndDownloadContracts() and createNewInclusionProperties() instead
@Deprecated
anatoliy-balakirev marked this conversation as resolved.
Show resolved Hide resolved
public File unpackedDownloadedContracts(ContractVerifierConfigProperties config) {
File contractsDirectory = unpackAndDownloadContracts();
updatePropertiesWithInclusion(contractsDirectory, config);
return contractsDirectory;
}

// Use createNewInclusionProperties() instead
@Deprecated
public ContractVerifierConfigProperties updatePropertiesWithInclusion(
anatoliy-balakirev marked this conversation as resolved.
Show resolved Hide resolved
File contractsDirectory, ContractVerifierConfigProperties config) {
final InclusionProperties newInclusionProperties = createNewInclusionProperties(
contractsDirectory);
config.setIncludedContracts(newInclusionProperties.getIncludedContracts());
config.setIncludedRootFolderAntPattern(
newInclusionProperties.getIncludedRootFolderAntPattern());
return config;
}

/**
* Downloads JAR containing all the contracts. The JAR with the contracts contains all
* the contracts for all the projects. We're interested only in its subset.
* @return location of the unpacked downloaded stubs
*/
public File unpackAndDownloadContracts() {
if (log.isDebugEnabled()) {
log.debug("Will download contracts for [" + this.contractsJarStubConfiguration
+ "]");
}
Map.Entry<StubConfiguration, File> unpackedContractStubs = this.stubDownloader
.downloadAndUnpackStubJar(this.contractsJarStubConfiguration);
if (unpackedContractStubs == null) {
throw new IllegalStateException("The contracts failed to be downloaded!");
}
return unpackedContractStubs.getValue();
}

/**
* After JAR with all the contracts is downloaded and unpacked - we need to get new
* inclusion pattern for those contracts. The JAR with the contracts contains all the
* contracts for all the projects. We're interested only in its subset.
* @param contractsDirectory - location of the unpacked downloaded stubs.
* @return new inclusion properties, calculated for those downloaded contracts.
*/
public InclusionProperties createNewInclusionProperties(File contractsDirectory) {
String pattern;
String includedAntPattern;
if (StringUtils.hasText(this.contractsPath)) {
Expand Down Expand Up @@ -107,9 +145,7 @@ public ContractVerifierConfigProperties updatePropertiesWithInclusion(
}
log.info("Pattern to pick contracts equals [" + pattern + "]");
log.info("Ant Pattern to pick files equals [" + includedAntPattern + "]");
config.setIncludedContracts(pattern);
config.setIncludedRootFolderAntPattern(includedAntPattern);
return config;
return new InclusionProperties(pattern, includedAntPattern);
}

private File contractsSubDirIfPresent(File contractsDirectory) {
Expand Down Expand Up @@ -162,19 +198,6 @@ private String wrapWithAntPattern(String path) {
+ "**/";
}

private File unpackAndDownloadContracts() {
anatoliy-balakirev marked this conversation as resolved.
Show resolved Hide resolved
if (log.isDebugEnabled()) {
log.debug("Will download contracts for [" + this.contractsJarStubConfiguration
+ "]");
}
Map.Entry<StubConfiguration, File> unpackedContractStubs = this.stubDownloader
.downloadAndUnpackStubJar(this.contractsJarStubConfiguration);
if (unpackedContractStubs == null) {
throw new IllegalStateException("The contracts failed to be downloaded!");
}
return unpackedContractStubs.getValue();
}

private String groupArtifactToPattern(File contractsDirectory) {
return ("^" + contractsDirectory.getAbsolutePath() + "(" + File.separator + ")?"
+ ".*" + slashSeparatedGroupId() + File.separator + this.projectArtifactId
Expand All @@ -189,4 +212,36 @@ private String slashSeparatedGroupId() {
return this.projectGroupId.replace(".", File.separator);
}

/**
* Holder for updated inclusion properties, which are calculated after jar with
* contracts was downloaded / unpacked.
*/
public static class InclusionProperties {
anatoliy-balakirev marked this conversation as resolved.
Show resolved Hide resolved

/**
* @see ContractVerifierConfigProperties.includedContracts
*/
private final String includedContracts;

/**
* @see ContractVerifierConfigProperties.includedRootFolderAntPattern
*/
private final String includedRootFolderAntPattern;

InclusionProperties(final String includedContracts,
final String includedRootFolderAntPattern) {
this.includedContracts = includedContracts;
this.includedRootFolderAntPattern = includedRootFolderAntPattern;
}

public String getIncludedContracts() {
return includedContracts;
}

public String getIncludedRootFolderAntPattern() {
return includedRootFolderAntPattern;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties;
import org.springframework.cloud.contract.verifier.converter.RecursiveFilesConverter;
import org.springframework.cloud.contract.verifier.converter.StubGenerator;
import org.springframework.cloud.contract.verifier.converter.StubGeneratorProvider;
Expand Down Expand Up @@ -149,12 +148,10 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {

private void generateNewMappings(Path path) {
File unpackedLocation = path.toFile();
ContractVerifierConfigProperties configProperties = new ContractVerifierConfigProperties();
configProperties
.setContractsDslDir(subfolderIfPresent(unpackedLocation, "contracts"));
configProperties
.setStubsOutputDir(subfolderIfPresent(unpackedLocation, "mappings"));
RecursiveFilesConverter converter = new RecursiveFilesConverter(configProperties);
RecursiveFilesConverter converter = new RecursiveFilesConverter(
subfolderIfPresent(unpackedLocation, "mappings"),
subfolderIfPresent(unpackedLocation, "contracts"), new ArrayList<>(),
".*", false);
converter.processFiles();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package org.springframework.cloud.contract.stubrunner

import spock.lang.Specification

import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties

/**
* @author Marcin Grzejszczak
*/
Expand All @@ -35,33 +33,28 @@ class ContractDownloaderSpec extends Specification {
String contractPath = File.separator + ['a', 'b', 'c', 'd'].join(File.separator)
ContractDownloader contractDownloader = new ContractDownloader(stubDownloader,
stubConfiguration, contractPath, '', '', '')
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
and:
stubDownloader.downloadAndUnpackStubJar(_) >> new AbstractMap.SimpleEntry(stubConfiguration, file)
when:
contractDownloader.unpackedDownloadedContracts(properties)
ContractDownloader.InclusionProperties inclusionProperties = contractDownloader.createNewInclusionProperties(file)
then:
properties.includedContracts.startsWith('^')
properties.includedContracts.endsWith('$')
properties.includedContracts.contains(fileSeparated('/some/path/to/somewhere(/)?.*/a/b/c/d/.*'))
properties.includedRootFolderAntPattern == "**/a/b/c/d/**/"
inclusionProperties.includedContracts.startsWith('^')
inclusionProperties.includedContracts.endsWith('$')
inclusionProperties.includedContracts.contains(fileSeparated('/some/path/to/somewhere(/)?.*/a/b/c/d/.*'))
inclusionProperties.includedRootFolderAntPattern == "**/a/b/c/d/**/"
}

def 'should set inclusion pattern on config when path pattern was explicitly provided without a separator at the beginning'() {
given:
String contractPath = ['a', 'b', 'c', 'd'].join(File.separator)
ContractDownloader contractDownloader = new ContractDownloader(stubDownloader,
stubConfiguration, contractPath, '', '', '')
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
and:
stubDownloader.downloadAndUnpackStubJar(_) >> new AbstractMap.SimpleEntry(stubConfiguration, file)
when:
contractDownloader.unpackedDownloadedContracts(properties)
ContractDownloader.InclusionProperties inclusionProperties =
contractDownloader.createNewInclusionProperties(file)
then:
properties.includedContracts.startsWith('^')
properties.includedContracts.endsWith('$')
properties.includedContracts.contains(fileSeparated('/some/path/to/somewhere(/)?.*/a/b/c/d/.*'))
properties.includedRootFolderAntPattern == "**/a/b/c/d/**/"
inclusionProperties.includedContracts.startsWith('^')
inclusionProperties.includedContracts.endsWith('$')
inclusionProperties.includedContracts.contains(fileSeparated('/some/path/to/somewhere(/)?.*/a/b/c/d/.*'))
inclusionProperties.includedRootFolderAntPattern == "**/a/b/c/d/**/"
}

private static String fileSeparated(String string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,41 @@ import org.springframework.cloud.contract.verifier.wiremock.DslToWireMockClientC
class RecursiveFilesConverter {

private final StubGeneratorProvider holder
private final ContractVerifierConfigProperties props
private final File outMappingsDir
private final File contractsDslDir
private final List<String> excludedFiles
private final String includedContracts
private final boolean excludeBuildFolders

// Use constructor without ContractVerifierConfigProperties
@Deprecated
anatoliy-balakirev marked this conversation as resolved.
Show resolved Hide resolved
RecursiveFilesConverter(ContractVerifierConfigProperties props, StubGeneratorProvider holder = null) {
this.props = props
this.outMappingsDir = props.stubsOutputDir
this.holder = holder ?: new StubGeneratorProvider()
this(props.stubsOutputDir, props.contractsDslDir, props.excludedFiles, props.includedContracts, props.excludeBuildFolders, holder)
}

// Use constructor without ContractVerifierConfigProperties
@Deprecated
RecursiveFilesConverter(ContractVerifierConfigProperties props, File stubsOutputDir, StubGeneratorProvider holder = null) {
this(stubsOutputDir, props.contractsDslDir, props.excludedFiles, props.includedContracts, props.excludeBuildFolders, holder)
}

RecursiveFilesConverter(ContractVerifierConfigProperties props, File outMappingsDir, StubGeneratorProvider holder = null) {
this.props = props
this.outMappingsDir = outMappingsDir
RecursiveFilesConverter(File stubsOutputDir, File contractsDslDir, List<String> excludedFiles,
String includedContracts, boolean excludeBuildFolders, StubGeneratorProvider holder = null) {
this.outMappingsDir = stubsOutputDir
this.contractsDslDir = contractsDslDir
this.excludedFiles = excludedFiles
this.includedContracts = includedContracts
this.excludeBuildFolders = excludeBuildFolders
this.holder = holder ?: new StubGeneratorProvider()
}

void processFiles() {
ContractFileScanner scanner = new ContractFileScannerBuilder()
.baseDir(props.contractsDslDir)
.excluded(props.excludedFiles as Set)
.baseDir(contractsDslDir)
.excluded(excludedFiles as Set)
.ignored([] as Set)
.included([] as Set)
.includeMatcher(props.includedContracts)
.includeMatcher(includedContracts)
.build()
ListMultimap<Path, ContractMetadata> contracts = scanner.findContracts()
if (log.isDebugEnabled()) {
Expand All @@ -81,7 +94,7 @@ class RecursiveFilesConverter {
holder.converterForName(sourceFile.name)
try {
String path = sourceFile.path
if (props.isExcludeBuildFolders()
if (excludeBuildFolders
&& (
matchesPath(path, "target") || matchesPath(path, "build"))) {
if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -131,7 +144,7 @@ class RecursiveFilesConverter {
}

private Path createAndReturnTargetDirectory(File sourceFile) {
Path relativePath = Paths.get(props.contractsDslDir.toURI()).
Path relativePath = Paths.get(contractsDslDir.toURI()).
relativize(sourceFile.parentFile.toPath())
Path absoluteTargetPath = outMappingsDir.toPath().resolve(relativePath)
Files.createDirectories(absoluteTargetPath)
Expand Down
Loading