1 /*
<lambda>null2  * Copyright 2021 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.playground
18 
19 import java.net.InetAddress
20 import java.net.URI
21 import java.util.function.Function
22 import org.gradle.api.Plugin
23 import org.gradle.api.initialization.Settings
24 import org.gradle.caching.http.HttpBuildCache
25 import org.gradle.kotlin.dsl.develocity
26 
27 class GradleDevelocityConventionsPlugin : Plugin<Settings> {
28     override fun apply(settings: Settings) {
29         settings.apply(mapOf("plugin" to "com.gradle.develocity"))
30         settings.apply(mapOf("plugin" to "com.gradle.common-custom-user-data-gradle-plugin"))
31 
32         // Github Actions always sets a "CI" environment variable
33         val isCI = System.getenv("CI") != null
34 
35         settings.develocity {
36             server.set("https://ge.androidx.dev")
37             buildScan.apply {
38                 uploadInBackground.set(!isCI)
39                 capture.fileFingerprints.set(true)
40                 obfuscation.hostname(HostnameHider())
41                 obfuscation.ipAddresses(IpAddressHider())
42                 publishing.onlyIf {
43                     it.isAuthenticated
44                 }
45             }
46         }
47 
48         settings.buildCache.local { local ->
49             // Aggressively clean up stale build cache entries on CI
50             if (isCI) {
51                 local.removeUnusedEntriesAfterDays = 1
52             }
53         }
54 
55         settings.buildCache.remote(HttpBuildCache::class.java) { remote ->
56             remote.url = URI("https://ge.androidx.dev/cache/")
57             val buildCachePassword = System.getenv("GRADLE_BUILD_CACHE_PASSWORD")
58             if (isCI && !buildCachePassword.isNullOrEmpty()) {
59                 remote.isPush = true
60                 remote.credentials { credentials ->
61                     credentials.username = "ci"
62                     credentials.password = buildCachePassword
63                 }
64             } else {
65                 remote.isPush = false
66             }
67         }
68     }
69 
70     /**
71      * This class needs to be a concrete class and not a lambda due to
72      * https://github.com/gradle/gradle/issues/19047
73      */
74     private class HostnameHider : Function<String?, String> {
75         override fun apply(originalHostName: String?): String {
76             return "unset"
77         }
78     }
79 
80     /**
81      * This class needs to be a concrete class and not a lambda due to
82      * https://github.com/gradle/gradle/issues/19047
83      */
84     private class IpAddressHider : Function<List<InetAddress>, List<String>> {
85         override fun apply(list: List<InetAddress>): List<String> {
86             return listOf("0.0.0.0")
87         }
88     }
89 }
90