Skip to content

Commit 9676a06

Browse files
authored
Merge pull request #105 from philippart-s/write-next-version-gradle
feat: Write next version gradle
2 parents 294a2d8 + 0b45c87 commit 9676a06

File tree

3 files changed

+194
-3
lines changed

3 files changed

+194
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ The optional parameter `writeVersion` allow writing back to the file the next ca
134134
The supported configurations files :
135135
- pom.xml (Maven) : need the Maven CLI in the path,
136136
- package.json (NPM) : need the Npm CLI in the path,
137-
- chart.yaml (Helm).
137+
- chart.yaml (Helm),
138+
- build.gradle / gradle.properties (Gradle).
138139

139140
Example of use :
140141
With a project with a package.json as follows :

src/main/java/io/jenkins/plugins/conventionalcommits/utils/GradleProjectType.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
import com.github.zafarkhaja.semver.Version;
44
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
5+
import java.io.BufferedReader;
56
import java.io.File;
7+
import java.io.FileWriter;
68
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
11+
import java.nio.file.StandardCopyOption;
712
import java.util.Arrays;
813
import java.util.List;
14+
import java.util.Objects;
915

10-
/** Represent a Gradle project type (i.e with a build.gradle file). */
16+
/**
17+
* Represent a Gradle project type (i.e with a build.gradle file).
18+
*/
1119
public class GradleProjectType extends ProjectType {
1220

1321
public boolean check(File directory) {
@@ -41,7 +49,53 @@ public Version getCurrentVersion(File directory, ProcessHelper processHelper)
4149
return Version.valueOf(version);
4250
}
4351

52+
/**
53+
* Write the new calculated version in the build.gradle file.
54+
* If no version property is found, do nothing.
55+
*
56+
* @param directory The directory where find the build.gradle. <b>Mandatory</b>
57+
* @param nextVersion The next version to write. <b>Mandatory</b>
58+
* @param processHelper Not used.
59+
* @throws IOException If an error occurs when accessing to the build.gradle
60+
* @throws InterruptedException Not used.
61+
*/
4462
@Override
4563
public void writeVersion(File directory, Version nextVersion, ProcessHelper processHelper)
46-
throws IOException, InterruptedException {}
64+
throws IOException, InterruptedException {
65+
Objects.requireNonNull(directory);
66+
Objects.requireNonNull(nextVersion);
67+
68+
// Line to read
69+
String line;
70+
// Flag to know if a version tag is in the build.gradle
71+
boolean isVersionTag = false;
72+
// Absolute path to the build.temp file
73+
String buildTempPath =
74+
String.format("%s%sgradle.temp", directory.getAbsolutePath(), File.separator);
75+
// Absolute path to the build.gradle file
76+
String buildPath =
77+
String.format("%s%sgradle.properties", directory.getAbsolutePath(), File.separator);
78+
79+
try (BufferedReader reader = Files.newBufferedReader(Paths.get(buildPath))) {
80+
try (FileWriter fw = new FileWriter(buildTempPath)) {
81+
while ((line = reader.readLine()) != null) {
82+
if (line.contains("version")) {
83+
fw.write(String.format("version = %s%n", nextVersion));
84+
isVersionTag = true;
85+
} else {
86+
fw.write(String.format("%s%n", line));
87+
}
88+
}
89+
}
90+
}
91+
92+
if (isVersionTag) {
93+
// Replace gradle.properties with updated version
94+
Files.move(Paths.get(buildTempPath), Paths.get(buildPath),
95+
StandardCopyOption.REPLACE_EXISTING);
96+
} else {
97+
Files.deleteIfExists(Paths.get(buildPath));
98+
throw new IOException("Unable to get version in gradle.properties");
99+
}
100+
}
47101
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package io.jenkins.plugins.conventionalcommits.utils;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.core.StringContains.containsString;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.Mockito.when;
8+
9+
import com.github.zafarkhaja.semver.Version;
10+
import io.jenkins.plugins.conventionalcommits.process.DefaultProcessHelper;
11+
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
12+
import java.io.File;
13+
import java.io.FileWriter;
14+
import java.io.IOException;
15+
import java.nio.file.Files;
16+
import java.nio.file.Paths;
17+
import org.junit.Rule;
18+
import org.junit.Test;
19+
import org.junit.rules.TemporaryFolder;
20+
import org.junit.runner.RunWith;
21+
import org.mockito.Mock;
22+
import org.mockito.junit.MockitoJUnitRunner;
23+
24+
@RunWith(MockitoJUnitRunner.class)
25+
public class GradleProjectTypeTest {
26+
@Mock
27+
private ProcessHelper mockProcessHelper;
28+
29+
@Rule
30+
final public TemporaryFolder rootFolder = new TemporaryFolder();
31+
32+
final private String buildGradleWithVersionContent =
33+
"foo = foo value\n" +
34+
"version = 1.0.0\n" +
35+
"bar = bar value";
36+
final private String buildGradleWithoutVersionContent =
37+
"foo = foo value\n" +
38+
"bar = bar value";
39+
40+
private void createBuildGradleFiles(File gradleDir, String content) throws Exception {
41+
rootFolder.newFile(gradleDir.getName() + File.separator + "build.gradle");
42+
File buildGradle = rootFolder.newFile(gradleDir.getName() + File.separator + "gradle.properties");
43+
FileWriter gradleWriter = new FileWriter(buildGradle);
44+
gradleWriter.write(content);
45+
gradleWriter.close();
46+
47+
}
48+
49+
@Test
50+
public void shouldCheckGradleProjectOk() throws Exception {
51+
// Given a directory with a build.gradle
52+
File gradleDir = rootFolder.newFolder("SampleGradleProject");
53+
createBuildGradleFiles(gradleDir, buildGradleWithVersionContent);
54+
55+
// When asking if it's a gradle project
56+
GradleProjectType gradleProjectType = new GradleProjectType();
57+
boolean isGradleProject = gradleProjectType.check(gradleDir);
58+
59+
// Then answer true
60+
assertThat(isGradleProject, equalTo(true));
61+
}
62+
63+
@Test
64+
public void shouldCheckGradleProjectKo() throws Exception {
65+
// Given a directory with a build.gradle
66+
File gradleDir = rootFolder.newFolder("SampleFooProject");
67+
68+
// When asking if it's a gradle project
69+
GradleProjectType gradleProjectType = new GradleProjectType();
70+
boolean isGradleProject = gradleProjectType.check(gradleDir);
71+
72+
// Then answer true
73+
assertThat(isGradleProject, equalTo(false));
74+
}
75+
76+
@Test
77+
public void shouldGetCurrentVersion() throws Exception {
78+
// Given a gradle project in 1.0.0 version
79+
File gradleDir = rootFolder.newFolder("SampleGradleProject");
80+
createBuildGradleFiles(gradleDir, buildGradleWithVersionContent);
81+
// Set mock for npm version command
82+
when(mockProcessHelper.runProcessBuilder(any(), any())).thenReturn("foo: foo\nversion: 1.0.0\nbar: bar");
83+
84+
// When asking to get the current version
85+
GradleProjectType gradleProjectType = new GradleProjectType();
86+
Version version = gradleProjectType.getCurrentVersion(gradleDir, mockProcessHelper);
87+
88+
// Then answer 1.0.0
89+
assertThat(version, equalTo(Version.valueOf("1.0.0")));
90+
}
91+
92+
@Test
93+
public void shouldWriteNextVersionToFile() throws Exception {
94+
// Given a directory with a gradle.build file
95+
File gradleDir = rootFolder.newFolder("SampleGradleProject");
96+
createBuildGradleFiles(gradleDir, buildGradleWithVersionContent);
97+
98+
// When : write next version tp the file
99+
GradleProjectType gradleProjectType = new GradleProjectType();
100+
gradleProjectType.writeVersion(gradleDir, Version.valueOf("1.1.0"), null);
101+
102+
// Then : the file is updated
103+
String buildGradleExpected =
104+
"foo = foo value\n" +
105+
"version = 1.1.0\n" +
106+
"bar = bar value";
107+
assertThat(new String(
108+
Files.readAllBytes(Paths.get(gradleDir.getPath() + File.separator + "gradle.properties"))),
109+
containsString(buildGradleExpected));
110+
}
111+
112+
@Test(expected = IOException.class)
113+
public void shouldThrowIOExceptionIfNoBuildFile() throws Exception {
114+
// Given : a project without a build.file
115+
File gradleDir = rootFolder.newFolder("SampleGradleProject");
116+
117+
// When : ask to write next version in file
118+
GradleProjectType gradleProjectType = new GradleProjectType();
119+
gradleProjectType.writeVersion(gradleDir, Version.valueOf("1.1.0"), null);
120+
121+
// Then : IOException is thrown
122+
}
123+
124+
@Test(expected = IOException.class)
125+
public void shouldThrowIOExceptionIfNoVersionTag() throws Exception {
126+
// Given a directory with a gradle.build file
127+
File gradleDir = rootFolder.newFolder("SampleGradleProject");
128+
createBuildGradleFiles(gradleDir, buildGradleWithoutVersionContent);
129+
130+
// When : write next version tp the file
131+
GradleProjectType gradleProjectType = new GradleProjectType();
132+
gradleProjectType.writeVersion(gradleDir, Version.valueOf("1.1.0"), null);
133+
134+
// Then : IOException is thrown
135+
}
136+
}

0 commit comments

Comments
 (0)