Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include sort issue #352

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 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(ToolUtil.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 @@ -153,15 +157,14 @@ private static void listDistributions(PrintStream outStream, boolean allFlag, bo
writeLocalDistsIntoJson(distList);
outStream.println("\nDistributions available remotely:");
for (Channel channel : channels) {
if (channel.getName().contains("pre-release") && !prFlag) {
if (channel.getName().contains(ToolUtil.PRE_RELEASE) && !prFlag) {
continue;
}
else {
outStream.println("\n" + channel.getName() + "\n");
List<Distribution> channelDistList = channel.getDistributions();
if (channel.getName().equals("Swan Lake channel")) {
channelDistList.sort(Comparator.comparing(Distribution::getVersion));
Collections.reverse(channelDistList);
if (!channel.getName().contains(ToolUtil.PRE_RELEASE)) {
channelDistList = getSortedDistList(channelDistList);
}
if (!allFlag){
if (channelDistList.size() > maxListingDistributions) {
Expand Down Expand Up @@ -305,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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class ToolUtil {
private static final String PRODUCTION_URL = "https://api.central.ballerina.io/2.0/update-tool";
private static final String STAGING_URL = "https://api.staging-central.ballerina.io/2.0/update-tool/";
private static final String DEV_URL = "https://api.dev-central.ballerina.io/2.0/update-tool/";
public static final String PRE_RELEASE = "pre-release";
public static final String CLI_HELP_FILE_PREFIX = "dist-";
private static final String BALLERINA_1_X_VERSIONS = "1.0.";
private static final String CONNECTION_ERROR_MESSAGE = "connection to the remote server failed";
Expand Down
Loading