/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed 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 groovy.xml.XmlUtil

plugins {
    id 'idea'
    id 'java'
    id 'org.jetbrains.kotlin.jvm'
    id 'org.jetbrains.intellij' version "1.17.4"
}

apply plugin: "org.jetbrains.kotlin.jvm"
apply plugin: "org.jetbrains.intellij"

group 'com.facebook.litho.intellij'

sourceCompatibility = rootProject.sourceCompatibilityVersion

compileKotlin {
    kotlinOptions.jvmTarget = rootProject.targetCompatibilityVersion
}
compileTestKotlin {
    kotlinOptions.jvmTarget = rootProject.targetCompatibilityVersion
}

sourceSets {
    test.java.srcDirs += project(':litho-processor').file("src/test/java")
    test.java.srcDirs += project(':litho-sections-processor').file("src/test/java")
}

dependencies {
    // Kotlin
    compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compileOnly 'org.jetbrains:annotations:21.0.0'
    // Processor
    implementation project(':litho-sections-annotations')
    implementation project(':litho-sections-processor')
    implementation project(':litho-annotations')
    implementation project(':litho-processor')
    implementation deps.javapoet

    compileOnly deps.inferAnnotations

    // Testing
    testImplementation deps.junit
    testImplementation deps.mockitoCore
    testImplementation deps.assertjCore
}

intellij {
    version = '2024.1'
    updateSinceUntilBuild = false
    plugins = ['java', 'org.jetbrains.kotlin',]
}

task merge {
    doLast {
        def output = new File(project.buildDir, "patchedPluginXmlFiles/plugin.xml")

        def parser = new XmlSlurper(false, false)
        def base = parser.parse(output)
        def addition = parser.parse(project(':litho-intellij-plugin').file("src/main/resources/META-INF/redsymbols.xml"))

        base.'*'.find { node ->
            node.name() == 'extensions' && node.@defaultExtensionNs == 'com.intellij'
        }.appendNode(addition.extensions.'*')

        base.actions.'*'.find { node ->
            node.name() == 'group' && node.@id == 'litho.EditorPopupMenu'
        }.appendNode(addition.actions.'*')

        output.newWriter().withWriter { w ->
            def content = XmlUtil.serialize(base)
            w << content
        }
    }
}

merge.mustRunAfter patchPluginXml
processResources.dependsOn merge
