From 9e2c26ae737049cd6b73d660d45d6bda08bc1343 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Wed, 6 Sep 2023 23:18:07 +0800 Subject: [PATCH] feat: add propertiesFileNames function --- .../plugin/config/default/DefaultConfigs.kt | 19 +++++----- .../factory/SweetPropertyConfigsFactory.kt | 16 ++++---- .../config/proxy/ISweetPropertyConfigs.kt | 4 +- .../SweetPropertyConfigureExtension.kt | 29 +++++++++++++- .../plugin/helper/PropertiesDeployHelper.kt | 38 +++++++++++-------- 5 files changed, 68 insertions(+), 38 deletions(-) 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 3bc3d61..0c9e0ef 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 @@ -23,7 +23,6 @@ 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.utils.noBlank /** * 默认配置类实现类 @@ -63,10 +62,10 @@ internal object DefaultConfigs { get() = selfBase?.isEnable ?: globalBase?.isEnable ?: baseGenerateConfigs(name).isEnable - override val propertiesFileName - get() = selfBase?.propertiesFileName?.noBlank() - ?: globalBase?.propertiesFileName?.noBlank() - ?: baseGenerateConfigs(name).propertiesFileName + override val propertiesFileNames + get() = selfBase?.propertiesFileNames + ?: globalBase?.propertiesFileNames + ?: baseGenerateConfigs(name).propertiesFileNames override val permanentKeyValues get() = selfBase?.permanentKeyValues ?: globalBase?.permanentKeyValues @@ -115,10 +114,10 @@ internal object DefaultConfigs { get() = selfBase?.isEnable ?: globalBase?.isEnable ?: baseGenerateConfigs(name).isEnable - override val propertiesFileName - get() = selfBase?.propertiesFileName?.noBlank() - ?: globalBase?.propertiesFileName?.noBlank() - ?: baseGenerateConfigs(name).propertiesFileName + override val propertiesFileNames + get() = selfBase?.propertiesFileNames + ?: globalBase?.propertiesFileNames + ?: baseGenerateConfigs(name).propertiesFileNames override val permanentKeyValues get() = selfBase?.permanentKeyValues ?: globalBase?.permanentKeyValues @@ -157,7 +156,7 @@ internal object DefaultConfigs { private fun baseGenerateConfigs(name: String) = object : ISweetPropertyConfigs.IBaseGenerateConfigs { override val name get() = name override val isEnable get() = true - override val propertiesFileName get() = ISweetPropertyConfigs.DEFAULT_PROPERTIES_FILE_NAME + override val propertiesFileNames get() = mutableListOf(ISweetPropertyConfigs.DEFAULT_PROPERTIES_FILE_NAME) override val permanentKeyValues get() = mutableMapOf() override val excludeKeys get() = mutableListOf() override val includeKeys get() = mutableListOf() 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 aa7392c..6a7df2c 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 @@ -92,10 +92,10 @@ private fun SweetPropertyConfigureExtension.SourcesCodeGenerateConfigureExtensio ?: global?.isEnable ?: globalBase?.isEnable ?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).isEnable - override val propertiesFileName - get() = this@create.propertiesFileName.noBlank() - ?: global?.propertiesFileName?.noBlank() - ?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).propertiesFileName + override val propertiesFileNames + get() = this@create.propertiesFileNames + ?: global?.propertiesFileNames + ?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).propertiesFileNames override val permanentKeyValues get() = this@create.permanentKeyValues ?: global?.permanentKeyValues @@ -159,10 +159,10 @@ private fun SweetPropertyConfigureExtension.BuildScriptGenerateConfigureExtensio ?: global?.isEnable ?: globalBase?.isEnable ?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).isEnable - override val propertiesFileName - get() = this@create.propertiesFileName.noBlank() - ?: global?.propertiesFileName?.noBlank() - ?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).propertiesFileName + override val propertiesFileNames + get() = this@create.propertiesFileNames + ?: global?.propertiesFileNames + ?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).propertiesFileNames override val permanentKeyValues get() = this@create.permanentKeyValues ?: global?.permanentKeyValues 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 79297d9..26e455f 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 @@ -120,8 +120,8 @@ internal interface ISweetPropertyConfigs { /** 是否为当前功能生成代码 */ val isEnable: Boolean - /** 属性配置文件名称 */ - val propertiesFileName: String + /** 属性配置文件名称数组 */ + val propertiesFileNames: MutableList /** 固定存在的属性键值数组 */ val permanentKeyValues: MutableMap 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 54ae04d..76a92c4 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 @@ -200,6 +200,9 @@ open class SweetPropertyConfigureExtension internal constructor() { */ open inner class BaseGenerateConfigureExtension internal constructor() { + /** 当前属性配置文件路径数组 */ + internal var propertiesFileNames: MutableList? = null + /** 当前固定存在的属性键值数组 */ internal var permanentKeyValues: MutableMap? = null @@ -229,10 +232,11 @@ open class SweetPropertyConfigureExtension internal constructor() { /** * 设置属性配置文件名称 * - * 默认为 [ISweetPropertyConfigs.DEFAULT_PROPERTIES_FILE_NAME] + * - 此方法已弃用 - 在之后的版本中将直接被删除 * - * - 注意:一般情况下不需要修改此设置 - 错误的文件名将导致获取到空键值内容 + * - 请现在迁移到 [propertiesFileNames] */ + @Deprecated(message = "Migrate to propertiesFileNames(...)") var propertiesFileName = "" @JvmName("propertiesFileName") set @@ -266,6 +270,27 @@ open class SweetPropertyConfigureExtension internal constructor() { var isEnableValueInterpolation: Boolean? = null @JvmName("enableValueInterpolation") set + /** + * 设置属性配置文件名称数组 + * + * 属性配置文件将根据你设置的文件名称自动从当前根项目、子项目以及用户目录的根目录进行获取 + * + * 默认为 [ISweetPropertyConfigs.DEFAULT_PROPERTIES_FILE_NAME] + * + * 你可以添加多组属性配置文件名称 - 将按照顺序依次进行读取 + * + * - 注意:一般情况下不需要修改此设置 - 错误的文件名称将导致获取到空键值内容 + * @param names 要添加的属性配置文件名称数组 + * @param isAddDefault 是否添加默认的属性配置文件名称 - 默认是 + */ + @JvmOverloads + fun propertiesFileNames(vararg names: String, isAddDefault: Boolean = true) { + if (names.isEmpty()) SError.make("Properties file names must not be empty") + if (names.any { it.isBlank() }) SError.make("Properties file names must not have blank contents") + propertiesFileNames = names.distinct().toMutableList() + if (isAddDefault) propertiesFileNames?.add(0, ISweetPropertyConfigs.DEFAULT_PROPERTIES_FILE_NAME) + } + /** * 设置固定存在的属性键值数组 * 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 7c7a931..19d0223 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 @@ -45,8 +45,8 @@ import com.highcapable.sweetproperty.utils.hasInterpolation import com.highcapable.sweetproperty.utils.isEmpty import com.highcapable.sweetproperty.utils.noBlank import com.highcapable.sweetproperty.utils.noEmpty -import com.highcapable.sweetproperty.utils.parseFileSeparator import com.highcapable.sweetproperty.utils.replaceInterpolation +import com.highcapable.sweetproperty.utils.toStringMap import com.highcapable.sweetproperty.utils.uppercamelcase import org.gradle.api.DomainObjectCollection import org.gradle.api.Project @@ -256,18 +256,19 @@ internal object PropertiesDeployHelper { val propteries = mutableMapOf() configs.permanentKeyValues.forEach { (key, value) -> propteries[key] = value } configs.generateLocationTypes.forEach { - 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() - keyValues.filter { (key, value) -> + mutableMapOf().apply { + when (it) { + GenerateLocationType.CURRENT_PROJECT -> createProperties(configs, descriptor.currentDir).forEach { putAll(it) } + GenerateLocationType.ROOT_PROJECT -> createProperties(configs, descriptor.rootDir).forEach { putAll(it) } + GenerateLocationType.GLOBAL -> createProperties(configs, descriptor.homeDir).forEach { putAll(it) } + GenerateLocationType.SYSTEM -> putAll(System.getProperties()) + GenerateLocationType.SYSTEM_ENV -> putAll(System.getenv()) + } + }.filter { (key, value) -> if (configs.isEnableExcludeNonStringValue) key is CharSequence && key.isNotBlank() && value is CharSequence else key.toString().isNotBlank() && value != null - }.mapKeys { e -> e.key.toString() }.filter { (key, _) -> + }.toStringMap().filter { (key, _) -> configs.includeKeys.noEmpty()?.any { content -> when (content) { is Regex -> content.matches(key) @@ -292,11 +293,11 @@ internal object PropertiesDeployHelper { 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 + val resolveValue = if (configs.isEnableValueInterpolation) resolveKeyValues[matchKey] ?: "" else matchKey if (resolveValue.hasInterpolation()) resolveValue.resolveValue() else resolveValue } - if (value.toString().hasInterpolation()) resolveKeyValues[key] = value.toString().resolveValue() + if (value.hasInterpolation()) resolveKeyValues[key] = value.resolveValue() }; propteries.putAll(resolveKeyValues) } }; return propteries @@ -338,12 +339,17 @@ internal object PropertiesDeployHelper { } /** - * 创建新的 [Properties] + * 创建新的 [Properties] 数组 * @param configs 当前配置 * @param dir 当前目录 - * @return [Properties] or null + * @return [MutableList]<[Properties]> */ private fun createProperties(configs: ISweetPropertyConfigs.IBaseGenerateConfigs, dir: File?) = runCatching { - Properties().apply { load(FileReader(dir?.resolve(configs.propertiesFileName)?.absolutePath?.parseFileSeparator() ?: "")) } - }.getOrNull() + mutableListOf().apply { + configs.propertiesFileNames.forEach { + val propertiesFile = dir?.resolve(it) + if (propertiesFile?.exists() == true) add(Properties().apply { load(FileReader(propertiesFile.absolutePath)) }) + } + } + }.getOrNull() ?: mutableListOf() } \ No newline at end of file