From 4cc95e04f3ce25be91df820e10dd4b04d4111ce9 Mon Sep 17 00:00:00 2001 From: Charuka Tharindu Date: Mon, 20 Nov 2023 16:25:08 +0530 Subject: [PATCH] Add sorting for local distribution list --- .../command/cmd/ListCommand.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/ballerinalang/command/cmd/ListCommand.java b/src/main/java/org/ballerinalang/command/cmd/ListCommand.java index b15355e5..8ce3c76a 100644 --- a/src/main/java/org/ballerinalang/command/cmd/ListCommand.java +++ b/src/main/java/org/ballerinalang/command/cmd/ListCommand.java @@ -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 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()) { @@ -160,13 +164,7 @@ private static void listDistributions(PrintStream outStream, boolean allFlag, bo outStream.println("\n" + channel.getName() + "\n"); List channelDistList = channel.getDistributions(); if (!channel.getName().contains("pre-release")) { - Comparator 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) { @@ -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 getSortedDistList(List channelDistList) { + Comparator 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()) {