Skip to content

Commit

Permalink
Merge pull request #1 from UbiqueInnovation/feature/property-utils
Browse files Browse the repository at this point in the history
Add utility function to read gradle properties
  • Loading branch information
M-Wong authored Oct 2, 2024
2 parents 0d8e7d5 + 2da30f9 commit 78ba436
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This gradle plugin applies a preset of configurations we use at Ubique to an And
## Preset configuration

The following configurations are applied by this plugin:

* A `default` flavor dimension is created
* The `applicationIsSuffix` of each flavor (except `prod`) is set to `.{flavor}`
* Enable the generation of the `BuildConfig` class
Expand All @@ -20,6 +21,13 @@ The following configurations are applied by this plugin:
* Sets the Kotlin `jvmTarget` to Java 17
* Sets the `isAbortOnError` flag of the `lintOptions` to false

In addition to automatically applying a preset configuration, this plugin also provides a few utility functions for usage in build
scripts:

* Methods to read a property from either the project properties or a `local.properties` file in the module or project root
* `readProperty(propertyName)` for nullable properties
* `readPropertyWithDefault(propertyName, defaultValue)` for non-nullable properties

## Usage

```kotlin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
abstract class PresetPlugin : Plugin<Project> {

override fun apply(project: Project) {
val extension = project.extensions.create("presetPlugin", PresetPluginConfig::class.java, project)
val extension = project.extensions.create("ubiquePreset", PresetPluginConfig::class.java, project)

val androidExtension = getAndroidExtension(project)
val androidComponentExtension = getAndroidComponentsExtension(project)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ch.ubique.gradle.preset.utils

import org.gradle.api.Project
import java.util.*

/**
* Read a property from the project properties or from a local.properties file
*
* @return The value of the property or null if it is not found
*/
fun Project.readProperty(propertyName: String): String? {
if (this.hasProperty(propertyName)) {
return this.properties[propertyName] as? String?
} else {
val localPropertiesFileName = "local.properties"

val properties = Properties()
if (this.file(localPropertiesFileName).exists()) {
properties.load(this.file(localPropertiesFileName).inputStream())
} else if (this.rootProject.file(localPropertiesFileName).exists()) {
properties.load(this.rootProject.file(localPropertiesFileName).inputStream())
}

return if (properties.containsKey(propertyName)) {
properties.getProperty(propertyName)
} else {
null
}
}
}

/**
* Read a property from the project properties or from a local.properties file
*
* @param propertyName The key of the property to be read
* @param default The default value to be returned if the property is not found
* @return The value of the property or the default value if it is not found
*/
fun Project.readPropertyWithDefault(propertyName: String, default: String) = readProperty(propertyName) ?: default

0 comments on commit 78ba436

Please sign in to comment.