import org.gradle.plugins.ide.idea.model.*

import org.apache.tools.ant.filters.ReplaceTokens
//import t5build.*

description = "Central module for Tapestry, containing all core services and components"

project.ext {
    mainGeneratedDir = "src/main/generated"
    testGeneratedDir = "src/test/generated"
}

clean.delete mainGeneratedDir, testGeneratedDir

dependencies {
    api project(':tapestry-ioc')
    api project(':tapestry-json')
    api project(':beanmodel')
    api project(':tapestry-http')

    api "org.apache.commons:commons-lang3:${versions.commonsLang}"


    implementation 'jakarta.annotation:jakarta.annotation-api:2.0.0'
    implementation 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.2'

    provided project(":tapestry-test")
    provided project(":tapestry-test-constants")

    provided "jakarta.servlet:jakarta.servlet-api:${versions.servletapi}"

    implementation "commons-codec:commons-codec:1.13"

    testImplementation "org.junit.jupiter:junit-jupiter:${versions.junitJupiter}"
    testImplementation "org.apache.httpcomponents:httpclient:4.5.14"
    testRuntimeOnly project(':tapestry-spock')

    testRuntimeOnly "org.hsqldb:hsqldb:${versions.hsqldb}"
    testRuntimeOnly 'com.google.inject:guice:3.0'
}

def npmWorkingDir = "src/main/typescript/"

task npmInstall(type: Exec) {
    group "TypeScript"
    description "Runs npm install"
    
    workingDir = npmWorkingDir
    commandLine isWindows() ? "npm.cmd" : "npm", 'install'
}

task compileTypeScriptToAmd(type: Exec) {
    dependsOn npmInstall

    workingDir = npmWorkingDir
    commandLine isWindows() ? "npm.cmd" : "npm", 'run', 'build-amd'
}

task compileTypeScriptToEsModule(type: Exec) {
    dependsOn npmInstall
    
    workingDir = npmWorkingDir
    commandLine isWindows() ? "npm.cmd" : "npm", 'run', 'build-es-module'
}

task compileTypeScript() {
    dependsOn compileTypeScriptToAmd
    dependsOn compileTypeScriptToEsModule
}

task generateTypeScriptDocs(type: Exec) {
    dependsOn npmInstall
    
    workingDir = npmWorkingDir
    commandLine isWindows() ? "npm.cmd" : "npm", 'run', 'docs'
}

task cleanTypeScriptFiles(type: Delete) {
    delete fileTree("src/main/resources/META-INF/assets/es-modules/t5/core") {
        include '**.js'
    }
    delete fileTree("src/main/resources/META-INF/modules/t5/core") {
        include '**.js'
    }
    delete "src/main/typescript/docs"
}

processResources.dependsOn compileTypeScript
clean.dependsOn cleanTypeScriptFiles

// Not sure why this is necessary:
compileTestGroovy.dependsOn compileTestJava

test {
	// Needed to have XMLTokenStreamTests.testStreamEncoding() passing on Java 9+
    if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_1_9)) {
        jvmArgs("--add-opens=java.base/java.nio.charset=ALL-UNNAMED");
    }
	// TAP5-2722
	systemProperty 'user.language', 'en'
}

task runTestApp1(type:JavaExec) {
  description 'Start app1 integration test app, useful when debugging failing integration tests'
  main = 'org.apache.tapestry5.test.JettyRunner'
  args "-d", "src/test/app1", "-p", "8080"
  classpath += project.sourceSets.test.runtimeClasspath
}
task runTestApp2(type:JavaExec) {
  description 'Start app2 integration test app, useful when debugging failing integration tests'
  main = 'org.apache.tapestry5.test.JettyRunner'
  args "-d", "src/test/app2", "-p", "8080"
  classpath += project.sourceSets.test.runtimeClasspath
}
task runTestApp3(type:JavaExec) {
  description 'Start app3 integration test app, useful when debugging failing integration tests'
  main = 'org.apache.tapestry5.test.JettyRunner'
  args "-d", "src/test/app3", "-p", "8080"
  classpath += project.sourceSets.test.runtimeClasspath
}
task runTestApp4(type:JavaExec) {
  description 'Start app4 integration test app, useful when debugging failing integration tests'
  main = 'org.apache.tapestry5.test.JettyRunner'
  args "-d", "src/test/app4", "-p", "8080"
  classpath += project.sourceSets.test.runtimeClasspath
}
task runTestApp5(type:JavaExec) {
  description 'Start app5 integration test app, useful when debugging failing integration tests'
  main = 'org.apache.tapestry5.test.JettyRunner'
  args "-d", "src/test/app5", "-p", "8080"
  classpath += project.sourceSets.test.runtimeClasspath
}
task runTestApp7(type:JavaExec) {
  description 'Start app7 integration test app, useful when debugging failing integration tests'
  main = 'org.apache.tapestry5.test.JettyRunner'
  args "-d", "src/test/app7", "-p", "8080"
  classpath += project.sourceSets.test.runtimeClasspath
}
task runTestAppfolder(type:JavaExec) {
  description 'Start appFolder integration test app, useful when debugging failing integration tests'
  main = 'org.apache.tapestry5.test.JettyRunner'
  args "-d", "src/test/appfolder", "-p", "8080"
  classpath += project.sourceSets.test.runtimeClasspath
}

// Default is jQuery and Require.js enabled, so here are the tests for the
// other combinations

task testWithJqueryAndRequireJsDisabled(type:Test) {
  systemProperties."tapestry.javascript-infrastructure-provider" = "jquery"
  systemProperties."tapestry.require-js-enabled" = "false"
}

task testWithPrototypeAndRequireJsEnabled(type:Test) {
  systemProperties."tapestry.javascript-infrastructure-provider" = "prototype"
  systemProperties."tapestry.require-js-enabled" = "true"
}  

task testWithPrototypeAndRequireJsDisabled(type:Test) {
  systemProperties."tapestry.javascript-infrastructure-provider" = "prototype"
  systemProperties."tapestry.require-js-enabled" = "false"
}