Skip to content

Commit

Permalink
Add dependency availability check before setting up the distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
udda1996 committed Oct 25, 2023
1 parent b838c8e commit 8293788
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,29 @@ 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);
} else {
printStream.println("Dependency '" + dependencyForDistribution +
"' is already available locally");
}
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);
} else {
printStream.println("Dependency '" + dependencyForDistribution +
"' is already available locally");
}
setupDistribution(distribution, dependencyForDistribution);
return false;
} else {
throw ErrorUtil.createDistributionNotFoundException(distribution);
Expand All @@ -555,12 +571,23 @@ 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("dependency '" + dependency + "' is not available locally");
} else {
unzip(zipFileLocation, distPath);
addExecutablePermissionToFile(new File(distPath + File.separator + ToolUtil.getType(distribution)
+ "-" + distribution + File.separator + "bin"
Expand All @@ -583,14 +610,13 @@ 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");
Expand All @@ -612,14 +638,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;
}
Expand All @@ -636,6 +655,7 @@ public static void getDependency(PrintStream printStream, String distribution, S
conn.disconnect();
}
}
return dependencyName;
}

private static void downloadDependency(PrintStream printStream, String dependency, String distributionType,
Expand Down

0 comments on commit 8293788

Please sign in to comment.