plugins {
    id 'pmd'
    id 'java'
    id 'signing'
    id 'com.vaadin'
    id 'maven-publish'
    id 'io.freefair.lombok'
    id 'com.github.spotbugs'
    id 'com.diffplug.spotless'
    id 'org.springframework.boot'
    id "com.github.node-gradle.node"
    id 'io.spring.dependency-management'
}

repositories {
    mavenLocal()
    mavenCentral()
}

apply from: "$rootDir/tasks/revisions.gradle"

dependencyManagement {
    imports {
        mavenBom "io.sunshower.aire.ux.controls-aire-switch:bom-imported:${version}"
    }
}

configurations {
    developmentImplementation {
        extendsFrom implementation
    }


    endToEnd {
        extendsFrom implementation
    }
}


sourceSets {
    main {
        java {
            srcDir "${buildDir}/generated/sources/main/java"
        }
    }
    development {
        java {
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }


    test {
        java {
            compileClasspath += development.output
            runtimeClasspath += development.output
        }
    }

    e2e {
        java {
            srcDir "src/e2e/java"

            compileClasspath += main.output
            runtimeClasspath += main.output

            compileClasspath += development.output
            runtimeClasspath += development.output

            compileClasspath += test.compileClasspath
            runtimeClasspath += test.runtimeClasspath

            compileClasspath += configurations.endToEnd
            runtimeClasspath += configurations.endToEnd


        }
    }
}


compileJava.dependsOn populateVersions
compileJava.mustRunAfter populateVersions


dependencies {

    /**
     * test implementation dependencies
     */

    [
            'com.vaadin:flow-server',
            'com.vaadin:vaadin-icons-flow',
            'com.vaadin:vaadin-button-flow',
            'com.vaadin:vaadin-ordered-layout-flow',
            'com.vaadin:flow-html-components',
            'io.sunshower.arcus:arcus-lang',
            'io.sunshower.arcus:arcus-reflect'
    ].each {
        implementation it
        developmentImplementation it
    }


    /**
     * test implementation dependencies
     */
    [
            'javax.websocket:javax.websocket-api:1.1',
            'io.sunshower.aire-test:aire-test-vaadin',
            'io.sunshower.aire-test:aire-test-spring',
            'org.springframework.boot:spring-boot-test',
            'org.springframework.boot:spring-boot-starter-web',
            'org.springframework.boot:spring-boot-starter-test',
    ].each {
        testImplementation it
    }


    /**
     * e2e dependencies
     */
    [
            "org.testcontainers:selenium",
            "org.testcontainers:junit-jupiter",
            'org.seleniumhq.selenium:selenium-api',
            'org.seleniumhq.selenium:selenium-java',
            'org.seleniumhq.selenium:selenium-remote-driver',
            'org.seleniumhq.selenium:selenium-chrome-driver',
    ].each {
        endToEnd it
    }



    /**
     * development dependencies
     */
    developmentImplementation 'com.vaadin:vaadin-spring-boot-starter'

}

tasks.register('endToEndTest', Test) {

    useJUnitPlatform()
    /**
     *
     */
    group = "verification"


    description = "Run End-To-End Tests"
    classpath = sourceSets.e2e.runtimeClasspath
    testClassesDirs = sourceSets.e2e.output.classesDirs

    shouldRunAfter test

}

check.dependsOn endToEndTest

spotbugsTest {
    enabled = false
}

spotbugsE2e {
    enabled = false
}

spotbugsMain {
    reports {
        html {
            enabled = false
        }
    }
}

test {
    useJUnitPlatform()
}

spotless {
    java {
        targetExclude("build/**/*")
        googleJavaFormat('1.12.0')
    }
}


pmd {
    toolVersion = '6.34.0'
    sourceSets = [sourceSets.main]
    ruleSets = [
            "${project.rootDir}/check/pmd/rules/errorprone.xml"
    ]
}
publishing {

    repositories {
        mavenLocal()

        maven {
            credentials {
                username "$mavenRepositoryUsername"
                password "$mavenRepositoryPassword"
            }
            url "$mavenRepositoryUrl"
        }
    }

    task sourcesJar(type: Jar, dependsOn: classes) {
        archiveClassifier = 'sources'
        from sourceSets.main.allSource
    }

    task testJar(type: Jar) {
        archiveClassifier = 'tests'
        from sourceSets.test.output
    }

    task javadocJar(type: Jar) {
        from javadoc
        archiveClassifier = 'javadoc'
    }

    publications {

        pluginMaven(MavenPublication) {

            /**
             * components source
             */
            from components.java

            /**
             * maven coordinates
             */
            groupId = "io.sunshower.aire.ux.controls"
            version = project.version
            artifactId = project.name

            /**
             * included artifacts
             */

            artifact testJar
            artifact sourcesJar
            artifact javadocJar

            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
            pom {
                name = 'aire-switch'
                description = 'Generated Component'
                url = 'https://github.com/aire-ux/aire-switch'
                licenses {
                    license {
                        name = 'The Gnu Affero Public License, Version 3.0'
                        url = 'http://www.opensource.org/licenses/AGPL-3.0'
                    }
                }
                developers {
                    developer {
                        id = 'josiahhaswell'
                        name = 'JosiahHaswell'
                        email = 'josiah@sunshower.io'
                    }
                }
                scm {
                    connection = 'scm:git:git://github.com/aire-ux/aire-switch.git'
                    developerConnection = 'scm:git:ssh://github.com/aire-ux/aire-switch.git'
                    url = 'https://www.aire-ux.com/'
                }
            }
        }

    }

}

signing {
    useGpgCmd()
    sign publishing.publications.pluginMaven
}
node {
    download = true
    version = "18.7.0"
    npmVersion = "8.15.0"
    distBaseUrl = "https://nodejs.org/dist"
    nodeProjectDir = file("${project.projectDir}")
}


bootJar {
    mainClass = 'io.sunshower.aire.ux.controlsSwitchDemoApplication'
    classpath = sourceSets.development.runtimeClasspath
}

/**
 * run the development build
 */

task runDevelopment(
        type: org.springframework.boot.gradle.tasks.run.BootRun,
        dependsOn: [
                'build',
                'vaadinPrepareFrontend'
        ]) {
    group = 'development'
    main = 'io.sunshower.aire.ux.controls.SwitchDemoApplication'
    doFirst {
        classpath = sourceSets.development.runtimeClasspath
    }
}
