Skip to content

Commit

Permalink
Make properties null-by-default
Browse files Browse the repository at this point in the history
  • Loading branch information
ashdavies committed Dec 9, 2024
1 parent 22c3719 commit 6c75c09
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 79 deletions.
19 changes: 4 additions & 15 deletions app-check/app-check-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,10 @@ plugins {
}

buildConfig {
val firebaseAndroidAppId by stringPropertyOrNull { value ->
buildConfigField<String?>("FIREBASE_ANDROID_APP_ID", value)
}

val gcloudProject by stringPropertyOrNull { value ->
buildConfigField<String?>("GCLOUD_PROJECT", value)
}

val gcpProject by stringPropertyOrNull { value ->
buildConfigField<String?>("GCP_PROJECT", value)
}

val googleCloudProject by stringPropertyOrNull { value ->
buildConfigField<String?>("GOOGLE_CLOUD_PROJECT", value)
}
val firebaseAndroidAppId by stringProperty(::buildConfigField)
val gcloudProject by stringProperty(::buildConfigField)
val gcpProject by stringProperty(::buildConfigField)
val googleCloudProject by stringProperty(::buildConfigField)

packageName.set("io.ashdavies.check")
}
Expand Down
9 changes: 2 additions & 7 deletions app-launcher/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ android {
}

buildConfig {
val androidApiKey by stringProperty { value ->
buildConfigField("ANDROID_API_KEY", value)
}

val androidStrictMode by booleanProperty { value ->
buildConfigField("ANDROID_STRICT_MODE", value)
}
val androidApiKey by stringProperty(::buildConfigField)
val androidStrictMode by stringProperty(::buildConfigField)

packageName.set(android.namespace)
}
Expand Down
4 changes: 1 addition & 3 deletions app-launcher/desktop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ plugins {
}

buildConfig {
val browserApiKey by stringProperty { value ->
buildConfigField("BROWSER_API_KEY", value)
}
val browserApiKey by stringProperty(::buildConfigField)

packageName.set("io.ashdavies.playground")
}
Expand Down
14 changes: 3 additions & 11 deletions cloud-run/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@ application {
}

