Skip to content

Commit 021e8e1

Browse files
committed
WIP: Evaluate lazy
1 parent cfc36fb commit 021e8e1

File tree

11 files changed

+59
-39
lines changed

11 files changed

+59
-39
lines changed

core/src/main/kotlin/androidx/build/gradle/core/FileSystemStorageService.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package androidx.build.gradle.core
1919

20+
import org.gradle.api.provider.Provider
2021
import java.io.File
2122
import java.io.InputStream
2223
import java.nio.file.Files
@@ -26,7 +27,7 @@ import java.nio.file.Files
2627
*/
2728
class FileSystemStorageService(
2829
override val bucketName: String,
29-
override val isPush: Boolean,
30+
override val isPush: Provider<Boolean>,
3031
override val isEnabled: Boolean
3132
) : StorageService {
3233

@@ -50,7 +51,7 @@ class FileSystemStorageService(
5051
return false
5152
}
5253

53-
if (!isPush) {
54+
if (!isPush.getOrElse(false)) {
5455
return false
5556
}
5657

@@ -67,7 +68,7 @@ class FileSystemStorageService(
6768
return false
6869
}
6970

70-
if (!isPush) {
71+
if (!isPush.getOrElse(false)) {
7172
return false
7273
}
7374

core/src/main/kotlin/androidx/build/gradle/core/RemoteGradleBuildCache.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,28 @@
1717

1818
package androidx.build.gradle.core
1919

20+
import org.gradle.api.provider.Property
2021
import org.gradle.caching.configuration.AbstractBuildCache
2122

2223
/**
2324
* Gradle Build Cache that uses a cloud storage provider as a backing to load and store Gradle cache results.
2425
*/
2526
abstract class RemoteGradleBuildCache : AbstractBuildCache() {
2627

28+
/**
29+
* Runtime switch that determines whether the build attempts to upload entries
30+
* to the remote build cache.
31+
*
32+
* Unlike [org.gradle.caching.configuration.BuildCache.isPush], this flag is **not**
33+
* part of the Gradle Configuration Cache model’s fingerprint and is only used at execution
34+
* time.
35+
*
36+
* Keeping `push = true` in `settings.gradle[.kts]` and gating uploads with this property allows you to
37+
* toggle pushing without invalidating the configuration cache
38+
*
39+
*/
40+
abstract val runtimePush: Property<Boolean>
41+
2742
/**
2843
* The name of the bucket that is used to store all the gradle cache entries.
2944
* This essentially becomes the root of all cache entries.

core/src/main/kotlin/androidx/build/gradle/core/StorageService.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package androidx.build.gradle.core
1919

20+
import org.gradle.api.provider.Property
21+
import org.gradle.api.provider.Provider
2022
import java.io.Closeable
2123
import java.io.InputStream
2224

@@ -29,7 +31,7 @@ interface StorageService : Closeable {
2931
/**
3032
* `true` if the underlying storage service supports writes and deletes.
3133
*/
32-
val isPush: Boolean
34+
val isPush: Provider<Boolean>
3335

3436
/**
3537
* If `true`, use the underlying storage service.

gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package androidx.build.gradle.gcpbuildcache
2020
import androidx.build.gradle.core.FileSystemStorageService
2121
import androidx.build.gradle.core.blobKey
2222
import org.gradle.api.logging.Logging
23+
import org.gradle.api.provider.Provider
2324
import org.gradle.caching.BuildCacheEntryReader
2425
import org.gradle.caching.BuildCacheEntryWriter
2526
import org.gradle.caching.BuildCacheKey
@@ -39,7 +40,7 @@ internal class GcpBuildCacheService(
3940
private val bucketName: String,
4041
gcpCredentials: GcpCredentials,
4142
messageOnAuthenticationFailure: String,
42-
isPush: Boolean,
43+
isPush: Provider<Boolean>,
4344
isEnabled: Boolean,
4445
inTestMode: Boolean = false
4546
) : BuildCacheService {

gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheServiceFactory.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class GcpBuildCacheServiceFactory : BuildCacheServiceFactory<GcpBuildCache> {
3232
.type("GCP-backed")
3333
.config("projectId", buildCache.projectId)
3434
.config("bucketName", buildCache.bucketName)
35-
.config("isPushSupported", "${buildCache.isPush}")
3635
.config("isEnabled", "${buildCache.isEnabled}")
3736
.config(
3837
"usingExportedKeyCredentials",
@@ -44,7 +43,7 @@ class GcpBuildCacheServiceFactory : BuildCacheServiceFactory<GcpBuildCache> {
4443
buildCache.bucketName,
4544
buildCache.credentials,
4645
buildCache.messageOnAuthenticationFailure,
47-
buildCache.isPush,
46+
buildCache.runtimePush.orElse(buildCache.isPush),
4847
buildCache.isEnabled
4948
)
5049
service.validateConfiguration()

gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageService.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.google.cloud.http.HttpTransportOptions
2727
import com.google.cloud.storage.*
2828
import org.gradle.api.GradleException
2929
import org.gradle.api.logging.Logging
30+
import org.gradle.api.provider.Provider
3031
import java.io.InputStream
3132

3233
/**
@@ -37,13 +38,13 @@ internal class GcpStorageService(
3738
override val bucketName: String,
3839
gcpCredentials: GcpCredentials,
3940
messageOnAuthenticationFailure: String,
40-
override val isPush: Boolean,
41+
override val isPush: Provider<Boolean>,
4142
override val isEnabled: Boolean,
4243
private val sizeThreshold: Long = BLOB_SIZE_THRESHOLD
4344
) : StorageService {
4445

4546
private val storageOptions by lazy {
46-
storageOptions(projectId, gcpCredentials, messageOnAuthenticationFailure, isPush)
47+
storageOptions(projectId, gcpCredentials, messageOnAuthenticationFailure)
4748
}
4849

4950
override fun load(cacheKey: String): InputStream? {
@@ -62,7 +63,7 @@ internal class GcpStorageService(
6263
return false
6364
}
6465

65-
if (!isPush) {
66+
if (!isPush.get()) {
6667
logger.info("No push support")
6768
return false
6869
}
@@ -77,7 +78,7 @@ internal class GcpStorageService(
7778
return false
7879
}
7980

80-
if (!isPush) {
81+
if (!isPush.get()) {
8182
return false
8283
}
8384
val blobId = BlobId.of(bucketName, cacheKey)
@@ -164,12 +165,10 @@ internal class GcpStorageService(
164165
projectId: String,
165166
gcpCredentials: GcpCredentials,
166167
messageOnAuthenticationFailure: String,
167-
isPushSupported: Boolean
168168
): StorageOptions? {
169169
val credentials = credentials(
170170
gcpCredentials,
171171
messageOnAuthenticationFailure,
172-
isPushSupported
173172
) ?: return null
174173
val retrySettings = RetrySettings.newBuilder()
175174
retrySettings.maxAttempts = 3
@@ -241,14 +240,10 @@ internal class GcpStorageService(
241240
private fun credentials(
242241
gcpCredentials: GcpCredentials,
243242
messageOnAuthenticationFailure: String,
244-
isPushSupported: Boolean
245243
): GoogleCredentials? {
246-
val scopes = mutableListOf(
247-
STORAGE_READ_ONLY,
244+
val scopes = listOf(
245+
STORAGE_READ_ONLY, STORAGE_READ_WRITE, STORAGE_FULL_CONTROL
248246
)
249-
if (isPushSupported) {
250-
scopes += listOf(STORAGE_READ_WRITE, STORAGE_FULL_CONTROL)
251-
}
252247
return when (gcpCredentials) {
253248
is ApplicationDefaultGcpCredentials -> {
254249
defaultApplicationGcpCredentials(scopes, messageOnAuthenticationFailure, forceClearCache = false)

gcpbuildcache/src/test/kotlin/androidx/build/gradle/gcpbuildcache/GcpStorageServiceTest.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package androidx.build.gradle.gcpbuildcache
1919

20+
import org.gradle.api.model.ObjectFactory
21+
import org.gradle.testfixtures.ProjectBuilder
2022
import org.junit.Assume.assumeNotNull
2123
import org.junit.Test
2224
import java.io.File
@@ -27,6 +29,8 @@ import java.io.File
2729
class GcpStorageServiceTest {
2830
private val serviceAccountPath = System.getenv()["GRADLE_CACHE_SERVICE_ACCOUNT_PATH"]
2931

32+
private val objectFactory: ObjectFactory = ProjectBuilder.builder().build().objects
33+
3034
@Test
3135
fun testStoreBlob() {
3236
assumeNotNull(serviceAccountPath)
@@ -35,7 +39,7 @@ class GcpStorageServiceTest {
3539
bucketName = BUCKET_NAME,
3640
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
3741
messageOnAuthenticationFailure = "Please re-authenticate",
38-
isPush = true,
42+
isPush = objectFactory.property(Boolean::class.java).convention(true),
3943
isEnabled = true,
4044
sizeThreshold = 0L
4145
)
@@ -56,7 +60,7 @@ class GcpStorageServiceTest {
5660
bucketName = BUCKET_NAME,
5761
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
5862
messageOnAuthenticationFailure = "Please re-authenticate",
59-
isPush = true,
63+
isPush = objectFactory.property(Boolean::class.java).convention(true),
6064
isEnabled = true,
6165
sizeThreshold = 0L
6266
)
@@ -80,7 +84,7 @@ class GcpStorageServiceTest {
8084
bucketName = BUCKET_NAME,
8185
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
8286
messageOnAuthenticationFailure = "Please re-authenticate",
83-
isPush = false,
87+
isPush = objectFactory.property(Boolean::class.java).convention(false),
8488
isEnabled = true,
8589
sizeThreshold = 0L
8690
)
@@ -100,7 +104,7 @@ class GcpStorageServiceTest {
100104
bucketName = BUCKET_NAME,
101105
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
102106
messageOnAuthenticationFailure = "Please re-authenticate",
103-
isPush = true,
107+
isPush = objectFactory.property(Boolean::class.java).convention(true),
104108
isEnabled = true,
105109
sizeThreshold = 0L
106110
)
@@ -109,7 +113,7 @@ class GcpStorageServiceTest {
109113
bucketName = BUCKET_NAME,
110114
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath)),
111115
messageOnAuthenticationFailure = "Please re-authenticate",
112-
isPush = false,
116+
isPush = objectFactory.property(Boolean::class.java).convention(false),
113117
isEnabled = true,
114118
sizeThreshold = 0L
115119
)
@@ -135,7 +139,7 @@ class GcpStorageServiceTest {
135139
bucketName = BUCKET_NAME,
136140
gcpCredentials = ExportedKeyGcpCredentials(File(serviceAccountPath!!)),
137141
messageOnAuthenticationFailure = "Please re-authenticate",
138-
isPush = true,
142+
isPush = objectFactory.property(Boolean::class.java).convention(true),
139143
isEnabled = false,
140144
sizeThreshold = 0L
141145
)

s3buildcache/src/main/kotlin/androidx/build/gradle/s3buildcache/S3BuildCacheService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package androidx.build.gradle.s3buildcache
2020
import androidx.build.gradle.core.FileSystemStorageService
2121
import androidx.build.gradle.core.blobKey
2222
import org.gradle.api.logging.Logging
23+
import org.gradle.api.provider.Property
2324
import org.gradle.caching.BuildCacheEntryReader
2425
import org.gradle.caching.BuildCacheEntryWriter
2526
import org.gradle.caching.BuildCacheKey
@@ -42,7 +43,7 @@ class S3BuildCacheService(
4243
credentials: S3Credentials,
4344
region: String,
4445
bucketName: String,
45-
isPush: Boolean,
46+
isPush: Property<Boolean>,
4647
isEnabled: Boolean,
4748
reducedRedundancy: Boolean,
4849
inTestMode: Boolean = false

s3buildcache/src/main/kotlin/androidx/build/gradle/s3buildcache/S3BuildCacheServiceFactory.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ class S3BuildCacheServiceFactory : BuildCacheServiceFactory<S3BuildCache> {
3434
.config("region", buildCache.region)
3535
.config("bucketName", buildCache.bucketName)
3636
.config("reducedRedundancy", "${buildCache.reducedRedundancy}")
37-
.config("isPushSupported", "${buildCache.isPush}")
3837
.config("isEnabled", "${buildCache.isEnabled}")
3938
.config("credentialsType", "${buildCache.credentials}")
4039

4140
val service = S3BuildCacheService(
4241
region = buildCache.region,
4342
bucketName = buildCache.bucketName,
44-
isPush = buildCache.isPush,
43+
isPush = buildCache.runtimePush,
4544
isEnabled = buildCache.isEnabled,
4645
reducedRedundancy = buildCache.reducedRedundancy,
4746
credentials = buildCache.credentials

s3buildcache/src/main/kotlin/androidx/build/gradle/s3buildcache/S3StorageService.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import androidx.build.gradle.core.FileHandleInputStream
2121
import androidx.build.gradle.core.FileHandleInputStream.Companion.handleInputStream
2222
import androidx.build.gradle.core.StorageService
2323
import org.gradle.api.logging.Logging
24-
import software.amazon.awssdk.core.exception.SdkClientException
25-
import software.amazon.awssdk.core.exception.SdkException
24+
import org.gradle.api.provider.Property
2625
import software.amazon.awssdk.core.exception.SdkServiceException
2726
import software.amazon.awssdk.core.sync.RequestBody
2827
import software.amazon.awssdk.services.s3.S3Client
@@ -37,7 +36,7 @@ import kotlin.io.path.outputStream
3736

3837
class S3StorageService(
3938
override val bucketName: String,
40-
override val isPush: Boolean,
39+
override val isPush: Property<Boolean>,
4140
override val isEnabled: Boolean,
4241
private val client: S3Client,
4342
private val region: String,
@@ -65,7 +64,7 @@ class S3StorageService(
6564
return false
6665
}
6766

68-
if (!isPush) {
67+
if (!isPush.get()) {
6968
logger.info("No push support")
7069
return false
7170
}
@@ -94,7 +93,7 @@ class S3StorageService(
9493
return false
9594
}
9695

97-
if (!isPush) {
96+
if (!isPush.get()) {
9897
logger.info("No push support")
9998
return false
10099
}

0 commit comments

Comments
 (0)