apply plugin: 'io.github.gradle-nexus.publish-plugin'
apply from: 'config.gradle'
apply from: 'scripts/publish-root.gradle'

buildscript {
    apply from: 'config.gradle'

    repositories {
        google()
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$versions.gradlePluginVersion"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlinVersion"
        classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

def outputDir = "android/build/outputs"

task clean(type: Delete) {
    dependsOn(":GodotOpenXR:clean")

    delete(rootProject.buildDir)

    // Delete the contents of the outputs directory
    delete(outputDir)
}

/**
 * Generate the plugin binaries.
 */
task generatePluginBinary(type: Copy) {
    dependsOn(":GodotOpenXR:assembleDebug")
    dependsOn(":GodotOpenXR:assembleRelease")
    from("android/build/outputs/aar")
    into("$outputDir/pluginBin")
}

/**
 * Generate the plugin native shared libraries.
 */
task generateSharedLibs(type: Copy) {
    dependsOn(":GodotOpenXR:externalNativeBuildFullDebug")
    dependsOn(":GodotOpenXR:externalNativeBuildFullRelease")

    // Specify the base directory. All following 'into' targets will be relative
    // to this directory.
    into("$outputDir/sharedLibs")

    // Copy the generated debug shared libs into the outputs directory
    from("android/build/intermediates/cmake/fullDebug/obj") {
        into("debug")
    }
    from("android/src/fullDebug/jniLibs") {
        into("debug")
    }

    // Copy the generated release shared libs into the outputs directory
    from("android/build/intermediates/cmake/fullRelease/obj") {
        into("release")
    }
    from("android/src/fullRelease/jniLibs") {
        into("release")
    }

    // Copy the 64bit generated debug shared libs into our plugin
    // Note that our CI will distribute the runtime versions
    from("android/build/intermediates/cmake/fullDebug/obj/arm64-v8a") {
        into("../../../../demo/addons/godot-openxr/bin/android")
    }
    from("android/src/fullDebug/jniLibs/arm64-v8a") {
        into("../../../../demo/addons/godot-openxr/bin/android")
    }
}
