From 54ac04a6185ab378869aaf8d3754eeeb1b22d906 Mon Sep 17 00:00:00 2001 From: parkavi11 Date: Fri, 23 Oct 2020 14:02:11 +0530 Subject: [PATCH 1/2] Fix dist list to show local distributions --- .../ballerinalang/command/cmd/ListCommand.java | 18 ++++++++---------- .../ballerinalang/command/util/OSUtils.java | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/ballerinalang/command/cmd/ListCommand.java b/src/main/java/org/ballerinalang/command/cmd/ListCommand.java index 0dcbaadb..56e0af9d 100644 --- a/src/main/java/org/ballerinalang/command/cmd/ListCommand.java +++ b/src/main/java/org/ballerinalang/command/cmd/ListCommand.java @@ -27,7 +27,6 @@ import java.io.BufferedReader; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintStream; @@ -37,13 +36,13 @@ import java.util.Arrays; import java.util.List; +import static org.ballerinalang.command.util.OSUtils.getBallerinaDistListFilePath; + /** * This class represents the "Update" command and it holds arguments and flags specified by the user. */ @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 listCommands; @@ -166,10 +165,11 @@ private static String markVersion(String used, String current) { */ private static void writeLocalDistsIntoJson(List installedVersions) throws IOException { FileWriter writer; + String distListPath = 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()); @@ -186,17 +186,15 @@ private static void writeLocalDistsIntoJson(List 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(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()); } } } diff --git a/src/main/java/org/ballerinalang/command/util/OSUtils.java b/src/main/java/org/ballerinalang/command/util/OSUtils.java index 7c86e6cf..88e6c48a 100644 --- a/src/main/java/org/ballerinalang/command/util/OSUtils.java +++ b/src/main/java/org/ballerinalang/command/util/OSUtils.java @@ -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; @@ -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; /** @@ -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"; @@ -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 From 601a75331285c0fa30017bcfbbaa38b71345e08a Mon Sep 17 00:00:00 2001 From: parkavi11 Date: Fri, 23 Oct 2020 14:22:08 +0530 Subject: [PATCH 2/2] Add review suggestion --- .../java/org/ballerinalang/command/cmd/ListCommand.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/ballerinalang/command/cmd/ListCommand.java b/src/main/java/org/ballerinalang/command/cmd/ListCommand.java index 56e0af9d..23af238e 100644 --- a/src/main/java/org/ballerinalang/command/cmd/ListCommand.java +++ b/src/main/java/org/ballerinalang/command/cmd/ListCommand.java @@ -23,6 +23,7 @@ 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; @@ -36,8 +37,6 @@ import java.util.Arrays; import java.util.List; -import static org.ballerinalang.command.util.OSUtils.getBallerinaDistListFilePath; - /** * This class represents the "Update" command and it holds arguments and flags specified by the user. */ @@ -165,7 +164,7 @@ private static String markVersion(String used, String current) { */ private static void writeLocalDistsIntoJson(List installedVersions) throws IOException { FileWriter writer; - String distListPath = getBallerinaDistListFilePath(); + String distListPath = OSUtils.getBallerinaDistListFilePath(); try { writer = new FileWriter(distListPath); } catch (IOException e) { @@ -186,7 +185,7 @@ private static void writeLocalDistsIntoJson(List installedVersions) thro private static void readLocalDistsFromJson(PrintStream outStream, String currentBallerinaVersion) { BufferedReader reader; try { - reader = new BufferedReader(new FileReader(getBallerinaDistListFilePath())); + reader = new BufferedReader(new FileReader(OSUtils.getBallerinaDistListFilePath())); String line; while ((line = reader.readLine()) != null) { outStream.println(markVersion(currentBallerinaVersion, line) + " " + ToolUtil.getTypeName(line));