mirror of
https://github.com/HighCapable/SweetProperty.git
synced 2025-09-05 10:15:36 +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.config.proxy.ISweetPropertyConfigs
|
||||||
import com.highcapable.sweetproperty.plugin.extension.dsl.configure.SweetPropertyConfigureExtension
|
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
|
get() = selfBase?.isEnable
|
||||||
?: globalBase?.isEnable
|
?: globalBase?.isEnable
|
||||||
?: baseGenerateConfigs(name).isEnable
|
?: baseGenerateConfigs(name).isEnable
|
||||||
override val propertiesFileName
|
override val propertiesFileNames
|
||||||
get() = selfBase?.propertiesFileName?.noBlank()
|
get() = selfBase?.propertiesFileNames
|
||||||
?: globalBase?.propertiesFileName?.noBlank()
|
?: globalBase?.propertiesFileNames
|
||||||
?: baseGenerateConfigs(name).propertiesFileName
|
?: baseGenerateConfigs(name).propertiesFileNames
|
||||||
override val permanentKeyValues
|
override val permanentKeyValues
|
||||||
get() = selfBase?.permanentKeyValues
|
get() = selfBase?.permanentKeyValues
|
||||||
?: globalBase?.permanentKeyValues
|
?: globalBase?.permanentKeyValues
|
||||||
@@ -115,10 +114,10 @@ internal object DefaultConfigs {
|
|||||||
get() = selfBase?.isEnable
|
get() = selfBase?.isEnable
|
||||||
?: globalBase?.isEnable
|
?: globalBase?.isEnable
|
||||||
?: baseGenerateConfigs(name).isEnable
|
?: baseGenerateConfigs(name).isEnable
|
||||||
override val propertiesFileName
|
override val propertiesFileNames
|
||||||
get() = selfBase?.propertiesFileName?.noBlank()
|
get() = selfBase?.propertiesFileNames
|
||||||
?: globalBase?.propertiesFileName?.noBlank()
|
?: globalBase?.propertiesFileNames
|
||||||
?: baseGenerateConfigs(name).propertiesFileName
|
?: baseGenerateConfigs(name).propertiesFileNames
|
||||||
override val permanentKeyValues
|
override val permanentKeyValues
|
||||||
get() = selfBase?.permanentKeyValues
|
get() = selfBase?.permanentKeyValues
|
||||||
?: globalBase?.permanentKeyValues
|
?: globalBase?.permanentKeyValues
|
||||||
@@ -157,7 +156,7 @@ internal object DefaultConfigs {
|
|||||||
private fun baseGenerateConfigs(name: String) = object : ISweetPropertyConfigs.IBaseGenerateConfigs {
|
private fun baseGenerateConfigs(name: String) = object : ISweetPropertyConfigs.IBaseGenerateConfigs {
|
||||||
override val name get() = name
|
override val name get() = name
|
||||||
override val isEnable get() = true
|
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 permanentKeyValues get() = mutableMapOf<String, Any>()
|
||||||
override val excludeKeys get() = mutableListOf<Any>()
|
override val excludeKeys get() = mutableListOf<Any>()
|
||||||
override val includeKeys get() = mutableListOf<Any>()
|
override val includeKeys get() = mutableListOf<Any>()
|
||||||
|
@@ -92,10 +92,10 @@ private fun SweetPropertyConfigureExtension.SourcesCodeGenerateConfigureExtensio
|
|||||||
?: global?.isEnable
|
?: global?.isEnable
|
||||||
?: globalBase?.isEnable
|
?: globalBase?.isEnable
|
||||||
?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).isEnable
|
?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).isEnable
|
||||||
override val propertiesFileName
|
override val propertiesFileNames
|
||||||
get() = this@create.propertiesFileName.noBlank()
|
get() = this@create.propertiesFileNames
|
||||||
?: global?.propertiesFileName?.noBlank()
|
?: global?.propertiesFileNames
|
||||||
?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).propertiesFileName
|
?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).propertiesFileNames
|
||||||
override val permanentKeyValues
|
override val permanentKeyValues
|
||||||
get() = this@create.permanentKeyValues
|
get() = this@create.permanentKeyValues
|
||||||
?: global?.permanentKeyValues
|
?: global?.permanentKeyValues
|
||||||
@@ -159,10 +159,10 @@ private fun SweetPropertyConfigureExtension.BuildScriptGenerateConfigureExtensio
|
|||||||
?: global?.isEnable
|
?: global?.isEnable
|
||||||
?: globalBase?.isEnable
|
?: globalBase?.isEnable
|
||||||
?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).isEnable
|
?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).isEnable
|
||||||
override val propertiesFileName
|
override val propertiesFileNames
|
||||||
get() = this@create.propertiesFileName.noBlank()
|
get() = this@create.propertiesFileNames
|
||||||
?: global?.propertiesFileName?.noBlank()
|
?: global?.propertiesFileNames
|
||||||
?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).propertiesFileName
|
?: DefaultConfigs.buildScriptGenerateConfigs(name, selfBase, globalBase).propertiesFileNames
|
||||||
override val permanentKeyValues
|
override val permanentKeyValues
|
||||||
get() = this@create.permanentKeyValues
|
get() = this@create.permanentKeyValues
|
||||||
?: global?.permanentKeyValues
|
?: global?.permanentKeyValues
|
||||||
|
@@ -120,8 +120,8 @@ internal interface ISweetPropertyConfigs {
|
|||||||
/** 是否为当前功能生成代码 */
|
/** 是否为当前功能生成代码 */
|
||||||
val isEnable: Boolean
|
val isEnable: Boolean
|
||||||
|
|
||||||
/** 属性配置文件名称 */
|
/** 属性配置文件名称数组 */
|
||||||
val propertiesFileName: String
|
val propertiesFileNames: MutableList<String>
|
||||||
|
|
||||||
/** 固定存在的属性键值数组 */
|
/** 固定存在的属性键值数组 */
|
||||||
val permanentKeyValues: MutableMap<String, Any>
|
val permanentKeyValues: MutableMap<String, Any>
|
||||||
|
@@ -200,6 +200,9 @@ open class SweetPropertyConfigureExtension internal constructor() {
|
|||||||
*/
|
*/
|
||||||
open inner class BaseGenerateConfigureExtension internal constructor() {
|
open inner class BaseGenerateConfigureExtension internal constructor() {
|
||||||
|
|
||||||
|
/** 当前属性配置文件路径数组 */
|
||||||
|
internal var propertiesFileNames: MutableList<String>? = null
|
||||||
|
|
||||||
/** 当前固定存在的属性键值数组 */
|
/** 当前固定存在的属性键值数组 */
|
||||||
internal var permanentKeyValues: MutableMap<String, Any>? = 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 = ""
|
var propertiesFileName = ""
|
||||||
@JvmName("propertiesFileName") set
|
@JvmName("propertiesFileName") set
|
||||||
|
|
||||||
@@ -266,6 +270,27 @@ open class SweetPropertyConfigureExtension internal constructor() {
|
|||||||
var isEnableValueInterpolation: Boolean? = null
|
var isEnableValueInterpolation: Boolean? = null
|
||||||
@JvmName("enableValueInterpolation") set
|
@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.isEmpty
|
||||||
import com.highcapable.sweetproperty.utils.noBlank
|
import com.highcapable.sweetproperty.utils.noBlank
|
||||||
import com.highcapable.sweetproperty.utils.noEmpty
|
import com.highcapable.sweetproperty.utils.noEmpty
|
||||||
import com.highcapable.sweetproperty.utils.parseFileSeparator
|
|
||||||
import com.highcapable.sweetproperty.utils.replaceInterpolation
|
import com.highcapable.sweetproperty.utils.replaceInterpolation
|
||||||
|
import com.highcapable.sweetproperty.utils.toStringMap
|
||||||
import com.highcapable.sweetproperty.utils.uppercamelcase
|
import com.highcapable.sweetproperty.utils.uppercamelcase
|
||||||
import org.gradle.api.DomainObjectCollection
|
import org.gradle.api.DomainObjectCollection
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
@@ -256,18 +256,19 @@ internal object PropertiesDeployHelper {
|
|||||||
val propteries = mutableMapOf<String, Any>()
|
val propteries = mutableMapOf<String, Any>()
|
||||||
configs.permanentKeyValues.forEach { (key, value) -> propteries[key] = value }
|
configs.permanentKeyValues.forEach { (key, value) -> propteries[key] = value }
|
||||||
configs.generateLocationTypes.forEach {
|
configs.generateLocationTypes.forEach {
|
||||||
val keyValues = when (it) {
|
mutableMapOf<Any?, Any?>().apply {
|
||||||
GenerateLocationType.CURRENT_PROJECT -> createProperties(configs, descriptor.currentDir)
|
when (it) {
|
||||||
GenerateLocationType.ROOT_PROJECT -> createProperties(configs, descriptor.rootDir)
|
GenerateLocationType.CURRENT_PROJECT -> createProperties(configs, descriptor.currentDir).forEach { putAll(it) }
|
||||||
GenerateLocationType.GLOBAL -> createProperties(configs, descriptor.homeDir)
|
GenerateLocationType.ROOT_PROJECT -> createProperties(configs, descriptor.rootDir).forEach { putAll(it) }
|
||||||
GenerateLocationType.SYSTEM -> System.getProperties()
|
GenerateLocationType.GLOBAL -> createProperties(configs, descriptor.homeDir).forEach { putAll(it) }
|
||||||
GenerateLocationType.SYSTEM_ENV -> System.getenv()
|
GenerateLocationType.SYSTEM -> putAll(System.getProperties())
|
||||||
} ?: emptyMap()
|
GenerateLocationType.SYSTEM_ENV -> putAll(System.getenv())
|
||||||
keyValues.filter { (key, value) ->
|
}
|
||||||
|
}.filter { (key, value) ->
|
||||||
if (configs.isEnableExcludeNonStringValue)
|
if (configs.isEnableExcludeNonStringValue)
|
||||||
key is CharSequence && key.isNotBlank() && value is CharSequence
|
key is CharSequence && key.isNotBlank() && value is CharSequence
|
||||||
else key.toString().isNotBlank() && value != null
|
else key.toString().isNotBlank() && value != null
|
||||||
}.mapKeys { e -> e.key.toString() }.filter { (key, _) ->
|
}.toStringMap().filter { (key, _) ->
|
||||||
configs.includeKeys.noEmpty()?.any { content ->
|
configs.includeKeys.noEmpty()?.any { content ->
|
||||||
when (content) {
|
when (content) {
|
||||||
is Regex -> content.matches(key)
|
is Regex -> content.matches(key)
|
||||||
@@ -292,11 +293,11 @@ internal object PropertiesDeployHelper {
|
|||||||
fun String.resolveValue(): String = replaceInterpolation { matchKey ->
|
fun String.resolveValue(): String = replaceInterpolation { matchKey ->
|
||||||
if (resolveKeys.size > 5) SError.make("Key \"$key\" has been called recursively multiple times of those $resolveKeys")
|
if (resolveKeys.size > 5) SError.make("Key \"$key\" has been called recursively multiple times of those $resolveKeys")
|
||||||
resolveKeys.add(matchKey)
|
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()
|
if (resolveValue.hasInterpolation()) resolveValue.resolveValue()
|
||||||
else resolveValue
|
else resolveValue
|
||||||
}
|
}
|
||||||
if (value.toString().hasInterpolation()) resolveKeyValues[key] = value.toString().resolveValue()
|
if (value.hasInterpolation()) resolveKeyValues[key] = value.resolveValue()
|
||||||
}; propteries.putAll(resolveKeyValues)
|
}; propteries.putAll(resolveKeyValues)
|
||||||
}
|
}
|
||||||
}; return propteries
|
}; return propteries
|
||||||
@@ -338,12 +339,17 @@ internal object PropertiesDeployHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建新的 [Properties]
|
* 创建新的 [Properties] 数组
|
||||||
* @param configs 当前配置
|
* @param configs 当前配置
|
||||||
* @param dir 当前目录
|
* @param dir 当前目录
|
||||||
* @return [Properties] or null
|
* @return [MutableList]<[Properties]>
|
||||||
*/
|
*/
|
||||||
private fun createProperties(configs: ISweetPropertyConfigs.IBaseGenerateConfigs, dir: File?) = runCatching {
|
private fun createProperties(configs: ISweetPropertyConfigs.IBaseGenerateConfigs, dir: File?) = runCatching {
|
||||||
Properties().apply { load(FileReader(dir?.resolve(configs.propertiesFileName)?.absolutePath?.parseFileSeparator() ?: "")) }
|
mutableListOf<Properties>().apply {
|
||||||
}.getOrNull()
|
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