diff --git a/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/CompositeBomParser.java b/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/CompositeBomParser.java index af179900..9331309c 100644 --- a/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/CompositeBomParser.java +++ b/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/CompositeBomParser.java @@ -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; - } - } diff --git a/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/GradleBomParser.java b/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/GradleBomParser.java index 72941079..2935180c 100644 --- a/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/GradleBomParser.java +++ b/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/GradleBomParser.java @@ -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 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 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); + } } } diff --git a/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/GradleUpdater.java b/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/GradleUpdater.java index 1451f582..aac8f28b 100644 --- a/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/GradleUpdater.java +++ b/spring-cloud-release-tools-core/src/main/java/org/springframework/cloud/release/internal/buildsystem/GradleUpdater.java @@ -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)); diff --git a/spring-cloud-release-tools-core/src/test/java/org/springframework/cloud/release/internal/buildsystem/GradleBomParserTests.java b/spring-cloud-release-tools-core/src/test/java/org/springframework/cloud/release/internal/buildsystem/GradleBomParserTests.java new file mode 100644 index 00000000..171c19f5 --- /dev/null +++ b/spring-cloud-release-tools-core/src/test/java/org/springframework/cloud/release/internal/buildsystem/GradleBomParserTests.java @@ -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 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); + } + +} \ No newline at end of file