// Build library: gradlew build
// Install library locally for development with: gradlew install

plugins {
    id 'java'
    id 'maven-publish'
    id 'com.enonic.defaults' version '2.0.1'
    id "com.moowork.node" version "1.1.1"
}

jar {
    baseName = "${projectName}"
}

dependencies {
    compile "com.enonic.xp:script-api:${xpVersion}"
    compile "com.enonic.xp:lib-io:${xpVersion}"
    compile "com.enonic.xp:lib-context:${xpVersion}"
    compile "com.enonic.xp:lib-repo:${xpVersion}"
    compile "com.enonic.xp:lib-node:${xpVersion}"
    compile "com.enonic.xp:core-api:${xpVersion}"
    compile "com.enonic.xp:lib-portal:${xpVersion}"

    compile 'com.enonic.lib:util:1.3.0'
    compile 'com.enonic.lib:lib-cache:1.1.0'

    compile group: 'org.jdom', name: 'jdom', version: '2.0.2'
    compile group: 'org.json', name: 'json', version: '20180813'
    compile group: 'jaxen', name: 'jaxen', version: '1.1.6'
    
    testCompile "com.enonic.xp:testing:${xpVersion}"
}

node {
    download = true
    version = '10.15.3'
}

repositories {
    mavenLocal()
    jcenter()
    maven {
        url 'http://repo.enonic.com/public'
    }
}

npmInstall.inputs.file("package.json")
npmInstall.inputs.file("package-lock.json")
npmInstall.outputs.dir("node_modules")


task babelXP(type: NodeTask) {
    group 'React4xp runtime lib'
    description 'Compile server-side XP components from ES6'

    script = file('node_modules/babel-cli/bin/babel.js')
    args = ["src/main/resources", "--out-dir", "build/resources/main"]

    inputs.dir 'src/main/resources'
    outputs.dir("build/resources/main")
}
babelXP.dependsOn += 'processResources'
babelXP.dependsOn += 'npmInstall'

def ROOT = new File("").absolutePath + "/"

task nashornPolyfills(type: NodeTask) {
    group 'React4xp runtime lib'
    description 'Build default polyfill code, allowing XP to run the (node-written) React server-side rendering in Nashorn'

    script = file('node_modules/webpack/bin/webpack.js')
    args = [
            '--config', 'node_modules/react4xp-runtime-nashornpolyfills/webpack.config.js',
            '--env.NASHORNPOLYFILLS_SOURCE=node_modules/react4xp-runtime-nashornpolyfills/nashornPolyfills.es6',

            // (env.BUILD_R4X + env.NASHORNPOLYFILLS_FILENAME) must match ('build/resources/main' + defaultPolyfillFileName)
            // in com/enonic/lib/react4xp/ssr/EngineFactory.java!
            '--env.BUILD_R4X=' + ROOT + 'build/resources/main/lib/enonic/react4xp/default',
            '--env.NASHORNPOLYFILLS_FILENAME=nashornPolyfills'
    ]
}
nashornPolyfills.dependsOn += 'babelXP'

task client(type: NodeTask) {
    group 'React4xp runtime lib'
    description 'Include the client rendering wrapper in the lib'

    script = file('node_modules/webpack/bin/webpack.js')
    args = [
            '--config', 'node_modules/react4xp-runtime-client/webpack.config.js',
            '--env.BUILD_R4X=' + ROOT + 'build/resources/main/services/react4xp-client',
            '--env.BUILD_ENV=production',
            '--env.CHUNK_CONTENTHASH=[name].js',
            '--env.CLIENT_CHUNKS_FILENAME=__delete_me_im_redundant__',

            // Placeholders used in services/react4xp-client/react4xp-client.es6. Keep up to date!
            '--env.LIBRARY_NAME=__REACT4XP__RUNTIME__CLIENT__LIBRARY_NAME__PLACEHOLDER__',
            '--env.SERVICE_ROOT_URL=__REACT4XP__RUNTIME__CLIENT__SERVICE_ROOT_URL__PLACEHOLDER__'
    ]

    doLast {
        def f = new File(ROOT + 'build/resources/main/services/react4xp-client/__delete_me_im_redundant__')
        f.delete()
    }
}
client.dependsOn += 'babelXP'

jar.dependsOn += 'nashornPolyfills'
jar.dependsOn += 'client'
jar.outputs.dir('build/libs')
