refactor: standardize Gradle project naming

This commit is contained in:
2023-09-26 06:33:58 +08:00
parent 1bcd23113d
commit f08d25912d
5 changed files with 38 additions and 28 deletions

View File

@@ -43,11 +43,12 @@ internal class ProjectDescriptor private constructor() {
* @return [ProjectDescriptor] * @return [ProjectDescriptor]
*/ */
internal fun create(settings: Settings, name: String = "") = ProjectDescriptor().also { internal fun create(settings: Settings, name: String = "") = ProjectDescriptor().also {
val isRootProject = name.isBlank() || name == settings.rootProject.name val isRootProject = name.isBlank() || name.lowercase() == settings.rootProject.name.lowercase()
val subProjectNotice = "if this is a sub-project, please set it like \":$name\""
it.type = Type.SETTINGS it.type = Type.SETTINGS
it.name = name.noBlank() ?: settings.rootProject.name it.name = name.noBlank() ?: settings.rootProject.name
it.currentDir = (if (isRootProject) settings.rootProject else settings.findProject(":$name"))?.projectDir it.currentDir = (if (isRootProject) settings.rootProject else settings.findProject(name))?.projectDir
?: SError.make("Project \"$name\" not found") ?: SError.make("Project \"$name\" not found${if (name.startsWith(":").not()) ", $subProjectNotice" else ""}")
it.rootDir = settings.rootDir it.rootDir = settings.rootDir
it.homeDir = settings.gradle.gradleUserHomeDir it.homeDir = settings.gradle.gradleUserHomeDir
} }
@@ -59,7 +60,7 @@ internal class ProjectDescriptor private constructor() {
*/ */
internal fun create(project: Project) = ProjectDescriptor().also { internal fun create(project: Project) = ProjectDescriptor().also {
it.type = Type.PROJECT it.type = Type.PROJECT
it.name = project.fullName it.name = project.fullName()
it.currentDir = project.projectDir it.currentDir = project.projectDir
it.rootDir = project.rootDir it.rootDir = project.rootDir
it.homeDir = project.gradle.gradleUserHomeDir it.homeDir = project.gradle.gradleUserHomeDir

View File

@@ -29,23 +29,23 @@ import org.gradle.kotlin.dsl.repositories
/** /**
* 获取指定项目的完整名称 * 获取指定项目的完整名称
* @param isUseColon 是否在子项目前使用冒号 - 默认是
* @return [String] * @return [String]
*/ */
internal val Project.fullName internal fun Project.fullName(isUseColon: Boolean = true): String {
get(): String { val isRoot = this == rootProject
val baseNames = mutableListOf<String>() val baseNames = mutableListOf<String>()
/** /**
* 递归子项目 * 递归子项目
* @param project 当前项目 * @param project 当前项目
*/ */
fun fetchChild(project: Project) { fun fetchChild(project: Project) {
project.parent?.also { if (it != it.rootProject) fetchChild(it) } project.parent?.also { if (it != it.rootProject) fetchChild(it) }
baseNames.add(project.name) baseNames.add(project.name)
} }; fetchChild(project = this)
fetchChild(project = this) return buildString { baseNames.onEach { append(":$it") }.clear() }.let { if (isUseColon && isRoot.not()) it else it.drop(1) }
return buildString { baseNames.onEach { append(":$it") }.clear() }.drop(1) }
}
/** /**
* 向构建脚本添加自定义依赖 * 向构建脚本添加自定义依赖

View File

@@ -33,7 +33,7 @@ import org.gradle.api.Project
* @param project 当前项目 * @param project 当前项目
* @return [ISweetPropertyConfigs.ISubConfigs] * @return [ISweetPropertyConfigs.ISubConfigs]
*/ */
internal fun ISweetPropertyConfigs.with(project: Project) = projects[project.fullName] ?: global internal fun ISweetPropertyConfigs.with(project: Project) = projects[project.fullName()] ?: global
/** /**
* 创建 [ISweetPropertyConfigs.ISubConfigs] 实例 * 创建 [ISweetPropertyConfigs.ISubConfigs] 实例

View File

@@ -92,14 +92,23 @@ open class SweetPropertyConfigureExtension internal constructor() {
* 配置根项目 * 配置根项目
* @param action 配置方法体 * @param action 配置方法体
*/ */
fun rootProject(action: Action<SubConfigureExtension>) = project(ROOT_PROJECT_TAG, action) fun rootProject(action: Action<SubConfigureExtension>) = configureProject(ROOT_PROJECT_TAG, action, isLowercase = false)
/** /**
* 配置指定项目 * 配置指定项目
* @param name 项目完整名称 * @param name 项目完整名称
* @param action 配置方法体 * @param action 配置方法体
*/ */
fun project(name: String, action: Action<SubConfigureExtension>) = action.execute(SubConfigureExtension().also { projectConfigures[name] = it }) fun project(name: String, action: Action<SubConfigureExtension>) = configureProject(name, action)
/**
* 配置项目
* @param name 项目完整名称
* @param action 配置方法体
* @param isLowercase 是否转换 [name] 为小写 - 默认是
*/
private fun configureProject(name: String, action: Action<SubConfigureExtension>, isLowercase: Boolean = true) =
action.execute(SubConfigureExtension().also { projectConfigures[if (isLowercase) name.lowercase() else name] = it })
/** /**
* 子配置方法体实现类 * 子配置方法体实现类
@@ -483,14 +492,14 @@ open class SweetPropertyConfigureExtension internal constructor() {
val currentGlobal = globalConfigure.create() val currentGlobal = globalConfigure.create()
val currentProjects = mutableMapOf<String, ISweetPropertyConfigs.ISubConfigs>() val currentProjects = mutableMapOf<String, ISweetPropertyConfigs.ISubConfigs>()
val rootName = settings.rootProject.name val rootName = settings.rootProject.name
if (projectConfigures.containsKey(rootName)) if (projectConfigures.containsKey(rootName.lowercase()))
SError.make("This name \"$rootName\" is a root project, please use rootProject function to configure it, not project(\"$rootName\")") SError.make("This name \"$rootName\" is a root project, please use rootProject function to configure it, not project(\"$rootName\")")
if (projectConfigures.containsKey(ROOT_PROJECT_TAG)) { if (projectConfigures.containsKey(ROOT_PROJECT_TAG)) {
projectConfigures[rootName] = projectConfigures[ROOT_PROJECT_TAG] ?: SError.make("Internal error") projectConfigures[rootName.lowercase()] = projectConfigures[ROOT_PROJECT_TAG] ?: SError.make("Internal error")
projectConfigures.remove(ROOT_PROJECT_TAG) projectConfigures.remove(ROOT_PROJECT_TAG)
} }
projectConfigures.forEach { (name, subConfigure) -> projectConfigures.forEach { (name, subConfigure) ->
name.checkingStartWithLetter(description = "Project") name.replaceFirst(":", "").checkingStartWithLetter(description = "Project")
subConfigure.checkingNames() subConfigure.checkingNames()
currentProjects[name] = subConfigure.create(name, globalConfigure) currentProjects[name] = subConfigure.create(name, globalConfigure)
}; return object : ISweetPropertyConfigs { }; return object : ISweetPropertyConfigs {

View File

@@ -207,11 +207,11 @@ internal object PropertiesDeployHelper {
val outputDir = file(configs.generateDirPath) val outputDir = file(configs.generateDirPath)
if (configs.isEnable.not()) return if (configs.isEnable.not()) return
val properties = generatedProperties(configs, ProjectDescriptor.create(project = this)) val properties = generatedProperties(configs, ProjectDescriptor.create(project = this))
if (isConfigsModified.not() && properties == cachedProjectProperties[fullName] && outputDir.isEmpty().not()) { if (isConfigsModified.not() && properties == cachedProjectProperties[fullName()] && outputDir.isEmpty().not()) {
if (configs.isEnable) configureSourceSets(project = this) if (configs.isEnable) configureSourceSets(project = this)
return return
}; outputDir.apply { if (exists()) deleteRecursively() } }; outputDir.apply { if (exists()) deleteRecursively() }
cachedProjectProperties[fullName] = properties cachedProjectProperties[fullName()] = properties
val packageName = generatedPackageName(configs, project = this) val packageName = generatedPackageName(configs, project = this)
val className = generatedClassName(configs, project = this) val className = generatedClassName(configs, project = this)
sourcesGenerator.build(configs, properties, packageName, className).writeTo(outputDir) sourcesGenerator.build(configs, properties, packageName, className).writeTo(outputDir)
@@ -323,7 +323,7 @@ internal object PropertiesDeployHelper {
val packageName = configs.packageName.noBlank() val packageName = configs.packageName.noBlank()
?: project.namespace() ?: project.namespace()
?: project.group.toString().noBlank() ?: project.group.toString().noBlank()
?: "$DEFAULT_PACKAGE_NAME.${project.fullName.replace(":", "").flatted()}" ?: "$DEFAULT_PACKAGE_NAME.${project.fullName(isUseColon = false).replace(":", "").flatted()}"
return "$packageName.generated" return "$packageName.generated"
} }
@@ -335,7 +335,7 @@ internal object PropertiesDeployHelper {
*/ */
private fun generatedClassName(configs: ISweetPropertyConfigs.ISourcesCodeGenerateConfigs, project: Project): String { private fun generatedClassName(configs: ISweetPropertyConfigs.ISourcesCodeGenerateConfigs, project: Project): String {
val className = configs.className.noBlank() val className = configs.className.noBlank()
?: project.fullName.replace(":", "_").uppercamelcase().noBlank() ?: project.fullName(isUseColon = false).replace(":", "_").uppercamelcase().noBlank()
?: "Undefined" ?: "Undefined"
return "${className.uppercamelcase()}Properties" return "${className.uppercamelcase()}Properties"
} }