import org.apache.tools.ant.taskdefs.condition.Os
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.3.61"
    id("org.springframework.boot") version "2.2.4.RELEASE" apply false
    id("io.spring.dependency-management") version "1.0.9.RELEASE" apply false
    id("com.moowork.node") version "1.3.1" apply false
    kotlin("plugin.spring") version "1.3.61" apply false
    kotlin("plugin.jpa") version "1.3.61" apply false
}

allprojects {
    group = "com.rera"
    version = "0.0.1-SNAPSHOT"
    description = "My Solar Scan project"

    repositories {
        mavenCentral()
    }
    tasks.withType<Test> {
        useJUnitPlatform()
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "1.8"
        }
    }
}

sourceSets {
    main {
        resources {
            setSrcDirs(listOf("$projectDir/frontend/build"))
        }
    }
}



tasks.register<Delete>("cleanReactResourcesDir") {
    delete("$projectDir/backend/src/main/resources/static")
}

tasks.register<Exec>("installReact") {
    workingDir = File("$projectDir/frontend")
    inputs.dir("$projectDir/frontend")
    commandLine = if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        listOf("cmd", "/c", "npm audit fix")
        listOf("cmd", "/c", "npm install")
    } else {
        listOf("sh", "-c", "npm audit fix")
        listOf("sh", "-c", "npm install")
    }
}

tasks.register<Exec>("buildReact") {
    workingDir = File("$projectDir/frontend")
    inputs.dir("$projectDir/frontend")
    commandLine = if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        listOf("cmd", "/c", "npm run-script build")
    } else {
        listOf("sh", "-c", "npm run-script build")
    }
    dependsOn("installReact")
}

tasks.register<Copy>("copyReactResources") {
    from("$projectDir/frontend/build")
    into("$projectDir/backend/src/main/resources/static")
    dependsOn("cleanReactResourcesDir", "buildReact")
}

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "com.rera.mss.MySolarScanApplicationKt"
    }
    configurations["compileClasspath"].forEach { file: File ->
        from(zipTree(file.absoluteFile))
    }
}
