/*
 * 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.
 */

apply plugin: 'elasticsearch.internal-es-plugin'
apply plugin: 'elasticsearch.internal-cluster-test'

esplugin {
  name = 'x-pack-sql'
  description = 'The Elasticsearch plugin that powers SQL for Elasticsearch'
  classname = 'org.elasticsearch.xpack.sql.plugin.SqlPlugin'
  extendedPlugins = ['x-pack-ql', 'lang-painless']
}

ext {
  // SQL dependency versions
  jlineVersion = "3.21.0"

  // SQL test dependency versions
  csvjdbcVersion = "1.0.34"
  h2Version = "1.4.197"
  h2gisVersion = "1.5.0"
}

configurations {
  // Bundles the sql-cli.jar into the distribution
  bin
}

base {
  archivesName = 'x-pack-sql'
}

dependencies {
  compileOnly project(path: xpackModule('core'))
  compileOnly(project(':modules:lang-painless:spi'))
  api project('sql-action')
  api project(':modules:aggregations')
  compileOnly project(path: xpackModule('ql'))
  testImplementation project(':test:framework')
  testImplementation(testArtifact(project(xpackModule('core'))))
  testImplementation(testArtifact(project(xpackModule('ql'))))
  testImplementation project(path: ':modules:reindex')
  testImplementation project(path: ':modules:parent-join')
  testImplementation project(path: ':modules:analysis-common')
  bin(project(path: xpackModule('sql:sql-cli'), configuration: 'shadow'))
}

/* Bundle the sql-cli into the binary files. It should end up
 * in $ES_HOME/bin/x-pack/. This is useful because it is an
 * executable jar that can be moved wherever it is needed.
 */
esplugin.bundleSpec.from({configurations.bin}) {
    into 'bin'
}

addQaCheckDependencies(project)

/**********************************************
 *          SQL Parser regeneration           *
 **********************************************/

configurations {
  regenerate
}

dependencies {
  regenerate "org.antlr:antlr4:${versions.antlr4}"
}

String grammarPath = 'src/main/antlr'
String outputPath = 'src/main/java/org/elasticsearch/xpack/sql/parser'

pluginManager.withPlugin('com.diffplug.spotless') {
  spotless {
    java {
      targetExclude "${outputPath}/*.java"
    }
  }
}

tasks.register("cleanGenerated", Delete) {
  delete fileTree(grammarPath) {
    include '*.tokens'
  }
  delete fileTree(outputPath) {
    include 'SqlBase*.java'
  }
}

tasks.register("regenParser", JavaExec) {
  dependsOn "cleanGenerated"
  mainClass = 'org.antlr.v4.Tool'
  classpath = configurations.regenerate
  systemProperty 'file.encoding', 'UTF-8'
  systemProperty 'user.language', 'en'
  systemProperty 'user.country', 'US'
  systemProperty 'user.variant', ''
  args '-Werror',
    '-package', 'org.elasticsearch.xpack.sql.parser',
    '-listener',
    '-visitor',
    '-o', outputPath,
    "${file(grammarPath)}/SqlBase.g4"
}

tasks.register("regen") {
  dependsOn "regenParser"
  doLast {
    // moves token files to grammar directory for use with IDE's
    ant.move(file: "${outputPath}/SqlBase.tokens", toDir: grammarPath)
    ant.move(file: "${outputPath}/SqlBaseLexer.tokens", toDir: grammarPath)
    // make the generated classes package private
    ant.replaceregexp(match: 'public ((interface|class) \\QSqlBase\\E\\w+)',
      replace: '\\1',
      encoding: 'UTF-8') {
      fileset(dir: outputPath, includes: 'SqlBase*.java')
    }
    // nuke timestamps/filenames in generated files
    ant.replaceregexp(match: '\\Q// Generated from \\E.*',
      replace: '\\/\\/ ANTLR GENERATED CODE: DO NOT EDIT',
      encoding: 'UTF-8') {
      fileset(dir: outputPath, includes: 'SqlBase*.java')
    }
    // remove tabs in antlr generated files
    ant.replaceregexp(match: '\t', flags: 'g', replace: '  ', encoding: 'UTF-8') {
      fileset(dir: outputPath, includes: 'SqlBase*.java')
    }
    // fix line endings
    ant.fixcrlf(srcdir: outputPath, eol: 'lf') {
      patternset(includes: 'SqlBase*.java')
    }
  }
}

allprojects {
  tasks.register("checkNoBwc") {
    dependsOn tasks.withType(Test).matching { it.name.contains('bwc') == false }
  }
}

if (buildParams.inFipsJvm){
  // Test clusters run with security disabled
  tasks.named("internalClusterTest").configure{enabled = false }
}
