import org.elasticsearch.gradle.internal.toolchain.AdoptiumJdkToolchainResolver
import org.elasticsearch.gradle.internal.toolchain.ArchivedOracleJdkToolchainResolver
import org.elasticsearch.gradle.internal.toolchain.OracleOpenJdkToolchainResolver

pluginManagement {
  repositories {
    mavenCentral()
    gradlePluginPortal()
  }

  includeBuild "build-conventions"
  includeBuild "build-tools"
  includeBuild "build-tools-internal"
}

plugins {
  id "com.gradle.develocity" version "4.1.1"
  id 'elasticsearch.java-toolchain'
  id 'elasticsearch.component-metadata-rules'
}

enableFeaturePreview "STABLE_CONFIGURATION_CACHE"

rootProject.name = "elasticsearch"

dependencyResolutionManagement {
  versionCatalogs {
    buildLibs {
      from(files("gradle/build.versions.toml"))
    }
  }
}

toolchainManagement {
  jvm {
    javaRepositories {
      repository('bundledOracleOpendJdk') {
        resolverClass = OracleOpenJdkToolchainResolver
      }
      repository('adoptiumJdks') {
        resolverClass = AdoptiumJdkToolchainResolver
      }
      repository('archivedOracleJdks') {
        resolverClass = ArchivedOracleJdkToolchainResolver
      }
    }
  }
}

List projects = [
  'rest-api-spec',
  'docs',
  'client:rest',
  'client:sniffer',
  'client:test',
  'client:client-benchmark-noop-api-plugin',
  'client:benchmark',
  'benchmarks',
  'distribution:archives:integ-test-zip',
  'distribution:archives:windows-zip',
  'distribution:archives:darwin-tar',
  'distribution:archives:darwin-aarch64-tar',
  'distribution:archives:linux-aarch64-tar',
  'distribution:archives:linux-tar',
  'distribution:docker',
  'distribution:docker:cloud-ess-docker-export',
  'distribution:docker:cloud-ess-docker-aarch64-export',
  'distribution:docker:docker-aarch64-export',
  'distribution:docker:docker-export',
  'distribution:docker:ironbank-docker-aarch64-export',
  'distribution:docker:ironbank-docker-export',
  'distribution:docker:wolfi-docker-aarch64-export',
  'distribution:docker:wolfi-docker-export',
  'distribution:docker:cloud-ess-fips-docker-export',
  'distribution:docker:cloud-ess-fips-docker-aarch64-export',
  'distribution:packages:aarch64-deb',
  'distribution:packages:deb',
  'distribution:packages:aarch64-rpm',
  'distribution:packages:rpm',
  'distribution:bwc:major1',
  'distribution:bwc:major2',
  'distribution:bwc:major3',
  'distribution:bwc:major4',
  'distribution:bwc:minor1',
  'distribution:bwc:minor2',
  'distribution:bwc:minor3',
  'distribution:bwc:minor4',
  'distribution:bwc:main',
  'distribution:tools:java-version-checker',
  'distribution:tools:cli-launcher',
  'distribution:tools:server-cli',
  'distribution:tools:windows-service-cli',
  'distribution:tools:plugin-cli',
  'distribution:tools:keystore-cli',
  'distribution:tools:geoip-cli',
  'distribution:tools:ansi-console',
  'server',
  'test:framework',
  'test:fixtures:aws-ec2-fixture',
  'test:fixtures:aws-fixture-utils',
  'test:fixtures:aws-sts-fixture',
  'test:fixtures:azure-fixture',
  'test:fixtures:ec2-imds-fixture',
  'test:fixtures:gcs-fixture',
  'test:fixtures:hdfs-fixture',
  'test:fixtures:krb5kdc-fixture',
  'test:fixtures:minio-fixture',
  'test:fixtures:old-elasticsearch',
  'test:fixtures:s3-fixture',
  'test:fixtures:testcontainer-utils',
  'test:fixtures:geoip-fixture',
  'test:fixtures:url-fixture',
  'test:logger-usage',
  'test:test-clusters',
  'test:x-content',
  'test:yaml-rest-runner',
  'test:metadata-extractor',
  'test:immutable-collections-patch'
]

/**
 * Iterates over sub directories, looking for build.gradle, and adds a project if found
 * for that dir with the given path prefix. Note that this requires each level
 * of the dir hierarchy to have a build.gradle. Otherwise we would have to iterate
 * all files/directories in the source tree to find all projects.
 */
void addSubProjects(String path, File dir) {
  if (dir.isDirectory() == false) return;
  if (dir.name == 'buildSrc') return;
  if (new File(dir, 'build.gradle').exists() == false) return;
  if (new File(dir, 'settings.gradle').exists()) return;
  if (findProject(dir) != null) return;

  final String projectName = "${path}:${dir.name}"

  // This project has a problem with availability of Docker images after
  // release. Disabling individual tasks is tricky because it uses test
  // fixtures, so instead just skip the project entirely until we can
  // work out a way forward.
  if (projectName.equals(":qa:apm")) {
    return
  }

  include projectName
  if (path.isEmpty() || path.startsWith(':example-plugins')) {
    project(projectName).projectDir = dir
  }
  for (File subdir : dir.listFiles()) {
    addSubProjects(projectName, subdir)
  }
}

addSubProjects('', new File(rootProject.projectDir, 'libs'))
addSubProjects('', new File(rootProject.projectDir, 'modules'))
addSubProjects('', new File(rootProject.projectDir, 'plugins'))
addSubProjects('', new File(rootProject.projectDir, 'qa'))
addSubProjects('test', new File(rootProject.projectDir, 'test/external-modules'))
addSubProjects('', new File(rootProject.projectDir, 'x-pack'))
addSubProjects('', new File(rootProject.projectDir, 'x-pack/libs'))
addSubProjects('', new File(rootProject.projectDir, 'x-pack/extras/plugins'))

include projects.toArray(new String[0])

project(":libs:native:libraries").name = "native-libraries"

project(":test:external-modules").children.each { testProject ->
  testProject.name = "test-${testProject.name}"
}

// look for extra plugins for elasticsearch
File extraProjects = new File(rootProject.projectDir.parentFile, "${rootProject.projectDir.name}-extra")
if (extraProjects.exists()) {
  for (File extraProjectDir : extraProjects.listFiles()) {
    addSubProjects('', extraProjectDir)
  }
}

include 'qa:vector'
