/*
 * 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 = 'GoCD Agent Bootstrapper'

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

  testImplementation project(':base')
  testImplementation(project(':agent-common')) { capabilities { requireFeature('agent-client') } }
  testImplementation(testFixtures(project(':test:test-utils')))
  testImplementation(testFixtures(project(':agent-common')))
  testImplementation project.deps.mockitoCore
  testImplementation project.deps.junit5Api
  testRuntimeOnly project.deps.junit5Engine
  testRuntimeOnly project.deps.junit5PlatformLauncher
  testImplementation project.deps.assertJ

  packagingOnly project(path: ':agent-launcher', configuration: 'fatJarConfig')
  extractedAtTopLevel project(':jar-class-loader')
}

jar {
  archiveClassifier = 'classes'
}

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

  manifest {
    attributes 'Main-Class': 'com.thoughtworks.gocd.Boot'
    attributes 'GoCD-Main-Class': 'com.thoughtworks.go.agent.bootstrapper.AgentBootstrapper'
  }

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

  from({ configurations.packagingOnly }) {
    into "defaultFiles/"
    rename "(.*)-${project.version}.jar", '$1.jar'
  }

  from({ configurations.extractedAtTopLevel.collect { it.isDirectory() ? it : zipTree(it) } }) {
    into("/")
  }
}

tasks.register('verifyJar', VerifyJarTask) {
  jarTask = fatJar
  expectedJars = [
    'defaultFiles': [
      "agent-launcher.jar",
    ],
    'lib': [
      "agent-bootstrapper-${project.version}-classes.jar",
      "agent-common-${project.version}.jar",
      "base-${project.version}.jar",
      "commons-io-${project.versions.commonsIO}.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
}
