@file:JvmName("PropertiesUtil")

package org.moe.gradle.utils


import groovy.lang.MissingPropertyException
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.internal.project.DefaultProject
import java.util.Properties

private const val LOCAL_PROPERTIES: String = "local.properties"

private fun Project.tryLocalProperty(key: String): String? {
    val f = file(LOCAL_PROPERTIES)
    if (f.exists()) {
        val propFile = Properties()
        propFile.load(f.inputStream())

        return propFile.getProperty(key)
    }

    return null
}

fun Project.tryGetProperty(key: String): String? {
    val localProp = tryLocalProperty(key)
    if (localProp != null) {
        return localProp
    }

    if (hasProperty(key)) {
        val result = property(key)
        if (result != null && result !is String) {
            throw GradleException("Value of $key property is not a String")
        }

        return result as String?
    }

    return null
}

@Throws(MissingPropertyException::class)
fun Project.getProperty(key: String): String {
    val localProp = tryLocalProperty(key)
    if (localProp != null) {
        return localProp
    }

    val result = property(key) ?: throw (this as DefaultProject).asDynamicObject.getMissingProperty(key)

    if (result !is String) {
        throw GradleException("Value of $key property is not a String")
    }

    return result
}

@file:JvmName("PropertiesUtil")

package org.moe.gradle.utils


import groovy.lang.MissingPropertyException
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.internal.project.DefaultProject
import java.util.Properties

private const val LOCAL_PROPERTIES: String = "local.properties"

private fun Project.tryLocalProperty(key: String): String? {
    val f = file(LOCAL_PROPERTIES)
    if (f.exists()) {
        val propFile = Properties()
        propFile.load(f.inputStream())

        return propFile.getProperty(key)
    }

    return parent?.tryLocalProperty(key)
}

fun Project.tryGetProperty(key: String): String? {
    val localProp = tryLocalProperty(key)
    if (localProp != null) {
        return localProp
    }

    if (hasProperty(key)) {
        val result = property(key)
        if (result != null && result !is String) {
            throw GradleException("Value of $key property is not a String")
        }

        return result as String?
    }

    return null
}

@Throws(MissingPropertyException::class)
fun Project.getProperty(key: String): String {
    val localProp = tryLocalProperty(key)
    if (localProp != null) {
        return localProp
    }

    val result = property(key) ?: throw (this as DefaultProject).asDynamicObject.getMissingProperty(key)

    if (result !is String) {
        throw GradleException("Value of $key property is not a String")
    }

    return result
}

Support local property on multi-module project
Support local property on multi-module project

(cherry picked from commit a3b6dabb0305860cfd36a3c76e1fcf80e3361bf0)

multi-os-engine/moe-plugin-gradle,multi-os-engine/moe-plugin-gradle