From 16fc7f9c00bfb6c6d76278208a46a75a772a75d4 Mon Sep 17 00:00:00 2001 From: Lamberto Basti Date: Thu, 23 May 2024 17:24:52 +0200 Subject: [PATCH] Add functionality to generate new build file Extended the PackageSearchGradleModule to generate a new build file if it does not exist when adding a dependency. The build file is created with appropriate syntax based on whether its format is Kotlin or not. This enhancement caters for both scenarios enhancing our application's flexibility. (cherry picked from commit 5492ba3fdb254e7ce2bc81b98df48d9b54aa5390) --- .../gradle/PackageSearchGradleModule.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/plugin/gradle/base/src/main/kotlin/com/jetbrains/packagesearch/plugin/gradle/PackageSearchGradleModule.kt b/plugin/gradle/base/src/main/kotlin/com/jetbrains/packagesearch/plugin/gradle/PackageSearchGradleModule.kt index ab2f8dcf..2142a41e 100644 --- a/plugin/gradle/base/src/main/kotlin/com/jetbrains/packagesearch/plugin/gradle/PackageSearchGradleModule.kt +++ b/plugin/gradle/base/src/main/kotlin/com/jetbrains/packagesearch/plugin/gradle/PackageSearchGradleModule.kt @@ -20,6 +20,10 @@ import com.jetbrains.packagesearch.plugin.core.utils.validateRepositoryType import com.jetbrains.packagesearch.plugin.gradle.utils.toUnifiedRepository import com.jetbrains.packagesearch.plugin.gradle.utils.validateRepositoryType import java.nio.file.Path +import kotlin.io.path.createParentDirectories +import kotlin.io.path.exists +import kotlin.io.path.extension +import kotlin.io.path.writeText import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import org.jetbrains.packagesearch.api.v3.ApiPackage @@ -86,6 +90,22 @@ data class PackageSearchGradleModule( selectedScope: String?, ) { validateMavenPackageType(apiPackage) + + if (buildFilePath == null || !buildFilePath.exists()) { + val isKotlin = buildFilePath?.extension?.equals("kts", ignoreCase = true) == true + buildFilePath?.createParentDirectories() + ?.writeText(buildString { + appendLine("dependencies {") + if (isKotlin) { + appendLine(" $selectedScope(\"${apiPackage.groupId}:${apiPackage.artifactId}:${selectedVersion}\")") + } else { + appendLine(" $selectedScope '${apiPackage.groupId}:${apiPackage.artifactId}:${selectedVersion}'") + } + appendLine("}") + }) + return + } + modifier.addDependency( module = nativeModule, descriptor = UnifiedDependency(