mirror of
https://github.com/HighCapable/SweetProperty.git
synced 2025-09-04 01:35:37 +08:00
feat: add includeKeys function
This commit is contained in:
@@ -75,6 +75,10 @@ internal object DefaultConfigs {
|
||||
get() = selfBase?.excludeKeys
|
||||
?: globalBase?.excludeKeys
|
||||
?: baseGenerateConfigs(name).excludeKeys
|
||||
override val includeKeys
|
||||
get() = selfBase?.includeKeys
|
||||
?: globalBase?.includeKeys
|
||||
?: baseGenerateConfigs(name).includeKeys
|
||||
override val isEnableExcludeNonStringValue
|
||||
get() = selfBase?.isEnableExcludeNonStringValue
|
||||
?: globalBase?.isEnableExcludeNonStringValue
|
||||
@@ -123,6 +127,10 @@ internal object DefaultConfigs {
|
||||
get() = selfBase?.excludeKeys
|
||||
?: globalBase?.excludeKeys
|
||||
?: baseGenerateConfigs(name).excludeKeys
|
||||
override val includeKeys
|
||||
get() = selfBase?.includeKeys
|
||||
?: globalBase?.includeKeys
|
||||
?: baseGenerateConfigs(name).includeKeys
|
||||
override val isEnableExcludeNonStringValue
|
||||
get() = selfBase?.isEnableExcludeNonStringValue
|
||||
?: globalBase?.isEnableExcludeNonStringValue
|
||||
@@ -152,6 +160,7 @@ internal object DefaultConfigs {
|
||||
override val propertiesFileName get() = ISweetPropertyConfigs.DEFAULT_PROPERTIES_FILE_NAME
|
||||
override val permanentKeyValues get() = mutableMapOf<String, Any>()
|
||||
override val excludeKeys get() = mutableListOf<Any>()
|
||||
override val includeKeys get() = mutableListOf<Any>()
|
||||
override val isEnableExcludeNonStringValue get() = true
|
||||
override val isEnableTypeAutoConversion get() = true
|
||||
override val isEnableValueInterpolation get() = true
|
||||
|
@@ -104,6 +104,10 @@ private fun SweetPropertyConfigureExtension.SourcesCodeGenerateConfigureExtensio
|
||||
get() = this@create.excludeKeys
|
||||
?: global?.excludeKeys
|
||||
?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).excludeKeys
|
||||
override val includeKeys
|
||||
get() = this@create.includeKeys
|
||||
?: global?.includeKeys
|
||||
?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).includeKeys
|
||||
override val isEnableExcludeNonStringValue
|
||||
get() = this@create.isEnableExcludeNonStringValue
|
||||
?: selfBase?.isEnableExcludeNonStringValue
|
||||
@@ -167,6 +171,10 @@ private fun SweetPropertyConfigureExtension.BuildScriptGenerateConfigureExtensio
|
||||
get() = this@create.excludeKeys
|
||||
?: global?.excludeKeys
|
||||
?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).excludeKeys
|
||||
override val includeKeys
|
||||
get() = this@create.includeKeys
|
||||
?: global?.includeKeys
|
||||
?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).includeKeys
|
||||
override val isEnableExcludeNonStringValue
|
||||
get() = this@create.isEnableExcludeNonStringValue
|
||||
?: selfBase?.isEnableExcludeNonStringValue
|
||||
|
@@ -129,6 +129,9 @@ internal interface ISweetPropertyConfigs {
|
||||
/** 被排除的属性键值名称数组 */
|
||||
val excludeKeys: MutableList<Any>
|
||||
|
||||
/** 被包含的属性键值名称数组 */
|
||||
val includeKeys: MutableList<Any>
|
||||
|
||||
/** 是否启用排除非字符串类型键值内容 */
|
||||
val isEnableExcludeNonStringValue: Boolean
|
||||
|
||||
|
@@ -206,6 +206,9 @@ open class SweetPropertyConfigureExtension internal constructor() {
|
||||
/** 当前被排除的属性键值名称数组 */
|
||||
internal var excludeKeys: MutableList<Any>? = null
|
||||
|
||||
/** 当前被包含的属性键值名称数组 */
|
||||
internal var includeKeys: MutableList<Any>? = null
|
||||
|
||||
/** 当前生成位置类型数组 */
|
||||
internal var generateLocationTypes: Array<GenerateLocationType>? = null
|
||||
|
||||
@@ -313,6 +316,20 @@ open class SweetPropertyConfigureExtension internal constructor() {
|
||||
excludeKeys = keys.distinct().toMutableList()
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置需要包含的属性键值名称数组
|
||||
*
|
||||
* 在这里可以设置一些你希望从已知的属性键值中包含的键值名称
|
||||
*
|
||||
* 这些键值在属性键值存在它们时被包含 - 未被包含的键值不会出现在生成的代码中
|
||||
* @param keys 键值名称数组 - 你可以传入 [Regex] 或使用 [String.toRegex] 以使用正则功能
|
||||
*/
|
||||
fun includeKeys(vararg keys: Any) {
|
||||
if (keys.isEmpty()) SError.make("Include keys must not be empty")
|
||||
if (keys.any { it.toString().isBlank() }) SError.make("Include keys must not have blank contents")
|
||||
includeKeys = keys.distinct().toMutableList()
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置从何处生成属性键值
|
||||
*
|
||||
|
@@ -256,43 +256,49 @@ internal object PropertiesDeployHelper {
|
||||
val propteries = mutableMapOf<String, Any>()
|
||||
configs.permanentKeyValues.forEach { (key, value) -> propteries[key] = value }
|
||||
configs.generateLocationTypes.forEach {
|
||||
val nativeKeyValues = when (it) {
|
||||
val keyValues = when (it) {
|
||||
GenerateLocationType.CURRENT_PROJECT -> createProperties(configs, descriptor.currentDir)
|
||||
GenerateLocationType.ROOT_PROJECT -> createProperties(configs, descriptor.rootDir)
|
||||
GenerateLocationType.GLOBAL -> createProperties(configs, descriptor.homeDir)
|
||||
GenerateLocationType.SYSTEM -> System.getProperties()
|
||||
GenerateLocationType.SYSTEM_ENV -> System.getenv()
|
||||
} ?: emptyMap()
|
||||
val resolveKeyValues = mutableMapOf<String, Any>()
|
||||
nativeKeyValues.forEach native@{ (key, value) ->
|
||||
val hasExcludeKeys = configs.excludeKeys.noEmpty()?.any { content ->
|
||||
when (content) {
|
||||
is Regex -> content.toString().isNotBlank() && content.matches(key.toString())
|
||||
else -> content.toString() == key
|
||||
}
|
||||
} ?: false
|
||||
if (hasExcludeKeys) return@native
|
||||
val isAvailableKeyValue = if (configs.isEnableExcludeNonStringValue)
|
||||
keyValues.filter { (key, value) ->
|
||||
if (configs.isEnableExcludeNonStringValue)
|
||||
key is CharSequence && key.isNotBlank() && value is CharSequence
|
||||
else key.toString().isNotBlank() && value != null
|
||||
if (isAvailableKeyValue) resolveKeyValues[key.toString()] = value
|
||||
}
|
||||
resolveKeyValues.forEach { (key, value) ->
|
||||
val resolveKeys = mutableListOf<String>()
|
||||
}.mapKeys { e -> e.key.toString() }.filter { (key, _) ->
|
||||
configs.includeKeys.noEmpty()?.any { content ->
|
||||
when (content) {
|
||||
is Regex -> content.matches(key)
|
||||
else -> content.toString() == key
|
||||
}
|
||||
} ?: true
|
||||
}.filter { (key, _) ->
|
||||
configs.excludeKeys.noEmpty()?.none { content ->
|
||||
when (content) {
|
||||
is Regex -> content.matches(key)
|
||||
else -> content.toString() == key
|
||||
}
|
||||
} ?: true
|
||||
}.toMutableMap().also { resolveKeyValues ->
|
||||
resolveKeyValues.forEach { (key, value) ->
|
||||
val resolveKeys = mutableListOf<String>()
|
||||
|
||||
/**
|
||||
* 处理键值内容
|
||||
* @return [String]
|
||||
*/
|
||||
fun String.resolveValue(): String = replaceInterpolation { matchKey ->
|
||||
if (resolveKeys.size > 5) SError.make("Key \"$key\" has been called recursively multiple times of those $resolveKeys")
|
||||
resolveKeys.add(matchKey)
|
||||
val resolveValue = if (configs.isEnableValueInterpolation) resolveKeyValues[matchKey]?.toString() ?: "" else matchKey
|
||||
if (resolveValue.hasInterpolation()) resolveValue.resolveValue()
|
||||
else resolveValue
|
||||
}
|
||||
if (value.toString().hasInterpolation()) resolveKeyValues[key] = value.toString().resolveValue()
|
||||
}; propteries.putAll(resolveKeyValues)
|
||||
/**
|
||||
* 处理键值内容
|
||||
* @return [String]
|
||||
*/
|
||||
fun String.resolveValue(): String = replaceInterpolation { matchKey ->
|
||||
if (resolveKeys.size > 5) SError.make("Key \"$key\" has been called recursively multiple times of those $resolveKeys")
|
||||
resolveKeys.add(matchKey)
|
||||
val resolveValue = if (configs.isEnableValueInterpolation) resolveKeyValues[matchKey]?.toString() ?: "" else matchKey
|
||||
if (resolveValue.hasInterpolation()) resolveValue.resolveValue()
|
||||
else resolveValue
|
||||
}
|
||||
if (value.toString().hasInterpolation()) resolveKeyValues[key] = value.toString().resolveValue()
|
||||
}; propteries.putAll(resolveKeyValues)
|
||||
}
|
||||
}; return propteries
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user