view init20-git.gradle @ 359:77ca22c4a0b3

remove maven publications
author Marc Davis <marc.davis@mcoecn.org>
date Mon, 13 Nov 2023 10:29:55 -0500
parents 64a8d53c6d98
children de8c350c511a
line wrap: on
line source
import groovy.sql.Sql
import groovy.transform.Sortable
import groovy.transform.ToString
import groovy.transform.TupleConstructor

buildscript {
    repositories {
        maven { url 'https://docker.ssdt.io/artifactory/ssdt-repo' }
        maven { url 'https://docker.ssdt.io/artifactory/gradle-plugins' }
    }
    dependencies {
        classpath 'postgresql:postgresql:9.1-901.jdbc4'
        classpath  'com.netflix.nebula:gradle-dependency-lock-plugin:5.0.6'
    }
}

final GradleVersion gradleCurrent = GradleVersion.current()
final GradleVersion gradleV20 = GradleVersion.version('2.0')
final GradleVersion gradleV31 = GradleVersion.version('3.1')

println "Gradle Version: $gradleCurrent"

if (gradleCurrent < gradleV20) {
    throw new RuntimeException("this init script requires Gradle version 2.0 or higher")
}

gradle.ext.ssdtDevelkitLocation = gradle.ext.has('ssdtDevelkitLocation') ? gradle.ssdtDevelkitLocation : 'http://hg.ssdt-ohio.org/browse/public/develkit'

ant.property(file: System.getProperty('user.home') + "/.ssdt/private.properties")
gradle.ext.ivyUserDir = ant.properties['ivy.default.ivy.user.dir'] ?: System.getProperty('user.home') + "/.ivy2"

gradle.ext.ssdtProjectId = System.getenv('bamboo_project_id') ?: rootProject.name

gradle.addListener(new ArtifactoryGradleSettings())

def hostname
try {
    hostname = "hostname".execute().text.toLowerCase().readLines().first()
} catch (e) {
    hostname = 'unknown'
}

gradle.ext.bambooBuild = System.getenv().any {
    it.key.toLowerCase().contains('bamboo')
} || hostname?.startsWith('ssdt-ba')

gradle.ext.bambooPlan = (System.getenv('BAMBOO_PLAN') ?: 'UNKNOWN-UNKNOWN-JOB1').split('-')[0..1].join('-')
logger.info "Bamboo plan: ${gradle.bambooPlan}"

gradle.ext.buildTimestamp = System.currentTimeMillis().toString().padLeft(14, '0')

gradle.ext.hgRepositoryUrl = ""

try {
    gradle.ext.hgRepositoryUrl = ("hg path".execute().text.split('=') ?: ['', ''])[1].trim()
} catch (e) {
}

def springModuleTranslator = [
        'spring-transaction': 'spring-tx',
        'spring-web-servlet': 'spring-webmvc',
].withDefault { it }

gradle.ext.normalizeSpring = { DependencyResolveDetails details ->
    if (details.requested.group == 'org.springframework' && details.requested.name.startsWith('org.springframework.')) {
        def shortName = springModuleTranslator[details.requested.name.replace('org.springframework.', 'spring-').replace('.', '-')]
        details.useTarget(group: 'org.springframework', name: shortName, version: details.requested.version)
    }
    if (details.requested.group == 'org.springframework.security' && details.requested.name.startsWith('org.springframework.')) {
        def shortName = springModuleTranslator[details.requested.name.replace('org.springframework.', 'spring-').replace('.', '-')]
        details.useTarget("${details.requested.group}:$shortName:${details.requested.version}")
    }
}

gradle.ext.runtimeInfo = new RuntimeInfo()


if (System.env.DOCKER_HOST ) {
    if (System.env.DOCKER_HOST.contains('tcp:')) {
        gradle.ext.dockerEngineUrl = "https:${System.env.DOCKER_HOST?.minus('tcp:')}"
    }
    gradle.ext.dockerEngineUrl = System.env.DOCKER_HOST
}

