/*
 * Copyright Thoughtworks, Inc.
 *
 * 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.
 */

import com.thoughtworks.go.build.GoVersions

description = 'GoCD Base'

java {
  // For use of the LogConfigurator or LogHelper
  registerFeature('logging') {
    usingSourceSet(sourceSets.create('logging'))
  }

  // For use of the GoAgentServerClientBuilder
  registerFeature('agentClient') {
    usingSourceSet(sourceSets.create('agentClient'))
  }
}

CapabilitySupport.extendMain(project, 'logging', 'agentClient')

dependencies {
  api project.deps.slf4jApi
  loggingApi project.deps.logback

  agentClientApi project.deps.logback
  agentClientImplementation project.deps.bouncyCastle
  agentClientImplementation project.deps.bouncyCastlePkix

  agentClientImplementation(project.deps.apacheHttpComponents) {
    exclude(module: 'commons-codec')
  }
  if (project.deps.apacheHttpComponents) {
    agentClientRuntimeOnly project.deps.slf4jJcl // Allow commons-logging replacement
  }

  compileOnly project.deps.jakartaAnnotation
  compileOnly project.deps.jetBrainsAnnotations

  testImplementation(testFixtures(project(':test:test-utils')))
  testImplementation(testFixtures(project(':agent-common')))
  testImplementation project.deps.junit5Api
  testRuntimeOnly project.deps.junit5Engine
  testRuntimeOnly project.deps.junit5PlatformLauncher
  testImplementation project.deps.junit5Params
  testImplementation project.deps.systemStubs
  testImplementation project.deps.assertJ
}

def generatedResourcesOutput = file("resources-generated")
def generatedResourcesInput = file('resource-templates')

sourceSets {
  main {
    resources {
      output.dir generatedResourcesOutput, builtBy: 'generateResources'
      srcDirs += generatedResourcesOutput
    }
  }
}

clean {
  delete(generatedResourcesOutput)
}

tasks.register('generateResources', Copy) {
  inputs.dir(generatedResourcesInput)
  outputs.dir(generatedResourcesOutput)

  def goVersions = project.goVersions as GoVersions
  def props = [
    'goVersion'    : goVersions.goVersion,
    'distVersion'  : goVersions.distVersion,
    'fullVersion'  : goVersions.fullVersion,
    'gitRevision'  : goVersions.gitRevision,
  ]
  inputs.properties(props)

  doFirst {
    delete(generatedResourcesOutput)
    generatedResourcesOutput.mkdirs()
  }

  into generatedResourcesOutput
  from(generatedResourcesInput) {
    expand(props)
  }
}
licenseMain.dependsOn generateResources
processResources.dependsOn generateResources

tasks.register('prepare') {
  dependsOn generateResources
}
