From 22e9266ce0d0baa7747bfd59172f278252cd4d58 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Sat, 15 Nov 2025 21:52:00 +0800 Subject: [PATCH] refactor: move package "internal" to "debug" and improve logging throughout the codebase --- .../gropify/{internal => debug}/Exception.kt | 2 +- .../com/highcapable/gropify/debug/Logger.kt | 52 ++++++++++++++ .../gradle/api/entity/ProjectDescriptor.kt | 2 +- .../gradle/api/extension/ExtensionAware.kt | 2 +- .../highcapable/gropify/internal/Logger.kt | 59 ---------------- .../gropify/plugin/DefaultDeployer.kt | 70 ++++++++++++++++--- .../gropify/plugin/GropifyLifecycle.kt | 11 ++- .../gropify/plugin/GropifyPlugin.kt | 6 +- .../gropify/plugin/compiler/CodeCompiler.kt | 20 +++++- .../plugin/deployer/BuildscriptDeployer.kt | 25 +++++-- .../plugin/deployer/SourceCodeDeployer.kt | 36 +++++++--- .../configure/GropifyConfigureExtension.kt | 19 ++++- .../plugin/generator/BuildscriptGenerator.kt | 16 ++--- .../plugin/generator/JavaCodeGenerator.kt | 2 +- .../plugin/generator/KotlinCodeGenerator.kt | 2 +- .../plugin/generator/SourceCodeGenerator.kt | 23 +++++- .../plugin/generator/config/SourceCodeSpec.kt | 2 +- .../generator/extension/PropertyType.kt | 8 +-- .../plugin/helper/AndroidProjectHelper.kt | 4 +- 19 files changed, 245 insertions(+), 116 deletions(-) rename gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/{internal => debug}/Exception.kt (97%) create mode 100644 gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/debug/Logger.kt delete mode 100644 gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/internal/Logger.kt diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/internal/Exception.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/debug/Exception.kt similarity index 97% rename from gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/internal/Exception.kt rename to gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/debug/Exception.kt index df4ed86..db38ee1 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/internal/Exception.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/debug/Exception.kt @@ -21,7 +21,7 @@ */ @file:Suppress("UnusedReceiverParameter") -package com.highcapable.gropify.internal +package com.highcapable.gropify.debug import com.highcapable.gropify.plugin.Gropify import kotlin.contracts.ExperimentalContracts diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/debug/Logger.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/debug/Logger.kt new file mode 100644 index 0000000..cdf9aab --- /dev/null +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/debug/Logger.kt @@ -0,0 +1,52 @@ +/* + * Gropify - A type-safe and modern properties plugin for Gradle. + * Copyright (C) 2019 HighCapable + * https://github.com/HighCapable/Gropify + * + * Apache License Version 2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This file is created by fankes on 2025/10/16. + */ +@file:Suppress("unused", "LoggingStringTemplateAsArgument") + +package com.highcapable.gropify.debug + +import com.highcapable.gropify.plugin.Gropify + +/** + * Gropify logger. + */ +internal object Logger { + + /** Debug mode flag. */ + var debugMode = false + + fun debug(msg: Any) { + if (!debugMode) return + println("\u001B[36m[${Gropify.TAG}][DEBUG] $msg\u001B[0m") + } + + fun info(msg: Any) { + println("${Gropify.TAG}][INFO] $msg") + } + + fun warn(msg: Any) { + println("\u001B[33m[${Gropify.TAG}][WARN] $msg\u001B[0m") + } + + fun error(msg: Any) { + println("\u001B[31m[${Gropify.TAG}][ERROR] $msg\u001B[0m") + } +} \ No newline at end of file diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/gradle/api/entity/ProjectDescriptor.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/gradle/api/entity/ProjectDescriptor.kt index f448591..18c2082 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/gradle/api/entity/ProjectDescriptor.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/gradle/api/entity/ProjectDescriptor.kt @@ -21,8 +21,8 @@ */ package com.highcapable.gropify.gradle.api.entity +import com.highcapable.gropify.debug.error import com.highcapable.gropify.gradle.api.extension.getFullName -import com.highcapable.gropify.internal.error import com.highcapable.gropify.plugin.Gropify import org.gradle.api.Project import org.gradle.api.initialization.Settings diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/gradle/api/extension/ExtensionAware.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/gradle/api/extension/ExtensionAware.kt index b4f4734..2b645fa 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/gradle/api/extension/ExtensionAware.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/gradle/api/extension/ExtensionAware.kt @@ -21,7 +21,7 @@ */ package com.highcapable.gropify.gradle.api.extension -import com.highcapable.gropify.internal.error +import com.highcapable.gropify.debug.error import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.utils.extension.camelcase import com.highcapable.kavaref.extension.classOf diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/internal/Logger.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/internal/Logger.kt deleted file mode 100644 index 0e09fd1..0000000 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/internal/Logger.kt +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Gropify - A type-safe and modern properties plugin for Gradle. - * Copyright (C) 2019 HighCapable - * https://github.com/HighCapable/Gropify - * - * Apache License Version 2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * This file is created by fankes on 2025/10/16. - */ -@file:Suppress("unused", "LoggingStringTemplateAsArgument") - -package com.highcapable.gropify.internal - -import com.highcapable.gropify.plugin.Gropify -import org.gradle.api.Project -import org.gradle.api.logging.Logger - -/** - * Gropify logger. - */ -internal class Logger private constructor() { - - internal companion object { - - /** - * Create logger with project. - * @param project the project. - * @return [Logger] - */ - fun with(project: Project) = Logger().apply { - logger = project.logger - } - - /** - * Create empty logger instance. - * @return [Logger] - */ - fun get() = Logger() - } - - private var logger: Logger? = null - - internal fun debug(msg: Any) = "[${Gropify.TAG}][DEBUG] $msg".let { logger?.debug(it) ?: println(it) } - internal fun info(msg: Any) = "[${Gropify.TAG}][INFO] $msg".let { logger?.info(it) ?: println(it) } - internal fun warn(msg: Any) = "[${Gropify.TAG}][WARN] $msg".let { logger?.warn(it) ?: println(it) } - internal fun error(msg: Any) = "[${Gropify.TAG}][ERROR] $msg".let { logger?.error(it) ?: println(it) } -} \ No newline at end of file diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/DefaultDeployer.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/DefaultDeployer.kt index 95f8122..1136430 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/DefaultDeployer.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/DefaultDeployer.kt @@ -21,8 +21,9 @@ */ package com.highcapable.gropify.plugin +import com.highcapable.gropify.debug.Logger +import com.highcapable.gropify.debug.require import com.highcapable.gropify.gradle.api.entity.ProjectDescriptor -import com.highcapable.gropify.internal.require import com.highcapable.gropify.plugin.config.proxy.GropifyConfig import com.highcapable.gropify.plugin.config.type.GropifyLocation import com.highcapable.gropify.plugin.deployer.BuildscriptDeployer @@ -69,10 +70,11 @@ internal object DefaultDeployer { */ fun init(settings: Settings, config: GropifyConfig) { DefaultDeployer.config = config - if (!config.isEnabled) return + if (!isEnabled()) return checkingConfigModified(settings) + Logger.debug("Initializing deployers, config modified: $configModified") deployers.forEach { it.init(settings, configModified) } } @@ -81,8 +83,9 @@ internal object DefaultDeployer { * @param rootProject the current root project. */ fun resolve(rootProject: Project) { - if (!config.isEnabled) return + if (!isEnabled()) return + Logger.debug("Resolving deployers, config modified: $configModified") deployers.forEach { it.resolve(rootProject, configModified) } } @@ -91,8 +94,9 @@ internal object DefaultDeployer { * @param rootProject the current root project. */ fun deploy(rootProject: Project) { - if (!config.isEnabled) return + if (!isEnabled()) return + Logger.debug("Deploying deployers, config modified: $configModified") deployers.forEach { it.deploy(rootProject, configModified) } } @@ -106,16 +110,41 @@ internal object DefaultDeployer { val properties = mutableMapOf() val resolveProperties = mutableMapOf() + val locations = mutableMapOf() + config.permanentKeyValues.forEach { (key, value) -> properties[key] = value.createTypeValueByType(config.useTypeAutoConversion, key) + locations[key] = "Permanent Key-Value" } config.locations.forEach { location -> when (location) { - GropifyLocation.CurrentProject -> createProperties(config, descriptor.currentDir).forEach { resolveProperties.putAll(it) } - GropifyLocation.RootProject -> createProperties(config, descriptor.rootDir).forEach { resolveProperties.putAll(it) } - GropifyLocation.Global -> createProperties(config, descriptor.homeDir).forEach { resolveProperties.putAll(it) } - GropifyLocation.System -> resolveProperties.putAll(System.getProperties()) - GropifyLocation.SystemEnv -> resolveProperties.putAll(System.getenv()) + GropifyLocation.CurrentProject -> createProperties(config, descriptor.currentDir).forEach { + resolveProperties.putAll(it) + + it.forEach { (key, _) -> locations[key.toString()] = location.name } + } + GropifyLocation.RootProject -> createProperties(config, descriptor.rootDir).forEach { + resolveProperties.putAll(it) + + it.forEach { (key, _) -> locations[key.toString()] = location.name } + } + GropifyLocation.Global -> createProperties(config, descriptor.homeDir).forEach { + resolveProperties.putAll(it) + + it.forEach { (key, _) -> locations[key.toString()] = location.name } + } + GropifyLocation.System -> { + val system = System.getProperties() + resolveProperties.putAll(system) + + system.forEach { (key, _) -> locations[key.toString()] = location.name } + } + GropifyLocation.SystemEnv -> { + val systemEnv = System.getenv() + resolveProperties.putAll(systemEnv) + + systemEnv.forEach { (key, _) -> locations[key] = location.name } + } } } @@ -167,6 +196,7 @@ internal object DefaultDeployer { // Replace all key-values if exists. config.replacementKeyValues.forEach { (key, value) -> properties[key] = value.createTypeValueByType(config.useTypeAutoConversion, key) + locations[key] = "Replacement Key-Value${locations[key]?.let { ", $it" }}" } // Apply key-values rules. @@ -175,6 +205,23 @@ internal object DefaultDeployer { val resolveValue = mapper(value.raw).createTypeValue(config.useTypeAutoConversion, key, type) properties[key] = resolveValue + + locations[key] = "Rule-based Key-Value${locations[key]?.let { ", $it" }}" + } + + properties.forEach { (key, value) -> + Logger.debug( + """ + Generated property for ${config.name} + ---------- + [Key]: $key + [Value]: ${value.raw} + [Code Value]: ${value.codeValue} + [Type]: ${value.type.simpleName} + [Location]: ${locations[key] ?: "Unknown"} + ---------- + """.trimIndent() + ) } return properties @@ -200,4 +247,9 @@ internal object DefaultDeployer { lastModifiedHashCode = gradleHashCode } } + + private fun isEnabled(): Boolean { + if (!config.isEnabled) Logger.debug("Gropify is disabled, skipping deployment process.") + return config.isEnabled + } } \ No newline at end of file diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/GropifyLifecycle.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/GropifyLifecycle.kt index 0ee3ed4..0d6025c 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/GropifyLifecycle.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/GropifyLifecycle.kt @@ -21,10 +21,11 @@ */ package com.highcapable.gropify.plugin +import com.highcapable.gropify.debug.Logger +import com.highcapable.gropify.debug.error import com.highcapable.gropify.gradle.api.GradleDescriptor import com.highcapable.gropify.gradle.api.extension.getOrCreate import com.highcapable.gropify.gradle.api.plugin.PluginLifecycle -import com.highcapable.gropify.internal.error import com.highcapable.gropify.plugin.extension.dsl.configure.GropifyConfigureExtension import org.gradle.api.Project import org.gradle.api.initialization.Settings @@ -45,14 +46,22 @@ internal class GropifyLifecycle : PluginLifecycle { override fun onSettingsEvaluated(settings: Settings) { val config = configure?.build(settings) ?: Gropify.error("Extension \"${GropifyConfigureExtension.NAME}\" create failed.") + Logger.debugMode = config.debugMode + Logger.debug("Gropify ${Gropify.VERSION} running on Gradle ${GradleDescriptor.version}") + Logger.debug("Loaded configuration.") + DefaultDeployer.init(settings, config) } override fun beforeProjectEvaluate(rootProject: Project) { + Logger.debug("Before project evaluate: $rootProject") + DefaultDeployer.resolve(rootProject) } override fun afterProjectEvaluate(rootProject: Project) { + Logger.debug("After project evaluate: $rootProject") + DefaultDeployer.deploy(rootProject) } } \ No newline at end of file diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/GropifyPlugin.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/GropifyPlugin.kt index 58fe83a..3b74aa8 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/GropifyPlugin.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/GropifyPlugin.kt @@ -23,8 +23,8 @@ package com.highcapable.gropify.plugin -import com.highcapable.gropify.internal.Logger -import com.highcapable.gropify.internal.error +import com.highcapable.gropify.debug.Logger +import com.highcapable.gropify.debug.error import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.initialization.Settings @@ -56,7 +56,7 @@ class GropifyPlugin internal constructor() : Plugin { } } } - is Project -> Logger.with(target).error( + is Project -> Logger.error( "Gropify can only applied in settings.gradle or settings.gradle.kts, but current is $target, stop loading.", ) else -> Gropify.error("Gropify applied to an unknown target: $target, stop loading.") diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/compiler/CodeCompiler.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/compiler/CodeCompiler.kt index 5463bf5..cae74a3 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/compiler/CodeCompiler.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/compiler/CodeCompiler.kt @@ -21,9 +21,10 @@ */ package com.highcapable.gropify.plugin.compiler +import com.highcapable.gropify.debug.Logger +import com.highcapable.gropify.debug.error +import com.highcapable.gropify.debug.require import com.highcapable.gropify.gradle.api.entity.Dependency -import com.highcapable.gropify.internal.error -import com.highcapable.gropify.internal.require import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.utils.extension.deleteEmptyRecursively import com.highcapable.gropify.utils.extension.toFile @@ -111,7 +112,20 @@ internal object CodeCompiler { writeMetaInf(outputSourcesDir) createJar(dependency, outputDir, outputBuildDir, outputClassesDir, outputSourcesDir) - } else Gropify.error("Failed to compile java files into path: $outputDirPath\n$diagnosticsMessage") + } else { + Logger.debug("Compilation process failed, dumping source file content.") + files.forEach { + Logger.debug( + "Check this Java file: ${it.name}\n" + + "====== BEGIN FILE CONTENT ======\n" + + "${it.getCharContent(true)}\n" + + "====== END FILE CONTENT ======" + ) + } + Logger.debug("Please report those file content to us, you can remove sensitive information before this.") + + Gropify.error("Failed to compile Java files into path: $outputDirPath\n$diagnosticsMessage") + } } private fun createJar(dependency: Dependency, outputDir: File, buildDir: File, classesDir: File, sourcesDir: File) { diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/deployer/BuildscriptDeployer.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/deployer/BuildscriptDeployer.kt index b2a338c..f719d80 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/deployer/BuildscriptDeployer.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/deployer/BuildscriptDeployer.kt @@ -25,7 +25,9 @@ import com.highcapable.gropify.gradle.api.entity.Dependency import com.highcapable.gropify.gradle.api.entity.ProjectDescriptor import com.highcapable.gropify.gradle.api.extension.addDependencyToBuildscript import com.highcapable.gropify.gradle.api.extension.getOrCreate +import com.highcapable.gropify.gradle.api.extension.hasExtension import com.highcapable.gropify.gradle.api.extension.toClassOrNull +import com.highcapable.gropify.debug.Logger import com.highcapable.gropify.plugin.DefaultDeployer import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.plugin.compiler.extension.compile @@ -82,7 +84,7 @@ internal class BuildscriptDeployer(private val _config: () -> GropifyConfig) : D ) return cachedSettingsProperties = allProperties - buildscriptGenerator.build(config, allConfig, allProperties).compile( + buildscriptGenerator.build(allConfig, allProperties).compile( buildscriptAccessorsDependency, buildscriptAccessorsDir.absolutePath, buildscriptGenerator.compileStubFiles @@ -90,14 +92,16 @@ internal class BuildscriptDeployer(private val _config: () -> GropifyConfig) : D } override fun resolve(rootProject: Project, configModified: Boolean) { - if (!buildscriptAccessorsDir.resolve(buildscriptAccessorsDependency.relativePath).isEmpty()) - rootProject.addDependencyToBuildscript(buildscriptAccessorsDir.absolutePath, buildscriptAccessorsDependency) + if (buildscriptAccessorsDir.resolve(buildscriptAccessorsDependency.relativePath).isEmpty()) return + + Logger.debug("Resolving classpath for $buildscriptAccessorsDependency") + rootProject.addDependencyToBuildscript(buildscriptAccessorsDir.absolutePath, buildscriptAccessorsDependency) } override fun deploy(rootProject: Project, configModified: Boolean) { fun Project.deploy() { val config = config.from(this).buildscript - if (!config.isEnabled) return + if (!isEnabled(config)) return val className = buildscriptGenerator.propertiesClass(config.name) val accessorsClass = className.toClassOrNull(this) ?: throw RuntimeException( @@ -108,7 +112,13 @@ internal class BuildscriptDeployer(private val _config: () -> GropifyConfig) : D """.trimIndent() ) - getOrCreate(config.extensionName.camelcase(), accessorsClass) + val extensionName = config.extensionName.camelcase() + + getOrCreate(extensionName, accessorsClass) + + if (hasExtension(extensionName)) + Logger.debug("Created buildscript extension \"$extensionName\" for $this") + else Logger.warn("Failed to create buildscript extension \"$extensionName\" for $this") } rootProject.deploy() @@ -120,4 +130,9 @@ internal class BuildscriptDeployer(private val _config: () -> GropifyConfig) : D .resolve(GropifyConfigureExtension.NAME) .resolve(GropifyConfig.ARTIFACTS_NAME) .apply { mkdirs() } + + private fun isEnabled(config: GropifyConfig.BuildscriptGenerateConfig): Boolean { + if (!config.isEnabled) Logger.debug("Config buildscript is disabled in ${config.name}, skipping deployment process.") + return config.isEnabled + } } \ No newline at end of file diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/deployer/SourceCodeDeployer.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/deployer/SourceCodeDeployer.kt index 8671456..67c235d 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/deployer/SourceCodeDeployer.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/deployer/SourceCodeDeployer.kt @@ -21,11 +21,11 @@ */ package com.highcapable.gropify.plugin.deployer +import com.highcapable.gropify.debug.Logger +import com.highcapable.gropify.debug.error import com.highcapable.gropify.gradle.api.entity.ProjectDescriptor import com.highcapable.gropify.gradle.api.extension.getFullName import com.highcapable.gropify.gradle.api.extension.getOrNull -import com.highcapable.gropify.internal.Logger -import com.highcapable.gropify.internal.error import com.highcapable.gropify.plugin.DefaultDeployer.generateMap import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.plugin.config.extension.from @@ -85,7 +85,7 @@ internal class SourceCodeDeployer(private val _config: () -> GropifyConfig) : De val sourceCodeType = decideSourceCodeType(config, projectType) val generateDirPath = resolveGenerateDirPath(config) - if (!config.isEnabled) return + if (!isEnabled(config)) return val outputDir = file(generateDirPath) val properties = generateMap(config, ProjectDescriptor.create(project = this)) @@ -107,7 +107,7 @@ internal class SourceCodeDeployer(private val _config: () -> GropifyConfig) : De val generateConfig = GenerateConfig(packageName, className) - sourceCodeGenerator.build(config, generateConfig, properties).let { generator -> + sourceCodeGenerator.build(projectType, config, generateConfig, properties).let { generator -> generator.first { it.type == sourceCodeType } }.writeTo(outputDir) configureSourceSets(project = this) @@ -133,6 +133,8 @@ internal class SourceCodeDeployer(private val _config: () -> GropifyConfig) : De } ?: return } + Logger.debug("Configuring source sets in project '${project.getFullName()}' (${projectType.name}).") + val sourceCodeType = decideSourceCodeType(config, projectType) val resolveSourceCodeType = if (projectType == ProjectType.Java) SourceCodeSpec.Type.Java else sourceCodeType val generateDirPath = resolveGenerateDirPath(config) @@ -147,7 +149,9 @@ internal class SourceCodeDeployer(private val _config: () -> GropifyConfig) : De ProjectType.Java -> javaExtension ProjectType.KMP -> kotlinExtension else -> return - } ?: return + } ?: return Logger.debug("No supportable extension found for configuring source sets in $project") + + Logger.debug("Found $extension: ${extension.javaClass}") val collection = extension.asResolver().optional(!debugMode).firstMethodOrNull { name = "getSourceSets" @@ -157,8 +161,8 @@ internal class SourceCodeDeployer(private val _config: () -> GropifyConfig) : De it?.asResolver()?.optional(!debugMode)?.firstMethodOrNull { name = "getName" }?.invokeQuietly() == config.sourceSetName - } ?: return Logger.with(project).warn( - "Could not found source sets \"${config.sourceSetName}\" in project '${project.getFullName()}' ($projectType)." + } ?: return Logger.warn( + "Could not found source set \"${config.sourceSetName}\" in project '${project.getFullName()}' ($projectType)." ) val directorySet = sourceSet.asResolver().optional(!debugMode).firstMethodOrNull { @@ -172,6 +176,8 @@ internal class SourceCodeDeployer(private val _config: () -> GropifyConfig) : De name = "getSrcDirs" }?.invokeQuietly>() + Logger.debug("Deploying generated source to \"$generateDirPath\".") + val alreadyAdded = srcDirs?.any { it is File && it.canonicalPath.endsWith(generateDirPath) } == true if (!alreadyAdded) { val resolver = directorySet?.asResolver()?.optional(!debugMode)?.firstMethodOrNull { @@ -179,10 +185,10 @@ internal class SourceCodeDeployer(private val _config: () -> GropifyConfig) : De parameters(Any::class) superclass() } - resolver?.invokeQuietly(generateDirPath) ?: Logger.with(project).error( + resolver?.invokeQuietly(generateDirPath) ?: Logger.error( "Project '${project.getFullName()}' source sets deployed failed, method \"srcDir\" maybe failed during the processing." ) - } + } else Logger.debug("Source directory \"$generateDirPath\" already added to source set \"${config.sourceSetName}\", skipping.") } private fun decideSourceCodeType(config: GropifyConfig.CommonCodeGenerateConfig, type: ProjectType) = when (type) { @@ -213,4 +219,16 @@ internal class SourceCodeDeployer(private val _config: () -> GropifyConfig) : De return "${className.upperCamelcase()}Properties" } + + private fun isEnabled(config: GropifyConfig.SourceCodeGenerateConfig): Boolean { + if (!config.isEnabled) Logger.debug("Config ${ + when (config){ + is GropifyConfig.AndroidGenerateConfig -> "android" + is GropifyConfig.JvmGenerateConfig -> "jvm" + is GropifyConfig.KmpGenerateConfig -> "kmp" + else -> "unknown" + } + } is disabled in ${config.name}, skipping deployment process.") + return config.isEnabled + } } \ No newline at end of file diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/extension/dsl/configure/GropifyConfigureExtension.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/extension/dsl/configure/GropifyConfigureExtension.kt index ef585ab..3431c5f 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/extension/dsl/configure/GropifyConfigureExtension.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/extension/dsl/configure/GropifyConfigureExtension.kt @@ -23,9 +23,9 @@ package com.highcapable.gropify.plugin.extension.dsl.configure +import com.highcapable.gropify.debug.error +import com.highcapable.gropify.debug.require import com.highcapable.gropify.gradle.api.extension.isUnSafeExtName -import com.highcapable.gropify.internal.error -import com.highcapable.gropify.internal.require import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.plugin.config.extension.create import com.highcapable.gropify.plugin.config.proxy.GropifyConfig @@ -67,6 +67,10 @@ open class GropifyConfigureExtension internal constructor() { * * You can help us identify the problem by enabling this option * to print more debugging information in the logs. + * + * - Note: THIS IS ONLY FOR DEBUGGING! + * The debug log will contain your local environment, + * which may contain sensitive information. Please be sure to protect this information. */ var debugMode = false @JvmName("debugMode") set @@ -109,6 +113,17 @@ open class GropifyConfigureExtension internal constructor() { ) val isEnabled: Boolean get() = Gropify.error("No getter available.") + /** + * Please call it from top level [GropifyConfigureExtension]. + * @throws IllegalStateException + */ + @Suppress("unused") + @Deprecated( + message = "Please call it from top level `GropifyConfigureExtension`.", + level = DeprecationLevel.ERROR + ) + val debugMode: Boolean get() = Gropify.error("No getter available.") + /** * Configure common. * diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/BuildscriptGenerator.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/BuildscriptGenerator.kt index cd158b1..a2b93d7 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/BuildscriptGenerator.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/BuildscriptGenerator.kt @@ -21,10 +21,10 @@ */ package com.highcapable.gropify.plugin.generator +import com.highcapable.gropify.debug.Logger +import com.highcapable.gropify.debug.error +import com.highcapable.gropify.debug.require import com.highcapable.gropify.gradle.api.GradleDescriptor -import com.highcapable.gropify.internal.Logger -import com.highcapable.gropify.internal.error -import com.highcapable.gropify.internal.require import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.plugin.config.proxy.GropifyConfig import com.highcapable.gropify.plugin.extension.accessors.proxy.ExtensionAccessors @@ -293,7 +293,6 @@ internal class BuildscriptGenerator { * - Note: [allConfig] and [allKeyValues] must be equal in number. */ fun build( - config: GropifyConfig, allConfig: MutableList, allKeyValues: MutableList ) = runCatching { @@ -303,7 +302,7 @@ internal class BuildscriptGenerator { val files = mutableListOf() if (allConfig.isEmpty()) return@runCatching let { - if (config.debugMode) Logger.get().debug("No buildscript accessors classes to generate.") + Logger.debug("No buildscript accessors classes to generate.") files } @@ -322,12 +321,7 @@ internal class BuildscriptGenerator { files.add(buildTypeSpec().createJavaFile(ACCESSORS_PACKAGE_NAME)) } - if (config.debugMode) files.forEach { - Logger.get().debug( - "Generated buildscript accessors class: ${it.typeSpec().name()}\n" + - "====== BEGIN FILE CONTENT ======\n$it\n====== END FILE CONTENT ======" - ) - } + Logger.debug("Generated buildscript accessors classes: [${files.joinToString { it.typeSpec().name() }}]") files }.getOrElse { Gropify.error("Failed to generated accessors classes.\n${it.stackTraceToString()}") } diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/JavaCodeGenerator.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/JavaCodeGenerator.kt index a253a08..c65cca9 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/JavaCodeGenerator.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/JavaCodeGenerator.kt @@ -21,7 +21,7 @@ */ package com.highcapable.gropify.plugin.generator -import com.highcapable.gropify.internal.error +import com.highcapable.gropify.debug.error import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.plugin.config.proxy.GropifyConfig import com.highcapable.gropify.plugin.generator.config.GenerateConfig diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/KotlinCodeGenerator.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/KotlinCodeGenerator.kt index 98140b0..82f714f 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/KotlinCodeGenerator.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/KotlinCodeGenerator.kt @@ -21,7 +21,7 @@ */ package com.highcapable.gropify.plugin.generator -import com.highcapable.gropify.internal.error +import com.highcapable.gropify.debug.error import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.plugin.config.proxy.GropifyConfig import com.highcapable.gropify.plugin.generator.config.GenerateConfig diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/SourceCodeGenerator.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/SourceCodeGenerator.kt index 28592fe..75465df 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/SourceCodeGenerator.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/SourceCodeGenerator.kt @@ -21,9 +21,11 @@ */ package com.highcapable.gropify.plugin.generator -import com.highcapable.gropify.internal.require +import com.highcapable.gropify.debug.Logger +import com.highcapable.gropify.debug.require import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.plugin.config.proxy.GropifyConfig +import com.highcapable.gropify.plugin.deployer.extension.ProjectType import com.highcapable.gropify.plugin.generator.config.GenerateConfig import com.highcapable.gropify.plugin.generator.config.SourceCodeSpec import com.highcapable.gropify.plugin.generator.extension.PropertyMap @@ -38,16 +40,33 @@ internal class SourceCodeGenerator { /** * Build source code specs. + * @param projectType the project type. * @param config the current generate config. * @param generateConfig the generate config. * @param keyValues the properties' key-values map. * @return [List]<[SourceCodeSpec]> */ - fun build(config: GropifyConfig.SourceCodeGenerateConfig, generateConfig: GenerateConfig, keyValues: PropertyMap): List { + fun build( + projectType: ProjectType, + config: GropifyConfig.SourceCodeGenerateConfig, + generateConfig: GenerateConfig, + keyValues: PropertyMap + ): List { Gropify.require(config is GropifyConfig.CommonCodeGenerateConfig) { "Only Android, Jvm, Kotlin Multiplatform project is supported for now." } + Logger.debug( + """ + Generated source for ${config.name} + ---------- + [Class]: ${generateConfig.packageName}.${generateConfig.className} + [Project Type]: $projectType + [Source Set]: ${config.sourceSetName} + [Generate Dir]: ${config.generateDirPath} + ---------- + """.trimIndent() + ) return listOf( java.build(config, generateConfig, keyValues), kotlin.build(config, generateConfig, keyValues) diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/config/SourceCodeSpec.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/config/SourceCodeSpec.kt index 2f7a5c8..d8120a6 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/config/SourceCodeSpec.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/config/SourceCodeSpec.kt @@ -21,7 +21,7 @@ */ package com.highcapable.gropify.plugin.generator.config -import com.highcapable.gropify.internal.error +import com.highcapable.gropify.debug.error import com.highcapable.gropify.plugin.Gropify import com.palantir.javapoet.JavaFile import com.squareup.kotlinpoet.FileSpec diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/extension/PropertyType.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/extension/PropertyType.kt index 8a30d88..df1da5c 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/extension/PropertyType.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/generator/extension/PropertyType.kt @@ -21,8 +21,8 @@ */ package com.highcapable.gropify.plugin.generator.extension -import com.highcapable.gropify.internal.error -import com.highcapable.gropify.internal.require +import com.highcapable.gropify.debug.error +import com.highcapable.gropify.debug.require import com.highcapable.gropify.plugin.Gropify import com.highcapable.gropify.utils.extension.isNumeric import kotlin.reflect.KClass @@ -81,7 +81,7 @@ internal fun String.createTypeValue(autoConversion: Boolean, key: String, type: Gropify.require(doubleValue != null && !doubleValue.isInfinite()) { "The \"$key\" value \"$this\" cannot be converted to Double type." } - + trimmed } Float::class -> { @@ -89,7 +89,7 @@ internal fun String.createTypeValue(autoConversion: Boolean, key: String, type: Gropify.require(floatValue != null && !floatValue.isInfinite()) { "The \"$key\" value \"$this\" cannot be converted to Float type." } - + if (trimmed.endsWith("f") || trimmed.endsWith("F")) trimmed else "${trimmed}f" } else -> Gropify.error( diff --git a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/helper/AndroidProjectHelper.kt b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/helper/AndroidProjectHelper.kt index 78b65f2..6d422d6 100644 --- a/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/helper/AndroidProjectHelper.kt +++ b/gropify-gradle-plugin/src/main/kotlin/com/highcapable/gropify/plugin/helper/AndroidProjectHelper.kt @@ -21,10 +21,10 @@ */ package com.highcapable.gropify.plugin.helper +import com.highcapable.gropify.debug.Logger import com.highcapable.gropify.gradle.api.extension.getFullName import com.highcapable.gropify.gradle.api.extension.getOrNull import com.highcapable.gropify.gradle.api.extension.hasExtension -import com.highcapable.gropify.internal.Logger import com.highcapable.gropify.plugin.deployer.extension.ExtensionName import com.highcapable.gropify.plugin.extension.dsl.configure.GropifyConfigureExtension import com.highcapable.kavaref.KavaRef.Companion.asResolver @@ -73,7 +73,7 @@ internal object AndroidProjectHelper { }.onFailure { // If file broken, reset it. namespacesFile.writeText("{}") - Logger.with(this).warn("Android project namespaces file was broken and has been reset.") + Logger.warn("Android project namespaces file was broken and has been reset.") }.getOrDefault(hashMapOf()) val namespace = getExtensionNamespace()