setBranchInfo()

loadEnvironments()

gradle.environment.put('hgRepositoryUrl', gradle.hgRepositoryUrl)
gradle.environment.put('branchName', gradle.branch.name)
gradle.environment.put('branchStream', gradle.branch.stream)
gradle.environment.put('branchHash', gradle.branch.hash)


def cacheTimeout = 60 * 60 * 8
if (gradle.environment['dependencyTimeout']) {
    cacheTimeout = gradle.environment['dependencyTimeout'] as Integer
    println "setting changing dependency timeout to $cacheTimeout seconds"
}

gradle.ext.cacheTimeout = cacheTimeout

rootProject.ext.indyCapable = {
    boolean capable = true
    try {
        Class.forName('java.lang.invoke.MethodHandle')
    } catch (e) {
        capable = false
    }
    capable && !rootProject.hasProperty('skipIndy')
}

rootProject.ext.useIndy = {
    boolean indy = false
    // first, check if a system property activates indy support
    indy |= System.hasProperty('indy') && Boolean.valueOf(System.getProperty('indy'))

    // check ssdt environment for indy property.
    indy |= (gradle.environment.indy) ? gradle.environment.indy.toBoolean() : false

    // check if the main project has an extension property setting indy (-Pindy).
    if (rootProject.hasProperty('indy')) {
        indy = (Boolean.valueOf(rootProject.indy))
    }

    // set the groovy runtime system property to ensure forked junit test will get the indy flag properly
    if (indy && rootProject.indyCapable()) System.setProperty("groovy.target.indy", "true")

    indy && rootProject.indyCapable()
}

println "Indy available: ${rootProject.indyCapable()} enabled: ${rootProject.useIndy()}"

if (gradle.bambooBuild) {

    file('build-number.txt').text = "build.number=${gradle.branch.buildNumber ?: -1 }\n"
    logger.info "applying SSDT artifactory Gradle Settings (bamboo: $gradle.bambooBuild host: $hostname)"
    apply from: "${gradle.ssdtDevelkitLocation}/artifactory20.gradle"
}

if (!rootProject.hasProperty('disableMetrics')) {
    apply from: "${gradle.ssdtDevelkitLocation}/metrics.gradle"
}

rootProject.afterEvaluate { r ->
    if (gradle.bambooBuild && r.hasProperty('requireJavaVersion')) {
        gradle.runtimeInfo.requireJava( r.getProperty('requireJavaVersion') )
    }
}

def findComponent(project, name) {
    project.component.find { it.@name == name }
}

if (gradleCurrent >= gradleV31) {
    wrapper {
        distributionType = org.gradle.api.tasks.wrapper.Wrapper.DistributionType.ALL
    }
}

