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

Add dependency JRE availability check before setting up the distribution #329

Merged
Merged
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
51 changes: 33 additions & 18 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,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);
Expand All @@ -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"
Expand All @@ -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");
Expand All @@ -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);
udda1996 marked this conversation as resolved.
Show resolved Hide resolved
}
break;
}
Expand All @@ -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));
Expand Down
Loading