plugins {
    id 'java'
    id 'java-library'
    id 'application'
    id 'idea'

    // Creates fat JAR
    id 'com.github.johnrengelman.shadow' version '7.0.0'

    // Adds dependencyUpdates task
    id 'com.github.ben-manes.versions' version '0.39.0'
}

distZip.enabled = shadowDistZip.enabled = false
distTar.enabled = shadowDistTar.enabled = false

// Required by shadow but not necessary
mainClassName = 'com.amazonaws.greengrass.cddskeleton.App'

group = 'CDDSkeletonJava'
version = '1.0-SNAPSHOT'
def temp = "temp"
def tempName = "$group-$version-$temp" + ".jar"

description = """"""

sourceCompatibility = 1.8
targetCompatibility = 1.8

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

def gradleDependencyVersion = '6.8.1'

wrapper {
    gradleVersion = gradleDependencyVersion
    distributionType = Wrapper.DistributionType.ALL
}

repositories {
    mavenCentral()
    maven { url "https://plugins.gradle.org/m2/" }
    maven { url 'https://jitpack.io' }
}

// Guidance from: https://stackoverflow.com/questions/23446233/compile-jar-from-url-in-gradle
def githubJar = { organization, module, revision, name ->
    File file = new File("$buildDir/libs/${name}.jar")
    file.parentFile.mkdirs()
    if (!file.exists()) {
        def url = "https://github.com/$organization/$module/raw/v$revision/sdk/GreengrassJavaSDK.jar"
        new URL(url).withInputStream { downloadStream ->
            file.withOutputStream { fileOut ->
                fileOut << downloadStream
            }
        }
    }
    files(file.absolutePath)
}

def cddVersion = '0.8.74'
def gsonVersion = '2.8.7'
def slf4jVersion = '2.0.0-alpha1'
def jcabiVersion = '0.19.0'
def jacksonVersion = '2.11.3'
def awsLambdaJavaCoreVersion = '1.2.1'
def awsGreengrassCoreSdkJava = '1.4.1'
def junitVersion = '4.13.2'
def daggerVersion = '2.37'

dependencies {
    implementation githubJar('aws', 'aws-greengrass-core-sdk-java', awsGreengrassCoreSdkJava, 'GreengrassJavaSDK')
    implementation "com.github.aws-samples:aws-greengrass-lambda-functions:$cddVersion"

    // Dagger code generation
    annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"

    // Dependency injection with Dagger
    implementation "com.google.dagger:dagger:$daggerVersion"

    implementation "com.google.code.gson:gson:$gsonVersion"
    implementation "org.slf4j:slf4j-log4j12:$slf4jVersion"
    implementation "com.jcabi:jcabi-log:$jcabiVersion"
    implementation "com.fasterxml.jackson.core:jackson-core:$jacksonVersion"
    implementation "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
    implementation "com.amazonaws:aws-lambda-java-core:$awsLambdaJavaCoreVersion"

    testImplementation "junit:junit:$junitVersion"
}

// Required to stage the shadow JAR without the Greengrass library
shadowJar {
    archiveClassifier = temp
}

// Required to finalize the shadow JAR with the Greengrass library
task finalizeShadowJar(type: Jar) {
    archiveClassifier = "all"
    from zipTree("$buildDir/libs/$tempName")
    from(file("$buildDir/libs/GreengrassJavaSDK.jar")) { into('lib') }
    manifest { attributes('Main-Class': mainClassName) }
}

// Required to trigger finalization of the shadow JAR
shadowJar.finalizedBy finalizeShadowJar