feat: add enable / disable auto generate dependencies namespace function

This commit is contained in:
2023-09-26 04:13:07 +08:00
parent f767fb6694
commit 78527bb302
4 changed files with 53 additions and 24 deletions

View File

@@ -48,30 +48,43 @@ internal data class PreferencesDocument(
internal var versionFilter: VersionFilterDocument = VersionFilterDocument() internal var versionFilter: VersionFilterDocument = VersionFilterDocument()
) : IYamlDocument { ) : IYamlDocument {
/**
* 依赖命名空间文档实体
* @param plugins 插件依赖
* @param libraries 库依赖
*/
@Serializable @Serializable
internal data class DependenciesNamespaceDocument( internal data class DependenciesNamespaceDocument(
@SerialName("plugins") @SerialName("plugins")
var plugins: String = "libs", var plugins: NamespaceOptionDocument = NamespaceOptionDocument(name = "libs"),
@SerialName("libraries") @SerialName("libraries")
var libraries: String = "" var libraries: NamespaceOptionDocument = NamespaceOptionDocument()
) : IYamlDocument { ) : IYamlDocument {
init { init {
if (plugins.isNotBlank() && libraries.isNotBlank() && plugins == libraries) if (plugins.name.isNotBlank() && libraries.name.isNotBlank() && plugins.name == libraries.name)
SError.make("Duplicated dependencies namespace \"$plugins\"") SError.make("Duplicated dependencies namespace \"$plugins\"")
} }
}
/**
* 命名空间选项文档实体
* @param isEnable 是否启用
* @param name 名称
*/
@Serializable
internal data class NamespaceOptionDocument(
@SerialName("enable")
var isEnable: Boolean = true,
@SerialName("name")
var name: String = ""
) : IYamlDocument {
/** /**
* 获取插件依赖命名空间 * 获取名称
* @return [String] * @return [String]
*/ */
internal fun plugins() = plugins.apply { checkingName("plugins namespace", isCheckExtName = true) }.camelcase() internal fun name() = name.apply { checkingName("dependencies namespace", isCheckExtName = true) }.camelcase()
/**
* 获取库依赖命名空间
* @return [String]
*/
internal fun libraries() = libraries.apply { checkingName("libraries namespace", isCheckExtName = true) }.camelcase()
} }
/** /**

View File

@@ -76,9 +76,9 @@ internal object DependencyDeployHelper {
* @param settings 当前设置 * @param settings 当前设置
*/ */
internal fun generateVersionCatalogs(settings: Settings) { internal fun generateVersionCatalogs(settings: Settings) {
val pluginsNamespace = SweetDependencyConfigs.document.preferences().dependenciesNamespace.plugins() val pluginsOption = SweetDependencyConfigs.document.preferences().dependenciesNamespace.plugins
runCatching { if (pluginsOption.isEnable) runCatching {
settings.dependencyResolutionManagement.versionCatalogs.create(pluginsNamespace) { settings.dependencyResolutionManagement.versionCatalogs.create(pluginsOption.name()) {
Dependencies.plugins().forEach { (dependencyName, artifact) -> Dependencies.plugins().forEach { (dependencyName, artifact) ->
if (GradleTaskManager.isInternalRunningTask && artifact.version().isAutowire) SError.make( if (GradleTaskManager.isInternalRunningTask && artifact.version().isAutowire) SError.make(
""" """
@@ -140,8 +140,14 @@ internal object DependencyDeployHelper {
* @param rootProject 当前根项目 * @param rootProject 当前根项目
*/ */
internal fun resolveAccessors(rootProject: Project) { internal fun resolveAccessors(rootProject: Project) {
val librariesOption = SweetDependencyConfigs.document.preferences().dependenciesNamespace.libraries
if (librariesOption.isEnable.not()) {
accessorsPomData.relativePomPath.toFile().takeIf { it.exists() }?.deleteRecursively()
accessorsGenerator.clearGeneratedData()
return
}
if (Dependencies.isOutdate || accessorsDir.resolve(accessorsPomData.relativePomPath).isEmpty()) if (Dependencies.isOutdate || accessorsDir.resolve(accessorsPomData.relativePomPath).isEmpty())
accessorsGenerator.build().compile(accessorsPomData, accessorsDir.absolutePath, accessorsGenerator.compileStubFiles) accessorsGenerator.build(librariesOption.name()).compile(accessorsPomData, accessorsDir.absolutePath, accessorsGenerator.compileStubFiles)
rootProject.addDependencyToBuildScript(accessorsDir.absolutePath, accessorsPomData) rootProject.addDependencyToBuildScript(accessorsDir.absolutePath, accessorsPomData)
} }

View File

@@ -88,8 +88,14 @@ internal object RuntimeDebugTransaction {
"autowire-on-sync-mode" value preferences.autowireOnSyncMode, "autowire-on-sync-mode" value preferences.autowireOnSyncMode,
"repositories-mode" value preferences.repositoriesMode, "repositories-mode" value preferences.repositoriesMode,
"dependencies-namespace" to mapOf( "dependencies-namespace" to mapOf(
"plugins" value preferences.dependenciesNamespace.plugins().ifBlank { NONE }, "plugins" to mapOf(
"libraries" value preferences.dependenciesNamespace.libraries().ifBlank { NONE } "enable" value preferences.dependenciesNamespace.plugins.isEnable,
"name" value preferences.dependenciesNamespace.plugins.name().ifBlank { NONE }
),
"libraries" to mapOf(
"enable" value preferences.dependenciesNamespace.libraries.isEnable,
"name" value preferences.dependenciesNamespace.libraries.name().ifBlank { NONE }
)
), ),
"version-filter" with (if (vfExclusionList.isEmpty()) "(disabled)" else "") to versionFilterMap "version-filter" with (if (vfExclusionList.isEmpty()) "(disabled)" else "") to versionFilterMap
), ),

View File

@@ -29,7 +29,6 @@ import com.highcapable.sweetdependency.generated.SweetDependencyProperties
import com.highcapable.sweetdependency.gradle.delegate.entity.ExternalDependencyDelegate import com.highcapable.sweetdependency.gradle.delegate.entity.ExternalDependencyDelegate
import com.highcapable.sweetdependency.gradle.entity.ExternalDependency import com.highcapable.sweetdependency.gradle.entity.ExternalDependency
import com.highcapable.sweetdependency.manager.content.Dependencies import com.highcapable.sweetdependency.manager.content.Dependencies
import com.highcapable.sweetdependency.plugin.config.content.SweetDependencyConfigs
import com.highcapable.sweetdependency.plugin.extension.accessors.proxy.IExtensionAccessors import com.highcapable.sweetdependency.plugin.extension.accessors.proxy.IExtensionAccessors
import com.highcapable.sweetdependency.utils.camelcase import com.highcapable.sweetdependency.utils.camelcase
import com.highcapable.sweetdependency.utils.capitalize import com.highcapable.sweetdependency.utils.capitalize
@@ -91,6 +90,9 @@ internal class LibrariesAccessorsGenerator {
/** 生成的依赖库不重复 TAG 数组 */ /** 生成的依赖库不重复 TAG 数组 */
private val usedSuccessiveTags = mutableSetOf<String>() private val usedSuccessiveTags = mutableSetOf<String>()
/** 当前库依赖的命名空间 */
private var librariesNamespace = ""
/** /**
* 不重复调用 * 不重复调用
* @param tags 当前 TAG 数组 * @param tags 当前 TAG 数组
@@ -395,7 +397,7 @@ internal class LibrariesAccessorsGenerator {
} }
/** 清空所有已生成的数据 */ /** 清空所有已生成的数据 */
private fun clearGeneratedData() { internal fun clearGeneratedData() {
classSpecs.clear() classSpecs.clear()
constructorSpecs.clear() constructorSpecs.clear()
preAddConstructorSpecNames.clear() preAddConstructorSpecNames.clear()
@@ -403,14 +405,17 @@ internal class LibrariesAccessorsGenerator {
grandSuccessiveNames.clear() grandSuccessiveNames.clear()
grandSuccessiveDuplicateIndexs.clear() grandSuccessiveDuplicateIndexs.clear()
usedSuccessiveTags.clear() usedSuccessiveTags.clear()
librariesNamespace = ""
} }
/** /**
* 生成 [JavaFile] * 生成 [JavaFile]
* @param namespace 依赖命名空间
* @return [JavaFile] * @return [JavaFile]
* @throws SweetDependencyUnresolvedException 如果生成失败 * @throws SweetDependencyUnresolvedException 如果生成失败
*/ */
internal fun build() = runCatching { internal fun build(namespace: String) = runCatching {
librariesNamespace = namespace
clearGeneratedData() clearGeneratedData()
createTopClassSpec() createTopClassSpec()
Dependencies.libraries().forEach { (dependencyName, artifact) -> Dependencies.libraries().forEach { (dependencyName, artifact) ->
@@ -454,8 +459,7 @@ internal class LibrariesAccessorsGenerator {
* @return [List]<[Pair]<[String], [String]>> * @return [List]<[Pair]<[String], [String]>>
*/ */
internal val librariesClasses get() = internal val librariesClasses get() =
SweetDependencyConfigs.document.preferences().dependenciesNamespace.libraries().let { namespace -> if (librariesNamespace.isNotBlank())
if (namespace.isNotBlank()) listOf(namespace to createAccessorsName()) listOf(librariesNamespace to createAccessorsName())
else memoryExtensionClasses else memoryExtensionClasses
}
} }