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

configurations {
  pluginsZipConfig
}

def dependencies = [
  new GithubArtifact(
    user: 'gocd',
    repo: 'gocd-ldap-authentication-plugin',
    tagName: 'v3.0.0-466',
    asset: 'gocd-ldap-authentication-plugin-3.0.0-466.jar',
    checksum: '49a988f4b097bfce4ba47e652ecdfa7faed3771a4244f9454fbfab96910ab0ed'
  ),
  new GithubArtifact(
    user: 'gocd',
    repo: 'gocd-filebased-authentication-plugin',
    tagName: 'v2.2.0-365',
    asset: 'gocd-filebased-authentication-plugin-2.2.0-365.jar',
    checksum: 'f04f18e9af23180687b1a8805aeaf2c5cf3a6e781fca1e5031916a95ad8acc0a'
  ),
  new GithubArtifact(
    user: 'tomzo',
    repo: 'gocd-yaml-config-plugin',
    tagName: 'v1.0.0-488',
    asset: 'yaml-config-plugin-1.0.0-488.jar',
    checksum: 'ce6ba0ced525afeeebb2925910570aaed3f6cb93248d2de5075883bae67bb5b5'
  ),
  new GithubArtifact(
    user: 'tomzo',
    repo: 'gocd-json-config-plugin',
    tagName: 'v1.0.0-336',
    asset: 'json-config-plugin-1.0.0-336.jar',
    checksum: '6aef014d03c606947b42e1fec219e47f9cfe523b9130452a0da9dcfb216702c0'
  ),
  new GithubArtifact(
    user: 'gocd',
    repo: 'gocd-file-based-secrets-plugin',
    tagName: 'v1.2.0-380',
    asset: 'gocd-file-based-secrets-plugin-1.2.0-380.jar',
    checksum: 'af0f98d979acad85b1a2e51d69abefd561c63ad85485091c40d2e78acd215c39'
  )
]

tasks.register('createVersionFile') {
  def goVersions = project.goVersions as GoVersions
  inputs.property('goVersions', goVersions)

  def file = layout.buildDirectory.file("version.txt").get().asFile
  outputs.file file

  doLast {
    file.getParentFile().mkdirs()
    file.withWriter { out ->
      out.write("${goVersions.fullVersion} (${goVersions.gitRevision})")
    }
  }
}

def downloadPlugins = tasks.register('downloadPlugins')

def pluginsZip = tasks.register('pluginsZip', Zip) {
  finalizedBy 'verifyPluginZip'

  archiveBaseName = 'plugins'
  dependsOn createVersionFile
  from(createVersionFile)

  from(dependencies.collect { dep -> destFile(dep) })
}

tasks.register('verifyPluginZip', VerifyJarTask) {
  jarTask = pluginsZip
  expectedJars = [
    '/': dependencies.collect { dep -> "${dep.repo}.jar".toString() }
  ]
}

artifacts {
  pluginsZipConfig pluginsZip
}

dependencies.each { dep ->
  def downloadTask = tasks.register("download-${dep.repo}", DownloadFile) { downloadTask ->
    downloadTask.src dep.downloadUrl
    downloadTask.dest destFile(dep)
    downloadTask.checksum = dep.checksum
  }

  downloadPlugins.configure {
    dependsOn downloadTask
  }

  pluginsZip.configure { Zip zipTask ->
    zipTask.dependsOn downloadTask
    zipTask.from(destFile(dep))
  }
}

private File destFile(GithubArtifact dep) {
  new File(gradle.gradleUserHomeDir, "download-cache/${TextUtils.md5Hex(dep.downloadUrl)}/${dep.repo}.jar")
}

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