refactor(fix): add custom sourceSetName and fix sourceSets conflict in LocaleAnalysisHelper

This commit is contained in:
2025-08-19 17:37:12 +08:00
parent 3a600b1b3e
commit 33b00fc4e0
3 changed files with 27 additions and 3 deletions

View File

@@ -37,6 +37,11 @@ internal interface IFlexiLocaleConfigs {
* "build/generated/[FlexiLocaleProperties.PROJECT_MODULE_NAME]"
*/
internal const val DEFAULT_GENERATE_DIR_PATH = "build/generated/${FlexiLocaleProperties.PROJECT_MODULE_NAME}"
/**
* 默认的部署 `sourceSet` 名称
*/
internal const val DEFAULT_SOURCE_SET_NAME = "main"
}
/** 是否启用插件 */
@@ -45,6 +50,9 @@ internal interface IFlexiLocaleConfigs {
/** 自定义生成的目录路径 */
val generateDirPath: String
/** 自定义部署的 `sourceSet` 名称 */
val sourceSetName: String
/** 自定义生成的包名 */
val packageName: String
@@ -58,5 +66,5 @@ internal interface IFlexiLocaleConfigs {
* 获取内部 [hashCode]
* @return [Int]
*/
fun innerHashCode() = "$isEnable$generateDirPath$packageName$className$isEnableRestrictedAccess".hashCode()
fun innerHashCode() = "$isEnable$generateDirPath$sourceSetName$packageName$className$isEnableRestrictedAccess".hashCode()
}

View File

@@ -59,6 +59,16 @@ open class FlexiLocaleConfigureExtension internal constructor() {
var generateDirPath = IFlexiLocaleConfigs.DEFAULT_GENERATE_DIR_PATH
@JvmName("generateDirPath") set
/**
* 自定义部署的 `sourceSet` 名称
*
* 如果你的项目源码部署名称不是默认值 - 可以在这里自定义
*
* 默认为 [IFlexiLocaleConfigs.DEFAULT_SOURCE_SET_NAME]
*/
var sourceSetName = IFlexiLocaleConfigs.DEFAULT_SOURCE_SET_NAME
@JvmName("sourceSetName") set
/**
* 自定义生成的包名
*
@@ -104,12 +114,14 @@ open class FlexiLocaleConfigureExtension internal constructor() {
className.checkingValidClassName()
val currentEnable = isEnable
val currentGenerateDirPath = project.file(generateDirPath).absolutePath
val currentSourceSetName = sourceSetName
val currentPackageName = packageName
val currentClassName = "${className.ifBlank { project.fullName().uppercamelcase() }}Locale"
val currentEnableRestrictedAccess = isEnableRestrictedAccess
return object : IFlexiLocaleConfigs {
override val isEnable get() = currentEnable
override val generateDirPath get() = currentGenerateDirPath
override val sourceSetName get() = currentSourceSetName
override val packageName get() = currentPackageName
override val className get() = currentClassName
override val isEnableRestrictedAccess get() = currentEnableRestrictedAccess

View File

@@ -130,8 +130,12 @@ internal object LocaleAnalysisHelper {
*/
private fun initializePlugins(project: Project) {
runCatching {
fun BaseExtension.updateSourceDirs() = sourceSets.configureEach { kotlin.srcDir(configs.generateDirPath) }
fun KotlinProjectExtension.updateSourceDirs() = sourceSets.configureEach { kotlin.srcDir(configs.generateDirPath) }
fun BaseExtension.updateSourceDirs() = sourceSets.firstOrNull {
it.name == configs.sourceSetName
}?.kotlin?.srcDir(configs.generateDirPath) ?: FLog.warn("Could not found source set \"${configs.sourceSetName}\"")
fun KotlinProjectExtension.updateSourceDirs() = sourceSets.firstOrNull {
it.name == configs.sourceSetName
}?.kotlin?.srcDir(configs.generateDirPath) ?: FLog.warn("Could not found source set \"${configs.sourceSetName}\"")
fun BaseVariant.updateResDirectories() = sourceSets.forEach { provide -> provide.resDirectories?.also { resDirectories.addAll(it) } }
project.plugins.withId(APPLICATION_PLUGIN_NAME) {
project.get<AppExtension>().also { extension ->