-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from UbiqueInnovation/feature/property-utils
Add utility function to read gradle properties
- Loading branch information
Showing
3 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
preset/plugin/src/main/kotlin/ch/ubique/gradle/preset/utils/PropertyUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |