diff --git a/src/main/java/org/ballerinalang/command/util/ToolUtil.java b/src/main/java/org/ballerinalang/command/util/ToolUtil.java index a2acae65..de8a50b6 100644 --- a/src/main/java/org/ballerinalang/command/util/ToolUtil.java +++ b/src/main/java/org/ballerinalang/command/util/ToolUtil.java @@ -527,13 +527,23 @@ public static boolean downloadDistribution(PrintStream printStream, String distr String newUrl = conn.getHeaderField("Location"); HttpsURLConnection redirectedConn = getServerUrlWithProxyAuthentication(new URL(newUrl)); redirectedConn.setRequestProperty("content-type", "binary/data"); - ToolUtil.downloadAndSetupDist(printStream, redirectedConn, distribution); - ToolUtil.getDependency(printStream, distribution, distributionType, distributionVersion); + ToolUtil.downloadDistributionZip(printStream, redirectedConn, distribution); + String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution, distributionType, distributionVersion); + if (!ToolUtil.checkDependencyAvailable(dependencyForDistribution)) { + downloadDependency(printStream, dependencyForDistribution, distributionType, + distributionVersion); + } + setupDistribution(distribution, dependencyForDistribution); return false; } else if (conn.getResponseCode() == 200) { printStream.println("Fetching the '" + distribution + "' distribution from the remote server..."); - ToolUtil.downloadAndSetupDist(printStream, conn, distribution); - ToolUtil.getDependency(printStream, distribution, distributionType, distributionVersion); + ToolUtil.downloadDistributionZip(printStream, conn, distribution); + String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution, distributionType, distributionVersion); + if (!ToolUtil.checkDependencyAvailable(dependencyForDistribution)) { + downloadDependency(printStream, dependencyForDistribution, distributionType, + distributionVersion); + } + setupDistribution(distribution, dependencyForDistribution); return false; } else { throw ErrorUtil.createDistributionNotFoundException(distribution); @@ -555,12 +565,24 @@ public static boolean downloadDistribution(PrintStream printStream, String distr } } - private static void downloadAndSetupDist(PrintStream printStream, HttpURLConnection conn, + private static void downloadDistributionZip(PrintStream printStream, HttpURLConnection conn, String distribution) { try { - String distPath = getDistributionsPath(); String zipFileLocation = getDistributionsPath() + File.separator + distribution + ".zip"; downloadFile(conn, zipFileLocation, distribution, printStream); + } finally { + conn.disconnect(); + } + } + + public static void setupDistribution (String distribution, String dependency) { + String distPath = getDistributionsPath(); + String zipFileLocation = getDistributionsPath() + File.separator + distribution + ".zip"; + if (!ToolUtil.checkDependencyAvailable(dependency)) { + new File(zipFileLocation).delete(); + throw ErrorUtil.createCommandException("The required dependency '" + dependency + "' is not available locally." + + " Please try reinstalling the distribution."); + } else { unzip(zipFileLocation, distPath); addExecutablePermissionToFile(new File(distPath + File.separator + ToolUtil.getType(distribution) + "-" + distribution + File.separator + "bin" @@ -583,16 +605,14 @@ private static void downloadAndSetupDist(PrintStream printStream, HttpURLConnect } new File(zipFileLocation).delete(); - } finally { - conn.disconnect(); } } - public static void getDependency(PrintStream printStream, String distribution, String distributionType, + public static String getDependency(PrintStream printStream, String distribution, String distributionType, String distributionVersion) { HttpsURLConnection conn = null; + String dependencyName = ""; try { - printStream.println("\nFetching the dependencies for '" + distribution + "' from the remote server..."); URL url = new URL(ToolUtil.getServerURL() + "/distributions"); conn = getServerUrlWithProxyAuthentication(url); conn.setRequestMethod("GET"); @@ -612,14 +632,7 @@ public static void getDependency(PrintStream printStream, String distribution, S Pattern dependencyPattern = Pattern.compile(dependencyRegex); Matcher dependencyMatcher = dependencyPattern.matcher(distInfo); while (dependencyMatcher.find()) { - String dependencyName = dependencyMatcher.group(1); - if (!ToolUtil.checkDependencyAvailable(dependencyName)) { - downloadDependency(printStream, dependencyName, distributionType, - distributionVersion); - } else { - printStream.println("Dependency '" + dependencyName + - "' is already available locally"); - } + dependencyName = dependencyMatcher.group(1); } break; } @@ -636,11 +649,13 @@ public static void getDependency(PrintStream printStream, String distribution, S conn.disconnect(); } } + return dependencyName; } private static void downloadDependency(PrintStream printStream, String dependency, String distributionType, String distributionVersion) { try { + printStream.println("\nFetching the dependencies for '" + distributionVersion + "' from the remote server..."); String encodedDependencyName = encodePlusCharacters(dependency); String url = ToolUtil.getServerURL() + "/dependencies/" + encodedDependencyName; HttpsURLConnection conn = getServerUrlWithProxyAuthentication(new URL(url));