buildConfig {
val firebaseAndroidAppId by stringPropertyOrNull { value ->
buildConfigField<String?>("FIREBASE_ANDROID_APP_ID", value)
}

val googleServiceAccountId by stringPropertyOrNull { value ->
buildConfigField<String?>("GOOGLE_SERVICE_ACCOUNT_ID", value)
}

val integrationApiKey by stringPropertyOrNull { value ->
buildConfigField<String?>("INTEGRATION_API_KEY", value)
}
val firebaseAndroidAppId by stringProperty(::buildConfigField)
val googleServiceAccountId by stringProperty(::buildConfigField)
val integrationApiKey by stringProperty(::buildConfigField)

packageName.set("io.ashdavies.cloud")
}
Expand Down
26 changes: 13 additions & 13 deletions conferences-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ android {
enableV3Signing = true
enableV4Signing = true

val keyStoreFile by stringPropertyOrNull {
if (it != null) storeFile = rootProject.file(it)
val keyStoreFile by stringProperty { _, value ->
if (value != null) storeFile = rootProject.file(value)
}

val keyStorePassword by stringPropertyOrNull {
if (it != null) storePassword = it
val keyStorePassword by stringProperty { _, value ->
if (value != null) storePassword = value
}

val releaseKeyAlias by stringPropertyOrNull {
if (it != null) keyAlias = it
val releaseKeyAlias by stringProperty { _, value ->
if (value != null) keyAlias = value
}

val releaseKeyPassword by stringPropertyOrNull {
if (it != null) keyPassword = it
val releaseKeyPassword by stringProperty { _, value ->
if (value != null) keyPassword = value
}
}

Expand All @@ -68,8 +68,8 @@ android {
}

defaultConfig {
val versionCode by stringPropertyOrNull {
versionCode = it?.toInt() ?: 1
val versionCode by stringProperty { _, value ->
versionCode = value?.toInt() ?: 1
}

versionName = "1.0.0"
Expand All @@ -96,9 +96,9 @@ android {
}

buildConfig {
val androidApiKey by stringPropertyWithTag(::buildConfigField)
val androidStrictMode by booleanPropertyWithTag(::buildConfigField)
val browserApiKey by stringPropertyWithTag(::buildConfigField)
val androidApiKey by stringProperty(::buildConfigField)
val androidStrictMode by booleanProperty(::buildConfigField)
val browserApiKey by stringProperty(::buildConfigField)

packageName.set(android.namespace)
}
Expand Down
30 changes: 8 additions & 22 deletions fused-properties/src/main/kotlin/StringProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,23 @@ private fun Project.stringPropertyProvider(definition: PropertyDefinition): Prov
.orElse(providers.environmentVariable(definition.envPropertyName))
}

public fun Project.booleanProperty(block: (Boolean) -> Unit = { }): ReadOnlyDelegateProvider<Boolean> {
return readOnlyDelegateProvider { provider, _ -> provider.get().toBoolean().also(block) }
public fun Project.booleanProperty(block: (String, Boolean?) -> Unit): ReadOnlyDelegateProvider<Boolean?> {
return readOnlyDelegateProvider { provider, tag -> provider.get().toBoolean().also { block(tag, it) } }
}

public fun Project.booleanPropertyWithTag(action: (String, Boolean) -> Unit): ReadOnlyDelegateProvider<Boolean> {
return readOnlyDelegateProvider { provider, tag -> provider.get().toBoolean().also { action(tag, it) } }
}

public fun Project.stringProperty(block: (String) -> Unit = { }): ReadOnlyDelegateProvider<String> {
return readOnlyDelegateProvider { provider, _ -> provider.get().also(block) }
}

public fun Project.stringPropertyWithTag(action: (String, String) -> Unit): ReadOnlyDelegateProvider<String> {
return readOnlyDelegateProvider { provider, tag -> provider.get().also { action(tag, it) } }
}

public fun Project.stringPropertyOrNull(block: (String?) -> Unit = { }): ReadOnlyDelegateProvider<String?> {
return readOnlyDelegateProvider { provider, _ -> provider.orNull.also(block) }
public fun Project.stringProperty(block: (String, String?) -> Unit): ReadOnlyDelegateProvider<String?> {
return readOnlyDelegateProvider { provider, tag -> provider.orNull.also { block(tag, it) } }
}

private fun <T> Project.readOnlyDelegateProvider(
transform: (provider: Provider<String>, tag: String) -> T,
): ReadOnlyDelegateProvider<T> = ReadOnlyDelegateProvider { _, property ->
val definition = PropertyDefinition(property.name)
val provider = stringPropertyProvider(definition)

val value = transform(
stringPropertyProvider(definition),
definition.envPropertyName,
)

ReadOnlyProperty { _, _ -> value }
ReadOnlyProperty { _, _ ->
transform(provider, definition.envPropertyName)
}
}

private fun <S : Any, T> Provider<S>.mapOrNull(block: (S) -> T?): Provider<T> {
Expand Down
4 changes: 1 addition & 3 deletions identity-manager/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ android {
}

buildConfig {
val serverClientId by stringProperty { value ->
buildConfigField("SERVER_CLIENT_ID", value)
}
val serverClientId by stringProperty(::buildConfigField)

packageName.set(android.namespace)
}
Expand Down
8 changes: 3 additions & 5 deletions map-routes/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ plugins {

android {
defaultConfig {
val androidApiKey by stringProperty { value ->
manifestPlaceholders["ANDROID_API_KEY"] = value
val androidApiKey by stringProperty { _, value ->
manifestPlaceholders["ANDROID_API_KEY"] = "$value"
}
}

namespace = "io.ashdavies.routes"
}

buildConfig {
val androidApiKey by stringProperty { value ->
buildConfigField("ANDROID_API_KEY", value)
}
val androidApiKey by stringProperty(::buildConfigField)

packageName.set(android.namespace)
}
Expand Down

0 comments on commit 6c75c09

Please sign in to comment.