Skip to content

Commit

Permalink
Merge pull request #48 from parkavi11/sortListCmd
Browse files Browse the repository at this point in the history
Fix dist list command to show local distributions
  • Loading branch information
keizer619 authored Oct 23, 2020
2 parents 8bb3a61 + 601a753 commit de8a329
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
17 changes: 7 additions & 10 deletions src/main/java/org/ballerinalang/command/cmd/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import org.ballerinalang.command.util.Channel;
import org.ballerinalang.command.util.Distribution;
import org.ballerinalang.command.util.ErrorUtil;
import org.ballerinalang.command.util.OSUtils;
import org.ballerinalang.command.util.ToolUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintStream;
Expand All @@ -42,8 +42,6 @@
*/
@CommandLine.Command(name = "list", description = "List Ballerina Distributions")
public class ListCommand extends Command implements BCommand {
private static final String LOCAL_DISTRIBUTIONS_FILE = "local-dists.json";

@CommandLine.Parameters(description = "Command name")
private List<String> listCommands;

Expand Down Expand Up @@ -166,10 +164,11 @@ private static String markVersion(String used, String current) {
*/
private static void writeLocalDistsIntoJson(List<String> installedVersions) throws IOException {
FileWriter writer;
String distListPath = OSUtils.getBallerinaDistListFilePath();
try {
writer = new FileWriter(LOCAL_DISTRIBUTIONS_FILE);
writer = new FileWriter(distListPath);
} catch (IOException e) {
throw ErrorUtil.createCommandException("cannot write into " + LOCAL_DISTRIBUTIONS_FILE + " file.");
throw ErrorUtil.createCommandException("cannot write into " + distListPath + " file: " + e.getMessage());
}
for(String str: installedVersions) {
writer.write(str + System.lineSeparator());
Expand All @@ -186,17 +185,15 @@ private static void writeLocalDistsIntoJson(List<String> installedVersions) thro
private static void readLocalDistsFromJson(PrintStream outStream, String currentBallerinaVersion) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(LOCAL_DISTRIBUTIONS_FILE));
reader = new BufferedReader(new FileReader(OSUtils.getBallerinaDistListFilePath()));
String line;
while ((line = reader.readLine()) != null) {
outStream.println(markVersion(currentBallerinaVersion, line) + " " + ToolUtil.getTypeName(line));
}
outStream.println("\n");
reader.close();
} catch (FileNotFoundException fileNotFoundException) {
throw ErrorUtil.createCommandException("cannot find " + LOCAL_DISTRIBUTIONS_FILE + " file.");
} catch (IOException ioException) {
throw ErrorUtil.createCommandException("cannot read " + LOCAL_DISTRIBUTIONS_FILE + " file.");
} catch (IOException e) {
throw ErrorUtil.createCommandException("failed to read the file: " + e.getMessage());
}
}
}
16 changes: 14 additions & 2 deletions src/main/java/org/ballerinalang/command/util/OSUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URISyntaxException;
Expand All @@ -29,7 +28,6 @@
import java.time.LocalDate;
import java.util.Comparator;
import java.util.Locale;
import java.util.Properties;
import java.util.stream.Collectors;

/**
Expand All @@ -40,6 +38,7 @@ public class OSUtils {
private static final String OS = System.getProperty("os.name").toLowerCase(Locale.getDefault());
private static final String BALLERINA_HOME_DIR = ".ballerina";
private static final String BALLERINA_CONFIG = "ballerina-version";
public static final String BALLERINA_LIST_JSON = "local-dists.json";
private static final String UPDATE_NOTICE = "command-notice";
private static final String BIR_CACHE = "bir_cache";
private static final String JAR_CACHE = "jar_cache";
Expand Down Expand Up @@ -99,6 +98,19 @@ public static String getBallerinaVersionFilePath() throws IOException {
+ BALLERINA_HOME_DIR + File.separator + BALLERINA_CONFIG;
}

public static String getBallerinaDistListFilePath() throws IOException {
String userHome = getUserHome();
File file = new File(userHome + File.separator
+ BALLERINA_HOME_DIR + File.separator + BALLERINA_LIST_JSON);

if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
return getUserHome() + File.separator
+ BALLERINA_HOME_DIR + File.separator + BALLERINA_LIST_JSON;
}

/**
* Provide configuration file path of the installation.
* @return path to the file
Expand Down

0 comments on commit de8a329

Please sign in to comment.