buildscript {
    repositories {
        maven { url 'https://ncore-repo.red-soft.ru/repository/public' }
        mavenLocal()
    }
    dependencies {
        classpath 'biz.redsoft.gradle.ncore:gradle-ncore:1.3.+'
        classpath 'nu.studer:gradle-credentials-plugin:1.0.7'
    }
}

group = 'biz.redsoft.sample'

apply plugin: 'nu.studer.credentials'
apply plugin: 'idea'
apply plugin: 'application'
apply plugin: 'war'
apply plugin: 'ncore'

apply from: "dependencies.gradle"

targetCompatibility = 1.8

ncore {
    coreVersion = project.properties.'biz.redsoft.ncore.version'
    sourceEncoding = 'UTF-8'
}

repositories {
    mavenLocal()
    maven {
        url 'https://ncore-repo.red-soft.ru/repository/public'
        if (project.credentials.user != null && project.credentials.password != null) {
          credentials {
            username project.credentials.user
            password project.credentials.password
          }
        }
    }
}

dependencies {
    provided 'javax.servlet:javax.servlet-api:3.1.0'

    testCompile 'org.junit.jupiter:junit-jupiter:5.8.2'
    runtimeClasspath files(ncoreGenerateControlDataJar)
    runtimeClasspath files(genJar)
}

// writing build process to a log file
// --------------------- BUILD LOG SETTINGS ----------------------
def tstamp = new Date().format('yyyy-MM-dd_HH-mm-ss')
def buildLogDir = "${rootDir}/logs"
mkdir("${buildLogDir}")
def buildLog = new File("${buildLogDir}/${tstamp}_buildLog.log")

import org.gradle.internal.logging.*
System.setProperty('org.gradle.color.error', 'RED')

gradle.services.get(LoggingOutputInternal).addStandardOutputListener (new StandardOutputListener () {
    void onOutput(CharSequence output) {
        buildLog << output
    }
})

gradle.services.get(LoggingOutputInternal).addStandardErrorListener (new StandardOutputListener () {
    void onOutput(CharSequence output) {
        buildLog << output
    }
})
// --------------------- BUILD LOG SETTINGS ----------------------

run.args = ['client',
            '-config', 'ncore-properties.xml',
            '-config', 'ncore-properties-local.xml']

task copyDist(type: Copy) {
    from distZip
    from war
    def releaseDir
    if (project.hasProperty('biz.redsoft.release.dir') && project.getProperty('biz.redsoft.release.dir'))
        releaseDir = "${project.getProperty('biz.redsoft.release.dir')}"
    else
        releaseDir = "$projectDir/../releases"
    into "$releaseDir/$project.name/$version"
}

release {
    tagPrefix = 'v'
}

task buildRelease(type: GradleBuild) {
    tasks = ['clean', 'writeVersion', 'nbackup', 'build', 'copyDist', 'publish']
}

idea { module.scopes.PROVIDED.plus += [configurations.provided] }

jar{
    manifest{
        attributes(
                'Main-Class': 'biz.redsoft.ncore.client.MainForm',
                'Class-Path': configurations.runtimeClasspath.collect { it.name }.join(' ')
        )
    }
}

startScripts{
    project.startScripts.defaultJvmOpts = ['-Dlogback.configurationFile=./config/logback.xml']
}