/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

buildscript {
  repositories {
    mavenCentral()
    google()
    mavenLocal()
    jcenter()
  }
  dependencies {
    classpath plugs.kotlin
    classpath plugs.publish
    classpath plugs.dokka
  }
}

apply plugin: 'groovy'
apply plugin: 'kotlin'
apply plugin: "java-gradle-plugin"
apply plugin: 'com.vanniktech.maven.publish'

final GENERATED_PACKAGE_NAME = "com.facebook.testing.screenshot.generated"
final GENERATED_BUILD_DIR =
    new File(
        projectDir.absolutePath,
        "src/generated/kotlin/${GENERATED_PACKAGE_NAME.replace('.', '/')}")

task generateBuildConfig {
  doLast {
    GENERATED_BUILD_DIR.deleteDir()
    GENERATED_BUILD_DIR.mkdirs()

    final className = "ScreenshotTestBuildConfig"
    final configClass = new File(GENERATED_BUILD_DIR, "${className}.kt")
    BufferedWriter writer = configClass.newWriter()
    try {
      writer.writeLine("package $GENERATED_PACKAGE_NAME")
      writer.writeLine("object $className { val VERSION = \"${project.version.toString()}\" }")
      writer.flush()
    } finally {
      writer.close()
    }
  }
}

dependencies {
  implementation deps.kotlinStdlib
  compileOnly plugs.agp
  
  testImplementation deps.junit
  testImplementation plugs.agp
  testImplementation deps.mockito
  testImplementation deps.hamcrest
}

group = 'com.facebook.testing.screenshot'

compileKotlin {
  dependsOn generateBuildConfig
  sourceSets {
    main {
      kotlin.srcDirs += 'src/generated'
    }
  }
}

compileJava {
  sourceSets {
    main {
      resources.srcDirs 'src/py'
      resources {
        exclude '**/*.pyc'
        exclude '**/test_*.py'
        exclude '**/fixtures/**'
      }
    }
  }
}

clean {
  delete GENERATED_BUILD_DIR
}

task pyTests(type: Exec) {
  workingDir file('./src/py')
  commandLine 'python3', '-m', 'unittest', 'discover'
}

task py3Tests(type: Exec) {
  workingDir file('./src/py')
  commandLine 'python3', '-m', 'unittest', 'discover'
}


