Skip to content

Commit

Permalink
Add sorting for local distribution list
Browse files Browse the repository at this point in the history
  • Loading branch information
udda1996 committed Nov 20, 2023
1 parent ac96255 commit 4cc95e0
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/main/java/org/ballerinalang/command/cmd/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ private static void listDistributions(PrintStream outStream, boolean allFlag, bo
JSONObject channelJson = new JSONObject();
JSONArray releases = new JSONArray();
channelJson.put("name", channel.getName());
List<Distribution> channelDistListLocal = channel.getDistributions();
if (!channel.getName().contains("pre-release")) {
channelDistListLocal = getSortedDistList(channelDistListLocal);
}
if (listOfFiles != null) {
for (Distribution distribution : channel.getDistributions()) {
for (Distribution distribution : channelDistListLocal) {
Arrays.sort(listOfFiles);
for (File file : listOfFiles) {
if (file.isDirectory()) {
Expand Down Expand Up @@ -160,13 +164,7 @@ private static void listDistributions(PrintStream outStream, boolean allFlag, bo
outStream.println("\n" + channel.getName() + "\n");
List<Distribution> channelDistList = channel.getDistributions();
if (!channel.getName().contains("pre-release")) {
Comparator<Distribution> semverComparator = Comparator
.comparingInt((Distribution distribution) -> Integer.parseInt(distribution.getVersion().split("\\.")[0]))
.thenComparingInt((Distribution distribution) -> Integer.parseInt(distribution.getVersion().split("\\.")[1]))
.thenComparingInt((Distribution distribution) -> Integer.parseInt(distribution.getVersion().split("\\.")[2]));

channelDistList.sort(semverComparator);
Collections.reverse(channelDistList);
channelDistList = getSortedDistList(channelDistList);
}
if (!allFlag){
if (channelDistList.size() > maxListingDistributions) {
Expand Down Expand Up @@ -310,6 +308,21 @@ private static void listLocalDists(File[] listOfFiles, PrintStream outStream, St
}
}

/**
* Sorts the distributions under a channel according to their semver version.
*
* @param channelDistList The list of distributions under a channel
*/
private static List<Distribution> getSortedDistList(List<Distribution> channelDistList) {
Comparator<Distribution> semverComparator = Comparator
.comparingInt((Distribution distribution) -> Integer.parseInt(distribution.getVersion().split("\\.")[0]))
.thenComparingInt((Distribution distribution) -> Integer.parseInt(distribution.getVersion().split("\\.")[1]))
.thenComparingInt((Distribution distribution) -> Integer.parseInt(distribution.getVersion().split("\\.")[2]));
channelDistList.sort(semverComparator);
Collections.reverse(channelDistList);
return channelDistList;
}

private static boolean isUpdated(File[] listOfFiles) {
try {
if (!new File(OSUtils.getBallerinaDistListFilePath()).exists()) {
Expand Down

0 comments on commit 4cc95e0

Please sign in to comment.