aboutsummaryrefslogtreecommitdiff
path: root/atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/Project.kt
blob: f3f49e1ca40b4f099e961d553690b5cf3ebf66d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.atomicfu.plugin.gradle

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import java.io.File

class Project(val projectDir: File) {
    init {
        projectDir.resolve("build.gradle").modify {
            buildScript + "\n\n" + it
        }
    }

    private var isDebug = false
    private var printStdout = false

    @Deprecated("Should be used for debug only!")
    @Suppress("unused")
    fun debug() {
        isDebug = true
    }

    /**
     * Redirects Gradle runner output to stdout. Useful for debugging.
     */
    @Deprecated("Should be used for debug only!")
    @Suppress("unused")
    fun printStdout() {
        printStdout = true
    }

    fun gradle(vararg tasks: String): GradleRunner =
            GradleRunner.create()
                .withDebug(isDebug)
                .withProjectDir(projectDir)
                .withArguments(*(defaultArguments() + tasks))
                .run {
                    if (printStdout) {
                        forwardStdOutput(System.out.bufferedWriter())
                    } else {
                        this
                    }
                }

    fun build(vararg tasks: String, fn: BuildResult.() -> Unit = {}) {
        val gradle = gradle(*tasks)
        val buildResult = gradle.build()
        buildResult.fn()
    }

    @Suppress("unused")
    fun buildAndFail(vararg tasks: String, fn: BuildResult.() -> Unit = {}) {
        val gradle = gradle(*tasks)
        val buildResult = gradle.buildAndFail()
        buildResult.fn()
    }

    private fun defaultArguments(): Array<String> =
        arrayOf("--stacktrace")

    companion object {
        private fun readFileList(fileName: String): String {
            val resource = Project::class.java.classLoader.getResource(fileName)
                    ?: throw IllegalStateException("Could not find resource '$fileName'")
            val files = File(resource.toURI())
                    .readLines()
                    .map { File(it).absolutePath.replace("\\", "\\\\") } // escape backslashes in Windows paths
            return files.joinToString(", ") { "'$it'" }
        }

        private val buildScript = run {
            """
                buildscript {
                    dependencies {
                        classpath files(${readFileList("plugin-classpath.txt")})
                    }
                }

                repositories {
                    jcenter()
                    mavenCentral()
                    maven { url 'https://cache-redirector.jetbrains.com/maven.pkg.jetbrains.space/kotlin/p/kotlin/dev' }
                    maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
                    maven { url 'https://dl.bintray.com/kotlin/kotlin-dev' }
                    maven { url 'https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev' }
                    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
                }

                def atomicfuJvm = files(${readFileList("atomicfu-jvm.txt")})
                def atomicfuJs = files(${readFileList("atomicfu-js.txt")})
                def atomicfuMetadata = files(${readFileList("atomicfu-metadata.txt")})
            """.trimIndent()
        }
    }
}