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

Prompt user to update the tool based on compatibility #302

Merged
merged 6 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
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
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
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 @@

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;

Check warning on line 74 in src/main/java/org/ballerinalang/command/cmd/PullCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/cmd/PullCommand.java#L74

Added line #L74 was not covered by tests
}
}

// 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 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;

Check warning on line 91 in src/main/java/org/ballerinalang/command/cmd/UpdateCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/cmd/UpdateCommand.java#L91

Added line #L91 was not covered by tests
}
}
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;
}

Check warning on line 31 in src/main/java/org/ballerinalang/command/util/Tool.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/Tool.java#L28-L31

Added lines #L28 - L31 were not covered by tests

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 @@
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 @@
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 @@
*
* @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");

Check warning on line 871 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L871

Added line #L871 was not covered by tests
} 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();

Check warning on line 877 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L873-L877

Added lines #L873 - L877 were not covered by tests
} 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");

Check warning on line 885 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L884-L885

Added lines #L884 - L885 were not covered by tests
} finally {
try {
OSUtils.deleteFiles(Paths.get(getToolUnzipLocation()));
} catch (IOException e) {
printStream.println("Error occurred while removing files");

Check warning on line 890 in src/main/java/org/ballerinalang/command/util/ToolUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/ballerinalang/command/util/ToolUtil.java#L889-L890

Added lines #L889 - L890 were not covered by tests
}
}
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