mirror of
https://github.com/HighCapable/SweetProperty.git
synced 2025-09-04 01:35:37 +08:00
feat: add propertiesFileNames function
This commit is contained in:
@@ -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<String, Any>()
|
||||
override val excludeKeys get() = mutableListOf<Any>()
|
||||
override val includeKeys get() = mutableListOf<Any>()
|
||||
|
@@ -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
|
||||
|
@@ -120,8 +120,8 @@ internal interface ISweetPropertyConfigs {
|
||||
/** 是否为当前功能生成代码 */
|
||||
val isEnable: Boolean
|
||||
|
||||
/** 属性配置文件名称 */
|
||||
val propertiesFileName: String
|
||||
/** 属性配置文件名称数组 */
|
||||
val propertiesFileNames: MutableList<String>
|
||||
|
||||
/** 固定存在的属性键值数组 */
|
||||
val permanentKeyValues: MutableMap<String, Any>
|
||||
|
@@ -200,6 +200,9 @@ open class SweetPropertyConfigureExtension internal constructor() {
|
||||
*/
|
||||
open inner class BaseGenerateConfigureExtension internal constructor() {
|
||||
|
||||
/** 当前属性配置文件路径数组 */
|
||||
internal var propertiesFileNames: MutableList<String>? = null
|
||||
|
||||
/** 当前固定存在的属性键值数组 */
|
||||
internal var permanentKeyValues: MutableMap<String, Any>? = 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)
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置固定存在的属性键值数组
|
||||
*
|
||||
|
@@ -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<String, Any>()
|
||||
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<Any?, Any?>().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<Properties>().apply {
|
||||
configs.propertiesFileNames.forEach {
|
||||
val propertiesFile = dir?.resolve(it)
|
||||
if (propertiesFile?.exists() == true) add(Properties().apply { load(FileReader(propertiesFile.absolutePath)) })
|
||||
}
|
||||
}
|
||||
}.getOrNull() ?: mutableListOf()
|
||||
}
|
Reference in New Issue
Block a user