Skip to content

Commit

Permalink
Make supported locales lazily computed providers (#1139)
Browse files Browse the repository at this point in the history
Allows lazier property chaining

<!--
  ⬆ Put your description above this! ⬆

  Please be descriptive and detailed.
  
Please read our [Contributing
Guidelines](https://github.com/tinyspeck/foundry/blob/main/.github/CONTRIBUTING.md)
and [Code of Conduct](https://slackhq.github.io/code-of-conduct).

Don't worry about deleting this, it's not visible in the PR!
-->
  • Loading branch information
ZacSweers authored Dec 11, 2024
1 parent 8dbfab6 commit a9cf346
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class foundry/gradle/properties/PropertyResolver {
public final fun optionalStringValue (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
public static synthetic fun optionalStringValue$default (Lfoundry/gradle/properties/PropertyResolver;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
public final fun providerFor (Ljava/lang/String;)Lorg/gradle/api/provider/Provider;
public final fun requiredStringProvider (Ljava/lang/String;)Lorg/gradle/api/provider/Provider;
public final fun stringValue (Ljava/lang/String;)Ljava/lang/String;
public final fun stringValue (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ public class PropertyResolver(
?: error("No property for '$key' found and no default value was provided.")
}

public fun requiredStringProvider(key: String): Provider<String> {
return optionalStringProvider(key)
.orElse(
providers.provider {
error("No property for '$key' found and no default value was provided.")
}
)
}

public fun optionalStringValue(key: String, defaultValue: String? = null): String? {
return providerFor(key).orNull ?: defaultValue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,21 @@ internal fun robolectricJars(gradleUserHomeDir: File, createDirsIfMissing: Boole
}
}

public fun Project.supportedLanguages(supportedLanguages: SupportedLanguagesEnum): List<String> {
public fun Project.supportedLanguages(
supportedLanguages: SupportedLanguagesEnum
): Provider<List<String>> {
val foundryProperties = project.foundryProperties
val gaLanguages = foundryProperties.supportedLanguages.split(",")
val gaLanguages = foundryProperties.supportedLanguages.map { it.split(",") }

val internalLanguages = foundryProperties.supportedLanguagesInternal.split(",")
val internalLanguages = foundryProperties.supportedLanguagesInternal.map { it.split(",") }

val betaLanguages = foundryProperties.supportedLanguagesBeta.split(",")
val betaLanguages = foundryProperties.supportedLanguagesBeta.map { it.split(",") }

return when (supportedLanguages) {
SupportedLanguagesEnum.GA -> gaLanguages.toList().filter { it.isNotBlank() }
SupportedLanguagesEnum.INTERNAL ->
internalLanguages.union(gaLanguages).toList().filter { it.isNotBlank() }
SupportedLanguagesEnum.BETA ->
betaLanguages.union(gaLanguages).toList().filter { it.isNotBlank() }
}
SupportedLanguagesEnum.GA -> gaLanguages
SupportedLanguagesEnum.INTERNAL -> internalLanguages.zip(gaLanguages, List<String>::union)
SupportedLanguagesEnum.BETA -> betaLanguages.zip(gaLanguages, List<String>::union)
}.map { it.filter { it.isNotBlank() } }
}

public enum class SupportedLanguagesEnum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ internal constructor(
get() = booleanProperty("foundry.config.noPlatform")

/** Property corresponding to the supported languages in GA builds */
public val supportedLanguages: String
get() = stringProperty("foundry.android.supportedLanguages")
public val supportedLanguages: Provider<String>
get() = resolver.requiredStringProvider("foundry.android.supportedLanguages")

/** Property corresponding to the supported languages in Internal builds */
public val supportedLanguagesInternal: String
get() = stringProperty("foundry.android.supportedLanguagesInternal")
public val supportedLanguagesInternal: Provider<String>
get() = resolver.requiredStringProvider("foundry.android.supportedLanguagesInternal")

/** Property corresponding to the supported languages in Beta builds */
public val supportedLanguagesBeta: String
get() = stringProperty("foundry.android.supportedLanguagesBeta")
public val supportedLanguagesBeta: Provider<String>
get() = resolver.requiredStringProvider("foundry.android.supportedLanguagesBeta")

/**
* Property corresponding to the file path of a custom versions.json file for use with
Expand Down

0 comments on commit a9cf346

Please sign in to comment.