
plugins {
    id("scala")
    id("org.jetbrains.intellij") version "1.5.3"
    id("de.undercouch.download") version "4.1.2"
    id("com.palantir.git-version") version "0.12.3"
    id("cz.alenkacz.gradle.scalafmt") version "1.16.2"
}

repositories {
    mavenCentral()
}

// Build number ranges:   https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
// Python CE plugin:      https://plugins.jetbrains.com/plugin/7322-python-community-edition
// Python plugin:         https://plugins.jetbrains.com/plugin/631-python
// Intellij repositories: https://www.jetbrains.com/intellij-repository/releases
//                        https://www.jetbrains.com/intellij-repository/snapshots

version = gitVersion()

def intellijVersion = System.getenv().getOrDefault("IDEA_VERSION", "IC-2022.1")

def pythonPluginForVersion = [
        "IC-2021.1": "PythonCore:211.6693.119",
        "IC-2021.2": "PythonCore:212.4746.96",
        "IC-2021.3": "PythonCore:213.5744.248",
        "IC-2022.1": "PythonCore:221.5080.210"
]

intellij {
    version = intellijVersion
    plugins = [pythonPluginForVersion.get(intellijVersion)]
}

publishPlugin {
    token = System.getenv().getOrDefault("INTELLIJ_PUBLISH_TOKEN", "")
} doFirst {
    def requiredVersion = JavaVersion.VERSION_11
    if(JavaVersion.current() != requiredVersion)
        throw new GradleException("The plugin should be published using JavaVersion $requiredVersion" )
}

import org.jetbrains.intellij.tasks.RunPluginVerifierTask.FailureLevel
runPluginVerifier {
    ideVersions = [intellijVersion]
    failureLevel = FailureLevel.ALL
}

patchPluginXml {
    sinceBuild = "211"
    untilBuild = "221.*"
}

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

// Following two assignments are necessary to let the scala plugin handle both java and scala compilation
sourceSets.main.java.srcDirs = []
sourceSets.test.java.srcDirs = []
//
sourceSets.main.scala.srcDir("src/main/java")
sourceSets.main.scala.srcDir("src/main/java-gen")
sourceSets.test.scala.srcDir("src/test/java")

def scalaVersion = System.getenv().getOrDefault("TRAVIS_SCALA_VERSION", "2.13.3")
dependencies {
    implementation("org.scala-lang:scala-library:$scalaVersion")
    testImplementation("org.scalatest:scalatest_2.13:3.2.1")
    testImplementation("org.scalatestplus:junit-4-12_2.13:3.2.1.0")
}

import de.undercouch.gradle.tasks.download.Download
task downloadJFlex(type: Download) {
    src("https://cache-redirector.jetbrains.com/intellij-dependencies/org/jetbrains/intellij/deps/jflex/jflex/1.7.0-2/jflex-1.7.0-2.jar")
    dest("${buildDir}/downloads/JFlex.jar")
    onlyIfNewer(true)
    overwrite(false)
}

task downloadSkeleton(dependsOn: downloadJFlex, type: Download) {
    src("https://github.com/JetBrains/intellij-community/raw/master/tools/lexer/idea-flex.skeleton")
    dest("${buildDir}/downloads/idea-flex.skeleton")
    onlyIfNewer(true)
    overwrite(false)
}

task generateLexer(dependsOn: downloadSkeleton, type: JavaExec) {
    classpath = files("${buildDir}/downloads/JFlex.jar")
    args = """--nobak
           |--skel
           |${buildDir}/downloads/idea-flex.skeleton
           |-d
           |${projectDir}/src/main/java-gen/amailp/intellij/robot/lexer
           |${projectDir}/src/main/resources/amailp/intellij/robot/lexer/Robot.flex""".stripMargin().split("\n").toList()
    inputs.file("${projectDir}/src/main/resources/amailp/intellij/robot/lexer/Robot.flex")
    outputs.dir("${projectDir}/src/main/java-gen/amailp/intellij/robot/lexer")
}

tasks.named("wrapper") {
    distributionType = Wrapper.DistributionType.ALL
}

compileScala.dependsOn(generateLexer)

test {
    testLogging {
        showStandardStreams = true
    }
}
