/*
 * 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.VerifyJarTask

description = 'Agent Launcher Jar'

configurations {
  testRuntimeClasspath.extendsFrom(packagingOnly)
}

dependencies {
  implementation(project(':base')) { capabilities { requireFeature('agent-client') } }
  implementation(project(':agent-common')) { capabilities { requireFeature('agent-client') } }
  implementation project.deps.commonsIO
  implementation project.deps.slf4jApi

  testImplementation(testFixtures(project(':agent-common')))
  testImplementation project.deps.junit5Api
  testImplementation project.deps.mockitoCore
  testRuntimeOnly project.deps.junit5Engine
  testRuntimeOnly project.deps.junit5PlatformLauncher
  testImplementation project.deps.assertJ
  testImplementation project.deps.systemStubs

}

jar {
  archiveClassifier = 'classes'
}

def fatJar = tasks.register('fatJar', Jar) {
  finalizedBy 'verifyJar'
  dependsOn jar, configurations.packagingOnly, configurations.runtimeClasspath

  manifest {
    attributes 'Go-Agent-Launcher-Class': 'com.thoughtworks.go.agent.launcher.AgentLauncherImpl'
    attributes 'Go-Agent-Launcher-Lib-Dir': 'libs'
  }

  from({ configurations.runtimeClasspath + configurations.packagingOnly + jar.archiveFile }) { into "libs/" }
}

tasks.register('verifyJar', VerifyJarTask) {
  jarTask = fatJar
  expectedJars = [
    "libs": [
      "agent-common-${project.version}.jar",
      "agent-launcher-${project.version}-classes.jar",
      "base-${project.version}.jar",
      "bcpkix-jdk18on-${project.versions.bouncyCastle}.jar",
      "bcprov-jdk18on-${project.versions.bouncyCastle}.jar",
      "bcutil-jdk18on-${project.versions.bouncyCastle}.jar",
      "commons-io-${project.versions.commonsIO}.jar",
      "httpclient-${project.versions.apacheHttpComponents}.jar",
      "httpcore-4.4.16.jar",
      "jcl-over-slf4j-${project.versions.slf4jBom}.jar",
      "jcommander-${project.versions.jcommander}.jar",
      "logback-classic-${project.versions.logback}.jar",
      "logback-core-${project.versions.logback}.jar",
      "slf4j-api-${project.versions.slf4jBom}.jar",
    ]
  ]
}

artifacts {
  fatJarConfig fatJar
}
