buildscript {
    dependencies {
        classpath brazilGradle.tool('BrazilGradleQualityDefaults')
    }
}

plugins {
    id 'brazil-quality-defaults'
    id "java"
    id "jacoco"
    id 'com.github.johnrengelman.shadow'
}

def resource_name_with_dashes = 'aws-iotwireless-wirelessdeviceimporttask'
def schema_file = "${resource_name_with_dashes}.json"

description 'CloudFormation Resource Provider - AWS::IoTWireless::WirelessDeviceImportTask'

// Include RPDK generated source by `cfn generate`
sourceSets.main.java.srcDirs += ['target/generated-sources/rpdk']

// Include schema into jar
sourceSets.main.resources {
    srcDir 'target/schema'
    include schema_file
}

dependencies {
    compile brazilGradle.build()
    testCompile brazilGradle.testbuild()
}

test {
    useJUnitPlatform()
    environment 'AWS_REGION', 'us-west-2'
}

def cfn_command = "${brazilGradle.path('[AWSCloudFormationRPDKJavaPluginTool]run.runtimefarm')}/bin/cfn"

// Generate RPDK wrapper source before compile
task rpdkGenerateSource(type: Exec) {
    workingDir '.'
    executable = cfn_command
    args = ['generate']
}

task copySchema(type: Copy) {
    from(".") {
        include schema_file
    }
    into "target/schema"
}

compileJava.dependsOn(rpdkGenerateSource, copySchema)

// Run the tests by default
release.dependsOn(check)

// Build a shadow jar for resource provider
shadowJar {
    // cfn-cli requires the artifact under target and suffixed with SNAPSHOT.jar
    classifier = null
    baseName = "${resource_name_with_dashes}"
    destinationDir = file('./target')
}

// Use `cfn` to build a RPDK package include resource provider shadow jar, schema etc.
task cfnPackage(type: Exec) {
    dependsOn shadowJar
    workingDir '.'
    executable = cfn_command
    args = ['submit', '--dry-run']
}

// Inject internal files here for further canary and FAS configuration
task rpdkPackage(type: Zip) {
    dependsOn cfnPackage
    from zipTree("${resource_name_with_dashes}.zip")
    // Inject internal files
    from ("${rootDir}/${resource_name_with_dashes}") {
        include "canary-bundle/**/*"
        include "settings.internal.json"
        include "contract-tests-artifacts/*"
    }
    from('contract-tests-artifacts/pre-script/buildspec.yml') {
        into('contract-tests-artifacts/pre-script')
    }
    from('contract-tests-artifacts/post-script/buildspec.yml') {
        into('contract-tests-artifacts/post-script')
    }
    destinationDir = file('./build/rpdk')
    archiveName("${resource_name_with_dashes}.zip")
}

assemble.dependsOn(rpdkPackage)

// Delete target folder for clean
clean.doLast {
    file('./target').deleteDir()
    file("${resource_name_with_dashes}.zip").delete()
}