apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

def mini = getEnvValue("mini", 'false')
def commitTag = getEnvValue("commitTag", 'false')

buildscript {

    repositories {
        mavenCentral()
        jcenter()
        google()
    }

    dependencies {
        classpath "com.taobao.android:atlasplugin:3.0.1-rc55"
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        classpath "com.github.dcendents:android-maven-gradle-plugin:1.4.1"
    }
}
repositories {
    mavenCentral()
    jcenter()

}


group = 'com.taobao.android'
description = """atlas_core"""
String postFix = mini=='true' ? "-mini" : "";


version "5.1.0.9-rc26"


String getEnvValue(key, defValue) {
    def val = System.getProperty(key);
    if (null != val) {
        return val;
    }
    val = System.getenv(key);
    if (null != val) {
        return val;
    }
    return defValue;
}


sourceCompatibility = 1.7
targetCompatibility = 1.7


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    provided ('com.android.support:support-v4:25.3.1'){
        transitive = true
    }
    provided 'com.android.support:support-annotations:24.1.1'

}

tasks.whenTaskAdded {task ->

    if (task.name.startsWith("compile") && task.name.endsWith("JavaWithJavac")) {
        task.doLast {
            new File(task.getDestinationDir(),"android/app/Instrumentation.class").delete()
            new File(task.getDestinationDir(),"android/app/Instrumentation.class").delete();
            new File(task.getDestinationDir(),"android/app/Instrumentation\$ActivityMonitor.class").delete();
            new File(task.getDestinationDir(),"android/app/Instrumentation\$ActivityResult.class").delete();
            new File(task.getDestinationDir(),"android/app/PreVerifier.class").delete();
            new File(task.getDestinationDir(),"android/app/IActivityManager.class").delete();
            new File(task.getDestinationDir(),"android/app/IActivityManager\$1.class").delete();
            new File(task.getDestinationDir(),"android/app/IActivityManager\$ContentProviderHolder.class").delete();
            new File(task.getDestinationDir(),"android/app/IActivityManager\$ContentProviderHolder\$1.class").delete();
            new File(task.getDestinationDir(),"android/app/IActivityManager\$1.class").delete();
            new File(task.getDestinationDir(), "android/app/ContentProviderHolder.class").delete();
            new File(task.getDestinationDir(), "android/app/ContentProviderHolder\$1.class").delete();
            new File(task.getDestinationDir(),"android/app/IServiceConnection.class").delete();
            new File(task.getDestinationDir(),"android/app/IServiceConnection\$Stub.class").delete();
            new File(task.getDestinationDir(),"android/app/IServiceConnection\$Stub\$Proxy.class").delete();
            new File(task.getDestinationDir(),"android/content/IContentProvider.class").delete();
            new File(task.getDestinationDir(),"android/databinding/DataBindingComponent.class").delete();
            new File(task.getDestinationDir(),"android/databinding/ViewDataBinding.class").delete();
            new File(task.getDestinationDir(),"android/taobao/atlas/framework/FrameworkProperties.class").delete();
            new File(task.getDestinationDir(),"android/taobao/atlas/bundleInfo/AtlasBundleInfoGenerator.class").delete();
        }
    } else if(task.name.equals('mergeReleaseJniLibFolders')){
        task.doLast {
            if(mini=='true') {
                new File(project.buildDir, "intermediates/jniLibs/release/armeabi-v7a").deleteDir()
                new File(project.buildDir, "intermediates/jniLibs/release/mips64").deleteDir()
                new File(project.buildDir, "intermediates/jniLibs/release/arm64-v8a").deleteDir()
                new File(project.buildDir, "intermediates/jniLibs/release/mips").deleteDir()
                new File(project.buildDir, "intermediates/jniLibs/release/x86").deleteDir()
                new File(project.buildDir, "intermediates/jniLibs/release/x86_64").deleteDir()
            }

        }
    }

//    if(task.name == "bintrayUpload"){
//        task.doFirst{
//            println "tag has been created!"
//        }
//
//        task.doLast {
//            String newTag = "v${version}"
//            println "new Tag is ${newTag}"
//            "git tag v${version}".execute()
//            "git push origin v${version}".execute()
//            println "tag v${version} has been created!"
//        }
//    }

}


project.gradle.buildFinished{
    if(commitTag=='true') {
        "git tag v${version}".execute()
        "git push origin v${version}".execute()
        "git push origin v${version}".execute()
        println "tag v${version} has been created!"
    }
}





android {
    compileSdkVersion 28
    buildToolsVersion '26.0.2'
    lintOptions {
        abortOnError false
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }



    defaultConfig {
        consumerProguardFiles 'proguard.cfg'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        release {
            if(mini=='true') {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
            }
        }
    }

}
def siteUrl = 'https://github.com/alibaba/atlas'
// 项目的主页
def gitUrl = 'https://github.com/alibaba/atlas'
// Git仓库的url

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                // Add your description here
                name 'atlas' //项目描述
                groupId 'com.taobao.android'
                artifactId 'atlas_core'
                url siteUrl
                // Set your license
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'alibabaatlas' //填写的一些基本信息
                        name 'atlas-framework'
                        email 'alibabaatlasframework@gmail.com'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}

//task sourcesJar(type: Jar) {
//    from('src/main/java') {
//        include '**'
//    }
//    classifier = 'sources'
//}

//artifacts {
//    archives sourcesJar
//}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
    user = project.hasProperty('bintray.user') ? project.getProperty('bintray.user') : properties.getProperty("bintray.user")
    key = project.hasProperty('bintray.apikey') ? project.getProperty('bintray.apikey') : properties.getProperty("bintray.apikey")
    configurations = ['archives']
    pkg {
        repo = "maven"
        name = "atlas" //发布到JCenter上的项目名字
        websiteUrl = "https://github.com/alibaba/atlas"
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}
