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

description = 'GoCD Release Scripts'

tasks.register('updateBuildMap') {
  outputs.upToDateWhen { false }
  def checkoutDir = "${layout.buildDirectory.get()}/build-map"
  String username = System.getenv('BUILD_MAP_USER')
  String password = System.getenv('BUILD_MAP_PASSWORD')

  onlyIf { username && password }

  def injected = project.objects.newInstance(Injected)
  doFirst {
    delete(checkoutDir)

    injected.execOps.exec {
      commandLine = ['git', 'clone', '--quiet', "https://github.com/gocd/build_map", checkoutDir]
      standardOutput = System.out
      errorOutput = System.err
    }

    injected.execOps.exec {
      commandLine = ['./build_go_cd_commit_to_package.rb']
      workingDir = checkoutDir
      standardOutput = System.out
      errorOutput = System.err
    }
  }
}
