apply plugin: 'com.android.application'
apply plugin: 'findbugs'
apply plugin: 'pmd'

def ABORT_ON_CHECK_FAILURE=false

tasks.withType(Test) {
    systemProperty 'MiFirmwareDir', System.getProperty('MiFirmwareDir', null)
    systemProperty 'logback.configurationFile', System.getProperty('user.dir', null) + '/app/src/main/assets/logback.xml'
    systemProperty 'GB_LOGFILES_DIR', java.nio.file.Files.createTempDirectory('gblog').toString();
}

android {
    compileOptions {
        // for KitKat
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "nodomain.freeyourgadget.gadgetbridge"
        minSdkVersion 19
        targetSdkVersion 23

        // note: always bump BOTH versionCode and versionName!
        versionName "0.12.0"
        versionCode 59
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        abortOnError ABORT_ON_CHECK_FAILURE
        lintConfig file("${project.rootDir}/config/lint/lint.xml")

// if true, generate an HTML report (with issue explanations, sourcecode, etc)
        htmlReport true
// optional path to report (default will be lint-results.html in the builddir)
        htmlOutput file("$project.buildDir/reports/lint/lint.html")
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }
}

pmd {
    toolVersion = '5.5.1'
}

dependencies {
//    testCompile 'ch.qos.logback:logback-classic:1.1.3'
//    testCompile 'ch.qos.logback:logback-core:1.1.3'
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.9.5"
    testCompile "org.robolectric:robolectric:3.1.2"

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.android.support:design:23.3.0'
    compile 'com.github.tony19:logback-android-classic:1.1.1-4'
    compile 'org.slf4j:slf4j-api:1.7.7'
    compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
    compile 'com.github.pfichtner:durationformatter:0.1.1'
    compile 'de.cketti.library.changelog:ckchangelog:1.2.2'
    compile 'net.e175.klaus:solarpositioning:0.0.9'
    compile 'com.github.freeyourgadget:greendao:1998d7cd2d21f662c6044f6ccf3b3a251bbad341'
    compile 'com.github.woxthebox:draglistview:1.2.6'

//    compile project(":DaoCore")
}

preBuild.dependsOn(":GBDaoGenerator:genSources")
gradle.beforeProject {
    preBuild.dependsOn(":GBDaoGenerator:genSources")
}

check.dependsOn 'findbugs', 'pmd', 'lint'

task pmd(type: Pmd) {
    ruleSetFiles = files("${project.rootDir}/config/pmd/pmd-ruleset.xml")
    ignoreFailures = !ABORT_ON_CHECK_FAILURE
    ruleSets = [
            'java-android',
            'java-basic',
            'java-braces',
            'java-clone',
            'java-codesize',
            'java-controversial',
            'java-coupling',
            'java-design',
            'java-empty',
            'java-finalizers',
            'java-imports',
            'java-junit',
            'java-optimizations',
            'java-strictexception',
            'java-strings',
            'java-sunsecure',
            'java-typeresolution',
            'java-unnecessary',
            'java-unusedcode'
    ]

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    reports {
        xml.enabled = false
        html.enabled = true
        xml {
            destination "$project.buildDir/reports/pmd/pmd.xml"
        }
        html {
            destination "$project.buildDir/reports/pmd/pmd.html"
        }
    }
}

task findbugs(type: FindBugs) {
    ignoreFailures = !ABORT_ON_CHECK_FAILURE
    effort = "default"
    reportLevel = "medium"
    excludeFilter = new File("${project.rootDir}/config/findbugs/findbugs-filter.xml")
    classes = files("${project.rootDir}/app/build/intermediates/classes")
    source = fileTree('src/main/java/')
    classpath = files()
    reports {
        xml.enabled = false
        html.enabled = true
        xml {
            destination "$project.buildDir/reports/findbugs/findbugs-output.xml"
        }
        html {
            destination "$project.buildDir/reports/findbugs/findbugs-output.html"
        }
    }
}