From f657de9e5e20819f8f12d060ad7c91361f79aaf9 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Wed, 16 Aug 2023 15:27:00 +0530 Subject: [PATCH 1/9] Remove system exit when running tests --- .../src/main/java/io/ballerina/cli/cmd/PushCommand.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/PushCommand.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/PushCommand.java index 17656c9915aa..ecc055916732 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/PushCommand.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/PushCommand.java @@ -124,7 +124,9 @@ public void execute() { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(PUSH_COMMAND); outStream.println(commandUsageInfo); // Exit status, zero for OK, non-zero for error - Runtime.getRuntime().exit(0); + if (exitWhenFinish) { + Runtime.getRuntime().exit(0); + } return; } From 81ad442736fa9a1b907f784b6c02fbdbcb43dff4 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Wed, 16 Aug 2023 15:29:06 +0530 Subject: [PATCH 2/9] Remove duplicate BuildNativeImageCommandTest --- cli/ballerina-cli/src/test/resources/testng.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/ballerina-cli/src/test/resources/testng.xml b/cli/ballerina-cli/src/test/resources/testng.xml index 3b5a34826b1b..085680368188 100644 --- a/cli/ballerina-cli/src/test/resources/testng.xml +++ b/cli/ballerina-cli/src/test/resources/testng.xml @@ -41,7 +41,6 @@ under the License. - From 0b29d210974faaab22e7dd561d1b58bc33263265 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Wed, 16 Aug 2023 15:30:24 +0530 Subject: [PATCH 3/9] Handle multiple java versions in maven resolver --- .../ballerina/cli/task/ResolveMavenDependenciesTask.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/ResolveMavenDependenciesTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/ResolveMavenDependenciesTask.java index 95def0e0033b..be3819baa712 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/ResolveMavenDependenciesTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/ResolveMavenDependenciesTask.java @@ -45,22 +45,23 @@ public ResolveMavenDependenciesTask(PrintStream out) { @Override public void execute(Project project) { + List> platformLibraries = new ArrayList<>(); + List> platformRepositories = new ArrayList<>(); PackageManifest.Platform platform = null; for (JvmTarget jvmTarget : JvmTarget.values()) { platform = project.currentPackage().manifest().platform(jvmTarget.code()); if (platform != null) { - break; + platformLibraries.addAll(platform.dependencies()); + platformRepositories.addAll(platform.repositories()); } } if (platform == null) { return; } - List> platformLibraries = platform.dependencies(); - List> platformRepositories = platform.repositories(); List> mavenCustomRepos = new ArrayList<>(); List> mavenDependencies = new ArrayList<>(); - if (platformLibraries == null) { + if (platformLibraries.isEmpty()) { return; } From c6d6a3b6c1f3dff5a619be557c7d88ce764b4d0a Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Wed, 16 Aug 2023 15:30:39 +0530 Subject: [PATCH 4/9] Handle multiple java versions in bindgen --- .../ballerinalang/bindgen/utils/BindgenMvnResolver.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java b/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java index 8ec4f11e196c..336d4011dd42 100644 --- a/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java +++ b/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java @@ -126,7 +126,13 @@ private void populateBallerinaToml(String groupId, String artifactId, String ver PackageManifest packageManifest = ManifestBuilder.from(tomlDocument, null, projectRoot) .packageManifest(); if (packageManifest != null) { - PackageManifest.Platform platform = packageManifest.platform(JvmTarget.JAVA_17.code()); + PackageManifest.Platform platform = null; + for (JvmTarget jvmTarget : JvmTarget.values()) { + platform = packageManifest.platform(jvmTarget.code()); + if (platform != null) { + break; + } + } if (platform != null && platform.dependencies() != null) { for (Map library : platform.dependencies()) { if (library.get("path") == null && From 2ae465eed47932f5e3f9c1f62b6d2a84224c143e Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Thu, 3 Aug 2023 16:30:09 +0530 Subject: [PATCH 5/9] Fix cli test failures --- .../io/ballerina/cli/cmd/PushCommandTest.java | 4 +++- .../project_b_100/resources/expectedDeps.toml | 17 +++++++++++++---- .../unix/run-project-with-dump-raw-graphs.txt | 4 ++-- .../unix/test-project-with-dump-graph.txt | 3 ++- .../unix/test-project-with-dump-raw-graphs.txt | 7 ++++--- cli/ballerina-cli/src/test/resources/testng.xml | 2 +- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PushCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PushCommandTest.java index 7227393ee132..dd2398daff16 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PushCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PushCommandTest.java @@ -186,6 +186,7 @@ public void testPushWithCustomPath() throws IOException { try (MockedStatic repoUtils = Mockito.mockStatic(RepoUtils.class)) { repoUtils.when(RepoUtils::createAndGetHomeReposPath).thenReturn(mockRepo); + repoUtils.when(RepoUtils::getBallerinaShortVersion).thenReturn("1.0.0"); pushCommand.execute(); } @@ -287,6 +288,7 @@ public void testPushToCustomRepo() throws IOException { new CommandLine(pushCommand).parse(args); try (MockedStatic repoUtils = Mockito.mockStatic(RepoUtils.class)) { repoUtils.when(RepoUtils::createAndGetHomeReposPath).thenReturn(mockRepo); + repoUtils.when(RepoUtils::getBallerinaShortVersion).thenReturn("1.0.0"); pushCommand.execute(); } @@ -338,7 +340,7 @@ public void testPushWithEmptyPackageMd() throws IOException { Files.delete(projectPath.resolve(ProjectConstants.PACKAGE_MD_FILE_NAME)); // Push - String expected = "package md file cannot be empty"; + String expected = "md file cannot be empty"; PushCommand pushCommand = new PushCommand(projectPath, printStream, printStream, false); new CommandLine(pushCommand).parse(); diff --git a/cli/ballerina-cli/src/test/resources/test-resources/balTestWithStickyFlag/project_b_100/resources/expectedDeps.toml b/cli/ballerina-cli/src/test/resources/test-resources/balTestWithStickyFlag/project_b_100/resources/expectedDeps.toml index 746c03dbeac8..cf1fc2d02a2e 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/balTestWithStickyFlag/project_b_100/resources/expectedDeps.toml +++ b/cli/ballerina-cli/src/test/resources/test-resources/balTestWithStickyFlag/project_b_100/resources/expectedDeps.toml @@ -5,6 +5,7 @@ [ballerina] dependencies-toml-version = "2" +distribution-version = "2201.8.0-SNAPSHOT" [[package]] org = "ballerina" @@ -14,12 +15,22 @@ scope = "testOnly" [[package]] org = "ballerina" -name = "test" +name = "lang.error" version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] + +[[package]] +org = "ballerina" +name = "test" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.error"} +] modules = [ {org = "ballerina", packageName = "test", moduleName = "test"} ] @@ -42,6 +53,4 @@ dependencies = [ ] modules = [ {org = "pramjs", packageName = "project_b", moduleName = "project_b"} -] - - +] \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/run-project-with-dump-raw-graphs.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/run-project-with-dump-raw-graphs.txt index 6b621acd24ba..9f942c9acf47 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/run-project-with-dump-raw-graphs.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/run-project-with-dump-raw-graphs.txt @@ -7,7 +7,7 @@ digraph "Initial" { node [shape=record] "foo/package_a" [label="<0.1.0> foo/package_a:0.1.0"]; "foo/package_b" [label="<0.1.0> foo/package_b:0.1.0"]; - "foo/package_c" [label="<0.1.0> foo/package_c:0.1.0"]; + "foo/package_c" [unresolved="true",color="grey",label="<0.1.0> foo/package_c:0.1.0"]; // Edges "foo/package_a":"0.1.0" -> "foo/package_b":"0.1.0"; @@ -51,7 +51,7 @@ digraph "Initial" { node [shape=record] "foo/package_a" [label="<0.1.0> foo/package_a:0.1.0"]; "foo/package_b" [label="<0.1.0> foo/package_b:0.1.0"]; - "foo/package_c" [label="<0.1.0> foo/package_c:0.1.0"]; + "foo/package_c" [unresolved="true",color="grey",label="<0.1.0> foo/package_c:0.1.0"]; // Edges "foo/package_a":"0.1.0" -> "foo/package_b":"0.1.0"; diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/test-project-with-dump-graph.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/test-project-with-dump-graph.txt index f1518124fded..e1e1e57fb8cf 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/test-project-with-dump-graph.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/test-project-with-dump-graph.txt @@ -12,4 +12,5 @@ digraph "foo/package_a:0.1.0" { // Edges "foo/package_a":"0.1.0" -> "foo/package_b":"0.1.0"; "foo/package_b":"0.1.0" -> "foo/package_c":"0.1.0"; -} \ No newline at end of file +} + No tests found \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/test-project-with-dump-raw-graphs.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/test-project-with-dump-raw-graphs.txt index 2742dc69f14f..9e8d3eeac3d0 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/test-project-with-dump-raw-graphs.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/test-project-with-dump-raw-graphs.txt @@ -7,7 +7,7 @@ digraph "Initial" { node [shape=record] "foo/package_a" [label="<0.1.0> foo/package_a:0.1.0"]; "foo/package_b" [label="<0.1.0> foo/package_b:0.1.0"]; - "foo/package_c" [label="<0.1.0> foo/package_c:0.1.0"]; + "foo/package_c" [unresolved="true",color="grey",label="<0.1.0> foo/package_c:0.1.0"]; // Edges "foo/package_a":"0.1.0" -> "foo/package_b":"0.1.0"; @@ -51,7 +51,7 @@ digraph "Initial" { node [shape=record] "foo/package_a" [label="<0.1.0> foo/package_a:0.1.0"]; "foo/package_b" [label="<0.1.0> foo/package_b:0.1.0"]; - "foo/package_c" [label="<0.1.0> foo/package_c:0.1.0"]; + "foo/package_c" [unresolved="true",color="grey",label="<0.1.0> foo/package_c:0.1.0"]; // Edges "foo/package_a":"0.1.0" -> "foo/package_b":"0.1.0"; @@ -89,4 +89,5 @@ digraph "Final" { // Edges "foo/package_a":"0.1.0" -> "foo/package_b":"0.1.0"; "foo/package_b":"0.1.0" -> "foo/package_c":"0.1.0"; -} \ No newline at end of file +} + No tests found \ No newline at end of file diff --git a/cli/ballerina-cli/src/test/resources/testng.xml b/cli/ballerina-cli/src/test/resources/testng.xml index 085680368188..c9d19802ae64 100644 --- a/cli/ballerina-cli/src/test/resources/testng.xml +++ b/cli/ballerina-cli/src/test/resources/testng.xml @@ -20,7 +20,7 @@ under the License. - + From 5313784c9ae2aec025617b3eadc549e848c7ec06 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Fri, 18 Aug 2023 14:35:58 +0530 Subject: [PATCH 6/9] Fix GraalVM compatibility warnings --- .../io/ballerina/cli/task/CreateBalaTask.java | 3 +- .../cli/task/CreateExecutableTask.java | 3 +- .../cli/task/RunNativeImageTestTask.java | 3 +- .../cli/utils/GraalVMCompatibilityUtils.java | 29 ++++++++----------- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateBalaTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateBalaTask.java index 42d4a3d66511..74e27374d0f7 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateBalaTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateBalaTask.java @@ -71,8 +71,7 @@ public void execute(Project project) { if (project.buildOptions().dumpBuildTime()) { start = System.currentTimeMillis(); } - String warning = GraalVMCompatibilityUtils.getWarningForPackage( - project.currentPackage(), jBallerinaBackend.targetPlatform().code()); + String warning = GraalVMCompatibilityUtils.getWarningForPackage(project.currentPackage()); if (warning != null) { out.println("\n" + warning); } diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java index d4c9835a940e..1d89252d6d13 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java @@ -104,8 +104,7 @@ public void execute(Project project) { } EmitResult emitResult; if (project.buildOptions().nativeImage() && project.buildOptions().cloud().equals("")) { - String warnings = GraalVMCompatibilityUtils.getAllWarnings( - project.currentPackage(), jBallerinaBackend.targetPlatform().code(), false); + String warnings = GraalVMCompatibilityUtils.getAllWarnings(project.currentPackage(), false); if (!warnings.isEmpty()) { out.println(warnings); } diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunNativeImageTestTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunNativeImageTestTask.java index be9e151fc2fb..6f826620a1cd 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunNativeImageTestTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunNativeImageTestTask.java @@ -403,8 +403,7 @@ public void execute(Project project) { if (hasTests) { int testResult = 1; try { - String warnings = GraalVMCompatibilityUtils.getAllWarnings( - project.currentPackage(), jBallerinaBackend.targetPlatform().code(), true); + String warnings = GraalVMCompatibilityUtils.getAllWarnings(project.currentPackage(), true); if (!warnings.isEmpty()) { out.println(warnings); } diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/GraalVMCompatibilityUtils.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/GraalVMCompatibilityUtils.java index 459a84d1e8d1..1449bbdfd2f7 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/GraalVMCompatibilityUtils.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/utils/GraalVMCompatibilityUtils.java @@ -19,6 +19,7 @@ package io.ballerina.cli.utils; import io.ballerina.projects.AnyTarget; +import io.ballerina.projects.JvmTarget; import io.ballerina.projects.PackageDependencyScope; import io.ballerina.projects.PackageManifest; import io.ballerina.projects.ResolvedPackageDependency; @@ -37,27 +38,22 @@ */ public class GraalVMCompatibilityUtils { - private static boolean hasExternalPlatformDependencies(io.ballerina.projects.Package pkg, String targetPlatform) { - // Check if external platform dependencies are defined - PackageManifest manifest = pkg.manifest(); - return manifest.platform(targetPlatform) != null && - !manifest.platform(targetPlatform).dependencies().isEmpty(); - } - /** * Get the GraalVM compatibility warning message for the given package. * * @param pkg Package to be verified - * @param targetPlatform Target platform * @return Warning message */ - public static String getWarningForPackage(io.ballerina.projects.Package pkg, String targetPlatform) { + public static String getWarningForPackage(io.ballerina.projects.Package pkg) { // Verify that Java dependencies (if exist) of this package are GraalVM compatible - if (hasExternalPlatformDependencies(pkg, targetPlatform)) { - PackageManifest.Platform platform = pkg.manifest().platform(targetPlatform); + PackageManifest manifest = pkg.manifest(); + for (JvmTarget jvmTarget : JvmTarget.values()) { + PackageManifest.Platform platform = manifest.platform(jvmTarget.code()); + if (platform == null || platform.dependencies().isEmpty()) { + continue; + } String packageName = pkg.manifest().name().value(); - - if (platform == null || platform.graalvmCompatible() == null) { + if (platform.graalvmCompatible() == null) { return String.format( "************************************************************%n" + "* WARNING: Package is not verified with GraalVM. *%n" + @@ -68,7 +64,7 @@ public static String getWarningForPackage(io.ballerina.projects.Package pkg, Str "are compatible with GraalVM. Subsequently, update the Ballerina.toml file under " + "the section '[platform.%s]' with the attribute 'graalvmCompatible = true'.%n%n" + "************************************************************%n", - packageName, targetPlatform); + packageName, jvmTarget.code()); } else if (!platform.graalvmCompatible()) { return String.format( "************************************************************%n" + @@ -116,14 +112,13 @@ private static String getWarningForDependencies(io.ballerina.projects.Package pk * Get all the applicable warning messages for GraalVM compatibility of the package. * * @param pkg Package to be verified - * @param targetPlatform Target platform * @param isTestExec Whether it is a test execution * @return Warning message */ - public static String getAllWarnings(io.ballerina.projects.Package pkg, String targetPlatform, boolean isTestExec) { + public static String getAllWarnings(io.ballerina.projects.Package pkg, boolean isTestExec) { StringBuilder warnings = new StringBuilder(); // Verify that Java dependencies (if exist) of this package are GraalVM compatible - String packageWarning = getWarningForPackage(pkg, targetPlatform); + String packageWarning = getWarningForPackage(pkg); if (packageWarning != null) { warnings.append(packageWarning); } From 18aae777d0d685b5ec866ec0c2d5d1f88d0a0135 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Fri, 18 Aug 2023 14:36:30 +0530 Subject: [PATCH 7/9] Fix push command tests --- .../test/java/io/ballerina/cli/cmd/PushCommandTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PushCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PushCommandTest.java index dd2398daff16..3c004bf3e430 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PushCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/PushCommandTest.java @@ -187,6 +187,7 @@ public void testPushWithCustomPath() throws IOException { try (MockedStatic repoUtils = Mockito.mockStatic(RepoUtils.class)) { repoUtils.when(RepoUtils::createAndGetHomeReposPath).thenReturn(mockRepo); repoUtils.when(RepoUtils::getBallerinaShortVersion).thenReturn("1.0.0"); + repoUtils.when(RepoUtils::readSettings).thenReturn(Settings.from()); pushCommand.execute(); } @@ -258,7 +259,7 @@ public void testPushCommandArgAndHelp() throws IOException { new CommandLine(pushCommand).parse(args); pushCommand.execute(); - Assert.assertTrue(readOutput().contains("ballerina-push - Push packages to Ballerina Central")); + Assert.assertTrue(readOutput().contains("ballerina-push - Push the Ballerina Archive (BALA)")); } @Test(description = "Test push command with help flag") @@ -270,7 +271,7 @@ public void testPushCommandWithHelp() throws IOException { new CommandLine(pushCommand).parse(args); pushCommand.execute(); - Assert.assertTrue(readOutput().contains("ballerina-push - Push packages to Ballerina Central")); + Assert.assertTrue(readOutput().contains("ballerina-push - Push the Ballerina Archive (BALA)")); } @Test @@ -289,6 +290,7 @@ public void testPushToCustomRepo() throws IOException { try (MockedStatic repoUtils = Mockito.mockStatic(RepoUtils.class)) { repoUtils.when(RepoUtils::createAndGetHomeReposPath).thenReturn(mockRepo); repoUtils.when(RepoUtils::getBallerinaShortVersion).thenReturn("1.0.0"); + repoUtils.when(RepoUtils::readSettings).thenReturn(Settings.from()); pushCommand.execute(); } @@ -365,7 +367,8 @@ public void testPushToAnUnsupportedRepo() throws IOException { PushCommand pushCommand = new PushCommand(projectPath, printStream, printStream, false); new CommandLine(pushCommand).parse(args); pushCommand.execute(); - String errMsg = "unsupported repository 'stdlib.local' found. Only 'local' repository is supported."; + String errMsg = "unsupported repository 'stdlib.local' found. Only 'local' repository and repositories " + + "mentioned in the Settings.toml are supported."; Assert.assertTrue(readOutput().contains(errMsg)); } } From c7165f9175e6e80f027b42444cb119161da2a115 Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Fri, 18 Aug 2023 18:28:06 +0530 Subject: [PATCH 8/9] Support bindgen with multiple java versions --- .../bindgen/command/BindingsGenerator.java | 72 +++++++++++++------ .../bindgen/utils/BindgenMvnResolver.java | 60 ++++++++-------- .../bindgen/MavenSupportTest.java | 14 ++++ .../MultipleJavaPlatformsBallerina.toml | 36 ++++++++++ .../Ballerina.toml | 18 +++++ .../multipleJavaPlatformsBalProject/File.bal | 26 +++++++ .../multipleJavaPlatformsBalProject/main.bal | 7 ++ 7 files changed, 183 insertions(+), 50 deletions(-) create mode 100644 misc/ballerina-bindgen/src/test/resources/mvn-test-resources/assert-files/MultipleJavaPlatformsBallerina.toml create mode 100644 misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/Ballerina.toml create mode 100644 misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/File.bal create mode 100644 misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/main.bal diff --git a/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/command/BindingsGenerator.java b/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/command/BindingsGenerator.java index 00827ab16177..dc7501d17126 100644 --- a/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/command/BindingsGenerator.java +++ b/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/command/BindingsGenerator.java @@ -76,13 +76,15 @@ public class BindingsGenerator { void generateJavaBindings() throws BindgenException { outStream.println("\nResolving maven dependencies..."); // Resolve existing platform.libraries specified in the Ballerina.toml - resolvePlatformLibraries(); + JvmTarget parentJvmTarget = getParentDependencyJvmTarget(); + resolvePlatformLibraries(parentJvmTarget); + // Resolve the maven dependency received through the tool and update the Ballerina.toml file // with the direct and transitive platform.libraries if ((mvnGroupId != null) && (mvnArtifactId != null) && (mvnVersion != null)) { new BindgenMvnResolver(outStream, env).mavenResolver(mvnGroupId, mvnArtifactId, mvnVersion, - env.getProjectRoot(), true); + env.getProjectRoot(), true, parentJvmTarget); } ClassLoader classLoader = setClassLoader(); @@ -140,33 +142,60 @@ void generateJavaBindings() throws BindgenException { } } - private void resolvePlatformLibraries() throws BindgenException { + private void resolvePlatformLibraries(JvmTarget parentJvmTarget) throws BindgenException { if (env.getProjectRoot() != null) { TomlDocument tomlDocument = env.getTomlDocument(); - PackageManifest.Platform platform = getPackagePlatform(tomlDocument); - if (platform != null && platform.dependencies() != null) { - for (Map library : platform.dependencies()) { - if (library.get("path") != null) { - handlePathDependency(library.get("path").toString()); - } else if (library.get("groupId") != null && library.get("artifactId") != null && - library.get("version") != null) { - resolveMvnDependency(library.get("groupId").toString(), - library.get("artifactId").toString(), library.get("version").toString()); + if (tomlDocument == null) { + return; + } + PackageManifest packageManifest = ManifestBuilder.from(tomlDocument, null, + env.getProjectRoot()).packageManifest(); + if (packageManifest == null) { + return; + } + for (JvmTarget jvmTarget : JvmTarget.values()) { + PackageManifest.Platform platform = packageManifest.platform(jvmTarget.code()); + if (platform != null && platform.dependencies() != null) { + for (Map library : platform.dependencies()) { + if (library.get("path") != null) { + handlePathDependency(library.get("path").toString()); + } else if (library.get("groupId") != null && library.get("artifactId") != null && + library.get("version") != null) { + resolveMvnDependency(library.get("groupId").toString(), + library.get("artifactId").toString(), library.get("version").toString(), + parentJvmTarget); + } } } } } } - private PackageManifest.Platform getPackagePlatform(TomlDocument tomlDocument) { - if (tomlDocument != null) { - PackageManifest packageManifest = ManifestBuilder.from(tomlDocument, null, - env.getProjectRoot()).packageManifest(); - if (packageManifest != null) { - return packageManifest.platform(JvmTarget.JAVA_17.code()); + private JvmTarget getParentDependencyJvmTarget() { + JvmTarget jvmTarget = JvmTarget.JAVA_17; + TomlDocument tomlDocument = env.getTomlDocument(); + if (tomlDocument == null) { + return jvmTarget; + } + PackageManifest packageManifest = ManifestBuilder.from(tomlDocument, null, + env.getProjectRoot()).packageManifest(); + if (packageManifest == null) { + return jvmTarget; + } + for (JvmTarget target : JvmTarget.values()) { + PackageManifest.Platform platform = packageManifest.platform(target.code()); + if (platform == null || platform.dependencies() == null) { + continue; + } + for (Map library : platform.dependencies()) { + if (library.get("groupId") != null && library.get("groupId").equals(mvnGroupId) && + library.get("artifactId") != null && library.get("artifactId").equals(mvnArtifactId) && + library.get("version") != null && library.get("version").equals(mvnVersion)) { + return target; + } } } - return null; + return jvmTarget; } private void handlePathDependency(String libPath) { @@ -179,10 +208,11 @@ private void handlePathDependency(String libPath) { env.addClasspath(libraryPath.toString()); } - private void resolveMvnDependency(String mvnGroupId, String mvnArtifactId, String mvnVersion) + private void resolveMvnDependency(String mvnGroupId, String mvnArtifactId, String mvnVersion, + JvmTarget parentJvmTarget) throws BindgenException { new BindgenMvnResolver(outStream, env).mavenResolver(mvnGroupId, mvnArtifactId, mvnVersion, - env.getProjectRoot(), false); + env.getProjectRoot(), false, parentJvmTarget); } private ClassLoader setClassLoader() throws BindgenException { diff --git a/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java b/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java index 336d4011dd42..78d5b33d3ae9 100644 --- a/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java +++ b/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java @@ -59,7 +59,7 @@ public BindgenMvnResolver(PrintStream outStream, BindgenEnv env) { } public void mavenResolver(String groupId, String artifactId, String version, Path projectRoot, - boolean resolve) throws BindgenException { + boolean resolve, JvmTarget parentJvmTarget) throws BindgenException { Path mvnRepository; if (projectRoot == null) { if (env.getOutputPath() != null) { @@ -71,16 +71,17 @@ public void mavenResolver(String groupId, String artifactId, String version, Pat mvnRepository = Paths.get(projectRoot.toString(), TARGET_DIR, MVN_REPO); } Dependency dependency = resolveDependency(groupId, artifactId, version, mvnRepository.toString()); - handleDependency(groupId, artifactId, version, mvnRepository.toString(), projectRoot, null); + handleDependency(groupId, artifactId, version, mvnRepository.toString(), projectRoot, null, parentJvmTarget); if (resolve) { - dependencyTraversal(dependency, mvnRepository.toString(), projectRoot); + dependencyTraversal(dependency, mvnRepository.toString(), projectRoot, parentJvmTarget); if (projectRoot != null) { outStream.println("\nUpdated the `Ballerina.toml` file with the new platform libraries."); } } } - private void dependencyTraversal(Dependency dependency, String mvnRepository, Path projectRoot) + private void dependencyTraversal(Dependency dependency, String mvnRepository, Path projectRoot, + JvmTarget parentJvmTarget) throws BindgenException { if (dependency.getDepedencies() == null) { return; @@ -88,8 +89,8 @@ private void dependencyTraversal(Dependency dependency, String mvnRepository, Pa String parent = dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getVersion(); for (Dependency transitive : dependency.getDepedencies()) { handleDependency(transitive.getGroupId(), transitive.getArtifactId(), transitive.getVersion(), - mvnRepository, projectRoot, parent); - dependencyTraversal(transitive, mvnRepository, projectRoot); + mvnRepository, projectRoot, parent, parentJvmTarget); + dependencyTraversal(transitive, mvnRepository, projectRoot, parentJvmTarget); } } @@ -104,19 +105,19 @@ private static Dependency resolveDependency(String groupId, String artifactId, S } private void handleDependency(String groupId, String artifactId, String version, String mvnRepository, - Path projectRoot, String parent) throws BindgenException { + Path projectRoot, String parent, JvmTarget parentJvmTarget) throws BindgenException { Path mvnPath = Paths.get(mvnRepository, getPathFromGroupId(groupId), artifactId, version); this.env.addClasspath(mvnPath.toString()); if (projectRoot != null) { File tomlFile = new File(Paths.get(projectRoot.toString(), ProjectConstants.BALLERINA_TOML).toString()); if (tomlFile.exists() && !tomlFile.isDirectory()) { - populateBallerinaToml(groupId, artifactId, version, tomlFile, projectRoot, parent); + populateBallerinaToml(groupId, artifactId, version, tomlFile, projectRoot, parent, parentJvmTarget); } } } private void populateBallerinaToml(String groupId, String artifactId, String version, File tomlFile, - Path projectRoot, String parent) throws BindgenException { + Path projectRoot, String parent, JvmTarget parentJvmTarget) throws BindgenException { try (FileWriterWithEncoding fileWriter = new FileWriterWithEncoding(tomlFile, StandardCharsets.UTF_8, true)) { TomlDocument tomlDocument = env.getTomlDocument(); if (tomlDocument == null) { @@ -125,14 +126,11 @@ private void populateBallerinaToml(String groupId, String artifactId, String ver // TODO: Syntax tree of the toml document could be used for the update. PackageManifest packageManifest = ManifestBuilder.from(tomlDocument, null, projectRoot) .packageManifest(); - if (packageManifest != null) { - PackageManifest.Platform platform = null; - for (JvmTarget jvmTarget : JvmTarget.values()) { - platform = packageManifest.platform(jvmTarget.code()); - if (platform != null) { - break; - } - } + if (packageManifest == null) { + return; + } + for (JvmTarget jvmTarget : JvmTarget.values()) { + PackageManifest.Platform platform = packageManifest.platform(jvmTarget.code()); if (platform != null && platform.dependencies() != null) { for (Map library : platform.dependencies()) { if (library.get("path") == null && @@ -143,24 +141,28 @@ private void populateBallerinaToml(String groupId, String artifactId, String ver } } } - fileWriter.write("\n"); - if (parent != null) { - fileWriter.write("# transitive dependency of " + parent + "\n"); - } - fileWriter.write("[[platform.java17.dependency]]\n"); - String moduleName = getModuleName(projectRoot, env.getOutputPath()); - if (moduleName != null) { - fileWriter.write("modules = [\"" + moduleName + "\"]\n"); - } - fileWriter.write("groupId = \"" + groupId + "\"\n"); - fileWriter.write("artifactId = \"" + artifactId + "\"\n"); - fileWriter.write("version = \"" + version + "\"\n"); } + writeDependencyToBallerinaToml(groupId, artifactId, version, projectRoot, parent, parentJvmTarget, fileWriter); } catch (IOException e) { throw new BindgenException("error: unable to update the Ballerina.toml file: " + e.getMessage(), e); } } + private void writeDependencyToBallerinaToml(String groupId, String artifactId, String version, Path projectRoot, String parent, JvmTarget parentJvmTarget, FileWriterWithEncoding fileWriter) throws IOException { + fileWriter.write("\n"); + if (parent != null) { + fileWriter.write("# transitive dependency of " + parent + "\n"); + } + fileWriter.write("[[platform." + parentJvmTarget.code() + ".dependency]]\n"); + String moduleName = getModuleName(projectRoot, env.getOutputPath()); + if (moduleName != null) { + fileWriter.write("modules = [\"" + moduleName + "\"]\n"); + } + fileWriter.write("groupId = \"" + groupId + "\"\n"); + fileWriter.write("artifactId = \"" + artifactId + "\"\n"); + fileWriter.write("version = \"" + version + "\"\n"); + } + private static String getPathFromGroupId(String groupId) { String[] paths = groupId.split("\\."); if (paths.length == 0) { diff --git a/misc/ballerina-bindgen/src/test/java/org/ballerinalang/bindgen/MavenSupportTest.java b/misc/ballerina-bindgen/src/test/java/org/ballerinalang/bindgen/MavenSupportTest.java index 786bed25de41..e0cc8db126de 100644 --- a/misc/ballerina-bindgen/src/test/java/org/ballerinalang/bindgen/MavenSupportTest.java +++ b/misc/ballerina-bindgen/src/test/java/org/ballerinalang/bindgen/MavenSupportTest.java @@ -92,6 +92,20 @@ public void testBindgenMvnToml() throws IOException { Paths.get(testResources.toString(), "assert-files", "Ballerina.toml").toString())); } + @Test(description = "Test the maven support with multiple java platforms in the Bindgen tool to see if the " + + "Ballerina.toml is updated") + public void testBindgenMvnTomlMultipleJavaPlatforms() throws IOException { + String projectDir = Paths.get(testResources.toString(), "multipleJavaPlatformsBalProject").toString(); + String[] args = {"-mvn=commons-logging:commons-logging:1.1.1", "-o=" + projectDir, + "org.apache.commons.logging.LogFactory"}; + BindgenCommand bindgenCommand = new BindgenCommand(printStream, printStream, false); + new CommandLine(bindgenCommand).parseArgs(args); + + bindgenCommand.execute(); + Assert.assertTrue(isTomlUpdated(Paths.get(projectDir, "Ballerina.toml").toString(), + Paths.get(testResources.toString(), "assert-files", "MultipleJavaPlatformsBallerina.toml").toString())); + } + @Test(description = "Test the error given for a maven library that is unavailable") public void testUnavailableMvnLibrary() throws IOException { String projectDir = Paths.get(testResources.toString(), "balProject").toString(); diff --git a/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/assert-files/MultipleJavaPlatformsBallerina.toml b/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/assert-files/MultipleJavaPlatformsBallerina.toml new file mode 100644 index 000000000000..83b52d2846f7 --- /dev/null +++ b/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/assert-files/MultipleJavaPlatformsBallerina.toml @@ -0,0 +1,36 @@ +[package] +org= "bindgen" +name= "test" +version= "0.1.0" + +[[platform.java17.dependency]] +modules = ["balModule1"] +path = "./log4j/log4j/1.2.17/log4j-1.2.17.jar" + +[[platform.java11.dependency]] +groupId = "commons-logging" +artifactId = "commons-logging" +version = "1.1.1" + +[[platform.java17.dependency]] +groupId = "org.yaml" +artifactId = "snakeyaml" +version = "2.0" + +# transitive dependency of commons-logging:commons-logging:1.1.1 +[[platform.java11.dependency]] +groupId = "log4j" +artifactId = "log4j" +version = "1.2.12" + +# transitive dependency of commons-logging:commons-logging:1.1.1 +[[platform.java11.dependency]] +groupId = "logkit" +artifactId = "logkit" +version = "1.0.1" + +# transitive dependency of commons-logging:commons-logging:1.1.1 +[[platform.java11.dependency]] +groupId = "avalon-framework" +artifactId = "avalon-framework" +version = "4.1.3" diff --git a/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/Ballerina.toml b/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/Ballerina.toml new file mode 100644 index 000000000000..ae8f6ba65017 --- /dev/null +++ b/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/Ballerina.toml @@ -0,0 +1,18 @@ +[package] +org= "bindgen" +name= "test" +version= "0.1.0" + +[[platform.java17.dependency]] +modules = ["balModule1"] +path = "./log4j/log4j/1.2.17/log4j-1.2.17.jar" + +[[platform.java11.dependency]] +groupId = "commons-logging" +artifactId = "commons-logging" +version = "1.1.1" + +[[platform.java17.dependency]] +groupId = "org.yaml" +artifactId = "snakeyaml" +version = "2.0" diff --git a/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/File.bal b/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/File.bal new file mode 100644 index 000000000000..f2f72cb6475b --- /dev/null +++ b/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/File.bal @@ -0,0 +1,26 @@ +import ballerina/jballerina.java; + +# Ballerina class mapping for the Java `java.io.File` class. +@java:Binding {'class: "java.io.File"} +distinct class File { + + *java:JObject; + + # The `handle` field that stores the reference to the `java.io.File` object. + public handle jObj; + + # The init function of the Ballerina class mapping the `java.io.File` Java class. + # + # + obj - The `handle` value containing the Java reference of the object. + function init(handle obj) { + self.jObj = obj; + } + + # The function to retrieve the string representation of the Ballerina class mapping the `java.io.File` Java class. + # + # + return - The `string` form of the Java object instance. + function toString() returns string { + return java:toString(self.jObj) ?: "null"; + } +} + diff --git a/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/main.bal b/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/main.bal new file mode 100644 index 000000000000..b60e39d08661 --- /dev/null +++ b/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/main.bal @@ -0,0 +1,7 @@ +import ballerina/io; + +# Prints `Hello World`. + +public function main() { + io:println("Hello World!"); +} From d4fc035f0f9c88c558ac0b7c1ea720665f2a6d1b Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Fri, 18 Aug 2023 19:47:39 +0530 Subject: [PATCH 9/9] Fix checkstyle issues --- .../bindgen/utils/BindgenMvnResolver.java | 12 ++++++--- .../multipleJavaPlatformsBalProject/File.bal | 26 ------------------- 2 files changed, 8 insertions(+), 30 deletions(-) delete mode 100644 misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/File.bal diff --git a/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java b/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java index 78d5b33d3ae9..8f22b8d823c4 100644 --- a/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java +++ b/misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenMvnResolver.java @@ -116,8 +116,9 @@ private void handleDependency(String groupId, String artifactId, String version, } } - private void populateBallerinaToml(String groupId, String artifactId, String version, File tomlFile, - Path projectRoot, String parent, JvmTarget parentJvmTarget) throws BindgenException { + private void populateBallerinaToml(String groupId, String artifactId, String version, + File tomlFile, Path projectRoot, String parent, + JvmTarget parentJvmTarget) throws BindgenException { try (FileWriterWithEncoding fileWriter = new FileWriterWithEncoding(tomlFile, StandardCharsets.UTF_8, true)) { TomlDocument tomlDocument = env.getTomlDocument(); if (tomlDocument == null) { @@ -142,13 +143,16 @@ private void populateBallerinaToml(String groupId, String artifactId, String ver } } } - writeDependencyToBallerinaToml(groupId, artifactId, version, projectRoot, parent, parentJvmTarget, fileWriter); + writeDependencyToBallerinaToml(groupId, artifactId, version, projectRoot, parent, parentJvmTarget, + fileWriter); } catch (IOException e) { throw new BindgenException("error: unable to update the Ballerina.toml file: " + e.getMessage(), e); } } - private void writeDependencyToBallerinaToml(String groupId, String artifactId, String version, Path projectRoot, String parent, JvmTarget parentJvmTarget, FileWriterWithEncoding fileWriter) throws IOException { + private void writeDependencyToBallerinaToml(String groupId, String artifactId, String version, Path projectRoot, + String parent, JvmTarget parentJvmTarget, + FileWriterWithEncoding fileWriter) throws IOException { fileWriter.write("\n"); if (parent != null) { fileWriter.write("# transitive dependency of " + parent + "\n"); diff --git a/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/File.bal b/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/File.bal deleted file mode 100644 index f2f72cb6475b..000000000000 --- a/misc/ballerina-bindgen/src/test/resources/mvn-test-resources/multipleJavaPlatformsBalProject/File.bal +++ /dev/null @@ -1,26 +0,0 @@ -import ballerina/jballerina.java; - -# Ballerina class mapping for the Java `java.io.File` class. -@java:Binding {'class: "java.io.File"} -distinct class File { - - *java:JObject; - - # The `handle` field that stores the reference to the `java.io.File` object. - public handle jObj; - - # The init function of the Ballerina class mapping the `java.io.File` Java class. - # - # + obj - The `handle` value containing the Java reference of the object. - function init(handle obj) { - self.jObj = obj; - } - - # The function to retrieve the string representation of the Ballerina class mapping the `java.io.File` Java class. - # - # + return - The `string` form of the Java object instance. - function toString() returns string { - return java:toString(self.jObj) ?: "null"; - } -} -