/*
 * 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 (snapshots repository, CI job)
def apacheNexusSnapshotsRepository = "https://repository.apache.org/content/repositories/snapshots"

// 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("mavenToApacheSnapshots", {
  group = "Distribution"
  description = "Publish Lucene Maven artifacts to Apache Snapshots repository: ${apacheNexusSnapshotsRepository}"

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

LuceneBuildGlobalsExtension buildGlobals = rootProject.extensions.getByType(LuceneBuildGlobalsExtension)

def checkSnapshotsRepositoryPushPreconditions = tasks.register("checkSnapshotsRepositoryPushPreconditions", {
  doFirst {
    // Make sure we're pushing a snapshot version.
    if (!buildGlobals.snapshotBuild) {
      throw new GradleException("ASF snapshots repository will not accept a non-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.+ToApacheSnapshotsRepository/ }.configureEach {
    dependsOn checkSnapshotsRepositoryPushPreconditions
  }

  plugins.withType(PublishingPlugin).configureEach {
    publishing {
      repositories {
        maven {
          name = "ApacheSnapshots"
          url = apacheNexusSnapshotsRepository

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