apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'idea'

apply from: 'unittest.gradle'

group 'com.github.adrninistrator'
version = "3.3.3"
def projectName = "java-all-call-graph"
description = "${projectName}"
sourceCompatibility = 1.8
targetCompatibility = 1.8
def jar_output_dir = "jar_output_dir"

def defaultEncoding = 'UTF-8'

[compileJava, compileTestJava]*.options*.encoding = defaultEncoding

project.buildDir = 'build'

repositories {
    mavenCentral()
}

clean {
    delete "${buildDir}"
    delete "${jar_output_dir}"
}

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }

        resources {
            srcDir 'src/main/resources'
        }
    }

    test {
        java {
            srcDir 'src/test/java'
        }

        resources {
            srcDir 'src/test/resources'
        }
    }
}

List druid = ['com.alibaba:druid:1.2.15']

List mysql = ['com.mysql:mysql-connector-j:8.4.0']

List h2database = ["com.h2database:h2:2.2.220"]

List java_callgraph = ['com.github.adrninistrator:java-callgraph2:3.3.0']

List mybatis_parser = ['com.github.adrninistrator:mybatis-mysql-table-parser:0.1.1']

List jackson = ["com.fasterxml.jackson.core:jackson-databind:2.14.0"]

List jdom = ['org.jdom:jdom2:2.0.6.1']

def spring_version = "5.3.39"

List spring_jdbc = ["org.springframework:spring-jdbc:$spring_version"]

List spring = [
        "org.springframework:spring-aspects:$spring_version",
        "org.springframework:spring-context-support:$spring_version"
]

List spring_data_neo4j = ['org.springframework.data:spring-data-neo4j:5.3.9.RELEASE']

List log4j = [
        'org.apache.logging.log4j:log4j-api:2.20.0',
        'org.apache.logging.log4j:log4j-core:2.20.0',
        'org.apache.logging.log4j:log4j-slf4j-impl:2.20.0'
]

List disruptor = ['com.lmax:disruptor:3.3.7']

List junit = [
        'org.junit.platform:junit-platform-launcher:1.9.3',
        'org.junit.jupiter:junit-jupiter-engine:5.9.3',
        'org.junit.vintage:junit-vintage-engine:5.9.3'
]

List spring_for_test = [
        "org.springframework:spring-beans:$spring_version",
        "org.springframework:spring-context:$spring_version",
        "org.springframework:spring-web:$spring_version",
        "org.springframework:spring-webmvc:$spring_version"
]

List javax_inject = ['javax.inject:javax.inject:1']

List spring_test = ["org.springframework:spring-test:$spring_version"]

List servlet = ['javax.servlet:javax.servlet-api:4.0.1']

List commons_bean_utils = ['commons-beanutils:commons-beanutils:1.9.4']

List lombok = ['org.projectlombok:lombok:1.18.24']

List commons_chain = ['commons-chain:commons-chain:1.2']

List mybatis = [
        'org.mybatis:mybatis:3.5.9'
]

dependencies {
    api druid, mysql, h2database, java_callgraph, mybatis_parser, jackson, jdom, spring_jdbc, spring
    compileOnly spring_data_neo4j
    testImplementation log4j, disruptor, junit, spring_for_test, javax_inject, spring_test, servlet, commons_bean_utils, spring_data_neo4j, lombok, commons_chain, mybatis
    annotationProcessor 'org.projectlombok:lombok:1.18.24'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
}

compileJava.options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"

processResources {
    setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
}

processTestResources {
    setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
}

tasks.register('createProject') {
    doFirst {
        sourceSets*.java.srcDirs*.each { it.mkdirs() }
        sourceSets*.resources.srcDirs*.each { it.mkdirs() }
    }
}

jar {
    duplicatesStrategy = DuplicatesStrategy.INCLUDE

    from(sourceSets.test.allJava) {
        include 'test/jacg/*.java'
        into '/'
    }
}

// 生成可以直接执行的jar包，并拷贝相关文件
tasks.register('gen_run_jar', Jar) {
    archiveFileName.set('run_jacg.jar')
    from(sourceSets.main.output) {
        into '/'
    }
    from(sourceSets.test.output) {
        include 'test/jacg/**'
        into '/'
    }

    doFirst {
        delete "${buildDir}/libs/run_jacg.jar"
        delete "${jar_output_dir}"
    }

    doLast {
        def jacgConfigDirs = new File('src/main/resources').listFiles().findAll { File f -> (f.isDirectory() && f.name.startsWith('_jacg_')) }
        copy {
            from "${buildDir}/libs/run_jacg.jar"
            into "${jar_output_dir}/jar/"
        }
        // 拷贝配置文件目录
        // el
        jacgConfigDirs.each { jacgConfigDir ->
            def dirName = jacgConfigDir.getName()

            copy {
                from 'src/main/resources/' + dirName
                into "${jar_output_dir}/" + dirName
            }
        }
        copy {
            from new File('bat').listFiles().findAll { File f -> (f.name.endsWith('.bat')) }
            into "${jar_output_dir}/"
        }
        copy {
            from new File('shell').listFiles().findAll { File f -> (f.name.endsWith('.sh')) }
            into "${jar_output_dir}/"
        }
        copy {
            from 'src/test/resources/log4j2.xml'
            into new File("${jar_output_dir}/config/")
            rename { "log4j2_console.xml" }
        }
        copy {
            from 'src/test/resources/log4j2_file.xml'
            into new File("${jar_output_dir}/config/")
            rename { "log4j2.xml" }
        }
        copy {
            from configurations.runtimeClasspath.getFiles()
            into "${jar_output_dir}/lib"
        }
        copy {
            from configurations.testRuntimeClasspath.getFiles().findAll {
                    /*
                    需要使用以下日志相关的组件：
                    log4j
                    disruptor
                 */
                File f ->
                    (f.name.startsWith('log4j-')
                            || f.name.startsWith("disruptor-"))
            }
            into "${jar_output_dir}/lib"
        }
        // 复制 java-callgraph2 组件中的配置文件
        configurations.runtimeClasspath.each { File file ->
            if (file.name.startsWith('java-callgraph2-')) {
                copy {
                    from zipTree(file).matching {
                        include '_javacg2_*/*', '_el_example/*'
                    }
                    into "${jar_output_dir}/"
                }
            }
        }
    }
}

// 将gen_run_jar目录的文件生成tar.gz包
tasks.register('gen_tar_gz', Tar) {
    dependsOn gen_run_jar
    compression = Compression.GZIP
    // 若使用archiveBaseName，则生成的文件包中会包含“-当前版本号”
    archiveFileName.set("java-all-call-graph-" + getArchiveVersion().get() + ".tar.gz")
    destinationDirectory.set(file("${buildDir}/tar_gz"))
    from("${jar_output_dir}")
}

idea {
    module {
        // and some extra dirs that should be excluded by IDEA
        excludeDirs += file('.idea')
        excludeDirs += file('_jacg_o_ee')
        excludeDirs += file('_jacg_o_er')
        excludeDirs += file('gradle')
        excludeDirs += file("${jar_output_dir}")
        excludeDirs += file('bat')
        excludeDirs += file('shell')
        excludeDirs += file('log')
        excludeDirs += file('_unittest_output')
    }
}
