Skip to content

Commit

Permalink
Don't execute when path is not set (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin authored Jan 24, 2025
1 parent 1bc7149 commit 19e3b25
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ testing {

dependencies {
implementation(libs.junit.jupiter.api)
implementation(libs.junit.jupiter.params)
implementation(libs.truth)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class LightsaberPluginIntegrationTest {
* Copy project files from `resources` to temporary directories for isolation.
* This helps with the incremental build (up-to-date checks).
*/
private fun GradleRunner.withProjectDirFromResources(resourcePath: String) = apply {
internal fun GradleRunner.withProjectDirFromResources(resourcePath: String) = apply {
val resourceDir = File(javaClass.classLoader.getResource("testProjects/$resourcePath")!!.file)
val projectDir = Files.createTempDirectory(resourcePath).toFile()
resourceDir.copyRecursively(projectDir)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package schwarz.it.lightsaber.gradle

import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.io.File

class LightsaberPluginOtherTasksTest {

@ParameterizedTest
@ValueSource(strings = ["annotationProcessor", "kapt", "ksp", "androidAnnotationProcessor", "androidKapt", "androidKsp"])
fun `Execute test task correctly`(resourcePath: String) {
val dir: File
GradleRunner.create()
.withProjectDirFromResources(resourcePath)
.also { dir = it.projectDir }
.withPluginClasspath()
.withArguments("test")
.build()

check(dir.walkBottomUp().none { it.extension == "lightsaber" })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public class LightsaberDaggerProcessor : BindingGraphPlugin {
private lateinit var daggerProcessingEnv: DaggerProcessingEnv
private lateinit var fileGenerator: FileGenerator
private lateinit var config: DaggerConfig
private var enabled: Boolean = false

override fun visitGraph(bindingGraph: BindingGraph, diagnosticReporter: DiagnosticReporter) {
if (!enabled) return
val issues = listOf(
runRule(config.checkEmptyComponents, "EmptyComponents") {
checkEmptyComponents(bindingGraph, daggerProcessingEnv)
Expand Down Expand Up @@ -58,6 +60,8 @@ public class LightsaberDaggerProcessor : BindingGraphPlugin {
}

override fun init(processingEnv: DaggerProcessingEnv, options: MutableMap<String, String>) {
val path = options["Lightsaber.path"] ?: return
enabled = true
this.config = DaggerConfig(
checkEmptyComponents = options["Lightsaber.CheckEmptyComponents"] != "false",
checkUnusedBindsInstances = options["Lightsaber.CheckUnusedBindsInstances"] != "false",
Expand All @@ -68,7 +72,6 @@ public class LightsaberDaggerProcessor : BindingGraphPlugin {
checkUnusedScopes = options["Lightsaber.CheckUnusedScopes"] != "false",
)
this.daggerProcessingEnv = processingEnv
val path = checkNotNull(options["Lightsaber.path"]) { "Lightsaber.path argument not provided" }
this.fileGenerator = FileGenerator(Path(path))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import kotlin.io.path.Path
class LightsaberJavacProcessor : AbstractProcessor() {
private lateinit var fileGenerator: FileGenerator
private lateinit var rules: Set<Pair<String, LightsaberJavacRule>>
private var enabled: Boolean = false

override fun init(processingEnv: ProcessingEnvironment) {
val path = checkNotNull(processingEnv.options["Lightsaber.path"]) { "Lightsaber.path argument not provided" }
val path = processingEnv.options["Lightsaber.path"] ?: return
enabled = true
fileGenerator = FileGenerator(Path(path))
val elements = processingEnv.elementUtils
rules = buildSet {
Expand All @@ -30,6 +32,7 @@ class LightsaberJavacProcessor : AbstractProcessor() {
}

override fun process(annotations: Set<TypeElement>, roundEnv: RoundEnvironment): Boolean {
if (!enabled) return false
rules.forEach { (_, rule) -> rule.process(roundEnv) }

if (roundEnv.processingOver()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,21 @@ interface LightsaberKspRule {

class LightsaberKspProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
val path = environment.options["Lightsaber.path"] ?: return NoOpSymbolProcessor
val config = AnnotationProcessorConfig(
checkUnusedInject = environment.options["Lightsaber.CheckUnusedInject"] != "false",
checkUnusedScopes = environment.options["Lightsaber.CheckUnusedScopes"] != "false",
)
val path = checkNotNull(environment.options["Lightsaber.path"]) { "Lightsaber.path argument not provided" }
return LightsaberKspProcessor(FileGenerator(Path(path)), config)
}
}

private object NoOpSymbolProcessor : SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
return emptyList()
}
}

internal data class AnnotationProcessorConfig(
val checkUnusedInject: Boolean,
val checkUnusedScopes: Boolean,
Expand Down

0 comments on commit 19e3b25

Please sign in to comment.