allprojects {

    apply plugin: nebula.plugin.dependencylock.DependencyLockPlugin

    dependencyLock {
        globalLockFile =  gradle.branch.isRelease() ? 'release.lock' : 'global.lock'
        includeTransitives = true
    }

    configurations.all {
        resolutionStrategy.cacheChangingModulesFor gradle.cacheTimeout, 'seconds'
        resolutionStrategy.cacheDynamicVersionsFor gradle.cacheTimeout, 'seconds'
    }
    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'org.ssdt_ohio' && !details.requested.version ) {
                details.useVersion( "latest.${gradle.branch.defaultDependencyStatus}" )
            }
            if (details.requested.version == 'default') {
                details.useVersion("latest.${gradle.branch.defaultDependencyStatus}" )
            }
            if (project.hasProperty("overrideCommon")) {
                if (details.requested.group == 'org.ssdt_ohio' && details.requested.name.contains('ssdt.common.')) {
                    details.useVersion(project.overrideCommon)
                }
            }
            if (project.hasProperty("overrideVui")) {
                if (details.requested.group == 'org.ssdt_ohio' && details.requested.name.startsWith('vui.')) {
                    details.useVersion(project.overrideVui)
                }
            }
            if (project.hasProperty("overrideUsasCore")) {
                if (details.requested.group == 'org.ssdt_ohio' && details.requested.name.startsWith('usas.') && !details.requested.name.startsWith('usas.vui')) {
                    details.useVersion(project.overrideUsasCore)
                }
            }
            if (project.hasProperty("overrideUspsCore")) {
                if (details.requested.group == 'org.ssdt_ohio' && details.requested.name.startsWith('usps.') && !details.requested.name.startsWith('usps.vui')) {
                    details.useVersion(project.overrideUspsCore)
                }
            }

            resolutionStrategy.force 'xml-apis:xml-apis:1.4.01'
        }
    }

    task cleanLocal(description: "removes all artifacts from developer's local repository") {

        doLast {
            def local = project.repositories.find { it.name == 'local' }
            if (local) {
                logger.info "removing local repo: $it"
                new File(System.properties['user.home'] + "/.ssdt/local-repo").deleteDir()
                def localDir = new File(gradle.ivyUserDir + "/local")
                localDir.deleteDir()
                logger.info "verifying removal of local repo"
                if (localDir.exists()) {
                    throw new org.gradle.api.GradleException("Unable to clean ${localDir}. Files may be locked by another process.")
                }
            }
        }
    }

    cleanLocal.onlyIf {
        project.repositories.any { it.name == 'local' }
    }

    project.ext.previousBuildenv = project.file('build/buildenv.txt').exists() ? project.file('build/buildenv.txt').text : 'none'

    tasks.addRule("Pattern: <environment>As[Test]Properties: Generates <environment>.properties as resource or Test resource") { String taskName ->
        if ((taskName - 'Test').endsWith("AsProperties") && !taskName.startsWith('clean')) {
            //       def t = taskName.contains('Test') ? processTestResources.destinationDir : processResources.destinationDir
            def t = taskName.contains('Test') ? sourceSets.test.output.resourcesDir : sourceSets.main.output.resourcesDir
            def e = (taskName - 'Test' - 'AsProperties').capitalize()
            task(taskName) {
                ext.outputDir = t
                ext.propertyFile = "${e.toLowerCase()}.properties"
                ext.buildenv = project.file('build/buildenv.txt')
                inputs.files project.file("../environment${e}.groovy"), project.file("../private${e}.groovy"), project.file('../private.properties')
                outputs.files new File(outputDir,propertyFile), buildenv
                outputs.upToDateWhen {
                    gradle.env == project.previousBuildenv && outputs.getFiles().every { it.exists() }
                }
                doLast {
                    t.mkdirs()
                    outputDir.mkdirs()
                    buildenv.text = gradle.env
                    def ps = gradle."environment${e}".toProperties()
                    ps['ssdt.project'] = project.name
                    def pf = new File(outputDir,propertyFile)
                    ext.outputPropertyFile = pf
                    ps.store(pf.newOutputStream(), "by $taskName of $this")
                    def l = pf.readLines().sort()
                    pf.text = l.join('\n').replaceAll("\\.PARENT","")
                }
            }
        }
    }

    if (gradle.hasProperty('environmentDatabase')) {
        tasks.addRule("Pattern: database{Drop|Create}...: drops or creates the environment's database") { String taskName ->
            if (taskName.startsWith('database') && (taskName.contains('Drop') || taskName.contains('Create'))) {
                task(taskName) {
                    doLast {
                        def dropIt = taskName.contains("Drop")
                        def createIt = taskName.contains("Create")
                        def driverName = gradle.environmentDatabase.jdbc.driverClassName
                        def databaseUrl = gradle.environmentDatabase.jdbc.url
                        if (driverName != "org.postgresql.Driver") {
                            logger.info "skipping database operation for non-postgresql driver"
                            throw new StopActionException("driver $driverName not supported by $taskName")
                        }
                        if (!configurations.hasProperty('jdbcdriver')) {
                            throw new GradleException("databaseDrop requires a 'jdbcdriver' configuration with a postgresql (or other) driver")
                        }
                        configurations.jdbcdriver.each { File file ->
                            GroovyObject.class.classLoader.addURL(file.toURL())
                        }
                        Class.forName(driverName)
                        def url = new URI(databaseUrl.substring(5))
                        def jdbcBaseUrl = databaseUrl - url.path
                        def databaseName = url.path - "/"
                        def adminUrl = jdbcBaseUrl + "/postgres"
                        def sql = Sql.newInstance(adminUrl,
                                gradle.environmentDatabase.jdbc.username,
                                gradle.environmentDatabase.jdbc.password,
                                driverName)
                        if (dropIt) {
                            logger.info "disconnecting sessions from $databaseName"
                            sql.execute "REVOKE CONNECT ON DATABASE \"" + databaseName + "\" FROM public;"
                            sql.execute "REVOKE CONNECT ON DATABASE \"" + databaseName + "\" FROM " + gradle.environmentDatabase.jdbc.username + ";"
                            sql.execute "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '" + databaseName + "';"
                            logger.info "dropping $databaseName (if exists) from $adminUrl"
                            sql.execute 'DROP DATABASE IF EXISTS "' + databaseName + '";'
                        }

                        if (createIt) {
                            logger.info "Creating database $databaseName at $adminUrl"
                            sql.execute 'CREATE DATABASE "' + databaseName + '";'
                        }
                    }
                }

            }
        }
    }

}

