/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0; you may not use this file except in compliance with the Elastic License
 * 2.0.
 */

import org.apache.tools.ant.filters.ReplaceTokens

apply plugin: 'elasticsearch.internal-yaml-rest-test'

dependencies {
  yamlRestTestImplementation project(':x-pack:plugin:core')
}

String outputDir = "${buildDir}/generated-resources/${project.name}"
def copyXPackPluginProps = tasks.register("copyXPackPluginProps", Copy) {
  from project(xpackModule('core')).file('src/main/plugin-metadata')
  from project(xpackModule('core')).tasks.pluginProperties
  into outputDir
}
project.sourceSets.yamlRestTest.output.dir(outputDir, builtBy: copyXPackPluginProps)

ext.pluginPaths = []
ext.pluginNames = []
project(':plugins').getChildProjects().each { pluginName, pluginProject ->
  pluginPaths << pluginProject.path
  pluginNames << pluginName
}

dependencies {
  pluginPaths.each { pluginPath ->
    clusterPlugins(project(pluginPath))
  }
}

ext.expansions = [
  'expected.plugins.count': pluginPaths.size()
]

tasks.named("processYamlRestTestResources").configure {
  from(sourceSets.yamlRestTest.resources.srcDirs) {
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
    include '**/*.yml'
    inputs.properties(expansions)
    filter("tokens" : expansions.collectEntries {k, v -> [k, v.toString()]} /* must be a map of strings */, ReplaceTokens.class)
  }
}

tasks.named("yamlRestTest") {
  usesDefaultDistribution("smoke test requires default distro with all plugins installed")
  systemProperty('tests.plugin.names', pluginNames.join(','))
}
