Can read from gradle.properties

This commit is contained in:
Marcin Grzejszczak
2019-08-30 14:48:13 +02:00
parent 95014539ee
commit 46d00d17a8
4 changed files with 199 additions and 19 deletions

View File

@@ -19,11 +19,10 @@ package org.springframework.cloud.release.internal.buildsystem;
import java.io.File;
import org.springframework.cloud.release.internal.ReleaserProperties;
import org.springframework.cloud.release.internal.ReleaserPropertiesAware;
class CompositeBomParser implements BomParser, ReleaserPropertiesAware {
class CompositeBomParser implements BomParser {
private ReleaserProperties properties;
private final ReleaserProperties properties;
CompositeBomParser(ReleaserProperties releaserProperties) {
this.properties = releaserProperties;
@@ -32,7 +31,7 @@ class CompositeBomParser implements BomParser, ReleaserPropertiesAware {
@Override
public boolean isApplicable(File clonedBom) {
return new MavenBomParser(this.properties).isApplicable(clonedBom)
|| new GradleBomParser().isApplicable(clonedBom);
|| new GradleBomParser(this.properties).isApplicable(clonedBom);
}
@Override
@@ -41,16 +40,11 @@ class CompositeBomParser implements BomParser, ReleaserPropertiesAware {
}
private BomParser firstMatching(File thisProjectRoot) {
BomParser gradle = new GradleBomParser();
if (new GradleBomParser().isApplicable(thisProjectRoot)) {
BomParser gradle = new GradleBomParser(this.properties);
if (new GradleBomParser(this.properties).isApplicable(thisProjectRoot)) {
return gradle;
}
return new MavenBomParser(this.properties);
}
@Override
public void setReleaserProperties(ReleaserProperties properties) {
this.properties = properties;
}
}

View File

@@ -17,17 +17,86 @@
package org.springframework.cloud.release.internal.buildsystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.cloud.release.internal.ReleaserProperties;
class GradleBomParser implements BomParser {
private final ReleaserProperties properties;
GradleBomParser(ReleaserProperties releaserProperties) {
this.properties = releaserProperties;
}
@Override
public boolean isApplicable(File clonedBom) {
return new File(clonedBom, "build.gradle").exists();
return file(clonedBom, "build.gradle").exists();
}
File file(File clonedBom, String child) {
return new File(clonedBom, child);
}
@Override
public VersionsFromBom versionsFromBom(File thisProjectRoot) {
return null;
File gradleProperties = file(thisProjectRoot, "gradle.properties");
if (!gradleProperties.exists()) {
return VersionsFromBom.EMPTY_VERSION;
}
Properties properties = loadProps(gradleProperties);
final Map<String, String> substitution = this.properties.getGradle()
.getGradlePropsSubstitution();
VersionsFromBom versionsFromBom = new VersionsFromBom(this.properties);
properties.forEach((key, value) -> {
String projectName = projectName(substitution, key);
versionsFromBom.setVersion(projectName, value.toString());
});
return versionsFromBom;
}
String projectName(Map<String, String> substitution, Object key) {
String projectName = key.toString();
if (substitution.containsKey(key)) {
projectName = substitution.get(key);
}
else {
Pattern versionPattern = Pattern.compile("^([a-zA-Z]+)Version$");
Matcher matcher = versionPattern.matcher(projectName);
boolean versionMatches = matcher.matches();
if (versionMatches) {
projectName = matcher.group(1);
}
}
projectName = projectName.replaceAll("([A-Z])", "-$1").toLowerCase();
return projectName;
}
Properties loadProps(File file) {
Properties props = new Properties();
try {
props.load(new FileInputStream(file));
}
catch (IOException e) {
throw new IllegalStateException(e);
}
return props;
}
private String asString(Path path) {
try {
return new String(Files.readAllBytes(path));
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
}

View File

@@ -174,6 +174,12 @@ public class GradleUpdater implements ReleaserPropertiesAware {
}
}
private boolean pathIgnored(File file) {
String path = file.getPath();
return this.assertVersions && this.properties.getGradle()
.getIgnoredGradleRegex().stream().anyMatch(path::matches);
}
private Properties loadProps(File file) {
Properties props = new Properties();
try {
@@ -185,12 +191,6 @@ public class GradleUpdater implements ReleaserPropertiesAware {
return props;
}
private boolean pathIgnored(File file) {
String path = file.getPath();
return this.assertVersions && this.properties.getGradle()
.getIgnoredGradleRegex().stream().anyMatch(path::matches);
}
private String asString(Path path) {
try {
return new String(Files.readAllBytes(path));

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.release.internal.buildsystem;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.assertj.core.api.BDDAssertions;
import org.junit.jupiter.api.Test;
import org.springframework.cloud.release.internal.ReleaserProperties;
class GradleBomParserTests {
@Test
void should_read_versions_from_bom_from_properties() {
GradleBomParser parser = new GradleBomParser(new ReleaserProperties()) {
@Override
public boolean isApplicable(File clonedBom) {
return true;
}
@Override
File file(File clonedBom, String child) {
return clonedBom;
}
@Override
Properties loadProps(File file) {
Properties properties = new Properties();
properties.setProperty("springCloudContractVersion", "1.0.0.RELEASE");
return properties;
}
};
VersionsFromBom versionsFromBom = parser.versionsFromBom(new File("."));
BDDAssertions.then(versionsFromBom.versionForProject("spring-cloud-contract"))
.isEqualTo("1.0.0.RELEASE");
}
@Test
void should_return_a_version_from_bom_with_substitution() {
Map<String, String> gradleSubstitution = new HashMap<>();
gradleSubstitution.put("verifierVersion", "spring-cloud-contract");
ReleaserProperties releaserProperties = new ReleaserProperties();
releaserProperties.getGradle().setGradlePropsSubstitution(gradleSubstitution);
GradleBomParser parser = new GradleBomParser(releaserProperties) {
@Override
public boolean isApplicable(File clonedBom) {
return true;
}
@Override
File file(File clonedBom, String child) {
return clonedBom;
}
@Override
Properties loadProps(File file) {
Properties properties = new Properties();
properties.setProperty("verifierVersion", "1.0.0.RELEASE");
return properties;
}
};
VersionsFromBom versionsFromBom = parser.versionsFromBom(new File("."));
BDDAssertions.then(versionsFromBom.versionForProject("spring-cloud-contract"))
.isEqualTo("1.0.0.RELEASE");
}
@Test
void should_be_not_applicable_when_no_build_gradle_is_present() {
GradleBomParser parser = new GradleBomParser(new ReleaserProperties());
BDDAssertions.then(parser.isApplicable(new File("."))).isFalse();
}
@Test
void should_be_applicable_when_build_gradle_is_present() {
GradleBomParser parser = new GradleBomParser(new ReleaserProperties()) {
@Override
File file(File clonedBom, String child) {
return clonedBom;
}
};
BDDAssertions.then(parser.isApplicable(new File("."))).isTrue();
}
@Test
void should_return_empty_version_when_no_gradle_properties_is_present() {
GradleBomParser parser = new GradleBomParser(new ReleaserProperties());
VersionsFromBom versionsFromBom = parser.versionsFromBom(new File("."));
BDDAssertions.then(versionsFromBom).isSameAs(VersionsFromBom.EMPTY_VERSION);
}
}