diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/default/DefaultConfigs.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/default/DefaultConfigs.kt index 66c7b14..3bc3d61 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/default/DefaultConfigs.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/default/DefaultConfigs.kt @@ -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() override val excludeKeys get() = mutableListOf() + override val includeKeys get() = mutableListOf() override val isEnableExcludeNonStringValue get() = true override val isEnableTypeAutoConversion get() = true override val isEnableValueInterpolation get() = true diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/factory/SweetPropertyConfigsFactory.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/factory/SweetPropertyConfigsFactory.kt index 1475783..aa7392c 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/factory/SweetPropertyConfigsFactory.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/factory/SweetPropertyConfigsFactory.kt @@ -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 diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/proxy/ISweetPropertyConfigs.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/proxy/ISweetPropertyConfigs.kt index 65bc329..79297d9 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/proxy/ISweetPropertyConfigs.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/proxy/ISweetPropertyConfigs.kt @@ -129,6 +129,9 @@ internal interface ISweetPropertyConfigs { /** 被排除的属性键值名称数组 */ val excludeKeys: MutableList + /** 被包含的属性键值名称数组 */ + val includeKeys: MutableList + /** 是否启用排除非字符串类型键值内容 */ val isEnableExcludeNonStringValue: Boolean diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt index b7f0914..54ae04d 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt @@ -206,6 +206,9 @@ open class SweetPropertyConfigureExtension internal constructor() { /** 当前被排除的属性键值名称数组 */ internal var excludeKeys: MutableList? = null + /** 当前被包含的属性键值名称数组 */ + internal var includeKeys: MutableList? = null + /** 当前生成位置类型数组 */ internal var generateLocationTypes: Array? = 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() + } + /** * 设置从何处生成属性键值 * diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/helper/PropertiesDeployHelper.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/helper/PropertiesDeployHelper.kt index 5b0b7a2..7c7a931 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/helper/PropertiesDeployHelper.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/helper/PropertiesDeployHelper.kt @@ -256,43 +256,49 @@ internal object PropertiesDeployHelper { val propteries = mutableMapOf() 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() - 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() + }.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() - /** - * 处理键值内容 - * @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 }