rootProject.afterEvaluate {

    tasks.addRule("release{Major|Minor|Patch|n.n.n|Lock}: create release branch or update release Lock file") { String taskName ->

        if (taskName == 'releaseLock') {

            if (!gradle.branch.isRelease()) {
                throw new BuildCancelledException("releaseLock is only valid on release or hotfix branch.")
            }

            dependencyLock.globalLockFile =  'release.lock'

            task('doCommitLock') {
                doLast {
                    println 'hg status'.execute().text
                    println 'hg commit release.lock -A -m "lock dynamic dependencies for release"'.execute().text
                }
            }


            task('doForceDeleteLock') {
                doLast {
                    println "deleting $dependencyLock.globalLockFile"
                    rootProject.file(dependencyLock.globalLockFile).delete()
                }
            }


            def branchTasks = ['deleteGlobalLock', 'doForceDeleteLock', 'generateGlobalLock', 'saveGlobalLock', 'doCommitLock']

            task(taskName) {
                dependsOn branchTasks
            }

            branchTasks.tail().inject(branchTasks.head()) { a, b ->
                tasks[b].mustRunAfter a
                b
            }

        }

        def matcher = (taskName =~ /^release(Major|Minor|Patch|\d{1,3}\.\d{1,3}\.\d{1,3})$/)
        if (matcher.matches()) {

            dependencyLock.globalLockFile =  'release.lock'

            task('doReleaseBranch') {
                ext.requested = matcher[0][1].toLowerCase()
                doLast {
                    def releaseVersion = determineReleaseVersion(requested)
                    def releaseStream = releaseVersion.isHotfix() ? 'hotfix' : 'release'

                    println "-" * 60
                    println "Preparing to create branch\n"
                    println "\tproject:\t${gradle.rootProject.name}"
                    println "\tcurrent:\t${gradle.branch} ($gradle.branch.version)"
                    println()
                    println "\ttype   :\t${releaseStream.toUpperCase()}"
                    println "\tversion:\t${releaseVersion}"
                    println "\ttarget :\t${releaseStream}/v${releaseVersion}"
                    println()
                    println("-" * 60)
                    println "DRY RUN".center(60)
                    println("-" * 60)

                    println "hg flow ${releaseStream} start v${releaseVersion} --dirty --dry-run".execute().text

                    println "-" * 60

                    if (!confirmPrompt("Continue?")) {
                        throw new BuildCancelledException("release branching canceled by user request")
                    }

                    println "hg flow ${releaseStream} start v${releaseVersion} --dirty".execute().text
                    println "hg update ${releaseStream}/v${releaseVersion}".execute().text

                    setBranchInfo()

                    println "-" * 60
                    println " Be sure to execute 'releaseLock' task to update the release.lock file before proceeding."
                    println "-" * 60

                }
            }


            def branchTasks = ['doReleaseBranch']

            task(taskName) {
                dependsOn branchTasks
            }

            branchTasks.tail().inject(branchTasks.head()) { a, b ->
                tasks[b].mustRunAfter a
                b
            }

        }
    }

}

