/*
 * Copyright 2022 Arunkumar
 *
 * 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
 *
 *     http://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.
 */

plugins {
  id "build-common"
  id "java-gradle-plugin"
  alias(deps.plugins.binary.validator)
  alias(deps.plugins.gradle.publish)
  id "org.gradle.kotlin.kotlin-dsl" version "2.1.7"
}

group groupId
version publishVersion

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation platform("org.jetbrains.kotlin:kotlin-bom")
  implementation deps.kotlin
  compileOnly deps.agp

  testImplementation deps.junit
  testImplementation deps.truth
  testImplementation gradleTestKit()
}

gradlePlugin {
  plugins {
    scabbardPlugin {
      id = scabbardGradlePlugin
      displayName = "Scabbard - Dagger 2 Visualizer"
      description = "Gradle plugin to visualize Dagger 2 dependency graph"
      implementationClass = "dev.arunkumar.scabbard.gradle.ScabbardGradlePlugin"
    }
  }
}

pluginBundle {
  website = project.findProperty("website")
  vcsUrl = project.findProperty("website")
  tags = ["dagger 2", "dependency injection", "visualize dependency graph"]
}

sourceSets {
  functionalTest {
    java.srcDirs "src/functionalTest/kotlin"
  }
  main.java.srcDir "build/generated/kotlin"
}

gradlePlugin.testSourceSets(sourceSets.functionalTest)
configurations.functionalTestImplementation.extendsFrom(configurations.testImplementation)

task functionalTest(type: Test) {
  testClassesDirs = sourceSets.functionalTest.output.classesDirs
  classpath = sourceSets.functionalTest.runtimeClasspath
}

check {
  dependsOn(tasks.functionalTest)
}

def generatePluginVersion = tasks.register("generatePluginVersion") {
  def outputDir = file("build/generated/kotlin")
  inputs.property "version", version
  outputs.dir outputDir
  doLast {
    def packageName = groupId + "." + project.name.replace("-", ".")
    def versionFile = file("${outputDir}/${packageName.replace(".", "/")}/Version.kt")
    versionFile.parentFile.mkdirs()
    versionFile.text = """// Generated file. Do not edit!
package $packageName
const val VERSION = "${project.version}"
"""
  }
}

tasks.named("compileKotlin").configure {
  dependsOn(generatePluginVersion)
}
