Skip to content

Commit

Permalink
change output format of Dry-Run
Browse files Browse the repository at this point in the history
now it looks like MbrCommand#_check which is a string table showing the current and new version - basically a preview
e.g.
## Updates available
com.fasterxml.jackson.core:jackson-core:2.16.1                        2.16.2

Signed-off-by: Christoph Rueger <chrisrueger@gmail.com>
  • Loading branch information
chrisrueger committed Apr 28, 2024
1 parent f056175 commit 070c9f4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 73 deletions.
152 changes: 88 additions & 64 deletions biz.aQute.repository/src/aQute/bnd/repository/maven/provider/Mbr.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import aQute.bnd.version.Version;
import aQute.lib.collections.MultiMap;
import aQute.lib.io.IO;
import aQute.lib.justif.Justif;
import aQute.maven.api.Archive;

/**
Expand All @@ -29,7 +30,8 @@ public Mbr(MavenBndRepository repo) {
this.repo = repo;
}

public Map<Archive, MavenVersion> calculateUpdateRevisions(Scope scope, Collection<Archive> archives) throws Exception {
public Map<Archive, MavenVersion> calculateUpdateRevisions(Scope scope, Collection<Archive> archives)
throws Exception {
Map<Archive, MavenVersion> content = new HashMap<>();

MultiMap<Archive, MavenVersion> updates = getUpdates(scope, repo, archives, false);
Expand Down Expand Up @@ -61,9 +63,92 @@ enum Scope {
final static Predicate<MavenVersion> notSnapshotlikePredicate = v -> !SNAPSHOTLIKE_P.matcher(v.toString())
.find();

public boolean update(MavenBndRepository repo, Map<Archive, MavenVersion> translations) throws IOException {
StringBuilder sb = new StringBuilder();
boolean changes = buildGAVString(sb, repo, translations);
if (!changes)
return false;

repo.getIndexFile()
.getParentFile()
.mkdirs();
// bnd.trace("writing %s", repo.getIndexFile());
IO.store(sb.toString(), repo.getIndexFile());
return changes;
}

/**
* @param sb contains a list of Maven GAVs like in a central.mvn file
* @param repo
* @param translations
* @return <code>true</code> when there were changes / updates, otherwise
* <code>false</code>
* @throws IOException
*/
public boolean buildGAVString(StringBuilder sb, MavenBndRepository repo, Map<Archive, MavenVersion> translations)
throws IOException {
boolean changes = false;
Iterator<String> lc;
if (repo.getIndexFile()
.isFile()) {
lc = IO.reader(repo.getIndexFile())
.lines()
.iterator();
// bnd.trace("reading %s", repo.getIndexFile());
} else {
lc = Collections.emptyIterator();
}

for (Iterator<String> i = lc; i.hasNext();) {
String line = i.next()
.trim();
if (!line.startsWith("#") && !line.isEmpty()) {

Archive archive = Archive.valueOf(line);
if (archive != null) {
MavenVersion version = translations.get(archive);
if (version != null) {
if (!archive.revision.version.equals(version)) {
Archive updated = archive.update(version);
sb.append(updated)
.append("\n");
changes = true;
continue;
}
}
}
}
sb.append(line)
.append("\n");
}
return changes;
}

/**
* For each archive in the index, show the available higher versions. The
* result is similar to MbrCommand#_check
*
* @param scope
* @param archives
* @return a formatted string with the available updates for each archive.
* @throws Exception
*/
public String preview(Scope scope, Collection<Archive> archives) throws Exception {
MultiMap<Archive, MavenVersion> overlap = getUpdates(scope, repo, archives, false);

return format(overlap);
}

private String format(MultiMap<Archive, MavenVersion> overlap) {
Justif j = new Justif(140, 50, 60, 70, 80, 90, 100, 110);
j.formatter()
.format("%n## %60s%n", "Updates available");
j.table(overlap, "");
return j.wrap();
}

private MultiMap<Archive, MavenVersion> getUpdates(Scope scope, MavenBndRepository repo,
Collection<Archive> archives,
boolean snapshotlike) throws Exception {
Collection<Archive> archives, boolean snapshotlike) throws Exception {
MultiMap<Archive, MavenVersion> overlap = new MultiMap<>();

for (Archive archive : archives) {
Expand Down Expand Up @@ -128,65 +213,4 @@ private List<MavenVersion> filter(List<MavenVersion> versions, Version current,

}
}

public boolean update(MavenBndRepository repo, Map<Archive, MavenVersion> translations) throws IOException {
StringBuilder sb = new StringBuilder();
boolean changes = buildGAVString(sb, repo, translations);
if (!changes)
return false;

repo.getIndexFile()
.getParentFile()
.mkdirs();
// bnd.trace("writing %s", repo.getIndexFile());
IO.store(sb.toString(), repo.getIndexFile());
return changes;
}

/**
* @param sb contains a list of Maven GAVs like in a central.mvn file
* @param repo
* @param translations
* @return <code>true</code> when there were changes / updates, otherwise
* <code>false</code>
* @throws IOException
*/
public boolean buildGAVString(StringBuilder sb, MavenBndRepository repo, Map<Archive, MavenVersion> translations)
throws IOException {
boolean changes = false;
Iterator<String> lc;
if (repo.getIndexFile()
.isFile()) {
lc = IO.reader(repo.getIndexFile())
.lines()
.iterator();
// bnd.trace("reading %s", repo.getIndexFile());
} else {
lc = Collections.emptyIterator();
}

for (Iterator<String> i = lc; i.hasNext();) {
String line = i.next()
.trim();
if (!line.startsWith("#") && !line.isEmpty()) {

Archive archive = Archive.valueOf(line);
if (archive != null) {
MavenVersion version = translations.get(archive);
if (version != null) {
if (!archive.revision.version.equals(version)) {
Archive updated = archive.update(version);
sb.append(updated)
.append("\n");
changes = true;
continue;
}
}
}
}
sb.append(line)
.append("\n");
}
return changes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ Map<String, Runnable> getRepoActions(final Clipboard clipboard) throws Exception
});

map.put("Update Revisions :: Dry run to clipboard - Update to higher MICRO revision", () -> {
clipboard.copy(buildMvnFileString(micro));
clipboard.copy(preview(micro));
});
map.put("Update Revisions :: Dry run to clipboard - Update to higher MINOR revision", () -> {
clipboard.copy(buildMvnFileString(minor));
clipboard.copy(preview(minor));
});
map.put("Update Revisions :: Dry run to clipboard - Update to higher MAJOR revision", () -> {
clipboard.copy(buildMvnFileString(major));
clipboard.copy(preview(major));
});

return map;
Expand Down Expand Up @@ -247,14 +247,10 @@ private static final void addCopyToClipboardActions(MavenBndRepository repo, fin
});
}

private String buildMvnFileString(Scope scope) {
private String preview(Scope scope) {
try {
Mbr mbr = new Mbr(repo);
Map<Archive, MavenVersion> content = mbr.calculateUpdateRevisions(scope, repo.getArchives());

StringBuilder sb = new StringBuilder();
mbr.buildGAVString(sb, repo, content);
return sb.toString();
return mbr.preview(scope, repo.getArchives());

} catch (Exception e) {
throw Exceptions.duck(e);
Expand Down

0 comments on commit 070c9f4

Please sign in to comment.