buildscript {
    ext.kotlin_version = '1.4.10'

    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
        maven {
            url 'http://dl.bintray.com/jetbrains/intellij-plugin-service'
        }
        maven {
            url "https://dl.bintray.com/ferranpons/maven"
        }
        maven {
            url "https://dl.bintray.com/schibstedspain/maven"
        }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.ferranpons:twitter-gradle-plugin:1.1.0'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.6.2"
        classpath 'com.novoda:bintray-release:0.8.1'
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.14"
    }
}

plugins {
    id "org.jetbrains.intellij" version "0.4.10"
    id "io.gitlab.arturbosch.detekt" version "1.0.0.RC7-3"
    id 'org.jetbrains.kotlin.jvm' version '1.2.51'
    id "org.jlleitschuh.gradle.ktlint" version "9.2.1"
}

apply plugin: 'idea'
apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'twitter-gradle-plugin'
apply plugin: "org.jlleitschuh.gradle.ktlint"
apply plugin: 'com.novoda.bintray-release'

ext.versionProps = new Properties()
versionProps.load(new FileInputStream(rootDir.absolutePath + "/version.properties"))

def versionMajor = Integer.parseInt(versionProps['MAJOR'] as String)
def versionMinor = Integer.parseInt(versionProps['MINOR'] as String)
def versionPatch = Integer.parseInt(versionProps['PATCH'] as String)

def isArtifactoryDeploy = "true" == System.getenv("ARTIFACTORY_DEPLOY")

static def getDate() {
    return new Date().format('yyyyMMddHH')
}

def buildVersion
if (isArtifactoryDeploy) {
    buildVersion = "${versionMajor}.${versionMinor}.${versionPatch}-${getDate()}-SNAPSHOT"
} else {
    buildVersion = "${versionMajor}.${versionMinor}.${versionPatch}"
}

sourceCompatibility = 1.8

group 'com.schibsted.protein'
version buildVersion

def intelliJVersion = '202.7319.50'
def sinceVersion = '172.0'

intellij {
    version intelliJVersion
    plugins = ['java', 'coverage']
    pluginName 'Protein - Kotlin code generator for Retrofit2 and RxJava2 based on Swagger'
}

publish {
    userOrg = 'schibstedspain'
    groupId = 'com.schibsted.spain.protein'
    artifactId = 'protein'
    publishVersion = "${buildVersion}"
    desc = 'Protein is a plugin for intelliJ that automates the build of "Agents" for Android in Kotlin. It generates the code for Retrofit based on a Swagger endpoint.'
    website = 'https://github.com/AdevintaSpain/protein'
}

publishPlugin {
    token project.hasProperty('intellijPublishToken') ? project.intellijPublishToken : System.getenv()['intellijPublishToken']
    channels 'stable'
}

patchPluginXml {
    version buildVersion
    sinceBuild sinceVersion
    untilBuild intelliJVersion
    pluginDescription new File('media/plugin_description.html').text
    changeNotes new File('CHANGELOG.md').text
}

twitterPlugin {
    consumerKey = System.getenv()['TWITTER_API_KEY']
    consumerSecret = System.getenv()['TWITTER_API_SECRET']
    accessToken = System.getenv()['TWITTER_ACCESS_TOKEN']
    accessTokenSecret = System.getenv()['TWITTER_ACCESS_TOKEN_SECRET']
    message = "New Protein version ${versionMajor}.${versionMinor}.${versionPatch} pushed to the IntelliJ Plugin repository. Protein is a Kotlin code generator for Retrofit 2 and RxJava 3 based on Swagger #kotlin #intelliJ #plugin https://plugins.jetbrains.com/plugin/10206-protein--android-component-builder"
}

tasks.withType(Javadoc).all {
    enabled = false
}

repositories {
    mavenCentral()
    jcenter()
    maven {
      url "https://dl.bintray.com/schibstedspain/maven"
    }
}

detekt {
    version = "1.0.0.RC7-3"
    profile("main") {
        input = "$projectDir/src/main/kotlin"
        config = "$projectDir/config/detekt/detekt-config.yml"
        output = "$projectDir/build/reports/detekt/detekt.xml"
        filters = ".*test.*,.*/resources/.*,.*/tmp/.*"
    }
}

ktlint {
    reporters {
        reporter "plain"
        reporter "checkstyle"
    }
}

dependencies {
    implementation "io.reactivex.rxjava3:rxjava:3.0.4"
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation ("com.schibsted.spain:retroswagger:1.1.0") {
        exclude group: 'org.slf4j'
    }

    def jacksonVersion = '2.8.4'
    implementation "com.fasterxml.jackson.core:jackson-core:$jacksonVersion"
    implementation "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$jacksonVersion"
    implementation "com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion"

    implementation 'com.google.code.gson:gson:2.8.0'

    def swaggerVersion = '1.5.16'
    implementation "io.swagger:swagger-annotations:$swaggerVersion"
    implementation("io.swagger:swagger-core:$swaggerVersion") {
        exclude group: 'org.slf4j'
    }
    implementation("io.swagger:swagger-models:$swaggerVersion") {
        exclude group: 'org.slf4j'
    }
    implementation("io.swagger:swagger-parser:1.0.33") {
        exclude group: 'org.slf4j'
    }

    implementation 'commons-io:commons-io:2.5'

    implementation("com.bugsnag:bugsnag:3.+") {
        exclude group: 'org.slf4j'
    }

    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.1.3-2'
    testImplementation 'org.mockito:mockito-core:1.10.19'
    testImplementation 'junit:junit:4.12'
    testImplementation "com.schibsted.spain:retroswagger:1.1.0"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
