/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.lucene.gradle.plugins.globals.LuceneBuildGlobalsExtension

// Configure artifact push to apache nexus (releases repository).
def apacheNexusReleasesRepository = "https://repository.apache.org/service/local/staging/deploy/maven2"

// These access credentials must be passed by the release manager
// (either on command-line, via the environment or via ~/.gradle.properties).
Provider<String> asfNexusUsername = providers.gradleProperty("asfNexusUsername")
    .orElse(providers.systemProperty("asfNexusUsername"))
    .orElse(providers.environmentVariable("ASF_NEXUS_USERNAME"))

Provider<String> asfNexusPassword = providers.gradleProperty("asfNexusPassword")
    .orElse(providers.systemProperty("asfNexusPassword"))
    .orElse(providers.environmentVariable("ASF_NEXUS_PASSWORD"))

tasks.register("mavenToApacheReleases", {
  group = "Distribution"
  description = "Publish Lucene Maven artifacts to Apache Releases repository: ${apacheNexusReleasesRepository}"

  dependsOn rootProject.ext.mavenProjects.collect {
    it.tasks.matching { it.name == "publishSignedJarsPublicationToApacheReleasesRepository" }
  }
})

LuceneBuildGlobalsExtension buildGlobals = rootProject.extensions.getByType(LuceneBuildGlobalsExtension)

def checkReleasesRepositoryPushPreconditions = tasks.register("checkReleasesRepositoryPushPreconditions", {
  doFirst {
    // Make sure we're pushing a release version. The release repository
    // does not accept snapshots and returns cryptic errors upon trying.
    if (buildGlobals.snapshotBuild) {
      throw new GradleException("ASF releases repository will not accept a snapshot version: ${rootProject.version}")
    }

    // Make sure access credentials have been passed.
    if (!asfNexusUsername.isPresent() || !asfNexusPassword.isPresent()) {
      throw new GradleException("asfNexusUsername or asfNexusPassword is empty: these are required to publish to " +
      " ASF Nexus.")
    }
  }
})

configure(rootProject.ext.mavenProjects) { Project project ->
  // Make sure any actual publication task is preceded by precondition checks.
  tasks.matching { it.name ==~ /publish.+ToApacheReleasesRepository/ }.configureEach {
    dependsOn checkReleasesRepositoryPushPreconditions
  }

  // Configure the release repository.
  plugins.withType(PublishingPlugin).configureEach {
    publishing {
      repositories {
        maven {
          name = "ApacheReleases"
          url = apacheNexusReleasesRepository

          credentials {
            username = asfNexusUsername.getOrElse(null)
            password = asfNexusPassword.getOrElse(null)
          }
        }
      }
    }
  }
}
