From df3d49469202d17a66cf67452706bcaa1c4bf546 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Mon, 5 Aug 2024 08:40:56 +0530 Subject: [PATCH 01/12] Remove buildOnDocker variable from the plugin --- .../ballerina/plugin/BallerinaPlugin.groovy | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 1497be4..08f4667 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -60,7 +60,6 @@ class BallerinaPlugin implements Plugin { def needPublishToLocalCentral = false def graalvmFlag = '' def parallelTestFlag = '' - def buildOnDocker = false def ballerinaDockerTag = '' def distributionBinPath = '' @@ -88,8 +87,8 @@ class BallerinaPlugin implements Plugin { } project.dependencies { - if (buildOnDocker) { - println("[Warning] skipping downloading jBallerinaTools dependency: project uses docker to build the module") + if (ballerinaExtension.isConnector) { + println("[Warning] skipping downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") } else { if (ballerinaExtension.langVersion == null) { jbalTools("org.ballerinalang:jballerina-tools:${ballerinaExtension.langVersion}") { @@ -110,8 +109,8 @@ class BallerinaPlugin implements Plugin { } } - if (buildOnDocker) { - println("[Warning] skipping task 'unpackJballerinaTools': project uses docker to build the module") + if (ballerinaExtension.isConnector) { + println("[Warning] skipping downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") } else { doLast { project.configurations.jbalTools.resolvedConfiguration.resolvedArtifacts.each { artifact -> @@ -136,8 +135,8 @@ class BallerinaPlugin implements Plugin { project.tasks.register('unpackStdLibs') { dependsOn(project.unpackJballerinaTools) - if (buildOnDocker) { - println("[Warning] skipping task 'unpackStdLibs': project uses docker to build the module") + if (ballerinaExtension.isConnector) { + println("[Warning] skipping downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") } else { doLast { project.configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact -> @@ -152,8 +151,8 @@ class BallerinaPlugin implements Plugin { project.tasks.register('copyStdlibs') { dependsOn(project.unpackStdLibs) - if (buildOnDocker) { - println("[Warning] skipping task 'copyStdlibs': project uses docker to build the module") + if (ballerinaExtension.isConnector) { + println("[Warning] skipping downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") } else { doLast { /* Standard Libraries */ @@ -186,7 +185,6 @@ class BallerinaPlugin implements Plugin { project.tasks.register('initializeVariables') { if (ballerinaExtension.isConnector || project.hasProperty('buildUsingDocker')) { - buildOnDocker = true ballerinaDockerTag = getDockerImageTag(project) println("[Info] project builds on docker") println("[Info] using the Ballerina docker image tag: $ballerinaDockerTag") @@ -268,7 +266,7 @@ class BallerinaPlugin implements Plugin { def result = project.exec { workingDir project.projectDir environment 'JAVA_OPTS', '-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true' - if (buildOnDocker) { + if (ballerinaExtension.isConnector) { createDockerEnvFile("$project.projectDir/docker.env") def balPackWithDocker = """ docker run --env-file $project.projectDir/docker.env --rm --net=host -u root \ @@ -328,7 +326,7 @@ class BallerinaPlugin implements Plugin { project.exec { workingDir project.projectDir environment 'JAVA_OPTS', '-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true' - if (buildOnDocker) { + if (ballerinaExtension.isConnector) { def balPushWithDocker = """ docker run --env-file $project.projectDir/docker.env --rm --net=host -u root \ -v $parentDirectory:/home/ballerina/$parentDirectory.name \ @@ -371,7 +369,7 @@ class BallerinaPlugin implements Plugin { } } } - if (buildOnDocker) { + if (ballerinaExtension.isConnector) { deleteFile("$project.projectDir/docker.env") } } @@ -392,7 +390,7 @@ class BallerinaPlugin implements Plugin { project.exec { workingDir project.projectDir environment 'JAVA_OPTS', '-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true' - if (buildOnDocker) { + if (ballerinaExtension.isConnector) { createDockerEnvFile("$project.projectDir/docker.env") def balTestWithDocker = """ docker run --env-file $project.projectDir/docker.env --rm --net=host -u root \ @@ -414,14 +412,14 @@ class BallerinaPlugin implements Plugin { } } - if (buildOnDocker) { + if (ballerinaExtension.isConnector) { deleteFile("$project.projectDir/docker.env") } } } project.tasks.register('clean', Delete.class) { - if (buildOnDocker) { + if (ballerinaExtension.isConnector) { project.exec { def deleteUsingDocker = """ docker run -u root \ From 22754feca98493e12ddc6057dfaff2211a5874fb Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Mon, 5 Aug 2024 08:47:30 +0530 Subject: [PATCH 02/12] Remove docker based execution for plugin-build tasks --- .../ballerina/plugin/BallerinaPlugin.groovy | 63 ++----------------- 1 file changed, 6 insertions(+), 57 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 08f4667..eb34263 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -267,26 +267,10 @@ class BallerinaPlugin implements Plugin { workingDir project.projectDir environment 'JAVA_OPTS', '-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true' if (ballerinaExtension.isConnector) { - createDockerEnvFile("$project.projectDir/docker.env") - def balPackWithDocker = """ - docker run --env-file $project.projectDir/docker.env --rm --net=host -u root \ - -v $parentDirectory:/home/ballerina/$parentDirectory.name \ - -v $projectDirectory:/home/ballerina/$parentDirectory.name/$projectDirectory.name \ - ballerina/ballerina:$ballerinaDockerTag \ - /bin/sh -c "cd $parentDirectory.name/$projectDirectory.name && \ - bal pack --target-dir ${balBuildTarget}" - """ if (Os.isFamily(Os.FAMILY_WINDOWS)) { - println "Executing command on windows: ${balPackWithDocker}" -// commandLine 'cmd', '/c', "$balPackWithDocker && exit %%ERRORLEVEL%%" - def cmdStr = """ - docker run --env-file $project.projectDir/docker.env --rm --net=host -u root \ - alpine:latest \ - /bin/sh -c "ls -al" - """ - commandLine 'cmd', '/c', "$balPackWithDocker && exit %%ERRORLEVEL%%" + commandLine 'cmd', '/c', "bal.bat pack --target-dir ${balBuildTarget} && exit %%ERRORLEVEL%%" } else { - commandLine 'sh', '-c', "$balPackWithDocker" + commandLine 'sh', '-c', "bal pack --target-dir ${balBuildTarget}" } } else { if (Os.isFamily(Os.FAMILY_WINDOWS)) { @@ -327,18 +311,10 @@ class BallerinaPlugin implements Plugin { workingDir project.projectDir environment 'JAVA_OPTS', '-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true' if (ballerinaExtension.isConnector) { - def balPushWithDocker = """ - docker run --env-file $project.projectDir/docker.env --rm --net=host -u root \ - -v $parentDirectory:/home/ballerina/$parentDirectory.name \ - -v $projectDirectory:/home/ballerina/$parentDirectory.name/$projectDirectory.name \ - ballerina/ballerina:$ballerinaDockerTag \ - /bin/sh -c "cd $parentDirectory.name/$projectDirectory.name && \ - bal push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala" - """ if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', "$balPushWithDocker && exit %%ERRORLEVEL%%" + commandLine 'cmd', '/c', "bal.bat push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala && exit %%ERRORLEVEL%%" } else { - commandLine 'sh', '-c', "$balPushWithDocker" + commandLine 'sh', '-c', "bal push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala" } } else if (Os.isFamily(Os.FAMILY_WINDOWS)) { commandLine 'cmd', '/c', "$distributionBinPath/bal.bat push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala && exit %%ERRORLEVEL%%" @@ -369,9 +345,6 @@ class BallerinaPlugin implements Plugin { } } } - if (ballerinaExtension.isConnector) { - deleteFile("$project.projectDir/docker.env") - } } outputs.dir balaArtifact } @@ -391,19 +364,10 @@ class BallerinaPlugin implements Plugin { workingDir project.projectDir environment 'JAVA_OPTS', '-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true' if (ballerinaExtension.isConnector) { - createDockerEnvFile("$project.projectDir/docker.env") - def balTestWithDocker = """ - docker run --env-file $project.projectDir/docker.env --rm --net=host -u root \ - -v $parentDirectory:/home/ballerina/$parentDirectory.name \ - -v $projectDirectory:/home/ballerina/$parentDirectory.name/$projectDirectory.name \ - ballerina/ballerina:$ballerinaDockerTag \ - /bin/sh -c "cd $parentDirectory.name/$projectDirectory.name && \ - $balJavaDebugParam bal test ${graalvmFlag} ${parallelTestFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" - """ if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', "$balTestWithDocker && exit %%ERRORLEVEL%%" + commandLine 'cmd', '/c', "bal.bat test --offline ${graalvmFlag} ${parallelTestFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams} && exit %%ERRORLEVEL%%" } else { - commandLine 'sh', '-c', "$balTestWithDocker" + commandLine 'sh', '-c', "bal test --offline ${graalvmFlag} ${parallelTestFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams}" } } else if (Os.isFamily(Os.FAMILY_WINDOWS)) { commandLine 'cmd', '/c', "$balJavaDebugParam $distributionBinPath/bal.bat test --offline ${graalvmFlag} ${parallelTestFlag} ${testCoverageParams} ${groupParams} ${disableGroups} ${debugParams} && exit %%ERRORLEVEL%%" @@ -412,9 +376,6 @@ class BallerinaPlugin implements Plugin { } } - if (ballerinaExtension.isConnector) { - deleteFile("$project.projectDir/docker.env") - } } } @@ -441,18 +402,6 @@ class BallerinaPlugin implements Plugin { } } - static void createDockerEnvFile(String dockerEnvFilePath) { - def dockerEnvFileWriter = new PrintWriter("$dockerEnvFilePath", "UTF-8") - def excludedVariables = ["PATH", "JAVA_HOME", "HOME"] - def envVariables = System.getenv() - envVariables.each { key, value -> - if (!excludedVariables.contains(key) && !key.startsWith("=")) { - dockerEnvFileWriter.println("$key=$value") - } - } - dockerEnvFileWriter.close() - } - static void deleteFile(String filePath) { def file = new File(filePath) if (file.exists() && !file.delete()) { From f3e9289edae7c948ea7727e908588ee333270403 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Mon, 5 Aug 2024 08:57:46 +0530 Subject: [PATCH 03/12] Remove unused parameters and configurations --- .../ballerina/plugin/BallerinaPlugin.groovy | 30 ++----------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index eb34263..5b892d0 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -49,7 +49,6 @@ class BallerinaPlugin implements Plugin { def balBuildTarget = 'build/bal_build_target' def balaArtifact = new File("$project.projectDir/build/bala_unzipped/") def projectDirectory = new File("$project.projectDir") - def parentDirectory = new File("$projectDirectory.parent") def ballerinaCentralAccessToken = System.getenv('BALLERINA_CENTRAL_ACCESS_TOKEN') def groupParams = '' def disableGroups = '' @@ -184,7 +183,7 @@ class BallerinaPlugin implements Plugin { } project.tasks.register('initializeVariables') { - if (ballerinaExtension.isConnector || project.hasProperty('buildUsingDocker')) { + if (ballerinaExtension.isConnector) { ballerinaDockerTag = getDockerImageTag(project) println("[Info] project builds on docker") println("[Info] using the Ballerina docker image tag: $ballerinaDockerTag") @@ -380,35 +379,12 @@ class BallerinaPlugin implements Plugin { } project.tasks.register('clean', Delete.class) { - if (ballerinaExtension.isConnector) { - project.exec { - def deleteUsingDocker = """ - docker run -u root \ - -v $parentDirectory:/home/ballerina/$parentDirectory.name \ - ballerina/ballerina:$ballerinaDockerTag \ - /bin/sh -c "find /home/ballerina/$parentDirectory.name -type d -name 'build' -exec rm -rf {} + && find /home/ballerina/$parentDirectory.name -type d -name 'target' -exec rm -rf {} +" - """ - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', "$deleteUsingDocker && exit %%ERRORLEVEL%%" - } else { - commandLine 'sh', '-c', "$deleteUsingDocker" - } - } - } else { - delete "$project.projectDir/target" - delete "$project.projectDir/build" - } + delete "$project.projectDir/target" + delete "$project.projectDir/build" delete "$project.rootDir/target" } } - static void deleteFile(String filePath) { - def file = new File(filePath) - if (file.exists() && !file.delete()) { - println("Failed to delete $filePath.") - } - } - static String getDockerImageTag(Project project) { def ballerinaDockerTag = project.findProperty('ballerinaLangVersion') if (project.hasProperty('buildUsingDocker')) { From bab74a60ab9d22021482553ad36c0143b1671224 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Mon, 5 Aug 2024 10:45:38 +0530 Subject: [PATCH 04/12] Add new tasks to verify the locally installed and configured ballerina distribution version --- .../ballerina/plugin/BallerinaPlugin.groovy | 79 ++++++++++++++----- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 5b892d0..4f7067a 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -18,6 +18,7 @@ package io.ballerina.plugin import org.apache.tools.ant.taskdefs.condition.Os +import org.gradle.api.GradleException import org.gradle.api.InvalidUserDataException import org.gradle.api.Plugin import org.gradle.api.Project @@ -59,8 +60,8 @@ class BallerinaPlugin implements Plugin { def needPublishToLocalCentral = false def graalvmFlag = '' def parallelTestFlag = '' - def ballerinaDockerTag = '' def distributionBinPath = '' + def ignoreVersionMismatch = false if (project.version.matches(project.ext.timestampedVersionRegex)) { def splitVersion = project.version.split('-') @@ -184,9 +185,8 @@ class BallerinaPlugin implements Plugin { project.tasks.register('initializeVariables') { if (ballerinaExtension.isConnector) { - ballerinaDockerTag = getDockerImageTag(project) - println("[Info] project builds on docker") - println("[Info] using the Ballerina docker image tag: $ballerinaDockerTag") + def balVersion = getProjectBalVersion(project) + println("[Info] project builds using Ballerina distribution: $balVersion") } String packageName = ballerinaExtension.module @@ -232,10 +232,61 @@ class BallerinaPlugin implements Plugin { testCoverageParams = ballerinaExtension.testCoverageParam } } + + if (project.hasProperty("ignoreVersionMismatch")) { + ignoreVersionMismatch = true + } + } + + project.tasks.register("verifyLocalBalVersion") { + dependsOn(project.initializeVariables) + + if (!ballerinaExtension.isConnector) { + return + } + + if (ignoreVersionMismatch) { + println("[Warn] Ignoring system-installed and configured Ballerina version mismatch.") + return + } + + def output = new ByteArrayOutputStream() + def error = new ByteArrayOutputStream() + + try { + exec { + if (Os.isFamily(Os.FAMILY_WINDOWS)) { + commandLine 'cmd', '/c', "bal.bat -v" + } else { + commandLine 'sh', '-c', "bal -v" + } + standardOutput = output + errorOutput = error + ignoreExitValue = true + } + } catch (Exception e) { + throw new GradleException("Ballerina installation not found, hence exiting", e) + } + + def versionOutput = output.toString() + def pattern = ~"Ballerina (\\d+\\.\\d+\\.\\d+) \\(Swan Lake Update \\d+\\)" + def matcher = pattern.matcher(versionOutput) + if (!matcher.find()) { + throw new GradleException("Failed to parse the Ballerina version") + } + + def installedVersion = matcher.group(1) + def configuredVersion = getProjectBalVersion(project) + + if (installedVersion != configuredVersion) { + throw new GradleException("Ballerina version mismatch. Expected: $configuredVersion, but found: $installedVersion, hence exiting") + } + } project.tasks.register('build') { dependsOn(project.initializeVariables) + dependsOn(project.verifyLocalBalVersion) dependsOn(project.updateTomlFiles) finalizedBy(project.commitTomlFiles) dependsOn(project.test) @@ -355,6 +406,7 @@ class BallerinaPlugin implements Plugin { project.tasks.register('test') { dependsOn(project.initializeVariables) + dependsOn(project.verifyLocalBalVersion) dependsOn(project.updateTomlFiles) finalizedBy(project.commitTomlFiles) doLast { @@ -385,21 +437,8 @@ class BallerinaPlugin implements Plugin { } } - static String getDockerImageTag(Project project) { - def ballerinaDockerTag = project.findProperty('ballerinaLangVersion') - if (project.hasProperty('buildUsingDocker')) { - ballerinaDockerTag = project.findProperty('buildUsingDocker') - if (ballerinaDockerTag == '') { - return 'nightly' - } - } - if (isTimeStampVersion(ballerinaDockerTag)) { - return 'nightly' - } - return ballerinaDockerTag - } - - static boolean isTimeStampVersion(String version) { - return version.trim().split("-").length > 1 + static String getProjectBalVersion(Project project) { + def projectBalVersion = project.findProperty('ballerinaLangVersion') + return projectBalVersion } } From 112d12e3d942050fc87693d1a5bbf57bda2368b8 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Mon, 5 Aug 2024 10:48:21 +0530 Subject: [PATCH 05/12] Restructure the code --- .../groovy/io/ballerina/plugin/BallerinaPlugin.groovy | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 4f7067a..457f3fe 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -245,11 +245,6 @@ class BallerinaPlugin implements Plugin { return } - if (ignoreVersionMismatch) { - println("[Warn] Ignoring system-installed and configured Ballerina version mismatch.") - return - } - def output = new ByteArrayOutputStream() def error = new ByteArrayOutputStream() @@ -279,6 +274,10 @@ class BallerinaPlugin implements Plugin { def configuredVersion = getProjectBalVersion(project) if (installedVersion != configuredVersion) { + if (ignoreVersionMismatch) { + println("[Warn] Ignoring Ballerina version mismatch. Expected: $configuredVersion, but found: $installedVersion.") + return + } throw new GradleException("Ballerina version mismatch. Expected: $configuredVersion, but found: $installedVersion, hence exiting") } From f40ca172928c2633a817e917136b3da4206ee728 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 6 Aug 2024 13:12:45 +0530 Subject: [PATCH 06/12] Incorporate review suggestions --- .../ballerina/plugin/BallerinaPlugin.groovy | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 457f3fe..3db7965 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -184,11 +184,6 @@ class BallerinaPlugin implements Plugin { } project.tasks.register('initializeVariables') { - if (ballerinaExtension.isConnector) { - def balVersion = getProjectBalVersion(project) - println("[Info] project builds using Ballerina distribution: $balVersion") - } - String packageName = ballerinaExtension.module String organization if (ballerinaExtension.packageOrganization == null) { @@ -264,27 +259,28 @@ class BallerinaPlugin implements Plugin { } def versionOutput = output.toString() - def pattern = ~"Ballerina (\\d+\\.\\d+\\.\\d+) \\(Swan Lake Update \\d+\\)" - def matcher = pattern.matcher(versionOutput) - if (!matcher.find()) { - throw new GradleException("Failed to parse the Ballerina version") + def installedVersion = "" + try { + def balVersion = versionOutput.split("\n")[0] + installedVersion = balVersion + .replaceAll("Ballerina ", '') + .replaceAll("\\(Swan Lake Update \\d+\\)", '') + .replaceAll("\\s+", '') + } catch (Exception e) { + throw new GradleException("Failed to parse the Ballerina version: $versionOutput", e) } - def installedVersion = matcher.group(1) - def configuredVersion = getProjectBalVersion(project) - + def configuredVersion = project.findProperty('ballerinaLangVersion') if (installedVersion != configuredVersion) { if (ignoreVersionMismatch) { - println("[Warn] Ignoring Ballerina version mismatch. Expected: $configuredVersion, but found: $installedVersion.") + println("[Warning] Ignoring Ballerina version mismatch. Expected: $configuredVersion, but found: $installedVersion.") return } throw new GradleException("Ballerina version mismatch. Expected: $configuredVersion, but found: $installedVersion, hence exiting") } - } project.tasks.register('build') { - dependsOn(project.initializeVariables) dependsOn(project.verifyLocalBalVersion) dependsOn(project.updateTomlFiles) finalizedBy(project.commitTomlFiles) @@ -404,7 +400,6 @@ class BallerinaPlugin implements Plugin { } project.tasks.register('test') { - dependsOn(project.initializeVariables) dependsOn(project.verifyLocalBalVersion) dependsOn(project.updateTomlFiles) finalizedBy(project.commitTomlFiles) @@ -435,9 +430,4 @@ class BallerinaPlugin implements Plugin { delete "$project.rootDir/target" } } - - static String getProjectBalVersion(Project project) { - def projectBalVersion = project.findProperty('ballerinaLangVersion') - return projectBalVersion - } } From 4ae17cc34c324b7c3c1391a6a2085497e8e88047 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 6 Aug 2024 19:02:25 +0530 Subject: [PATCH 07/12] Restructure build-dependent tasks --- .../ballerina/plugin/BallerinaPlugin.groovy | 117 +++++++++--------- 1 file changed, 59 insertions(+), 58 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 3db7965..6888b00 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -88,16 +88,16 @@ class BallerinaPlugin implements Plugin { project.dependencies { if (ballerinaExtension.isConnector) { - println("[Warning] skipping downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") + return + } + + if (ballerinaExtension.langVersion == null) { + jbalTools("org.ballerinalang:jballerina-tools:${ballerinaExtension.langVersion}") { + transitive = false + } } else { - if (ballerinaExtension.langVersion == null) { - jbalTools("org.ballerinalang:jballerina-tools:${ballerinaExtension.langVersion}") { - transitive = false - } - } else { - jbalTools("org.ballerinalang:jballerina-tools:${ballerinaExtension.langVersion}") { - transitive = false - } + jbalTools("org.ballerinalang:jballerina-tools:${ballerinaExtension.langVersion}") { + transitive = false } } } @@ -110,73 +110,74 @@ class BallerinaPlugin implements Plugin { } if (ballerinaExtension.isConnector) { - println("[Warning] skipping downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") - } else { - doLast { - project.configurations.jbalTools.resolvedConfiguration.resolvedArtifacts.each { artifact -> - project.copy { - from project.zipTree(artifact.getFile()) - into new File("${project.buildDir}/") - } + return + } - project.copy { - from(project.zipTree(artifact.getFile())) { - eachFile { fcd -> - fcd.relativePath = new RelativePath(!fcd.file.isDirectory(), fcd.relativePath.segments.drop(1)) - } - includeEmptyDirs = false + doLast { + project.configurations.jbalTools.resolvedConfiguration.resolvedArtifacts.each { artifact -> + project.copy { + from project.zipTree(artifact.getFile()) + into new File("${project.buildDir}/") + } + + project.copy { + from(project.zipTree(artifact.getFile())) { + eachFile { fcd -> + fcd.relativePath = new RelativePath(!fcd.file.isDirectory(), fcd.relativePath.segments.drop(1)) } - into "${project.rootDir}/target/ballerina-runtime" + includeEmptyDirs = false } + into "${project.rootDir}/target/ballerina-runtime" } } } } project.tasks.register('unpackStdLibs') { - dependsOn(project.unpackJballerinaTools) if (ballerinaExtension.isConnector) { - println("[Warning] skipping downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") - } else { - doLast { - project.configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact -> - project.copy { - from project.zipTree(artifact.getFile()) - into new File("${project.buildDir}/extracted-stdlibs", artifact.name + '-zip') - } + return + } + + dependsOn(project.unpackJballerinaTools) + doLast { + project.configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact -> + project.copy { + from project.zipTree(artifact.getFile()) + into new File("${project.buildDir}/extracted-stdlibs", artifact.name + '-zip') } } } } project.tasks.register('copyStdlibs') { - dependsOn(project.unpackStdLibs) if (ballerinaExtension.isConnector) { - println("[Warning] skipping downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") - } else { - doLast { - /* Standard Libraries */ - project.configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact -> - def artifactExtractedPath = "${project.buildDir}/extracted-stdlibs/" + artifact.name + '-zip' - project.copy { - def ballerinaDist = "build/jballerina-tools-${ballerinaExtension.langVersion}" - into ballerinaDist - into('repo/bala') { - from "${artifactExtractedPath}/bala" - } - into('repo/cache') { - from "${artifactExtractedPath}/cache" - } + println("[Warning] skip downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") + return + } + + dependsOn(project.unpackStdLibs) + doLast { + /* Standard Libraries */ + project.configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact -> + def artifactExtractedPath = "${project.buildDir}/extracted-stdlibs/" + artifact.name + '-zip' + project.copy { + def ballerinaDist = "build/jballerina-tools-${ballerinaExtension.langVersion}" + into ballerinaDist + into('repo/bala') { + from "${artifactExtractedPath}/bala" } - project.copy { - def runtimePath = "${project.rootDir}/target/ballerina-runtime" - into runtimePath - into('repo/bala') { - from "${artifactExtractedPath}/bala" - } - into('repo/cache') { - from "${artifactExtractedPath}/cache" - } + into('repo/cache') { + from "${artifactExtractedPath}/cache" + } + } + project.copy { + def runtimePath = "${project.rootDir}/target/ballerina-runtime" + into runtimePath + into('repo/bala') { + from "${artifactExtractedPath}/bala" + } + into('repo/cache') { + from "${artifactExtractedPath}/cache" } } } From a45b7c3363967e34ef2cc13969ac2e96a33bdb91 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 6 Aug 2024 19:21:52 +0530 Subject: [PATCH 08/12] Restructure bal-version parsing error handling logic --- .../io/ballerina/plugin/BallerinaPlugin.groovy | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 6888b00..5e0211b 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -260,15 +260,15 @@ class BallerinaPlugin implements Plugin { } def versionOutput = output.toString() - def installedVersion = "" - try { - def balVersion = versionOutput.split("\n")[0] - installedVersion = balVersion - .replaceAll("Ballerina ", '') - .replaceAll("\\(Swan Lake Update \\d+\\)", '') - .replaceAll("\\s+", '') - } catch (Exception e) { - throw new GradleException("Failed to parse the Ballerina version: $versionOutput", e) + def balVersion = versionOutput.split("\n")[0] + def installedVersion = balVersion + .replaceAll("Ballerina ", '') + .replaceAll("\\(Swan Lake Update \\d+\\)", '') + .replaceAll("\\s+", '') + + def balVersionPattern = ~/^\d{4}\.\d+\.\d+$/ + if (!balVersionPattern.matcher(installedVersion).matches()) { + throw new GradleException("Failed to parse the Ballerina version. Balllerina CLI output: $versionOutput Extracted version:$installedVersion") } def configuredVersion = project.findProperty('ballerinaLangVersion') From 1d7b800520e97d6e3dea6ae4a19496dc17889d75 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 6 Aug 2024 19:24:03 +0530 Subject: [PATCH 09/12] Restructure build task depedencies --- src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 5e0211b..24aa643 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -134,11 +134,12 @@ class BallerinaPlugin implements Plugin { } project.tasks.register('unpackStdLibs') { + dependsOn(project.unpackJballerinaTools) + if (ballerinaExtension.isConnector) { return } - dependsOn(project.unpackJballerinaTools) doLast { project.configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact -> project.copy { @@ -150,12 +151,13 @@ class BallerinaPlugin implements Plugin { } project.tasks.register('copyStdlibs') { + dependsOn(project.unpackStdLibs) + if (ballerinaExtension.isConnector) { println("[Warning] skip downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") return } - dependsOn(project.unpackStdLibs) doLast { /* Standard Libraries */ project.configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact -> From 3f722d660061aec2e12b624ab231e6d018f53d5e Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 6 Aug 2024 19:25:08 +0530 Subject: [PATCH 10/12] Restructure build task depedencies --- src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 24aa643..d5b1ad9 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -152,7 +152,7 @@ class BallerinaPlugin implements Plugin { project.tasks.register('copyStdlibs') { dependsOn(project.unpackStdLibs) - + if (ballerinaExtension.isConnector) { println("[Warning] skip downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") return From 56c535c784feb7415f0f9df0734e243f5da6bd6f Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 6 Aug 2024 23:01:16 +0530 Subject: [PATCH 11/12] Fix issue in jbalTools download logic --- src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index d5b1ad9..5415bef 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -91,11 +91,7 @@ class BallerinaPlugin implements Plugin { return } - if (ballerinaExtension.langVersion == null) { - jbalTools("org.ballerinalang:jballerina-tools:${ballerinaExtension.langVersion}") { - transitive = false - } - } else { + if (ballerinaExtension.langVersion != null) { jbalTools("org.ballerinalang:jballerina-tools:${ballerinaExtension.langVersion}") { transitive = false } From 55570ed2761ecff7e4301fa5376b44fbc1a45628 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 6 Aug 2024 23:08:12 +0530 Subject: [PATCH 12/12] Print warning log for using local bal-distribution in depedencies block --- .../ballerina/plugin/BallerinaPlugin.groovy | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy index 5415bef..c1f25c4 100644 --- a/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy +++ b/src/main/groovy/io/ballerina/plugin/BallerinaPlugin.groovy @@ -88,6 +88,7 @@ class BallerinaPlugin implements Plugin { project.dependencies { if (ballerinaExtension.isConnector) { + println("[Warning] skip downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") return } @@ -105,10 +106,6 @@ class BallerinaPlugin implements Plugin { } } - if (ballerinaExtension.isConnector) { - return - } - doLast { project.configurations.jbalTools.resolvedConfiguration.resolvedArtifacts.each { artifact -> project.copy { @@ -132,10 +129,6 @@ class BallerinaPlugin implements Plugin { project.tasks.register('unpackStdLibs') { dependsOn(project.unpackJballerinaTools) - if (ballerinaExtension.isConnector) { - return - } - doLast { project.configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact -> project.copy { @@ -149,11 +142,6 @@ class BallerinaPlugin implements Plugin { project.tasks.register('copyStdlibs') { dependsOn(project.unpackStdLibs) - if (ballerinaExtension.isConnector) { - println("[Warning] skip downloading jBallerinaTools dependency: project uses locally installed Ballerina distribution to build the module") - return - } - doLast { /* Standard Libraries */ project.configurations.ballerinaStdLibs.resolvedConfiguration.resolvedArtifacts.each { artifact -> @@ -374,18 +362,18 @@ class BallerinaPlugin implements Plugin { project.exec { workingDir project.projectDir environment 'JAVA_OPTS', '-DBALLERINA_DEV_COMPILE_BALLERINA_ORG=true' - if (!ballerinaExtension.isConnector) { + if (ballerinaExtension.isConnector) { if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', "$distributionBinPath/bal.bat push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala --repository=local && exit %%ERRORLEVEL%%" + commandLine 'cmd', '/c', "bal.bat push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala --repository=local && exit %%ERRORLEVEL%%" } else { - commandLine 'sh', '-c', "$distributionBinPath/bal push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala --repository=local" + commandLine 'sh', '-c', "bal push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala --repository=local" } } else { if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', "bal.bat push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala --repository=local && exit %%ERRORLEVEL%%" + commandLine 'cmd', '/c', "$distributionBinPath/bal.bat push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala --repository=local && exit %%ERRORLEVEL%%" } else { - commandLine 'sh', '-c', "bal push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala --repository=local" - } + commandLine 'sh', '-c', "$distributionBinPath/bal push ${balBuildTarget}/bala/${packageOrg}-${packageName}-${platform}-${balaVersion}.bala --repository=local" + } } } }