apply from: 'dependencies.gradle'

buildscript {
	// Gradle will not find vars defined in an external file when referring to them
    // in the buildscript block, unless you link it from the buildscript block, too.
    apply from: 'dependencies.gradle'

    repositories {
        jcenter()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'
        classpath "org.junit.platform:junit-platform-gradle-plugin:$versions.junitPlatform"
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
    }
}

allprojects {
    repositories {
        jcenter()
    }

    apply plugin: 'com.github.ben-manes.versions'
}

def gitTag() {
    def tag = 'git tag --list --points-at HEAD'.execute((List) null, rootProject.projectDir).text.trim()

    if (tag.split(System.lineSeparator()).length > 1) {
        throw new IllegalStateException("gitTag is accessed but commit has multiple tags: $tag")
    }

    return tag
}

def projectVersion() {
    def tag = gitTag()

    if (tag.startsWith('v')) {
        return tag.substring(1)
    } else if (tag.isEmpty()) {
        return "development"
    }

    return tag
}

def validateTagAndVersion() {
    if (gitTag().isEmpty()) {
        throw new IllegalStateException('Publishing is not allowed because current commit has no tag')
    }

    if (projectVersion().isEmpty()) {
        throw new IllegalStateException('Publishing is not allowed because current projectVersion is empty')
    }
}