private static String readLine(String message, String defaultValue = null) {
    String _message = "> $message" + (defaultValue ? " [$defaultValue] " : "")
    if (System.console()) {
        return System.console().readLine(_message) ?: defaultValue
    }
    println "$_message "

    System.in.newReader().readLine() ?: defaultValue
}

private static boolean confirmPrompt(String message, boolean defaultValue = false) {
    String defaultStr = defaultValue ? 'Y' : 'n'
    String consoleVal = readLine("${message} (Y|n)", defaultStr)
    if (consoleVal) {
        return consoleVal.toLowerCase().startsWith('y')
    }

    defaultValue
}

private Version determineReleaseVersion(String requested) {
    if (requested == 'major') {
        return gradle.branch.version.nextMajor()
    } else if (requested == 'minor') {
        return gradle.branch.version.nextMinor()
    } else if (requested == 'patch') {
        return gradle.branch.version.nextPatch()
    } else {
        return new Version(*requested.split(/\./)*.toInteger(), false)
    }
}

class ArtifactoryGradleSettings extends BuildAdapter implements BuildListener {

    def void projectsEvaluated(Gradle gradle) {
        def ssdtArtifactory = 'https://docker.ssdt.io/artifactory'
        Project root = gradle.getRootProject()


        def branchVersioning = gradle.rootProject.version == 'unspecified'

        root.allprojects {

            def thisProject = delegate
            thisProject.status = 'integration'
            if (gradle.branchStream) {
                if (branchVersioning) {
                    thisProject.version = gradle.branch.version
                    thisProject.status = gradle.branch.defaultDependencyStatus
                } else {

                    thisProject.status = 'integration'
                    def fixupVersion = thisProject.version - ".SNAPSHOT"
                    if (gradle.branchStream == 'feature') {
                        fixupVersion = fixupVersion + ".SNAPSHOT"
                    }
                    if (gradle.branchStream == 'develop') {
                        fixupVersion = fixupVersion + ".SNAPSHOT"
                    }
                    if (gradle.branchStream in ['production', 'release', 'hotfix']) {
                        thisProject.status = 'release'
                    }
                    thisProject.version = fixupVersion
                }
            }

            repositories {

                if (!gradle.bambooBuild) {
                    ivy {
                        name = 'local'
                        artifactPattern gradle.ivyUserDir + '/local/[artifact]-[revision](-[classifier]).[ext]'
                        ivyPattern gradle.ivyUserDir + "/local/[module]-ivy-[revision].xml"
                    }
                    mavenLocal()
                }

                if (gradle.branchStream == 'feature') {
                    ivy {
                        name = 'ssdt-branches'
                        url = "${ssdtArtifactory}/ssdt-branches/${gradle.branchHash}/"
                        layout "pattern", {
                            artifact "[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"
                            ivy "[organization]/[module]/ivy-[revision].xml"
                        }
                    }
                }

                ivy {
                    name = 'ssdt-releases'
                    url = "${ssdtArtifactory}/ssdt-releases"
                    layout "pattern", {
                        artifact "[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"
                        artifact "[organization]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]"
                        ivy "[organization]/[module]/ivy-[revision].xml"
                        m2compatible = true
                    }
                }

                ivy {
                    name = 'ssdt-snapshots'
                    url = "${ssdtArtifactory}/ssdt-snapshots"
                    layout "pattern", {
                        artifact "[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]"
                        artifact "[organization]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]"
                        ivy "[organization]/[module]/ivy-[revision].xml"
                        m2compatible = true
                    }
                }

                maven {
                    name = 'ssdt-repository'
                    url = "${ssdtArtifactory}/repository"
                }

            }

            def remoteRepos = thisProject.repositories.findAll { it.hasProperty('url') && !(it.name.toLowerCase().contains('local') || it.url.toString().contains('ssdt')) }
            if (remoteRepos) {
                logger.warn "WARNING: Remote repositories configured for $thisProject:\n" + remoteRepos.collect { "\t$it.name $it.url " }.join('\n')  + "\n Moved to lowest priority..."
                remoteRepos.each {
                    thisProject.repositories.remove(it)
                    thisProject.repositories.addLast(it)
                }
            }
            logger.info "$thisProject configured repositories:\n" + thisProject.repositories.collect {"\t$it.name ${it.hasProperty('url') ? it.url : '' }" }.join('\n')


            if (thisProject.repositories.find { it.name == 'local' } && thisProject.getTasksByName('uploadArchives', false)) {
                uploadArchives {
                    repositories {
                        add thisProject.repositories.local
                    }
                }

                thisProject.tasks.create("publishLocal") {
                    description = "Publishes this projects artifacts to developer's local repository"
                    dependsOn = ["uploadArchives"]
                }
            }

        }

        root.subprojects { p ->
            if (root.useIndy()) {
                def groovyIndy = p.configurations.compile.files.find { f -> f.name.startsWith('groovy-all') && f.name.contains('-indy') }
                if (groovyIndy) {
                    println "enabling indy compilation on $p"
                    [compileGroovy.groovyOptions, compileTestGroovy.groovyOptions]*.with {
                        optimizationOptions = [indy: true]
                    }
                }
            }
        }
    }
}


