Skip to content

Commit

Permalink
refactoring to submenu
Browse files Browse the repository at this point in the history
and add options for scope MICRO, MINOR and MAJOR
Signed-off-by: Christoph Rueger <chrisrueger@gmail.com>
  • Loading branch information
chrisrueger committed Apr 27, 2024
1 parent 4f05595 commit 6dd1669
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
@@ -1,6 +1,7 @@
package aQute.bnd.repository.maven.provider;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -28,21 +29,15 @@ public Mbr(MavenBndRepository repo) {
this.repo = repo;
}

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

List<Archive> archives = repo.getArchives()
.stream()
.toList();

MultiMap<Archive, MavenVersion> updates = getUpdates(scope, repo, archives, false);

for (Archive archive : new TreeSet<>(repo.getArchives())) {
List<MavenVersion> list = updates.get(archive);
if (list == null || list.isEmpty()) {
if (!onlyUpdates) {
content.put(archive, archive.revision.version);
}
content.put(archive, archive.revision.version);
} else {
MavenVersion version = list.get(list.size() - 1);
// bnd.out.format(" %-70s %20s -> %s%n",
Expand All @@ -66,7 +61,8 @@ enum Scope {
final static Predicate<MavenVersion> notSnapshotlikePredicate = v -> !SNAPSHOTLIKE_P.matcher(v.toString())
.find();

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

Expand Down Expand Up @@ -135,7 +131,7 @@ 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 = buildUpdates(sb, repo, translations);
boolean changes = buildMvnFileString(sb, repo, translations);
if (!changes)
return false;

Expand All @@ -147,7 +143,15 @@ public boolean update(MavenBndRepository repo, Map<Archive, MavenVersion> transl
return changes;
}

public boolean buildUpdates(StringBuilder sb, MavenBndRepository repo, Map<Archive, MavenVersion> translations)
/**
* @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 buildMvnFileString(StringBuilder sb, MavenBndRepository repo, Map<Archive, MavenVersion> translations)
throws IOException {
boolean changes = false;
Iterator<String> lc;
Expand Down
Expand Up @@ -5,6 +5,7 @@
import static java.util.stream.Collectors.maxBy;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -36,31 +37,47 @@ class RepoActions {
Map<String, Runnable> getRepoActions(final Clipboard clipboard) throws Exception {
Map<String, Runnable> map = new LinkedHashMap<>();

map.put("Dry run update to higher MICRO revision to clipboard", () -> {
map.put("Update Revisions :: To higher MICRO revision if available", () -> {
try {

Mbr mbr = new Mbr(repo);
Map<Archive, MavenVersion> content = mbr.updateRevisions(Scope.micro, true);

StringBuilder sb = new StringBuilder();
boolean changes = mbr.buildUpdates(sb, repo, content);
clipboard.copy(sb.toString());

upgradeRevisions(Scope.micro);
} catch (Exception e) {
throw Exceptions.duck(e);
}
});

map.put("Update to higher MICRO revision if available", () -> {
map.put("Update Revisions :: To higher MINOR revision if available", () -> {
try {
upgradeRevisions(Scope.minor);
} catch (Exception e) {
throw Exceptions.duck(e);
}
});
map.put("Update Revisions :: To higher MAJOR revision if available", () -> {
try {
upgradeRevisions(Scope.major);
} catch (Exception e) {
throw Exceptions.duck(e);
}
});

Mbr mbr = new Mbr(repo);
Map<Archive, MavenVersion> content = mbr.updateRevisions(Scope.micro, false);
map.put("Update Revisions :: Dry run to clipboard - Update to higher MICRO revision", () -> {
try {
clipboard.copy(buildMvnFileString(Scope.micro).toString());
} catch (Exception e) {
throw Exceptions.duck(e);
}
});

if (mbr.update(repo, content)) {
repo.refresh();
}
map.put("Update Revisions :: Dry run to clipboard - Update to higher MINOR revision", () -> {
try {
clipboard.copy(buildMvnFileString(Scope.minor).toString());
} catch (Exception e) {
throw Exceptions.duck(e);
}
});

map.put("Update Revisions :: Dry run to clipboard - Update to higher MAJOR revision", () -> {
try {
clipboard.copy(buildMvnFileString(Scope.major).toString());
} catch (Exception e) {
throw Exceptions.duck(e);
}
Expand All @@ -69,7 +86,23 @@ Map<String, Runnable> getRepoActions(final Clipboard clipboard) throws Exception
return map;
}

private StringBuilder buildMvnFileString(Scope scope) throws Exception, IOException {
Mbr mbr = new Mbr(repo);
Map<Archive, MavenVersion> content = mbr.calculateUpdateRevisions(scope, repo.getArchives());

StringBuilder sb = new StringBuilder();
mbr.buildMvnFileString(sb, repo, content);
return sb;
}

private void upgradeRevisions(Scope scope) throws Exception, IOException {
Mbr mbr = new Mbr(repo);
Map<Archive, MavenVersion> content = mbr.calculateUpdateRevisions(scope, repo.getArchives());

if (mbr.update(repo, content)) {
repo.refresh();
}
}

Map<String, Runnable> getProgramActions(final String bsn) throws Exception {
Map<String, Runnable> map = new LinkedHashMap<>();
Expand All @@ -86,8 +119,7 @@ Map<String, Runnable> getProgramActions(final String bsn) throws Exception {
return map;
}

Map<String, Runnable> getRevisionActions(final Archive archive, final Clipboard clipboard)
throws Exception {
Map<String, Runnable> getRevisionActions(final Archive archive, final Clipboard clipboard) throws Exception {
Map<String, Runnable> map = new LinkedHashMap<>();
map.put("Clear from Cache", () -> {
File dir = repo.storage.toLocalFile(archive)
Expand Down Expand Up @@ -141,8 +173,6 @@ Map<String, Runnable> getRevisionActions(final Archive archive, final Clipboard
return map;
}



void addSources(final Archive archive, Map<String, Runnable> map) throws Exception {
Promise<File> pBinary = repo.storage.get(archive);
if (pBinary.getFailure() == null) {
Expand Down Expand Up @@ -258,5 +288,4 @@ private static final void addCopyToClipboardActions(MavenBndRepository repo, fin
});
}


}

0 comments on commit 6dd1669

Please sign in to comment.