/*
 * Copyright 2019 Fitbit, Inc. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.2'
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.17.2"
    }
}

apply plugin: 'com.android.library'
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'

if(group == null) {
    group = 'com.github.Fitbit'
}


def releaseTag = System.getenv("RELEASE_TAG")
if (releaseTag != null && !releaseTag.isEmpty()) {
    if (releaseTag.startsWith("v")) {
        releaseTag = releaseTag.substring(1)
    }
    version = releaseTag
    project.properties.put("release.version", releaseTag)

    println("Releasing with version " + version)
}

def artifactPath = "$buildDir/outputs/aar"


android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29
        multiDexEnabled true
        versionName version
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildTypes {
        release {
            minifyEnabled false
            consumerProguardFiles 'proguard-rules.pro'
            buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
        }
        debug {
            testCoverageEnabled true
            buildConfigField("String","VERSION_NAME","\"${defaultConfig.versionName}\"")
        }
    }
    testOptions {
        unitTests {
            returnDefaultValues = true
            includeAndroidResources = true
        }
    }
}

allprojects {
    repositories {
        jcenter()
        google()
        maven { url 'https://jitpack.io' }
    }
}

ext {
    MULTIDEX_VERSION = '2.0.1'
    APP_COMPAT_VERSION = '1.1.0'
    TIMBER_VERSION = '4.7.1'
    JUNIT_VERSION = '4.12'
    TEST_RUNNER_VERSION = '1.1.0'
    MOCKITO_VERSION = '3.6.0'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "androidx.multidex:multidex:$MULTIDEX_VERSION"
    implementation "androidx.appcompat:appcompat:$APP_COMPAT_VERSION"
    implementation "com.jakewharton.timber:timber:$TIMBER_VERSION"
    testImplementation "junit:junit:$JUNIT_VERSION"
    testImplementation "androidx.test:runner:$TEST_RUNNER_VERSION"
    testImplementation "androidx.test:rules:$TEST_RUNNER_VERSION"
    testImplementation "org.mockito:mockito-inline:$MOCKITO_VERSION"
    testImplementation "org.mockito:mockito-core:$MOCKITO_VERSION"
    testImplementation 'org.robolectric:robolectric:4.4'
    testImplementation 'androidx.test:core:1.3.0'
    androidTestImplementation "junit:junit:$JUNIT_VERSION"
    androidTestImplementation "androidx.test:runner:$TEST_RUNNER_VERSION"
    androidTestImplementation "androidx.test:rules:$TEST_RUNNER_VERSION"
    androidTestImplementation("org.mockito:mockito-android:$MOCKITO_VERSION")
    androidTestImplementation 'com.github.tmurakami:dexopener:2.0.0'
}

task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier "sources"
}

android.libraryVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}.aar"
    }
}


publishing {
    publications {
        release (MavenPublication) {
            from components.findByName("androidRelease")
            groupId  group
            artifactId 'bitgatt'
            version "$version-release"
            artifact("$artifactPath/release.aar")  {
                builtBy tasks.getByName("build")
            }
        }
    }
}
