diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/default/DefaultConfigs.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/default/DefaultConfigs.kt index bb0b928..f352173 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/default/DefaultConfigs.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/default/DefaultConfigs.kt @@ -56,6 +56,7 @@ internal object DefaultConfigs { ) = object : ISweetPropertyConfigs.ISourcesCodeGenerateConfigs { override val name get() = name override val generateDirPath get() = ISweetPropertyConfigs.DEFAULT_GENERATE_DIR_PATH + override val sourceSetName get() = ISweetPropertyConfigs.DEFAULT_SOURCE_SET_NAME override val packageName get() = "" override val className get() = "" override val isEnableRestrictedAccess get() = false diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/factory/SweetPropertyConfigsFactory.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/factory/SweetPropertyConfigsFactory.kt index 1b467c4..4cf24da 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/factory/SweetPropertyConfigsFactory.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/factory/SweetPropertyConfigsFactory.kt @@ -74,6 +74,10 @@ private fun SweetPropertyConfigureExtension.SourcesCodeGenerateConfigureExtensio get() = this@create.generateDirPath.noBlank() ?: global?.generateDirPath?.noBlank() ?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).generateDirPath + override val sourceSetName + get() = this@create.sourceSetName.noBlank() + ?: global?.sourceSetName?.noBlank() + ?: DefaultConfigs.sourcesCodeGenerateConfigs(name, selfBase, globalBase).sourceSetName override val packageName get() = this@create.packageName.noBlank() ?: global?.packageName?.noBlank() diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/proxy/ISweetPropertyConfigs.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/proxy/ISweetPropertyConfigs.kt index 97b4335..ab84d62 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/proxy/ISweetPropertyConfigs.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/config/proxy/ISweetPropertyConfigs.kt @@ -40,6 +40,11 @@ internal interface ISweetPropertyConfigs { */ internal const val DEFAULT_GENERATE_DIR_PATH = "build/generated/${SweetPropertyProperties.PROJECT_MODULE_NAME}" + /** + * 默认的部署 `sourceSet` 名称 + */ + internal const val DEFAULT_SOURCE_SET_NAME = "main" + /** * 默认的属性配置文件名称 * @@ -91,6 +96,9 @@ internal interface ISweetPropertyConfigs { /** 自定义生成的目录路径 */ val generateDirPath: String + /** 自定义部署的 `sourceSet` 名称 */ + val sourceSetName: String + /** 自定义生成的包名 */ val packageName: String diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt index 23c5bda..882363e 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/extension/dsl/configure/SweetPropertyConfigureExtension.kt @@ -173,6 +173,16 @@ open class SweetPropertyConfigureExtension internal constructor() { var generateDirPath = "" @JvmName("generateDirPath") set + /** + * 自定义部署的 `sourceSet` 名称 + * + * 如果你的项目源码部署名称不是默认值 - 可以在这里自定义 + * + * 默认为 [ISweetPropertyConfigs.DEFAULT_SOURCE_SET_NAME] + */ + var sourceSetName = "" + @JvmName("sourceSetName") set + /** * 自定义生成的包名 * diff --git a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/helper/PropertiesDeployHelper.kt b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/helper/PropertiesDeployHelper.kt index f67a39e..d7bc7b9 100644 --- a/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/helper/PropertiesDeployHelper.kt +++ b/sweetproperty-gradle-plugin/src/main/java/com/highcapable/sweetproperty/plugin/helper/PropertiesDeployHelper.kt @@ -41,6 +41,7 @@ import com.highcapable.sweetproperty.utils.camelcase import com.highcapable.sweetproperty.utils.code.entity.MavenPomData import com.highcapable.sweetproperty.utils.code.factory.compile import com.highcapable.sweetproperty.utils.debug.SError +import com.highcapable.sweetproperty.utils.debug.SLog import com.highcapable.sweetproperty.utils.flatted import com.highcapable.sweetproperty.utils.hasInterpolation import com.highcapable.sweetproperty.utils.isEmpty @@ -49,7 +50,6 @@ import com.highcapable.sweetproperty.utils.noEmpty 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 import org.gradle.api.initialization.Settings import java.io.File @@ -212,6 +212,8 @@ internal object PropertiesDeployHelper { if (configs.isEnable) configureSourceSets(project = this) return }; outputDir.apply { if (exists()) deleteRecursively() } + // 每次都会重新创建目录 + outputDir.mkdirs() cachedProjectProperties[fullName()] = properties val packageName = generatedPackageName(configs, project = this) val className = generatedClassName(configs, project = this) @@ -227,16 +229,26 @@ internal object PropertiesDeployHelper { * @param project 当前项目 */ private fun configureSourceSets(project: Project) { - fun Project.deploySourceSets(name: String) = runCatching { + val configs = configs.with(project).sourcesCode + fun Project.deploySourceSet(name: String, sourceSetName: String = configs.sourceSetName) = runCatching { val extension = get(name) - val collection = extension.javaClass.getMethod("getSourceSets").invoke(extension) as DomainObjectCollection<*> - collection.configureEach { - val kotlin = javaClass.getMethod("getKotlin").invoke(this) - kotlin.javaClass.getMethod("srcDir", Any::class.java).invoke(kotlin, configs.with(project).sourcesCode.generateDirPath) - } + val collection = extension.javaClass.getMethod("getSourceSets").invoke(extension) as? Iterable<*>? + val mainSet = collection?.firstOrNull { + it?.javaClass?.getMethod("getName")?.invoke(it) == sourceSetName + } ?: return@runCatching SLog.warn("Could not found source set \"$sourceSetName\" for $name extension") + val kotlin = mainSet.javaClass.getMethod("getKotlin").invoke(mainSet) + val generateDir = configs.generateDirPath + val generateFile = File(generateDir) + // 确保目录存在 + if (!generateFile.exists()) generateFile.mkdirs() + val srcDirs = kotlin.javaClass.getMethod("getSrcDirs").invoke(kotlin) as Set<*> + val alreadyAdded = srcDirs.any { it is File && it.canonicalPath == generateFile.canonicalPath } + if (!alreadyAdded) kotlin.javaClass.getMethod("srcDir", Any::class.java).invoke(kotlin, generateFile) + }.onFailure { + SLog.error(msg = "Failed to deploy source set \"$sourceSetName\" for $name extension\n${it.stackTraceToString()}") } - if (project.hasExtension("kotlin")) project.deploySourceSets(name = "kotlin") - if (project.hasExtension("android")) project.deploySourceSets(name = "android") + if (project.hasExtension("kotlin")) project.deploySourceSet(name = "kotlin") + if (project.hasExtension("android")) project.deploySourceSet(name = "android") } /**