All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.autonomousapps.extension.ProjectIssueHandler.kt Maven / Gradle / Ivy

// Copyright (c) 2024. Tony Robalik.
// SPDX-License-Identifier: Apache-2.0
@file:Suppress("unused")

package com.autonomousapps.extension

import org.gradle.api.Action
import org.gradle.api.Named
import org.gradle.api.model.ObjectFactory
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.setProperty
import javax.inject.Inject

/**
 * ```
 * dependencyAnalysis {
 *   issues {
 *     project(":lib") {
 *       // One or more source sets (by name) to ignore in dependency analysis.
 *       ignoreSourceSet(...)
 *
 *       // Specify severity and exclude rules for all types of dependency violations.
 *       onAny { ... }
 *
 *       // Specify severity and exclude rules for unused dependencies.
 *       onUnusedDependencies { ... }
 *
 *       // Specify severity and exclude rules for undeclared transitive dependencies.
 *       onUsedTransitiveDependencies { ... }
 *
 *       // Specify severity and exclude rules for dependencies declared on the wrong configuration.
 *       onIncorrectConfiguration { ... }
 *
 *       // Specify severity and exclude rules for dependencies that could be compileOnly but are
 *       // otherwise declared.
 *       onCompileOnly { ... }
 *
 *       // Specify severity and exclude rules for dependencies that could be runtimeOnly but are
 *       // otherwise declared.
 *       onRuntimeOnly { ... }
 *
 *       // Specify severity and exclude rules for unused annotation processors.
 *       onUnusedAnnotationProcessors { ... }
 *
 *       // Specify severity and exclude rules for redundant plugins.
 *       onRedundantPlugins { ... }
 *
 *       // Specify severity and exclude rules for module structure advice.
 *       onModuleStructure {
 *         severity(<'fail'|'warn'|'ignore'>)
 *         exclude('android')
 *       }
 *     }
 *   }
 * }
 * ```
 */
abstract class ProjectIssueHandler @Inject constructor(
  private val projectPath: String,
  objects: ObjectFactory,
) : Named {

  override fun getName(): String = projectPath

  internal val sourceSets = objects.domainObjectContainer(
    SourceSetsHandler::class.java,
    SourceSetsHandler.Factory(projectPath, objects)
  )

  internal val anyIssue = objects.newInstance()
  internal val unusedDependenciesIssue = objects.newInstance()
  internal val usedTransitiveDependenciesIssue = objects.newInstance()
  internal val incorrectConfigurationIssue = objects.newInstance()
  internal val unusedAnnotationProcessorsIssue = objects.newInstance()
  internal val compileOnlyIssue = objects.newInstance()
  internal val runtimeOnlyIssue = objects.newInstance()
  internal val redundantPluginsIssue = objects.newInstance()
  internal val moduleStructureIssue = objects.newInstance()

  internal val ignoreSourceSets = objects.setProperty()

  fun ignoreSourceSet(vararg ignore: String) {
    ignoreSourceSets.addAll(ignore.toSet())
  }

  /** Specify custom behavior for [sourceSetName]. */
  fun sourceSet(sourceSetName: String, action: Action) {
    sourceSets.maybeCreate(sourceSetName).let { handler ->
      action.execute(handler.project)
    }
  }

  fun onAny(action: Action) {
    action.execute(anyIssue)
  }

  fun onUnusedDependencies(action: Action) {
    action.execute(unusedDependenciesIssue)
  }

  fun onUsedTransitiveDependencies(action: Action) {
    action.execute(usedTransitiveDependenciesIssue)
  }

  fun onIncorrectConfiguration(action: Action) {
    action.execute(incorrectConfigurationIssue)
  }

  fun onCompileOnly(action: Action) {
    action.execute(compileOnlyIssue)
  }

  fun onRuntimeOnly(action: Action) {
    action.execute(runtimeOnlyIssue)
  }

  fun onUnusedAnnotationProcessors(action: Action) {
    action.execute(unusedAnnotationProcessorsIssue)
  }

  fun onRedundantPlugins(action: Action) {
    action.execute(redundantPluginsIssue)
  }

  fun onModuleStructure(action: Action) {
    action.execute(moduleStructureIssue)
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy