
buildscript {
    ext.cubaVersion = '6.6.4'
    repositories {
        mavenLocal()
        maven {
            url 'https://repo.cuba-platform.com/content/groups/work'
            credentials {
                username(rootProject.hasProperty('repoUser') ? rootProject['repoUser'] : 'cuba')
                password(rootProject.hasProperty('repoPass') ? rootProject['repoPass'] : 'cuba123')
            }
        }
        maven {
            url 'https://repo.cuba-platform.com/content/groups/premium'
            credentials {
                username(rootProject.hasProperty('premiumRepoUser') ? rootProject['premiumRepoUser'] : System.getenv('CUBA_PREMIUM_USER'))
                password(rootProject.hasProperty('premiumRepoPass') ? rootProject['premiumRepoPass'] : System.getenv('CUBA_PREMIUM_PASSWORD'))
            }
        }
    }
    dependencies {
        classpath "com.haulmont.gradle:cuba-plugin:$cubaVersion"
    }
}

def globalModule = project(':xxx-global')
def coreModule = project(':xxx-core')
def guiModule = project(':xxx-gui')
def webModule = project(':xxx-web')
def webToolkitModule = project(':xxx-web-toolkit')
def webThemesModule= project(':xxx-web-themes')

def servletApi = 'org.apache.tomcat:tomcat-servlet-api:8.0.26'


apply(plugin: 'idea')
apply(plugin: 'cuba')

cuba {
    artifact {
        group = 'kz.xxx.xxx'
        version = '0.1'
        isSnapshot = true
    }
    tomcat {
        dir = "$project.rootDir/deploy/tomcat"
        port = 8085
    }
}

dependencies {
    appComponent("com.haulmont.reports:reports-global:$cubaVersion")
    appComponent("com.haulmont.fts:fts-global:$cubaVersion")
    appComponent("com.haulmont.bpm:bpm-global:$cubaVersion")
    appComponent("kz.xxx.app:uwc-global:0.1-SNAPSHOT")

}

def postgres = 'org.postgresql:postgresql:9.4.1212'

configure([globalModule, coreModule, guiModule, webModule, webToolkitModule]) {
    apply(plugin: 'java')
    apply(plugin: 'maven')
    apply(plugin: 'idea')
    apply(plugin: 'cuba')

    dependencies {
        testCompile('junit:junit:4.12')
    }

    task sourceJar(type: Jar) {
        from file('src')
        classifier = 'sources'
    }

    artifacts {
        archives sourceJar
    }
}

configure(globalModule) {

    dependencies {
    }
    task enhance(type: CubaEnhancing)


}

configure(coreModule) {

    configurations {
        jdbc
        dbscripts
    }

    dependencies {
        compile(globalModule)
        provided(servletApi)
        jdbc(postgres)
        testRuntime(postgres)
        compile('org.telegram:telegrambots:3.3')

    }

    task cleanConf(description: 'Cleans up conf directory') {
        doLast {
            def dir = new File(cuba.tomcat.dir, '/conf/xxx-core')
            if (dir.isDirectory()) {
                ant.delete(includeemptydirs: true) {
                    fileset(dir: dir, includes: '**/*', excludes: 'local.app.properties')
                }
            }
        }
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = 'xxx-core'
        appJars('xxx-global', 'xxx-core')
    }

    task createDb(dependsOn: assembleDbScripts, description: 'Creates local database', type: CubaDbCreation) {
        dbms = 'postgres'
        host = 'localhost'
        dbName = 'xxx_dev'
        dbUser = 'xxx'
        dbPassword = 'xxx'
    }

    task updateDb(dependsOn: assembleDbScripts, description: 'Updates local database', type: CubaDbUpdate) {
        dbms = 'postgres'
        host = 'localhost'
        dbName = 'xxx_dev'
        dbUser = 'xxx'
        dbPassword = 'xxx'
    }
}

configure(guiModule) {
    dependencies {
        compile(globalModule)

    }

    task deployConf(type: Copy) {
        from file('src')
        include "kz/xxx/xxx/**"
        into "$cuba.tomcat.dir/conf/tal"
    }
}

configure(webModule) {
    configurations {
        webcontent
    }

    dependencies {
        provided(servletApi)
        compile(guiModule)

    }

    jar {
        with copySpec {
            from sourceSets.main.allJava
            include "kz/xxx/xxx/web/toolkit/ui/client/**"
        }
    }

    task webArchive(type: Zip) {
        from file("$buildDir/web")
        from file('web')
        classifier = 'web'
    }

    artifacts {
        archives webArchive
    }

    task deployConf(type: Copy) {
        from file('src')
        include "kz/xxx/xxx/**"
        into "$cuba.tomcat.dir/conf/tal"
    }

    task clearMessagesCache(type: CubaClearMessagesCache) {
        appName = 'tal'
    }
    deployConf.dependsOn clearMessagesCache

    task cleanConf(description: 'Cleans up conf directory') {
        doLast {
            def dir = new File(cuba.tomcat.dir, '/conf/tal')
            if (dir.isDirectory()) {
                ant.delete(includeemptydirs: true) {
                    fileset(dir: dir, includes: '**/*', excludes: 'local.app.properties')
                }
            }
        }
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = 'tal'
        appJars('tal-global', 'tal-gui', 'tal-web')
    }
    task buildScssThemes(type: CubaWebScssThemeCreation)
    task deployThemes(type: CubaDeployThemeTask, dependsOn: buildScssThemes)
    assemble.dependsOn buildScssThemes
}

configure(webToolkitModule) {
    dependencies {
        compile(webModule)


    }

    jar {
        from sourceSets.main.allJava
    }

    task buildWidgetSet(type: CubaWidgetSetBuilding) {
        widgetSetClass = 'kz.xxx.xxx.web.toolkit.ui.AppWidgetSet'
    }

    task debugWidgetSet(type: CubaWidgetSetDebug) {
        widgetSetClass = 'kz.xxx.xxx.web.toolkit.ui.AppWidgetSet'
    }

    task webArchive(dependsOn: buildWidgetSet, type: Zip) {
        from file("$buildDir/web")
        classifier = 'web'
    }

    artifacts {
        archives webArchive
    }

    task deploy(dependsOn: buildWidgetSet) {
        doLast {
            delete {
                "$cuba.tomcat.dir/webapps/tal/VAADIN/widgetsets"
            }
            copy {
                from "$buildDir/web"
                into "$cuba.tomcat.dir/webapps/tal"
            }
        }
    }
}




configure(webThemesModule) {
    apply(plugin: 'java')
    apply(plugin: 'maven')
    apply(plugin: 'cuba')

    appModuleType = 'web-themes'

    buildDir = file('../build/scss-themes')

    sourceSets {
        main {
            java {
                srcDir '.'
            }
            resources {
                srcDir '.'
            }
        }
    }
}


task undeploy(type: Delete, dependsOn: ':tal-web:cleanConf') {
    delete("$cuba.tomcat.dir/shared")
    delete("$cuba.tomcat.dir/webapps/tal-core")
    delete("$cuba.tomcat.dir/webapps/tal")
}

task restart(dependsOn: ['stop', ':tal-core:deploy', ':tal-web:deploy', ':tal-web-toolkit:deploy'], description: 'Redeploys applications and restarts local Tomcat') {
    doLast {
        ant.waitfor(maxwait: 6, maxwaitunit: 'second', checkevery: 2, checkeveryunit: 'second') {
            not {
                socket(server: 'localhost', port: '8787')
            }
        }
        start.execute()
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '3.4'
}

apply from: 'extra.gradle'

task buildWar(type: CubaWarBuilding) {
    coreContextXmlPath = 'modules/core/web/META-INF/war-context.xml'
    webXmlPath = 'modules/web/web/WEB-INF/single-war-web.xml'
    appProperties = ['cuba.automaticDatabaseUpdate' : false]
    includeContextXml = true
    appHome = '${app.home}'
}