mirror of
https://github.com/HighCapable/SweetProperty.git
synced 2025-09-04 01:35:37 +08:00
feat: add keyValuesRules function
This commit is contained in:
@@ -23,6 +23,7 @@ package com.highcapable.sweetproperty.plugin.config.default
|
||||
|
||||
import com.highcapable.sweetproperty.plugin.config.proxy.ISweetPropertyConfigs
|
||||
import com.highcapable.sweetproperty.plugin.extension.dsl.configure.SweetPropertyConfigureExtension
|
||||
import com.highcapable.sweetproperty.plugin.generator.factory.PropertyValueRule
|
||||
|
||||
/**
|
||||
* 默认配置类实现类
|
||||
@@ -78,6 +79,10 @@ internal object DefaultConfigs {
|
||||
get() = selfBase?.includeKeys
|
||||
?: globalBase?.includeKeys
|
||||
?: baseGenerateConfigs(name).includeKeys
|
||||
override val keyValuesRules
|
||||
get() = selfBase?.keyValuesRules
|
||||
?: globalBase?.keyValuesRules
|
||||
?: baseGenerateConfigs(name).keyValuesRules
|
||||
override val isEnableExcludeNonStringValue
|
||||
get() = selfBase?.isEnableExcludeNonStringValue
|
||||
?: globalBase?.isEnableExcludeNonStringValue
|
||||
@@ -130,6 +135,10 @@ internal object DefaultConfigs {
|
||||
get() = selfBase?.includeKeys
|
||||
?: globalBase?.includeKeys
|
||||
?: baseGenerateConfigs(name).includeKeys
|
||||
override val keyValuesRules
|
||||
get() = selfBase?.keyValuesRules
|
||||
?: globalBase?.keyValuesRules
|
||||
?: baseGenerateConfigs(name).keyValuesRules
|
||||
override val isEnableExcludeNonStringValue
|
||||
get() = selfBase?.isEnableExcludeNonStringValue
|
||||
?: globalBase?.isEnableExcludeNonStringValue
|
||||
@@ -160,6 +169,7 @@ internal object DefaultConfigs {
|
||||
override val permanentKeyValues get() = mutableMapOf<String, Any>()
|
||||
override val excludeKeys get() = mutableListOf<Any>()
|
||||
override val includeKeys get() = mutableListOf<Any>()
|
||||
override val keyValuesRules get() = mutableMapOf<String, PropertyValueRule>()
|
||||
override val isEnableExcludeNonStringValue get() = true
|
||||
override val isEnableTypeAutoConversion get() = true
|
||||
override val isEnableValueInterpolation get() = true
|
||||
|
@@ -108,6 +108,10 @@ private fun SweetPropertyConfigureExtension.SourcesCodeGenerateConfigureExtensio
|
||||
get() = this@create.includeKeys
|
||||
?: global?.includeKeys
|
||||
?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).includeKeys
|
||||
override val keyValuesRules
|
||||
get() = this@create.keyValuesRules
|
||||
?: global?.keyValuesRules
|
||||
?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).keyValuesRules
|
||||
override val isEnableExcludeNonStringValue
|
||||
get() = this@create.isEnableExcludeNonStringValue
|
||||
?: selfBase?.isEnableExcludeNonStringValue
|
||||
@@ -175,6 +179,10 @@ private fun SweetPropertyConfigureExtension.BuildScriptGenerateConfigureExtensio
|
||||
get() = this@create.includeKeys
|
||||
?: global?.includeKeys
|
||||
?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).includeKeys
|
||||
override val keyValuesRules
|
||||
get() = this@create.keyValuesRules
|
||||
?: global?.keyValuesRules
|
||||
?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).keyValuesRules
|
||||
override val isEnableExcludeNonStringValue
|
||||
get() = this@create.isEnableExcludeNonStringValue
|
||||
?: selfBase?.isEnableExcludeNonStringValue
|
||||
|
@@ -24,6 +24,7 @@ package com.highcapable.sweetproperty.plugin.config.proxy
|
||||
import com.highcapable.sweetproperty.SweetProperty
|
||||
import com.highcapable.sweetproperty.generated.SweetPropertyProperties
|
||||
import com.highcapable.sweetproperty.plugin.config.type.GenerateLocationType
|
||||
import com.highcapable.sweetproperty.plugin.generator.factory.PropertyValueRule
|
||||
|
||||
/**
|
||||
* [SweetProperty] 配置类接口类
|
||||
@@ -132,6 +133,9 @@ internal interface ISweetPropertyConfigs {
|
||||
/** 被包含的属性键值名称数组 */
|
||||
val includeKeys: MutableList<Any>
|
||||
|
||||
/** 属性键值规则数组 */
|
||||
val keyValuesRules: MutableMap<String, PropertyValueRule>
|
||||
|
||||
/** 是否启用排除非字符串类型键值内容 */
|
||||
val isEnableExcludeNonStringValue: Boolean
|
||||
|
||||
|
@@ -28,6 +28,7 @@ import com.highcapable.sweetproperty.gradle.factory.isUnSafeExtName
|
||||
import com.highcapable.sweetproperty.plugin.config.factory.create
|
||||
import com.highcapable.sweetproperty.plugin.config.proxy.ISweetPropertyConfigs
|
||||
import com.highcapable.sweetproperty.plugin.config.type.GenerateLocationType
|
||||
import com.highcapable.sweetproperty.plugin.generator.factory.PropertyValueRule
|
||||
import com.highcapable.sweetproperty.utils.debug.SError
|
||||
import com.highcapable.sweetproperty.utils.noEmpty
|
||||
import org.gradle.api.Action
|
||||
@@ -212,6 +213,9 @@ open class SweetPropertyConfigureExtension internal constructor() {
|
||||
/** 当前被包含的属性键值名称数组 */
|
||||
internal var includeKeys: MutableList<Any>? = null
|
||||
|
||||
/** 当前属性键值规则数组 */
|
||||
internal var keyValuesRules: MutableMap<String, PropertyValueRule>? = null
|
||||
|
||||
/** 当前生成位置类型数组 */
|
||||
internal var generateLocationTypes: Array<GenerateLocationType>? = null
|
||||
|
||||
@@ -355,6 +359,51 @@ open class SweetPropertyConfigureExtension internal constructor() {
|
||||
includeKeys = keys.distinct().toMutableList()
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置属性键值规则数组
|
||||
*
|
||||
* 你可以设置一组键值规则 - 使用 [createValueRule] 创建新的规则 - 用于解析得到的键值内容
|
||||
*
|
||||
* 示例如下 ↓
|
||||
*
|
||||
* ```kotlin
|
||||
* keyValuesRules(
|
||||
* "some.key1" to createValueRule { if (it.contains("_")) it.replace("_", "-") else it },
|
||||
* "some.key2" to createValueRule { "$it-value" }
|
||||
* )
|
||||
* ```
|
||||
*
|
||||
* 这些键值规则在属性键值存在它们时被应用
|
||||
* @param pairs 属性键值规则数组
|
||||
*/
|
||||
@JvmName("-kotlin-dsl-only-keyValuesRules-")
|
||||
fun keyValuesRules(vararg pairs: Pair<String, PropertyValueRule>) {
|
||||
if (pairs.isEmpty()) SError.make("Key-values rules must not be empty")
|
||||
if (pairs.any { it.first.isBlank() }) SError.make("Key-values rules must not have blank contents")
|
||||
keyValuesRules = mutableMapOf(*pairs)
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置属性键值规则数组 (Groovy 兼容方法)
|
||||
*
|
||||
* 你可以设置一组键值规则 - 使用 [createValueRule] 创建新的规则 - 用于解析得到的键值内容
|
||||
*
|
||||
* 这些键值规则在属性键值存在它们时被应用
|
||||
* @param rules 属性键值规则数组
|
||||
*/
|
||||
fun keyValuesRules(rules: Map<String, PropertyValueRule>) {
|
||||
if (rules.isEmpty()) SError.make("Key-values rules must not be empty")
|
||||
if (rules.any { it.key.isBlank() }) SError.make("Key-values rules must not have blank contents")
|
||||
keyValuesRules = rules.toMutableMap()
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的属性键值规则
|
||||
* @param rule 回调当前规则
|
||||
* @return [PropertyValueRule]
|
||||
*/
|
||||
fun createValueRule(rule: PropertyValueRule) = rule
|
||||
|
||||
/**
|
||||
* 设置从何处生成属性键值
|
||||
*
|
||||
|
@@ -26,6 +26,9 @@ import kotlin.reflect.KClass
|
||||
/** 属性键值数组类型定义 */
|
||||
internal typealias PropertyMap = MutableMap<String, Any>
|
||||
|
||||
/** 属性键值规则类型定义 */
|
||||
internal typealias PropertyValueRule = (value: String) -> String
|
||||
|
||||
/**
|
||||
* 解析到键值内容类型
|
||||
* @param isAutoConversion 是否自动转换类型
|
||||
|
@@ -283,7 +283,7 @@ internal object PropertiesDeployHelper {
|
||||
}
|
||||
} ?: true
|
||||
}.toMutableMap().also { resolveKeyValues ->
|
||||
resolveKeyValues.forEach { (key, value) ->
|
||||
resolveKeyValues.onEach { (key, value) ->
|
||||
val resolveKeys = mutableListOf<String>()
|
||||
|
||||
/**
|
||||
@@ -298,6 +298,8 @@ internal object PropertiesDeployHelper {
|
||||
else resolveValue
|
||||
}
|
||||
if (value.hasInterpolation()) resolveKeyValues[key] = value.resolveValue()
|
||||
}.takeIf { configs.keyValuesRules.isNotEmpty() }?.forEach { (key, value) ->
|
||||
configs.keyValuesRules[key]?.also { resolveKeyValues[key] = it(value) }
|
||||
}; propteries.putAll(resolveKeyValues)
|
||||
}
|
||||
}; return propteries
|
||||
|
Reference in New Issue
Block a user