task showEnvironments {

    doLast {
        println "Defined environments: $gradle.environments"
        gradle.environments.each { e ->
            println "\n $e:"
            gradle.getProperty(e).flatten().sort { it.key }.each { k, v ->
                println String.format('  %25s = %s', k, k.contains('password') ? "********" : v)
            }
        }
        if (logger.isInfoEnabled()) {
            println "System properties:"
            System.properties.each { println "    $it" }
            println "env variables:"
            System.getenv().each { println "    $it" }
        }
    }
}

def loadEnvironments() {
    def developerPrivate = new Properties()
    if (file('private.properties').exists()) {
        developerPrivate.load(file('private.properties').newReader())
    }
    def envOverrides = [:]

    if (!hasProperty('env')) {
        gradle.ext.env = developerPrivate.env ?: 'dev'
    } else {
        def values = getProperty('env').split(',')
        gradle.ext.env = values.first()
        values.tail().each {
            def (k, v) = it.split('=')
            envOverrides.put(k, v)
        }
    }

    println "Environment is: $gradle.env ($envOverrides)"
    def slurper = new ConfigSlurper(gradle.env)
    slurper.setBinding(['gradle': gradle])

    def environment = slurper.parse(
            '''deploy.mode="production"
       environments {
        test { deploy.mode="test" }
        dev { deploy.mode="development"}
       }''')
    if (developerPrivate['deploy.mode']) {
        environment.put('deploy.mode', developerPrivate['deploy.mode'])
    }

    environment.put('branchInfo',gradle.branch)
    environment.put('branchVersion',gradle.branch.version.toString())
    def environments = []
    gradle.ext.environment = environment
    file('.').listFiles().findAll { it.name ==~ /^environment.*\.groovy$/ }.sort { it.name }.each { envFile ->
        def envName = envFile.name - '.groovy'
        def privateFile = file("private" + envName - "environment" + ".groovy")
        logger.info("loading environment $envFile.name")

        def envCfg = slurper.parse(envFile.toURL())
        envCfg.merge(slurper.parse(developerPrivate))
        envCfg.put('ssdt.projectid', gradle.ssdtProjectId)
        envCfg.put('ssdt.environment', gradle.env)
        if (privateFile.exists()) {
            logger.info("loading private environment $privateFile")
            envCfg.merge(slurper.parse(privateFile.toURL()))
        }

        gradle.rootProject.getProperties().find { it.key.startsWith('environment') }.each {
            it.value.split(',').each { p ->
                def (k, v) = p.split('=')
                logger.info("$envName: overriding " + k + "=" + v + " in $it")
                envCfg.put(k, v)
            }
        }

        envOverrides.each { k, v ->
            logger.info("$envName: overriding " + k + "=" + v)
            envCfg.put(k, v)
        }
        environment.merge(envCfg)
        if (envName != 'environment') {
            gradle.ext[envName] = envCfg
            environments << envName
        }
    }
    environment.merge(slurper.parse(developerPrivate))
    def deployMode = environment.deploy.mode ?: 'development'
    environments.each { gradle.ext[it].put('ssdt.deployment.mode', deployMode) }
    environments << 'environment'
    gradle.ext.environments = environments

}


