Skip to content

Commit

Permalink
Merge pull request #302 from udda1996/prompt-tool-update
Browse files Browse the repository at this point in the history
Prompt user to update the tool based on compatibility
  • Loading branch information
udda1996 authored Jul 13, 2023
2 parents f01986c + ba950e3 commit a7b5d0c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 29 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ jreVersion=11.0.18+10
# For test purpose
swan-lake-version=2201.2.0
swan-lake-spec-version=2022R3
swan-lake-latest-version=2201.5.0
swan-lake-latest-spec-version=2022R4
swan-lake-latest-version=2201.7.0
swan-lake-latest-spec-version=2023R1
1-x-channel-version=1.2.0
1-x-channel-spec-version=2020R1
1-x-channel-latest-version=1.2.38
1-x-channel-latest-version=1.2.40
6 changes: 5 additions & 1 deletion src/main/java/org/ballerinalang/command/cmd/PullCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.ballerinalang.command.util.Channel;
import org.ballerinalang.command.util.Distribution;
import org.ballerinalang.command.util.ErrorUtil;
import org.ballerinalang.command.util.Tool;
import org.ballerinalang.command.util.ToolUtil;
import picocli.CommandLine;

Expand Down Expand Up @@ -68,7 +69,10 @@ public void execute() {

if (!testFlag) {
// Check and update the tool if any latest version available
ToolUtil.updateTool(printStream);
Tool toolDetails = ToolUtil.updateTool(printStream);
if (!toolDetails.getCompatibility().equals("true")) {
return;
}
}

// To handle bal dist pull latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.ballerinalang.command.BallerinaCliCommands;
import org.ballerinalang.command.util.ErrorUtil;
import org.ballerinalang.command.util.Tool;
import org.ballerinalang.command.util.ToolUtil;
import picocli.CommandLine;

Expand Down Expand Up @@ -85,7 +86,10 @@ public void setParentCmdParser(CommandLine parentCmdParser) {
public static void update(PrintStream printStream) {
if (!testFlag) {
// Check and update the tool if any latest version available
ToolUtil.updateTool(printStream);
Tool toolDetails = ToolUtil.updateTool(printStream);
if (!toolDetails.getCompatibility().equals("true")) {
return;
}
}
String version = ToolUtil.getCurrentBallerinaVersion();
String distVersion = ToolUtil.getType(version) + "-" + version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.ballerinalang.command.BallerinaCliCommands;
import org.ballerinalang.command.util.ErrorUtil;
import org.ballerinalang.command.util.Tool;
import org.ballerinalang.command.util.ToolUtil;
import picocli.CommandLine;

Expand Down Expand Up @@ -84,7 +85,8 @@ public void setParentCmdParser(CommandLine parentCmdParser) {
private static void updateCommands(PrintStream printStream) {
String version = ToolUtil.getCurrentToolsVersion();
printStream.println("Fetching the latest update tool version from the remote server...");
String latestVersion = ToolUtil.getLatestToolVersion();
Tool latestVersionDetails = ToolUtil.getLatestToolVersion();
String latestVersion = latestVersionDetails.getVersion();
if (latestVersion == null) {
printStream.println("Failed to find the latest update tool version");
return;
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/ballerinalang/command/util/Tool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ballerinalang.command.util;

public class Tool {
private String version;
private String compatibility;

public Tool() {
this.version = "";
this.compatibility = "";
}

public Tool(String version, String compatibility) {
this.version = version;
this.compatibility = compatibility;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getCompatibility() {
return compatibility;
}

public void setCompatibility(String compatibility) {
this.compatibility = compatibility;
}
}
64 changes: 42 additions & 22 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private static String getValue(String key, String json) {
return null;
}

public static String getLatestToolVersion() {
public static Tool getLatestToolVersion() {
HttpURLConnection conn = null;
try {
URL url = new URL(getServerURL() + "/versions/latest");
Expand All @@ -307,15 +307,19 @@ public static String getLatestToolVersion() {
getCurrentToolsVersion(), "jballerina"));
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() == 200) {
Tool tool = new Tool();
String json = convertStreamToString(conn.getInputStream());
Pattern p = Pattern.compile("\"version\":\"(.*?)\"");
Matcher matcher = p.matcher(json);
if (matcher.find()) {
conn.disconnect();
return matcher.group(1);
} else {
return null;
Matcher matcher = Pattern.compile("\"version\":\"(.*?)\"").matcher(json);
while (matcher.find()) {
tool.setVersion(matcher.group(1));
}

matcher = Pattern.compile("\"compatibility\":(true|false)").matcher(json);
while (matcher.find()) {
tool.setCompatibility(matcher.group(1));
}
conn.disconnect();
return tool;
}
if (conn.getResponseCode() == 404) {
return null;
Expand Down Expand Up @@ -851,29 +855,45 @@ private static void addExecutablePermissionToDirectory(String filePath) {
*
* @param printStream stream which messages should be printed
*/
public static void updateTool(PrintStream printStream) {
public static Tool updateTool(PrintStream printStream) {
String version = ToolUtil.getCurrentToolsVersion();
Tool toolDetails = new Tool();
printStream.println("Checking for newer versions of the update tool...");
String latestVersion = ToolUtil.getLatestToolVersion();
Tool latestVersionInfo = ToolUtil.getLatestToolVersion();
String latestVersion = latestVersionInfo == null ? null : latestVersionInfo.getVersion();
String backwardCompatibility = latestVersionInfo == null ? null : latestVersionInfo.getCompatibility();
toolDetails.setVersion(latestVersion);
toolDetails.setCompatibility(backwardCompatibility);
if (latestVersion == null) {
printStream.println("Failed to find the latest update tool version");
} else if (!latestVersion.equals(version)) {
ToolUtil.handleInstallDirPermission();
ToolUtil.downloadTool(printStream, latestVersion);
try {
executeFile(printStream);
printStream.println("Update successfully completed");
} catch (IOException | InterruptedException e) {
printStream.println("Update failed due to errors");
} finally {
if (backwardCompatibility == null) {
printStream.println("Failed to find the compatibility of the latest update tool version");
} else if (!backwardCompatibility.equals("true")) {
printStream.println();
printStream.println("ERROR: Outdated Ballerina update tool version found");
printStream.println("Use the following command to update the Ballerina update tool");
printStream.println(" bal update");
printStream.println();
} else {
ToolUtil.handleInstallDirPermission();
ToolUtil.downloadTool(printStream, latestVersion);
try {
OSUtils.deleteFiles(Paths.get(getToolUnzipLocation()));
} catch (IOException e) {
printStream.println("Error occurred while removing files");
executeFile(printStream);
printStream.println("Update successfully completed");
} catch (IOException | InterruptedException e) {
printStream.println("Update failed due to errors");
} finally {
try {
OSUtils.deleteFiles(Paths.get(getToolUnzipLocation()));
} catch (IOException e) {
printStream.println("Error occurred while removing files");
}
}
printStream.println();
}
printStream.println();
}
return toolDetails;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void listCommandTest() {
Assert.assertTrue(outContent.toString().contains("Distributions available locally"));
Assert.assertTrue(outContent.toString().contains("Distributions available remotely"));
Assert.assertTrue(outContent.toString().contains("1.* channel"));
Assert.assertTrue(outContent.toString().contains("1.2.30"));
Assert.assertTrue(outContent.toString().contains("1.2.40"));
Assert.assertTrue(outContent.toString().contains("Swan Lake channel"));
}

Expand Down

0 comments on commit a7b5d0c

Please sign in to comment.