ext.config = { key, defaultValue = null -> System.properties[key] ?: System.getenv()[key] ?: project.hasProperty(key) ? project.ext[key] : defaultValue }

project.buildscript{
    repositories {
        jcenter()
        maven {
            url 'https://dl.bintray.com/mrduguo/maven/'
        }
        if(config('additionalMavenRpoUrl') || 'https://dl.bintray.com/mrduguo/maven/'.startsWith('file')){
            maven {
                url config('additionalMavenRpoUrl') ?: 'https://dl.bintray.com/mrduguo/maven/'
            }
        }
    }
    dependencies {
        def versionRange=config('buildscriptVersion')
        if(!versionRange){
            versionRange='+'
            try{
                def gitBranch=config('branchName') ?: config('GIT_BRANCH') ?: 'git rev-parse --abbrev-ref HEAD'.execute().text.trim()
                def matcher = gitBranch =~ /\/(\d+)\.(\d+)\.(\d+)/
                if(!matcher.find()) {
                    matcher=null
                    def jobName=config('JOB_NAME')
                    if(jobName){
                        def envNameSection=jobName.split('-').first()
                        matcher = envNameSection =~ /(\d)(\d\d)(\d\d)/
                        if (!matcher.find()) {
                            matcher = envNameSection =~ /(\d)(\d\d)(\d)/
                            if (!matcher.find()) {
                                matcher = envNameSection =~ /(\d)(\d)(\d)/
                                if (!matcher.find()) {
                                    matcher=null
                                }
                            }
                        }

                    }
                }
                if (matcher!=null) {
                    def matcherResult=matcher[0]
                    versionRange= "(,${matcherResult[1]}.${matcherResult[2]}.${matcherResult[3].toInteger() + 1})".toString()
                }
            }catch(Exception mayNotGitRepo){
                println "WARN maybe not git repo: $mayNotGitRepo.message"
                mayNotGitRepo.printStackTrace()
            }
        }
        classpath "com.github.mrduguo.gradle:gradle-buildscript:$versionRange".toString()
        configurations.classpath.resolutionStrategy.cacheDynamicVersionsFor 0, 'seconds'
    }
}
