This commit is contained in:
Greg DiCristofaro 2023-09-05 20:08:21 -04:00
parent c5c6fc78b5
commit cdb9c56467
5 changed files with 94 additions and 26 deletions

View File

@ -81,15 +81,15 @@ public class APIDiff {
Map<String, File> prevJars = getJars(prev);
Map<String, File> currJars = getJars(curr);
Set<String> combined = new HashSet<>(prevJars.keySet());
combined.addAll(currJars.keySet());
List<Pair<File, File>> retMapping = new ArrayList<>();
for (String prevKey : (Iterable<String>) prevJars.keySet().stream().sorted(StringUtils::compareIgnoreCase)::iterator) {
for (String prevKey : (Iterable<String>) combined.stream().sorted(StringUtils::compareIgnoreCase)::iterator) {
File prevFile = prevJars.get(prevKey);
File curFile = currJars.get(prevKey);
if (prevFile != null && curFile != null) {
retMapping.add(Pair.of(prevFile, curFile));
}
retMapping.add(Pair.of(prevFile, curFile));
}
return retMapping;

View File

@ -56,24 +56,43 @@ public class Main {
for (Pair<File, File> prevCurJars : APIDiff.getCommonJars(cliArgs.getPreviousVersPath(), cliArgs.getCurrentVersPath())) {
try {
ModuleVersionNumbers prevVersionNums = ModuleUpdates.getVersionsFromJar(prevCurJars.getLeft());
ComparisonRecord record = APIDiff.getComparison(
cliArgs.getPreviousVersion(),
cliArgs.getCurrentVersion(),
prevCurJars.getLeft(),
prevCurJars.getRight());
ModuleVersionNumbers projectedVersionNums = ModuleUpdates.getModuleVersionUpdate(prevVersionNums, record.getChangeType());
File previous = prevCurJars.getLeft();
File current = prevCurJars.getRight();
String jarFileName;
if (prevCurJars.getLeft().getName().equalsIgnoreCase(prevCurJars.getRight().getName())) {
jarFileName = prevCurJars.getLeft().getName();
// if no current, then we can't update; just continue
if (current == null || !current.exists()) {
if (previous != null) {
LOGGER.log(Level.WARNING, "No matching current jar found for previous jar: " + previous);
}
continue;
}
String jarFileName = current.getName();
ModuleVersionNumbers projectedVersionNums;
if (previous == null || !previous.exists()) {
projectedVersionNums = ModuleVersionNumbers.getNewModule(current.getName());
outputNewModule(jarFileName, projectedVersionNums);
} else {
jarFileName = MessageFormat.format("[previous: {0}, current: {1}]", prevCurJars.getLeft().getName(), prevCurJars.getRight().getName());
ModuleVersionNumbers prevVersionNums = ModuleUpdates.getVersionsFromJar(prevCurJars.getLeft());
ComparisonRecord record = APIDiff.getComparison(
cliArgs.getPreviousVersion(),
cliArgs.getCurrentVersion(),
prevCurJars.getLeft(),
prevCurJars.getRight());
projectedVersionNums = ModuleUpdates.getModuleVersionUpdate(prevVersionNums, record.getChangeType());
outputDiff(jarFileName, record, prevVersionNums, projectedVersionNums);
}
if (previous.getName().equalsIgnoreCase(current.getName())) {
jarFileName = previous.getName();
} else {
jarFileName = MessageFormat.format("[previous: {0}, current: {1}]", previous.getName(), current.getName());
}
outputDiff(jarFileName, record, prevVersionNums, projectedVersionNums);
newVersionNumMapping.put(projectedVersionNums.getRelease().getModuleName(), projectedVersionNums);
} catch (IOException ex) {
@ -135,4 +154,35 @@ public class Main {
record.getHumanReadableApiChange()
));
}
/**
* Outputs new jar file name.
*
* @param jarFileName The jar file name.
* @param projectedVersionNums The calculated version numbers for current
* version.
*/
private static void outputNewModule(
String jarFileName,
ModuleVersionNumbers projectedVersionNums
) {
LOGGER.log(Level.INFO, MessageFormat.format("""
====================================
NEW MODULE: {0}
Current Version Numbers:
- release: {1}
- specification: {2}
- implementation: {3}
====================================
""",
jarFileName,
projectedVersionNums.getRelease().getFullReleaseStr(),
projectedVersionNums.getSpec().getSemVerStr(),
projectedVersionNums.getImplementation()
));
}
}

View File

@ -187,7 +187,7 @@ public class ModuleUpdates {
* @return The module version numbers.
* @throws IOException
*/
public static ModuleVersionNumbers getVersionsFromJar(File jarFile) throws IOException {
public static ModuleVersionNumbers getVersionsFromJar(File jarFile) throws IOException {
Attributes manifest = ManifestLoader.loadFromJar(jarFile);
String spec = manifest.getValue(MANIFEST_SPEC_KEY);
SemVer specSemVer = parseSemVer(spec, DEFAULT_SEMVER,
@ -220,6 +220,14 @@ public class ModuleUpdates {
public static ModuleVersionNumbers getModuleVersionUpdate(ModuleVersionNumbers prev, PublicApiChangeType apiChangeType) {
switch (apiChangeType) {
case NONE -> {
return new ModuleVersionNumbers(
prev.getModuleName(),
prev.getSpec(),
prev.getImplementation(),
prev.getRelease()
);
}
case INTERNAL_CHANGE -> {
return new ModuleVersionNumbers(
prev.getModuleName(),
prev.getSpec(),
@ -236,10 +244,7 @@ public class ModuleUpdates {
prev.getSpec().getPatch()
),
prev.getImplementation() + 1,
new ReleaseVal(
prev.getRelease().getModuleName(),
prev.getRelease().getReleaseVersion()
)
prev.getRelease()
);
}
case INCOMPATIBLE_CHANGE -> {
@ -247,8 +252,8 @@ public class ModuleUpdates {
prev.getModuleName(),
new SemVer(
prev.getSpec().getMajor() + 1,
prev.getSpec().getMinor(),
prev.getSpec().getPatch()
0,
null
),
prev.getImplementation() + 1,
new ReleaseVal(
@ -584,6 +589,19 @@ public class ModuleUpdates {
* Module version numbers record.
*/
public static class ModuleVersionNumbers {
/**
* Returns ModuleVersionNumbers for a brand new module.
* @param moduleName The module name.
* @return The module version numbers
*/
public static ModuleVersionNumbers getNewModule(String moduleName) {
return new ModuleVersionNumbers(
moduleName,
new SemVer(1, 0, null),
1,
new ReleaseVal(moduleName.replaceAll("-", "."), 1));
}
private final String moduleName;
private final SemVer spec;

View File

@ -25,7 +25,7 @@ import org.apache.commons.lang3.ObjectUtils;
* A public API change type (no change, compatible change, incompatible change).
*/
public enum PublicApiChangeType implements Comparator<PublicApiChangeType> {
NONE(0), COMPATIBLE_CHANGE(1), INCOMPATIBLE_CHANGE(2);
NONE(0), INTERNAL_CHANGE(1), COMPATIBLE_CHANGE(2), INCOMPATIBLE_CHANGE(3);
private final int level;