1 /*
2  * Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.atomicfu.gradle.plugin.test.framework.runner
6 
7 import java.io.File
8 import java.nio.file.Files
9 
10 internal class GradleBuild(val projectName: String, val targetDir: File) {
11     var enableJvmIrTransformation = false
12     var enableJsIrTransformation = false
13     var enableNativeIrTransformation = false
14 
15     private val properties
<lambda>null16         get() = buildList {
17             add("-P$KOTLIN_VERSION=$kotlinVersion")
18             add("-P${ATOMICFU_VERSION}=$atomicfuVersion")
19             add("-P$ENABLE_JVM_IR_TRANSFORMATION=$enableJvmIrTransformation")
20             add("-P$ENABLE_JS_IR_TRANSFORMATION=$enableJsIrTransformation")
21             add("-P$ENABLE_NATIVE_IR_TRANSFORMATION=$enableNativeIrTransformation")
22         }
23 
24     private var runCount = 0
25 
runGradlenull26     fun runGradle(commands: List<String>): BuildResult =
27         buildGradleByShell(runCount++, commands, properties).also {
28             require(it.isSuccessful) { "Running $commands on project $projectName FAILED with error:\n" + it.output }
29         }
30 }
31 
32 internal class BuildResult(exitCode: Int, private val logFile: File) {
33     val isSuccessful: Boolean = exitCode == 0
34 
<lambda>null35     val output: String by lazy { logFile.readText() }
36 
37     // Gets the list of dependencies for the given configuration
getDependenciesForConfignull38     fun getDependenciesForConfig(configuration: String): List<String> {
39         val lines = output.lines()
40         val result = mutableListOf<String>()
41         var index = 0
42         while (index < lines.size) {
43             val line = lines[index++]
44             if (line.substringBefore(" ") == configuration) break
45         }
46         while(index < lines.size) {
47             val line = lines[index++]
48             if (line.isBlank() || line == "No dependencies") break
49             // trim leading indentations (\---) and symbols in the end (*):
50             // \--- org.jetbrains.kotlinx:atomicfu:0.22.0-SNAPSHOT (n)
51             result.add(line.dropWhile { !it.isLetterOrDigit() }.substringBefore(" "))
52         }
53         return result
54     }
55 }
56 
createGradleBuildFromSourcesnull57 internal fun createGradleBuildFromSources(projectName: String): GradleBuild {
58     val projectDir = projectExamplesDir.resolve(projectName)
59     val targetDir = Files.createTempDirectory("${projectName.substringAfterLast('/')}-").toFile().apply {
60         projectDir.copyRecursively(this)
61     }
62     return GradleBuild(projectName, targetDir)
63 }
64