@ToString(includeNames=true)
class RuntimeInfo {
    // OS memory in megabytes, zero if unknown
    int systemMemory = 0
    int systemFreeMemory = 0
    String javaVersion = System.getProperty('java.version')

    RuntimeInfo() {
        try {
            new File('/proc/meminfo').readLines().findAll { it.startsWith 'Mem' }.collect { it.split(/\s+/) }.each {
                int value = (it[1] as Long) / 1024
                if (it[0].startsWith('MemTotal')) { systemMemory = value }
                if (it[0].startsWith('MemFree')) { systemFreeMemory = value }
            }

        } catch (e) {  }

    }

    void requireMemory(int megabytes) {
        if (systemFreeMemory > 0 && systemFreeMemory < megabytes) {
            println "WARNING: potentially insufficent OS memory for this build"
//            throw new GradleException("insufficent free OS memory for this build (available: ${systemFreeMemory}m, required: ${megabytes}m)")
        }
    }
    /**
     * Returns maximum memory available upto the value specified.
     */
    int maxMemory(int megabytes) {
        if (systemFreeMemory) {
            [systemFreeMemory,megabytes].min()
        } else { megabytes }

    }

    void requireJava(String version) {

        if ( version && !javaVersion.startsWith(version)) {
            throw new GradleException("Requires java version $version but running under $javaVersion")
        }
    }

}


@TupleConstructor
@Sortable
class Version {

    Integer major = 0
    Integer minor = 0
    Integer patch = 0
    Boolean snapshot = true

    Version nextMajor() {
        new Version(major + 1, 0, 0, false)
    }

    Version nextMinor() {
        if (snapshot) {
            new Version(major, minor , 0,false)
        } else {
            new Version(major, minor + 1, 0,false)
        }
    }

    Version nextPatch() {
        new Version(major, minor, patch + 1,false)
    }

    Version nextSnapshot() {
        new Version(major, minor + 1, 0)
    }

    boolean isHotfix() {
        !snapshot && patch > 0
    }

    String toString() {
        "${major}.${minor}.${patch}${snapshot ? '.SNAPSHOT' : ''}"
    }

}

void setBranchInfo() {
    gradle.ext.branch = new BranchInfo(System.getenv('bamboo_planRepository_branch'))
    gradle.ext.branchName = gradle.branch.name
    gradle.ext.branchStream = gradle.branch.stream
    gradle.ext.branchHash = gradle.branch.hash
    println "${gradle.hgRepositoryUrl} ${gradle.branch} ${gradle.branch.version}"
}


