diff --git a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/TechDebtPlugin.kt b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/TechDebtPlugin.kt index fcf7ce1..d145bcc 100644 --- a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/TechDebtPlugin.kt +++ b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/TechDebtPlugin.kt @@ -1,5 +1,6 @@ package com.escodro.techdebt.gradle +import com.escodro.techdebt.gradle.extension.TechDebtExtension import com.escodro.techdebt.gradle.extension.configureAndroidPlugins import com.escodro.techdebt.gradle.extension.configureJvmPlugins import com.escodro.techdebt.gradle.extension.configureKmpPlugins diff --git a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/ProjectExtensions.kt b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/ProjectExtensions.kt index f360000..2c9dd93 100644 --- a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/ProjectExtensions.kt +++ b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/ProjectExtensions.kt @@ -1,7 +1,6 @@ package com.escodro.techdebt.gradle.extension import com.escodro.techdebt.gradle.GenerateTechDebtReportTask -import com.escodro.techdebt.gradle.TechDebtExtension import com.google.devtools.ksp.gradle.KspExtension import java.util.Locale.getDefault import org.gradle.api.Project @@ -53,10 +52,11 @@ fun Project.configureKmpPlugins() { extensions.getByType(KotlinMultiplatformExtension::class.java).targets.all { target -> if (target.name == KMP_METADATA_TARGET) return@all - val configurationName = - "ksp${target.name.replaceFirstChar { + val targetName: String = + target.name.replaceFirstChar { if (it.isLowerCase()) it.titlecase(getDefault()) else it.toString() - }}" + } + val configurationName = "$KSP_PARAM$targetName" // Check if the configuration exists before adding the dependency if (configurations.findByName(configurationName) != null) { @@ -67,7 +67,7 @@ fun Project.configureKmpPlugins() { } else { // Fallback: Use the general 'ksp' configuration if the specific one doesn't // exist or just skip it if it's not applicable for this target. - dependencies.add("ksp", "$TECH_DEBT_PROCESSOR_DEPENDENCY:${getPluginVersion()}") + dependencies.add(KSP_PARAM, "$TECH_DEBT_PROCESSOR_DEPENDENCY:${getPluginVersion()}") } } dependencies.add( diff --git a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/PropertiesExtensions.kt b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/PropertiesExtensions.kt index 855fcca..a915d25 100644 --- a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/PropertiesExtensions.kt +++ b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/PropertiesExtensions.kt @@ -2,7 +2,6 @@ package com.escodro.techdebt.gradle.extension import com.escodro.techdebt.gradle.TechDebtPlugin import java.util.* -import org.apache.log4j.Logger private const val TECH_DEBT_PROPERTIES = "techdebt.properties" private const val VERSION_PROPERTY = "version" @@ -11,31 +10,28 @@ private const val VERSION_PROPERTY = "version" * Returns the current version of the plugin. The version is loaded from the `techdebt.properties` * file. * + * @param resourceName the resource name to load the version from. Defaults to `techdebt.properties` * @return the plugin version */ -fun getPluginVersion(): String { +fun getPluginVersion( + resourceName: String = TECH_DEBT_PROPERTIES, +): String { val props = Properties().apply { val classLoader = TechDebtPlugin::class.java.classLoader val resourceStream = - classLoader?.getResourceAsStream(TECH_DEBT_PROPERTIES) - ?: Thread.currentThread() - .contextClassLoader - ?.getResourceAsStream(TECH_DEBT_PROPERTIES) + classLoader?.getResourceAsStream(resourceName) + ?: Thread.currentThread().contextClassLoader?.getResourceAsStream(resourceName) if (resourceStream == null) { - // Fallback for cases where the resource might not be in the classpath during - // tests or specific environments. - Logger.getLogger(TechDebtPlugin::class.java) - .warn("techdebt.properties not found, " + "defaulting version to 1.0.0") - return "1.0.0" + error("Could not find $resourceName in the classpath") } resourceStream.use { stream -> load(stream) } } val version: String = - props[VERSION_PROPERTY] as? String ?: error("Version not found in techdebt.properties") + props[VERSION_PROPERTY] as? String ?: error("Version not found in $resourceName") if (version.isBlank()) { - error("Version cannot be blank in techdebt.properties") + error("Version cannot be blank in $resourceName") } return version } diff --git a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/SourceFileResolver.kt b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/SourceFileResolver.kt similarity index 97% rename from techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/SourceFileResolver.kt rename to techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/SourceFileResolver.kt index 5281f4b..27eaa95 100644 --- a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/SourceFileResolver.kt +++ b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/SourceFileResolver.kt @@ -1,4 +1,4 @@ -package com.escodro.techdebt.gradle.parser +package com.escodro.techdebt.gradle.extension import java.io.File diff --git a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/TechDebtExtension.kt b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/TechDebtExtension.kt similarity index 95% rename from techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/TechDebtExtension.kt rename to techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/TechDebtExtension.kt index f3fd7ae..0e6c041 100644 --- a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/TechDebtExtension.kt +++ b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/extension/TechDebtExtension.kt @@ -1,4 +1,4 @@ -package com.escodro.techdebt.gradle +package com.escodro.techdebt.gradle.extension import org.gradle.api.file.RegularFileProperty import org.gradle.api.provider.Property diff --git a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/GeneratedTechDebtParser.kt b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/GeneratedTechDebtParser.kt index 8c55970..77ce065 100644 --- a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/GeneratedTechDebtParser.kt +++ b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/GeneratedTechDebtParser.kt @@ -41,8 +41,7 @@ internal class GeneratedTechDebtParser { val parts = path.split("/") val kspIndex = parts.indexOf("ksp") if (kspIndex != -1 && kspIndex + 1 < parts.size) { - val sourceSetName = parts[kspIndex + 1] - return sourceSetName + return parts[kspIndex + 1] } return SOURCE_SET_UNKNOWN } diff --git a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/GitParser.kt b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/GitParser.kt index 552cb91..cc6b69f 100644 --- a/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/GitParser.kt +++ b/techdebt-gradle-plugin/src/main/kotlin/com/escodro/techdebt/gradle/parser/GitParser.kt @@ -1,5 +1,6 @@ package com.escodro.techdebt.gradle.parser +import com.escodro.techdebt.gradle.extension.SourceFileResolver import com.escodro.techdebt.gradle.model.TechDebtItem import java.io.File import java.io.IOException diff --git a/techdebt-gradle-plugin/src/test/kotlin/com/escodro/techdebt/gradle/extension/PropertiesExtensionsTest.kt b/techdebt-gradle-plugin/src/test/kotlin/com/escodro/techdebt/gradle/extension/PropertiesExtensionsTest.kt new file mode 100644 index 0000000..cf05e07 --- /dev/null +++ b/techdebt-gradle-plugin/src/test/kotlin/com/escodro/techdebt/gradle/extension/PropertiesExtensionsTest.kt @@ -0,0 +1,33 @@ +package com.escodro.techdebt.gradle.extension + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test + +class PropertiesExtensionsTest { + + @Test + fun `test getPluginVersion returns version from properties file`() { + val version = getPluginVersion("techdebt.properties") + assertEquals("0.4.0", version) + } + + @Test + fun `test getPluginVersion throws exception when file is missing`() { + assertThrows(IllegalStateException::class.java) { getPluginVersion("missing.properties") } + } + + @Test + fun `test getPluginVersion throws error when version is missing`() { + assertThrows(IllegalStateException::class.java) { + getPluginVersion("no_version.properties") + } + } + + @Test + fun `test getPluginVersion throws error when version is blank`() { + assertThrows(IllegalStateException::class.java) { + getPluginVersion("blank_version.properties") + } + } +} diff --git a/techdebt-gradle-plugin/src/test/kotlin/com/escodro/techdebt/gradle/parser/SourceFileResolverTest.kt b/techdebt-gradle-plugin/src/test/kotlin/com/escodro/techdebt/gradle/extension/SourceFileResolverTest.kt similarity index 98% rename from techdebt-gradle-plugin/src/test/kotlin/com/escodro/techdebt/gradle/parser/SourceFileResolverTest.kt rename to techdebt-gradle-plugin/src/test/kotlin/com/escodro/techdebt/gradle/extension/SourceFileResolverTest.kt index 3b753f8..36e29b9 100644 --- a/techdebt-gradle-plugin/src/test/kotlin/com/escodro/techdebt/gradle/parser/SourceFileResolverTest.kt +++ b/techdebt-gradle-plugin/src/test/kotlin/com/escodro/techdebt/gradle/extension/SourceFileResolverTest.kt @@ -1,4 +1,4 @@ -package com.escodro.techdebt.gradle.parser +package com.escodro.techdebt.gradle.extension import java.io.File import org.junit.jupiter.api.Assertions.assertEquals diff --git a/techdebt-gradle-plugin/src/test/resources/blank_version.properties b/techdebt-gradle-plugin/src/test/resources/blank_version.properties new file mode 100644 index 0000000..8ed6a8c --- /dev/null +++ b/techdebt-gradle-plugin/src/test/resources/blank_version.properties @@ -0,0 +1 @@ +version= diff --git a/techdebt-gradle-plugin/src/test/resources/no_version.properties b/techdebt-gradle-plugin/src/test/resources/no_version.properties new file mode 100644 index 0000000..76a30c3 --- /dev/null +++ b/techdebt-gradle-plugin/src/test/resources/no_version.properties @@ -0,0 +1 @@ +# No version here diff --git a/techdebt-gradle-plugin/src/test/resources/techdebt.properties b/techdebt-gradle-plugin/src/test/resources/techdebt.properties new file mode 100644 index 0000000..b31ef9a --- /dev/null +++ b/techdebt-gradle-plugin/src/test/resources/techdebt.properties @@ -0,0 +1 @@ +version=0.4.0 diff --git a/techdebt-processor/src/main/kotlin/com/escodro/techdebt/extension/KSAnnotationExtensions.kt b/techdebt-processor/src/main/kotlin/com/escodro/techdebt/extension/KSAnnotationExtensions.kt index 985c702..1f915d1 100644 --- a/techdebt-processor/src/main/kotlin/com/escodro/techdebt/extension/KSAnnotationExtensions.kt +++ b/techdebt-processor/src/main/kotlin/com/escodro/techdebt/extension/KSAnnotationExtensions.kt @@ -5,7 +5,6 @@ import com.google.devtools.ksp.symbol.KSAnnotated import com.google.devtools.ksp.symbol.KSAnnotation import com.google.devtools.ksp.symbol.KSDeclaration import com.google.devtools.ksp.symbol.KSFile -import com.google.devtools.ksp.symbol.Location /** * Returns the name of the given symbol, which can be a declaration or a file. @@ -27,10 +26,8 @@ fun getSymbolName(symbol: KSAnnotated): String = * @param sourceSet the source set name * @return the source location */ -fun getAnnotationLocation(annotation: KSAnnotation, sourceSet: String): String = - getLocation(annotation.location, sourceSet) - -private fun getLocation(location: Location, sourceSet: String): String { +fun getAnnotationLocation(annotation: KSAnnotation, sourceSet: String): String { + val location = annotation.location val sourceLocation = if (location is FileLocation) { "${location.filePath}:${location.lineNumber}" diff --git a/techdebt-processor/src/main/kotlin/com/escodro/techdebt/processor/SuppressSymbolProcessor.kt b/techdebt-processor/src/main/kotlin/com/escodro/techdebt/processor/SuppressSymbolProcessor.kt index a2187ee..c0e1067 100644 --- a/techdebt-processor/src/main/kotlin/com/escodro/techdebt/processor/SuppressSymbolProcessor.kt +++ b/techdebt-processor/src/main/kotlin/com/escodro/techdebt/processor/SuppressSymbolProcessor.kt @@ -29,14 +29,14 @@ internal class SuppressSymbolProcessor { moduleName: String, sourceSet: String ) { - resolver.getSymbolsWithAnnotation(Suppress::class.qualifiedName!!).forEach { symbol -> + resolver.getSymbolsWithAnnotation(SuppressName).forEach { symbol -> if (!symbol.validate()) return@forEach val suppressAnnotations = symbol.annotations - .filter { - it.annotationType.resolve().declaration.qualifiedName?.asString() == - Suppress::class.qualifiedName + .filter { annotation -> + annotation.annotationType.resolve().declaration.qualifiedName?.asString() == + SuppressName } .toList() if (suppressAnnotations.isEmpty()) return@forEach @@ -76,4 +76,8 @@ internal class SuppressSymbolProcessor { } } } + + companion object { + private val SuppressName: String = Suppress::class.qualifiedName!! + } } diff --git a/techdebt-processor/src/main/kotlin/com/escodro/techdebt/processor/TechDebtSymbolProcessor.kt b/techdebt-processor/src/main/kotlin/com/escodro/techdebt/processor/TechDebtSymbolProcessor.kt index 4b6474a..fe6c002 100644 --- a/techdebt-processor/src/main/kotlin/com/escodro/techdebt/processor/TechDebtSymbolProcessor.kt +++ b/techdebt-processor/src/main/kotlin/com/escodro/techdebt/processor/TechDebtSymbolProcessor.kt @@ -35,16 +35,16 @@ internal class TechDebtSymbolProcessor { sourceSet: String, unableToProcess: MutableList ) { - resolver.getSymbolsWithAnnotation(TechDebt::class.qualifiedName!!).forEach { symbol -> + resolver.getSymbolsWithAnnotation(TechDebtAnnotationName).forEach { symbol -> if (!symbol.validate()) { unableToProcess.add(symbol) return@forEach } val annotation = - symbol.annotations.firstOrNull { - it.annotationType.resolve().declaration.qualifiedName?.asString() == - TechDebt::class.qualifiedName + symbol.annotations.firstOrNull { annotation -> + annotation.annotationType.resolve().declaration.qualifiedName?.asString() == + TechDebtAnnotationName } ?: return@forEach val args = annotation.arguments.associate { it.name!!.asString() to it.value } @@ -75,4 +75,8 @@ internal class TechDebtSymbolProcessor { ) } } + + companion object { + private val TechDebtAnnotationName: String = TechDebt::class.qualifiedName!! + } }