// internal plugins
apply plugin: 'java'
apply plugin: 'eclipse' // Eclipse integration

description = """"""

repositories {
    mavenCentral()
}

configurations { antxr }

dependencies {
    antxr 'com.javadude:antxr:0.9.0'
    implementation files('../thirdparty/idl/build/libs/idlparser-3.0.0.jar')
    implementation group: 'com.javadude', name: 'antxr', version: '0.9.0'
}

task buildIDLParser(type: GradleBuild) {
    dir = '../thirdparty/idl'
    tasks = ['clean', 'build']
}

task generateWADLParser(type: JavaExec) {
    File outputDir = file(new File(buildDir, "generated-src/com/eprosima/fastrpc/wadl/grammar"))
    outputs.dir outputDir
    outputDir.exists() || outputDir.mkdirs()
    classpath configurations.antxr
    mainClass = 'com.javadude.antxr.Tool'
    args = [ "-o", "build/generated-src/com/eprosima/fastrpc/wadl/grammar", "grammars/wadl.antxr"]
    workingDir = projectDir
}

task copyResources {
    // Copy platforms file
    File outputDir = file(new File("${project.buildDir}", "resources/main"))
    outputDir.exists() || outputDir.mkdirs()
    project.copy {
        from(file(new File(projectDir, '../src/platforms')))
        into(outputDir)
    }

    // Create version file
    Properties pversion = new Properties()
    project.hasProperty('customversion') ? pversion.setProperty('version', project.customversion.toString()) : pversion.setProperty('version', '0.0.0')
    File versionFile = new File("${project.buildDir}/resources/main/version")
    versionFile.withWriter {
        pversion.store(it, null)
    }

    if(project.hasProperty('customvendor'))
    {
        Properties pvendor = new Properties()
        pvendor.setProperty('vendor', project.customvendor.toString())
        File vendorFile = new File("${project.buildDir}/resources/main/vendor")
        vendorFile.createNewFile();
        pvendor.store(vendorFile.newWriter(), null)
    }
}

sourceSets {
    main {
        java {
            srcDir 'src'
            srcDir '../thirdparty/fastcdr/src/java'
            srcDir 'build/generated-src'
        }

        resources {
            srcDir 'src'
            include '**/*.stg'
            srcDir '../thirdparty/fastcdr/src/java'
            include '**/*.stg'
        }
    }
}

task fastrpcgenJar(type: Jar) {
    duplicatesStrategy 'exclude'
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    archiveBaseName = 'fastrpcgen'
    manifest {
        attributes(
            "Created-By": "eProsima",
            "Main-Class": "com.eprosima.fastrpc.fastrpcgen",
            "Class-Path": configurations.runtimeClasspath.collect { it.getName() }.join(' ')
            )
    }
    doLast
    {
        copy {
            from fastrpcgenJar
            into "share/fastrpc"
        }
    }
}

task rpcddsgenJar(type: Jar) {
    duplicatesStrategy 'exclude'
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    archiveBaseName = 'rpcddsgen'
    manifest {
        attributes(
            "Created-By": "eProsima",
            "Main-Class": "com.eprosima.fastrpc.rpcddsgen",
            "Class-Path": configurations.runtimeClasspath.collect { it.getName() }.join(' ')
            )
    }
    doLast
    {
        copy {
            from rpcddsgenJar
            into "share/rpcdds"
        }
    }
}

task rpcrestgenJar(type: Jar) {
    duplicatesStrategy 'exclude'
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
    archiveBaseName = 'rpcrestgen'
    manifest {
        attributes(
            "Created-By": "eProsima",
            "Main-Class": "com.eprosima.fastrpc.rpcrestgen",
            "Class-Path": configurations.runtimeClasspath.collect { it.getName() }.join(' ')
            )
    }
    doLast
    {
        copy {
            from rpcrestgenJar
            into "share/rpcrest"
        }
    }
}

compileJava.dependsOn buildIDLParser, generateWADLParser
compileJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    options.compilerArgs.add('-Xlint:deprecation')
    options.compilerArgs.add('-Xlint:unchecked')
}
build.dependsOn fastrpcgenJar, rpcddsgenJar, rpcrestgenJar
