-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProjectVersionIncrementor.java
153 lines (136 loc) · 6.91 KB
/
ProjectVersionIncrementor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.exasol.projectkeeper.dependencyupdate;
import static com.exasol.projectkeeper.shared.config.ProjectKeeperModule.JAR_ARTIFACT;
import java.nio.file.Path;
import java.time.*;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import com.exasol.errorreporting.ExaError;
import com.exasol.projectkeeper.Logger;
import com.exasol.projectkeeper.shared.config.*;
import com.exasol.projectkeeper.sources.analyze.generic.*;
import com.exasol.projectkeeper.validators.changesfile.ChangesFile;
import com.exasol.projectkeeper.validators.changesfile.ChangesFileIO;
import com.vdurmont.semver4j.Semver;
/**
* This class can increment the project's version.
*/
// [impl->dsn~dependency-updater.increment-version~2]
class ProjectVersionIncrementor {
private static final ZoneId UTC_ZONE = ZoneId.of("UTC");
private final ProjectKeeperConfig config;
private final String currentProjectVersion;
private final ChangesFileIO changesFileIO;
private final Clock clock;
private final Path projectDir;
private final Logger logger;
private final XmlDocumentIO xmlFileIO;
private final CommandExecutor commandExecutor;
ProjectVersionIncrementor(final ProjectKeeperConfig config, final Logger logger, final Path projectDir,
final String currentProjectVersion) {
this(config, logger, projectDir, currentProjectVersion, new ChangesFileIO(), new XmlDocumentIO(),
new CommandExecutor(), Clock.systemUTC());
}
ProjectVersionIncrementor(final ProjectKeeperConfig config, final Logger logger, final Path projectDir,
final String currentProjectVersion, final ChangesFileIO changesFileIO, final XmlDocumentIO xmlFileIO,
final CommandExecutor commandExecutor, final Clock clock) {
this.config = config;
this.logger = logger;
this.projectDir = projectDir;
this.changesFileIO = changesFileIO;
this.xmlFileIO = xmlFileIO;
this.commandExecutor = commandExecutor;
this.clock = clock;
this.currentProjectVersion = Objects.requireNonNull(currentProjectVersion, "currentProjectVersion");
}
/**
* Check if the current version was released, i.e. has a changelog release date in the past or today.
*
* @return {@code true} if the current version was released (i.e. has a release date) or not (i.e. has no release
* date or a future date)
*/
boolean isCurrentVersionReleased() {
final Path changesFilePath = projectDir.resolve(ChangesFile.getPathForVersion(currentProjectVersion));
final ChangesFile changesFile = changesFileIO.read(changesFilePath);
final Optional<LocalDate> releaseDate = changesFile.getParsedReleaseDate();
if (releaseDate.isEmpty()) {
logger.info("Found invalid date '" + changesFile.getReleaseDate() + "' in changelog " + changesFilePath
+ ": version " + currentProjectVersion + " was not yet released");
return false;
}
final boolean released = releaseDate.get().isBefore(today());
if (released) {
logger.info("Version " + currentProjectVersion + " was released on " + changesFile.getReleaseDate()
+ " according to " + changesFilePath);
}
return released;
}
private LocalDate today() {
return LocalDate.ofInstant(clock.instant(), UTC_ZONE);
}
/**
* Increment the project's patch version. If the project produces a JAR (i.e. uses the {@code JAR_ARTIFACT} module),
* it runs its {@code artifact-reference-checker:unify} goal to update references to the JAR file.
*
* @return the new, incremented version
*/
String incrementProjectVersion() {
final String nextVersion = getIncrementedVersion(currentProjectVersion);
updatePomVersion(nextVersion);
sourcesUsingReferenceCheckerPlugin().forEach(this::updateReferences);
return nextVersion;
}
private void updatePomVersion(final String nextVersion) {
final Path path = getPomPath();
logger.info("Incrementing version from " + currentProjectVersion + " to " + nextVersion + " in POM " + path);
final Document pom = xmlFileIO.read(path);
incrementVersion(path, pom, nextVersion);
xmlFileIO.write(pom, path);
}
private Stream<Source> sourcesUsingReferenceCheckerPlugin() {
return config.getSources().stream().filter(source -> source.getModules().contains(JAR_ARTIFACT));
}
private void updateReferences(final Source source) {
final Path moduleDir = projectDir.resolve(source.getPath()).getParent();
logger.info("Unify artifact references in dir " + moduleDir + "...");
final ShellCommand command = MavenProcessBuilder.create().addArgument("artifact-reference-checker:unify")
.workingDir(moduleDir).timeout(Duration.ofSeconds(30)).buildCommand();
commandExecutor.execute(command);
}
private void incrementVersion(final Path path, final Document pom, final String nextVersion) {
final Optional<Node> versionNode = findVersionNode(pom);
if (versionNode.isEmpty()) {
logger.warn(ExaError.messageBuilder("W-PK-CORE-196")
.message("No version element found in pom file {{pom file path}}.", path)
.mitigation("Please update the version to {{next version}} manually.", nextVersion).toString());
return;
}
if (!this.currentProjectVersion.equals(versionNode.get().getTextContent())) {
throw new IllegalStateException(ExaError.messageBuilder("E-PK-CORE-174").message(
"Inconsistent project version {{version in pom file}} found in pom {{pom file path}}, expected {{expected version}}.",
versionNode.get().getTextContent(), path, currentProjectVersion).ticketMitigation().toString());
}
versionNode.get().setTextContent(nextVersion);
}
private Optional<Node> findVersionNode(final Document pom) {
final Optional<Node> versionNode = xmlFileIO.runXPath(pom, "/project/version");
if (versionNode.isEmpty() || versionNode.get().getTextContent().equals("${revision}")) {
final Optional<Node> revisionElement = xmlFileIO.runXPath(pom, "/project/properties/revision");
logger.info("Version element missing or refers to ${revision}, use revision property " + revisionElement);
return revisionElement;
}
return versionNode;
}
static String getIncrementedVersion(final String version) {
final Semver current = new Semver(version);
return current.nextPatch().toString();
}
private Path getPomPath() {
if (config.getVersionConfig() instanceof final VersionFromSource versionFromSource) {
return versionFromSource.getPathToPom();
}
return projectDir.resolve("pom.xml");
}
}