1 /*
<lambda>null2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.baselineprofile.gradle.utils
18 
19 import com.google.common.truth.StringSubject
20 import com.google.common.truth.Truth.assertThat
21 import java.io.File
22 import kotlin.io.path.Path
23 import org.gradle.testkit.runner.GradleRunner
24 
25 internal val GRADLE_CODE_PRINT_TASK =
26     """
27     abstract class DisplaySourceSets extends DefaultTask {
28         @Input abstract ListProperty<Directory> getSrcs()
29         @TaskAction void exec() {
30             srcs.get().forEach { directory -> println(directory) }
31         }
32     }
33 
34     abstract class PrintTask extends DefaultTask {
35         @Input abstract Property<String> getText()
36         @TaskAction void exec() { println(getText().get()) }
37     }
38 
39     androidComponents {
40         def agpVersion = pluginVersion.major + "." + pluginVersion.minor + "." + pluginVersion.micro
41         if (pluginVersion.previewType != null) {
42             agpVersion += "-" + pluginVersion.previewType + pluginVersion.preview
43         }
44         println("agpVersion=" + agpVersion)
45     }
46 
47     """
48         .trimIndent()
49 
50 internal fun GradleRunner.build(vararg arguments: String, block: (String) -> (Unit)) =
51     this.withArguments(*arguments, "--stacktrace").build().output.also(block)
52 
53 internal fun GradleRunner.buildAndFail(vararg arguments: String, block: (String) -> (Unit)) =
54     this.withArguments(*arguments, "--stacktrace").buildAndFail().output.also(block)
55 
56 internal fun GradleRunner.buildAndAssertThatOutput(
57     vararg arguments: String,
58     assertBlock: StringSubject.() -> (Unit)
59 ) {
60     this.build(*arguments) { assertBlock(assertThat(it)) }
61 }
62 
buildAndFailAndAssertThatOutputnull63 internal fun GradleRunner.buildAndFailAndAssertThatOutput(
64     vararg arguments: String,
65     assertBlock: StringSubject.() -> (Unit)
66 ) {
67     this.buildAndFail(*arguments) { assertBlock(assertThat(it)) }
68 }
69 
requireInOrdernull70 internal fun List<String>.requireInOrder(
71     vararg toFind: String,
72     predicate: (String, String) -> (Boolean) = { line, nextToFind -> line.startsWith(nextToFind) }
73 ): List<String> {
<lambda>null74     var remaining = toFind.filter { it.isNotBlank() }.toMutableList()
linenull75     for (line in this) {
76         val next = remaining.firstOrNull() ?: return emptyList()
77         if (predicate(line, next)) remaining.removeFirst()
78     }
79     return remaining
80 }
81 
requirenull82 internal fun List<String>.require(
83     vararg strings: String,
84     evaluate: (String, String) -> (Boolean) = { line, nextToFind -> line.startsWith(nextToFind) },
85 ): Set<String> {
86     val remaining = mutableSetOf(*strings)
87     val iterator = remaining.iterator()
88     while (iterator.hasNext()) {
89         val next = iterator.next()
90         for (string in this) {
91             if (evaluate(string, next)) {
92                 iterator.remove()
93                 break
94             }
95         }
96     }
97     return remaining
98 }
99 
containsOnlynull100 internal fun List<String>.containsOnly(vararg strings: String): Boolean =
101     toSet().union(setOf(*strings)).size == this.size
102 
103 fun camelCase(vararg strings: String): String {
104     if (strings.isEmpty()) return ""
105     return StringBuilder()
106         .apply {
107             var shouldCapitalize = false
108             for (str in strings.filter { it.isNotBlank() }) {
109                 append(if (shouldCapitalize) str.capitalized() else str)
110                 shouldCapitalize = true
111             }
112         }
113         .toString()
114 }
115 
toUrinull116 fun File.toUri() = Path(canonicalPath).toUri()
117