apply plugin: 'java'
apply plugin: 'eclipse'
repositories{
  mavenCentral()
}
def propertyFile = file "src/main/java/w2phtml/project.properties"
def propertyDestFile = file "src/main/tmp/w2phtml/project.properties"
Properties properties = new Properties()
propertyFile.withReader { properties.load(it) }
properties.setProperty('releaseDate', new Date().format('HH:mm:ss dd-MM-YYYY'))
properties.setProperty('releaseVersion', project.version )
mkdir "src/main/tmp/w2phtml"
propertyDestFile.withWriter { properties.store(it, null) }

sourceSets {
    regressionTests {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + files('src/main/java') 
            java.srcDir file('src/r_tests/java')
        }
    }
}
sourceCompatibility = 1.8
configurations{
    bundledLibs
    regressionTestsCompile.extendsFrom mainCompile
    regressionTestsRuntime.extendsFrom mainRuntime
    regressionTestsRuntime.extendsFrom bundledLibs
}
dependencies{
    regressionTestsImplementation group: 'commons-io', name: 'commons-io', version: '2.8.0'
    regressionTestsImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.1'
    regressionTestsImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.7.1'
    regressionTestsRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

    bundledLibs 'org.libreoffice:jurt:5.3.2'
    bundledLibs 'org.libreoffice:juh:5.3.2'
    bundledLibs 'org.libreoffice:ridl:5.3.2'
    bundledLibs 'org.libreoffice:unoil:5.3.2'
    bundledLibs 'org.json:json:20190722'
    bundledLibs group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    bundledLibs group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
    bundledLibs group: 'com.miglayout', name: 'miglayout-swing', version: '5.2'
    bundledLibs group: 'org.apache.jena', name: 'jena-core', version: '3.15.0'
    bundledLibs group: 'org.apache.jena', name: 'jena-arq', version: '3.15.0'
    bundledLibs group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
    bundledLibs group: 'com.opencsv', name: 'opencsv', version: '5.1'
    bundledLibs group: 'org.imgscalr', name: 'imgscalr-lib', version: '4.2'
    bundledLibs group: 'org.apache.commons', name: 'commons-imaging', version: '1.0-alpha2'

    configurations.compile.extendsFrom(configurations.bundledLibs)
}
jar {
    archiveName 'w2phtml.jar'
    manifest {
        attributes("Implementation-Title": rootProject.name,
                   "Implementation-Version": project.version,
           "Main-Class" : "w2phtml.Application",
           "Class-Path" : "jasp.jar parser.jar")
    }

    from {
            configurations.bundledLibs.collect { it.isDirectory() ? it : zipTree(it) }
    }
    from('src/main/java') {
        include '**/*.properties'
        include '**/*.rdf'
        include '**/*.xml'
        include 'pro/litvinovg/w2phtml/gui/resources/*.png'
        exclude '**/*Test.class'
        exclude '**/AllTests.class'
        exclude 'org/openoffice/**/*'
        exclude 'w2phtml/project.properties'
    }
    from('src/main/tmp') {
        include 'w2phtml/project.properties'
    }
}
task xhtml(type: Jar) {
    archiveName 'writer2phtml.jar'
    manifest {
        attributes("Implementation-Title": rootProject.name,
                   "Implementation-Version": project.version,
                   "RegistrationClassName" : "pro.litvinovg.w2phtml.RegistrationHandler",
                   "Class-Path" : "jasp.jar parser.jar")
    }
    baseName = "writer2phtml"
    from {
      configurations.bundledLibs.collect {
        exclude 'module-info.class'
        exclude '.gradle'
        exclude '**/LICENSE*'
        exclude '**/DEPENDENCIES'
        exclude '**/NOTICE*'
        exclude '**/pom.xml'
        exclude 'steps.txt'
        exclude 'META-INF/services'

        it.isDirectory() ? it : zipTree(it)
      }
    }
    from (sourceSets.main.output){
        exclude '**/*Test.class'
        exclude '**/AllTests.class'
        exclude '**/*.java'
        include '**/*.classes'
        include 'etc/*'
        include 'com/**'
        include 'com/**/*'
        include 'w2phtml/pageSplitters/**/*.class'
        include 'pro/litvinovg/**/*.class'
        include 'pro/litvinovg/**/*.png'
        include 'w2phtml/api/**/*.class'
        include 'w2phtml/*.class'
        include 'w2phtml/*.properties'
        include 'w2phtml/base/**/*.class'
        include 'w2phtml/rdf/**/*.class'
        include 'w2phtml/epub/**/*.class'
        include 'w2phtml/office/**/*.class'
        include 'w2phtml/util/**/*.class'
        include 'w2phtml/xhtml/**/*.class'
        include 'w2phtml/xhtml/**/*.xml'
        include 'w2phtml/xmerge/**/*.class'
        include 'org/**/*'
        include 'net/**/*'
        include 'logback.xml'
        include 'ch/**/*'
        include '**/*.properties'
        exclude '.gradle'
    }
    from ('src/main/java'){
        exclude '.gradle'
        include 'w2phtml/xhtml/**/*.properties'
        include 'w2phtml/xhtml/**/**/*.properties'
        include 'pro/litvinovg/**/*.classes'
        include 'logback.xml'
        include '**/*.png'
        include 'pro/litvinovg/w2phtml/gui/localizations/*.properties'
        exclude 'w2phtml/project.properties'
    }
    from('src/main/tmp') {
        exclude '.gradle'
        include 'w2phtml/project.properties'
    }
}


task oxt(type: Zip){
    dependsOn = [ 'xhtml', 'setVersion' ]
    archiveName 'w2phtml.oxt'
    from ('src/main/oxt'){
        include '*'
        include '**'
        exclude '.gradle'
        exclude 'description.xml'
    }
    from('src/main/tmp') {
        include 'description.xml'
    }
    from ('build/libs/writer2phtml.jar'){
        include '*'
    }
    from 'idl/writer2paginatedhtml.rdb'
    from 'releasenotes.txt'
    include '*'
}
task regressionTests(type: Test) {
  testClassesDirs = sourceSets.regressionTests.output.classesDirs
  classpath = sourceSets.regressionTests.runtimeClasspath
  useJUnitPlatform()
  testLogging {
    events "passed", "skipped", "failed", "standardOut", "standardError"
  }
  mustRunAfter jar
}

task setVersion(){   
   ant.taskdef(name: 'xmltask', classpath: 'buildPlugins/xmltask.jar', classname: 'com.oopsconsultancy.xmltask.ant.XmlTask') 
   ant.xmltask(source: 'src/main/oxt/description.xml', dest: 'src/main/tmp/description.xml', report:'true') {
     replace(path: "//*[local-name()='version']/@value", withText: "${project.version}")
   }
}