@ToString(includes=['name','shortName','buildVersion','imageId','deployName'],includeNames= true)
class BranchInfo {
    def name
    def stream = "none"
    def buildNumber = ""
    def changeset = ""
    def version

    BranchInfo(name) {
        this.name = name
        if (!name) {
            this.name = determineName() ?: ''
        }
        this.name = this.name.replace('@', '-')
        determineStream()
        buildNumber = System.getenv('bamboo_buildNumber') ?: ""
        changeset = System.getenv('bamboo_planRepository_revision') ?: ""
    }

    String getDefaultDependencyStatus() {
        return isRelease() ? 'release' : 'integration'
    }

    private boolean isRelease() {
        return stream in ['release', 'hotfix']
    }

    def getShortName() {
        def result = name.contains('/') ? name.split('/')[1] : name
    }

    String getBuildVersion() {
        def v = isRelease() ? shortName - "v": ""
        return v
    }

    def Version getVersion() {
        if (!version) {
            if (isRelease()) {
                version = new Version(*getBuildVersion().split('\\.')*.toInteger(), false)
            } else {
                version = findSnapshotVersion()
            }
        }
        return version
    }

    def getImageId() {
        (buildVersion ?:  shortName.take(25)) + (buildNumber ? "-${buildNumber}" : "-0")
    }

    def getDeployName() {
        (buildVersion ?:  shortName.take(25)).toLowerCase().collectReplacements {
            ('a'..'z').contains(it) || ('0'..'9').contains(it) || it == "-" ? null : '-'
        }
    }

    def getHash() {
        generateMD5(name)
    }
    def generateMD5(String s) {
        def digest = java.security.MessageDigest.getInstance("MD5")
        digest.update(s.bytes);
        new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
    }

    private findSnapshotVersion() {

        try {
            def repositoryUrl = System.getenv('bamboo_planRepository_repositoryUrl')
            if (repositoryUrl) {
                println "git pull $repositoryUrl".execute().text
            }
            def versions = "git tag".execute().text.split("\n")
                    .findAll { it != null || it != "" }
                    .collect { it.replace("v", "") }
                    .collect {
                        Version v = null
                        try {
                            v = new Version(*it.split('\\.')*.toInteger())
                            return v
                        } catch (ignored) {
                            //non semver tag found, ignoring
                        }
                    }
                    .sort { v1, v2 -> v2 <=> v1 }

            def branches = "git branch --remote".execute().text.split("\n")
                    .findAll { it.contains('release') || it.contains('hotfix') }
                    .collect { it.replaceAll('\\s+', ' ').split('/').last() - 'v' }
                    .collect { new Version(*it.split('\\.')*.toInteger()) }

            def results = (branches + versions).sort { v1, v2 -> v2 <=> v1 }
            if (results.isEmpty()) {
                return new Version().nextSnapshot()
            } else {
                return results.first().nextSnapshot()
            }
        } catch (ex) {
            println ex
            return new Version().nextSnapshot()
        }
    }


    def determineName()  {
        try {
            def branch = "git branch --show-current".execute().text.trim()
            return branch
        } catch (ignored) {
            return null
        }
    }

    /**
     * Try to determine the stream based on hgflow configs
     * git-flow doesn't have a local config, so we may need to keep this
     * file around just for the comparison.
     *
     * A new name for the file could help as well?
     */
    void determineStream() {
        def flowConfig = new File('.hgflow').exists() ? new File('.hgflow') : new File('../.hgflow')
        if (flowConfig.exists()) {
            def flows = new Properties()
            flows.load(flowConfig.newReader())
            flows.stringPropertyNames().each {
                if (!it.startsWith("[") && name.startsWith(flows.getProperty(it))) {
                    stream = it
                }
            }
        }
    }

}