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

Enable cache-redirector #453

Merged
merged 3 commits into from
Jul 30, 2024
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
5 changes: 0 additions & 5 deletions build-settings-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation(libs.gradle.develocity)
}
2 changes: 2 additions & 0 deletions build-settings-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
rootProject.name = "build-settings-logic"

dependencyResolutionManagement {

@Suppress("UnstableApiUsage")
repositories {
mavenCentral()
Expand All @@ -15,3 +16,4 @@ dependencyResolutionManagement {
}

apply(from = "src/main/kotlin/atomicfu-gradle-build-cache.settings.gradle.kts")
apply(from = "src/main/kotlin/atomicfu-cache-redirector.settings.gradle.kts")

Large diffs are not rendered by default.

37 changes: 15 additions & 22 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
rootProject.name = "buildSrc"

pluginManagement {
includeBuild("../build-settings-logic")
}

dependencyResolutionManagement {

@Suppress("UnstableApiUsage")
repositories {
/*
* This property group is used to build kotlinx.atomicfu against Kotlin compiler snapshots.
* When build_snapshot_train is set to true, mavenLocal and Sonatype snapshots are added to repository list
* (the former is required for AFU and public, the latter is required for compiler snapshots).
* DO NOT change the name of these properties without adapting kotlinx.train build chain.
*/
val buildSnapshotTrainGradleProperty = providers.gradleProperty("build_snapshot_train")
if (buildSnapshotTrainGradleProperty.isPresent) {
maven(url = uri("https://oss.sonatype.org/content/repositories/snapshots"))
}

val additionalRepositoryProperty = providers.gradleProperty("community.project.kotlin.repo")
.orElse(providers.gradleProperty("kotlin_repo_url"))
val additionalRepositoryProperty = providers.gradleProperty("kotlin_repo_url")
if (additionalRepositoryProperty.isPresent) {
maven(url = uri(additionalRepositoryProperty.get()))
logger.info("A custom Kotlin repository ${additionalRepositoryProperty.get()} was added")
Expand All @@ -27,26 +36,10 @@ dependencyResolutionManagement {
if (kotlinVersion != null) {
version("kotlin", kotlinVersion)
}

val overridingKotlinVersion = providers.gradleProperty("community.project.kotlin.version").orNull
if (overridingKotlinVersion != null) {
logger.info("An overriding Kotlin version of $overridingKotlinVersion was found for buildSrc")
version("kotlin", overridingKotlinVersion)
}

/*
* This property group is used to build kotlinx.atomicfu against Kotlin compiler snapshots.
* When build_snapshot_train is set to true, kotlin_version property is overridden with kotlin_snapshot_version.
* Additionally, mavenLocal and Sonatype snapshots are added to repository list
* (the former is required for AFU and public, the latter is required for compiler snapshots).
* DO NOT change the name of these properties without adapting kotlinx.train build chain.
*/
val buildSnapshotTrainGradleProperty = providers.gradleProperty("build_snapshot_train").orNull
if (buildSnapshotTrainGradleProperty != null && buildSnapshotTrainGradleProperty != "") {
val kotlinVersion = providers.gradleProperty("kotlin_snapshot_version").orNull
?: throw IllegalArgumentException("'kotlin_snapshot_version' should be defined when building with a snapshot compiler")
version("kotlin", kotlinVersion)
}
}
}
}

plugins {
id("atomicfu-cache-redirector")
}
2 changes: 0 additions & 2 deletions buildSrc/src/main/kotlin/KotlinConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ private val LOGGER: Logger = Logger.getLogger("Kotlin settings logger")
* or null otherwise
*/
fun getCustomKotlinRepositoryURL(project: Project): String? {
val communityPluginKotlinRepoURL = project.findProperty("community.project.kotlin.repo") as? String
val gradlePropertyKotlinRepoURL = project.findProperty("kotlin_repo_url") as? String
val kotlinRepoURL = when {
communityPluginKotlinRepoURL != null -> communityPluginKotlinRepoURL
gradlePropertyKotlinRepoURL != null -> gradlePropertyKotlinRepoURL
else -> return null
}
Expand Down
7 changes: 5 additions & 2 deletions integration-testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ val Project.konanHome: String
get() = rootProject.properties["kotlin.native.home"]?.toString()
?: NativeCompilerDownloader(project).compilerDirectory.absolutePath

val embeddableJar = File(project.konanHome).resolve("konan/lib/kotlin-native-compiler-embeddable.jar")

tasks.withType<Test> {
val embeddableJar = File(project.konanHome).resolve("konan/lib/kotlin-native-compiler-embeddable.jar")
// Pass the path to native jars
systemProperty("kotlin.native.jar", embeddableJar)

// Pass the path to the cache redirector
val cacheRedirectorPath = project.file("../build-settings-logic/src/main/kotlin/atomicfu-cache-redirector.settings.gradle.kts")
systemProperty("cache.redirector.path", cacheRedirectorPath.absolutePath)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class BuildResult(exitCode: Int, private val logFile: File) {
val line = lines[index++]
if (line.substringBefore(" ") == configuration) break
}
while(index < lines.size) {
while (index < lines.size) {
val line = lines[index++]
if (line.isBlank() || line == "No dependencies") break
// trim leading indentations (\---) and symbols in the end (*):
Expand All @@ -63,14 +63,19 @@ internal fun createGradleBuildFromSources(projectName: String): GradleBuild {
val targetDir = Files.createTempDirectory("${projectName.substringAfterLast('/')}-").toFile().apply {
projectDir.copyRecursively(this)
}

targetDir.toPath().enableCacheRedirector()

kotlinArtifactsRepo?.let {
// kotlinArtifactsRepo contains the directory on the TC agent, where Kotlin artifacts were published
// (this directory was passed as kotlin_repo_url parameter to the library build).

// Add maven(url = file:///mnt/agent/work/...) repository in build.gradle and settings.gradle files of all integration tests.
targetDir.walkTopDown().filter { it.isFile && (it.name.startsWith("build.gradle") || it.name.startsWith("settings.gradle")) }.forEach { buildGradleFile ->
buildGradleFile.addKotlinArtifactRepositoryToProjectBuild(kotlinArtifactsRepo)
}
targetDir.walkTopDown()
.filter { it.isFile && (it.name.startsWith("build.gradle") || it.name.startsWith("settings.gradle")) }
.forEach { buildGradleFile ->
buildGradleFile.addKotlinArtifactRepositoryToProjectBuild(kotlinArtifactsRepo)
}
}
return GradleBuild(projectName, targetDir)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
package kotlinx.atomicfu.gradle.plugin.test.framework.runner

import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.createDirectories

internal fun GradleBuild.buildGradleByShell(
runIndex: Int,
Expand All @@ -24,11 +28,56 @@ internal fun GradleBuild.buildGradleByShell(
return BuildResult(exitCode, logFile)
}

internal fun Path.enableCacheRedirector() {

val cacheRedirectorPath =
System.getProperty("cache.redirector.path").ifBlank { null } ?: error("Cache redirector has not been set")

// Path relative to the current gradle module project dir
val redirectorScript =
Paths.get(cacheRedirectorPath)
.toAbsolutePath()
.normalize()
.toFile()

val gradleDir = resolve("gradle").also { it.createDirectories() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val gradleDir = resolve("gradle").also { it.createDirectories() }
val gradleDir = resolve("gradle").createDirectories()

nitpick: createDirectories() returns the same value, so also {} could be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this change to #460

redirectorScript.copyTo(gradleDir.resolve("cache-redirector.settings.gradle.kts").toFile())

val settingsGradle = resolve("settings.gradle")
val settingsGradleKts = resolve("settings.gradle.kts")
when {
Files.exists(settingsGradle) -> settingsGradle.modify {
"""
|${it.substringBefore("pluginManagement {")}
|pluginManagement {
| apply from: 'gradle/cache-redirector.settings.gradle.kts'
|${it.substringAfter("pluginManagement {")}
""".trimMargin()
}

Files.exists(settingsGradleKts) -> settingsGradleKts.modify {
"""
|${it.substringBefore("pluginManagement {")}
|pluginManagement {
| apply(from = "gradle/cache-redirector.settings.gradle.kts")
|${it.substringAfter("pluginManagement {")}
""".trimMargin()
}
}
}

private fun buildSystemCommand(projectDir: File, commands: List<String>, properties: List<String>): List<String> {
return if (isWindows)
listOf("cmd", "/C", "gradlew.bat", "-p", projectDir.canonicalPath) + commands + properties + "--no-daemon"
else
listOf("/bin/bash", "gradlew", "-p", projectDir.canonicalPath) + commands + properties + "--no-daemon"
}

private val isWindows: Boolean = System.getProperty("os.name")!!.contains("Windows")
private val isWindows: Boolean = System.getProperty("os.name")!!.contains("Windows")

private fun Path.modify(transform: (currentContent: String) -> String) {
assert(Files.isRegularFile(this)) { "$this is not a regular file!" }

val file = toFile()
file.writeText(transform(file.readText()))
}
42 changes: 13 additions & 29 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
rootProject.name = "kotlinx-atomicfu"

pluginManagement {
includeBuild("build-settings-logic")
repositories {
mavenCentral()
gradlePluginPortal()

val additionalRepositoryProperty = providers.gradleProperty("community.project.kotlin.repo")
.orElse(providers.gradleProperty("kotlin_repo_url"))
val additionalRepositoryProperty = providers.gradleProperty("kotlin_repo_url")
if (additionalRepositoryProperty.isPresent) {
maven(url = uri(additionalRepositoryProperty.get()))
logger.info("A custom Kotlin repository ${additionalRepositoryProperty.get()} was added")
}

/*
* This property group is used to build kotlinx.atomicfu against Kotlin compiler snapshots.
* When build_snapshot_train is set to true, kotlin_version property is overridden with kotlin_snapshot_version.
* Additionally, mavenLocal and Sonatype snapshots are added to repository list
* When build_snapshot_train is set to true, mavenLocal and Sonatype snapshots are added to repository list
* (the former is required for AFU and public, the latter is required for compiler snapshots).
* DO NOT change the name of these properties without adapting kotlinx.train build chain.
*/
val buildSnapshotTrainGradleProperty = providers.gradleProperty("build_snapshot_train")
if (buildSnapshotTrainGradleProperty.isPresent) {
maven(url = uri("https://oss.sonatype.org/content/repositories/snapshots"))
}

}
}

dependencyResolutionManagement {
val buildSnapshotTrainGradleProperty = providers.gradleProperty("build_snapshot_train")

@Suppress("UnstableApiUsage")
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

@Suppress("UnstableApiUsage")
repositories {

/*
* This property group is used to build kotlinx.atomicfu against Kotlin compiler snapshots.
* When build_snapshot_train is set to true, mavenLocal and Sonatype snapshots are added to repository list
* (the former is required for AFU and public, the latter is required for compiler snapshots).
* DO NOT change the name of these properties without adapting kotlinx.train build chain.
*/
val buildSnapshotTrainGradleProperty = providers.gradleProperty("build_snapshot_train")
if (buildSnapshotTrainGradleProperty.isPresent) {
maven(url = uri("https://oss.sonatype.org/content/repositories/snapshots"))
}

val additionalRepositoryProperty = providers.gradleProperty("community.project.kotlin.repo")
.orElse(providers.gradleProperty("kotlin_repo_url"))
val additionalRepositoryProperty = providers.gradleProperty("kotlin_repo_url")
if (additionalRepositoryProperty.isPresent) {
maven(url = uri(additionalRepositoryProperty.get()))
logger.info("A custom Kotlin repository ${additionalRepositoryProperty.get()} was added")
Expand Down Expand Up @@ -81,35 +85,15 @@ dependencyResolutionManagement {
if (kotlinVersion != null) {
version("kotlin", kotlinVersion)
}

val overridingKotlinVersion = providers.gradleProperty("community.project.kotlin.version").orNull
if (overridingKotlinVersion != null) {
logger.info("An overriding Kotlin version of $overridingKotlinVersion was found for root `kotlinx-atomicfu`")
version("kotlin", overridingKotlinVersion)
}

/*
* This property group is used to build kotlinx.atomicfu against Kotlin compiler snapshots.
* When build_snapshot_train is set to true, kotlin_version property is overridden with kotlin_snapshot_version.
* Additionally, mavenLocal and Sonatype snapshots are added to repository list
* (the former is required for AFU and public, the latter is required for compiler snapshots).
* DO NOT change the name of these properties without adapting kotlinx.train build chain.
*/
if (buildSnapshotTrainGradleProperty.isPresent && buildSnapshotTrainGradleProperty.get() != "") {
val kotlinSnapshotVersion = providers.gradleProperty("kotlin_snapshot_version").orNull
?: throw IllegalArgumentException("'kotlin_snapshot_version' should be defined when building with a snapshot compiler")
version("kotlin", kotlinSnapshotVersion)
}
}
}
}

rootProject.name = "kotlinx-atomicfu"

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0")
id("atomicfu-gradle-build-scan")
id("atomicfu-gradle-build-cache")
id("atomicfu-cache-redirector")
}

include("atomicfu")
Expand Down