/*
 * 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 java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

plugins {
  id 'base'
  id 'solr.build-infra'

  alias(libs.plugins.owasp.dependencycheck)
  alias(libs.plugins.cutterslade.analyze)
  alias(libs.plugins.benmanes.versions)
  alias(libs.plugins.kotlin.multiplatform) apply false
  alias(libs.plugins.littlerobots.versioncatalogupdate) apply false
  alias(libs.plugins.thetaphi.forbiddenapis) apply false
  alias(libs.plugins.undercouch.download) apply false
  alias(libs.plugins.ltgt.errorprone) apply false
  alias(libs.plugins.diffplug.spotless) apply false
  alias(libs.plugins.nodegradle.node) apply false
  alias(libs.plugins.openapi.generator) apply false
}

// Declare default Java versions for the entire project and for SolrJ separately
rootProject.ext.minJavaVersionDefault = JavaVersion.toVersion(libs.versions.java.min.get())
rootProject.ext.minJavaVersionSolrJ = JavaVersion.toVersion(libs.versions.java.solrj.get())

// Check development mode for entire project (defaults to true if 'production' not provided and set to true)
rootProject.ext.development = !project.hasProperty('production') || project.findProperty('production') != 'true'

apply from: file('gradle/globals.gradle')

// General metadata.

// Calculate project version:
version = {
  // Release manager: update base version here after release:
  String baseVersion = '10.0.0'

  // On a release explicitly set release version in one go:
  //  -Dversion.release=x.y.z
  
  // Jenkins can just set just a suffix, overriding SNAPSHOT, e.g. using build id:
  //  -Dversion.suffix=jenkins123
  
  String versionSuffix = propertyOrDefault('version.suffix', 'SNAPSHOT')
  String v = propertyOrDefault('version.release', "${baseVersion}-${versionSuffix}")
  if (v.endsWith("-")) {
    v = v.substring(0, v.length() - 1)
  }
  return v
}()
description = 'Grandparent project for Apache Solr'

// Propagate version and derived properties across projects.
allprojects {
  version = rootProject.version

  gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xmaxerrs" << "1000"
    }
  }
}

ext {
  // "base" version is stripped of the qualifier. Compute it.
  baseVersion = {
    def m = (version =~ /^(\d+\.\d+\.\d+)(-(.+))?/)
    if (!m) {
      throw GradleException("Can't strip version to just x.y.z: " + rootProject.version)
    }
    return m[0][1]
  }()
  // "majorVersion" is an integer with just the major version. Compute it.
  majorVersion = {
    def m = (version =~ /^(\d+)\.\d+\.\d+(-(.+))?/)
    if (!m) {
      throw GradleException("Can't strip version to just major version: " + rootProject.version)
    }
    return m[0][1] as int
  }
  // snapshot build marker used in scripts.
  snapshotBuild = version.contains("SNAPSHOT")

  // Build timestamp.
  def tstamp = ZonedDateTime.now()
  buildDate = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(tstamp)
  buildTime = DateTimeFormatter.ofPattern("HH:mm:ss").format(tstamp)
  buildYear = DateTimeFormatter.ofPattern("yyyy").format(tstamp)

  // Allow definiting external tool locations using system props.
  externalTool = { name ->
    def resolved = propertyOrDefault("${name}.exe", name as String)
    logger.info("External tool '${name}' resolved to: ${resolved}")
    return resolved
  }
  
  luceneBaseVersionProvider = project.provider { 
    def luceneVersion = libs.versions.apache.lucene.get()
    def m = (luceneVersion =~ /^\d+\.\d+\.\d+\b/)
    if (!m) {
      throw GradleException("Can't strip base version from " + luceneVersion)
    }
    def luceneBaseVersion = m[0]
    logger.debug('Lucene base version (without suffix): {}', luceneBaseVersion)
    return luceneBaseVersion
  }

  // For testing code
  vectorIncubatorJavaVersions = [ JavaVersion.VERSION_21, JavaVersion.VERSION_22, JavaVersion.VERSION_23, JavaVersion.VERSION_24, JavaVersion.VERSION_25 ] as Set

  // Use gradle flag in root project for further referencing
  withUiModule = gradle.ext.withUiModule
}

// Include smaller chunks configuring dedicated build areas.
// Some of these intersect or add additional functionality.
// The order of inclusion of these files shouldn't matter (but may
// if the build file is incorrectly written and evaluates something
// eagerly).

apply from: file('gradle/conventions.gradle')
apply from: file('gradle/generation/local-settings.gradle')

// Set up defaults and configure aspects for certain modules or functionality
// (java, tests)
apply from: file('gradle/java/folder-layout.gradle')
apply from: file('gradle/java/javac.gradle')
apply from: file('gradle/testing/defaults-tests.gradle')
apply from: file('gradle/testing/randomization.gradle')
apply from: file('gradle/testing/fail-on-no-tests.gradle')
apply from: file('gradle/testing/alternative-jdk-support.gradle')
apply from: file('gradle/java/jar-manifest.gradle')
apply from: file('gradle/testing/retry-test.gradle')

// Publishing and releasing
apply from: file('gradle/maven/defaults-maven.gradle')

// IDE support, settings and specials.
apply from: file('gradle/ide/intellij-idea.gradle')
apply from: file('gradle/ide/eclipse.gradle')

// Validation tasks
apply from: file('gradle/validation/measure-task-times.gradle')
apply from: file('gradle/validation/error-prone.gradle')
apply from: file('gradle/validation/precommit.gradle')
apply from: file('gradle/validation/forbidden-apis.gradle')
apply from: file('gradle/validation/jar-checks.gradle')
apply from: file('gradle/validation/git-status.gradle')
apply from: file('gradle/validation/validate-source-patterns.gradle')
apply from: file('gradle/validation/rat-sources.gradle')
apply from: file('gradle/validation/owasp-dependency-check.gradle')
apply from: file('gradle/validation/dependency-analyze.gradle')
apply from: file('gradle/validation/ecj-lint.gradle')
apply from: file('gradle/validation/gradlew-scripts-tweaked.gradle')
apply from: file('gradle/validation/validate-log-calls.gradle')
apply from: file('gradle/validation/check-broken-links.gradle')

apply from: file('gradle/validation/solr.config-file-sanity.gradle')
apply from: file('gradle/validation/dependencies.gradle')
apply from: file('gradle/validation/spotless.gradle')

// Wire up included builds to some validation tasks.
rootProject.tasks.named("tidy").configure {
  dependsOn gradle.includedBuilds*.task(":tidy")
}
rootProject.tasks.named("clean").configure {
  dependsOn gradle.includedBuilds*.task(":clean")
}

// Source or data regeneration tasks
apply from: file('gradle/generation/regenerate.gradle')
apply from: file('gradle/generation/javacc.gradle')

// Additional development aids.
apply from: file('gradle/maven/maven-local.gradle')
apply from: file('gradle/testing/per-project-summary.gradle')
apply from: file('gradle/testing/slowest-tests-at-end.gradle')
apply from: file('gradle/testing/failed-tests-at-end.gradle')
apply from: file('gradle/testing/profiling.gradle')
apply from: file('gradle/testing/beasting.gradle')
apply from: file('gradle/help.gradle')

// Configures development for joint Lucene/ Solr composite build.
apply from: file('gradle/lucene-dev/lucene-dev-repo-composite.gradle')

apply from: file('gradle/documentation/pull-lucene-javadocs.gradle')
apply from: file('gradle/documentation/documentation.gradle')
apply from: file('gradle/documentation/changes-to-html.gradle')
apply from: file('gradle/documentation/markdown.gradle')
apply from: file('gradle/documentation/render-javadoc.gradle')

apply from: file('gradle/hacks/global-exclude-dependencies.gradle')
apply from: file('gradle/hacks/gradle-archives.gradle')

apply from: file('gradle/hacks/wipe-temp.gradle')
apply from: file('gradle/hacks/turbocharge-jvm-opts.gradle')
apply from: file('gradle/hacks/dummy-outputs.gradle')

apply from: file('gradle/solr/packaging.gradle')
apply from: file('gradle/solr/solr-forbidden-apis.gradle')

apply from: file('gradle/node.gradle')
