/*
 * 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
import groovy.json.JsonOutput

plugins {
  id 'base'
}

description = 'GoCD Installers'

configurations {
  agentBootstrapperJar {
    transitive = false
  }
  serverJar {
    transitive = false
  }
}

dependencies {
  agentBootstrapperJar project(path: ':agent-bootstrapper', configuration: 'fatJarConfig')
  serverJar project(path: ':server-launcher', configuration: 'fatJarConfig')
}

tasks.register('versionFile') {
  outputs.upToDateWhen { false }

  def goVersions = project.goVersions as GoVersions
  inputs.property('goVersions', goVersions)

  def versionFile = base.distsDirectory.file("meta/version.json")
  outputs.file versionFile

  doLast {
    versionFile.get().asFile.with {
      parentFile.mkdirs()
      withWriter { out ->
        out.write(JsonOutput.prettyPrint(JsonOutput.toJson([
          go_version         : goVersions.goVersion,
          previous_go_version: goVersions.previousVersion,
          next_go_version    : goVersions.nextVersion,
          go_build_number    : goVersions.distVersion,
          go_full_version    : goVersions.fullVersion,
          git_sha            : goVersions.gitRevision,
          pipeline_name      : System.getenv('GO_PIPELINE_NAME'),
          pipeline_counter   : System.getenv('GO_PIPELINE_COUNTER'),
          pipeline_label     : System.getenv('GO_PIPELINE_LABEL'),
          stage_name         : System.getenv('GO_STAGE_NAME'),
          stage_counter      : System.getenv('GO_STAGE_COUNTER'),
          job_name           : System.getenv('GO_JOB_NAME')
        ])))
      }
    }

  }
}

assemble.dependsOn(':installers:versionFile')

apply from: 'tanuki.gradle'
apply from: 'generic.gradle'
apply from: 'osx.gradle'
apply from: 'linux.gradle'
apply from: 'windows.gradle'

tasks.register("downloadAllJres") {
  dependsOn ':installers:downloadWindowsJre', ':installers:downloadMacJreX64', ':installers:downloadMacJreArm64'
}
