// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
plugins {
  id 'application'
  id "maven-publish"
  id "com.diffplug.spotless" version "7.0.2"
  id 'com.palantir.git-version' version '3.1.0'
  id 'net.ltgt.errorprone' version "4.1.0"
}
ext {
  packageName = "clickstream-analytics-java"
  javaPackagePath = "com.google.cloud.dataflow.solutions.clickstream_analytics"
  appName = "ClickStreamPubSubToBq"
  appVersion = "${gitVersion()}-SNAPSHOT"
  beamVersion = "2.63.0"
  slf4jVersion = "1.7.36"
  junitVersion = "4.13.2"
  hamcrestVersion = "3.0"
  googleJavaFormat = '1.24.0'
  errorProneCoreVersion = '2.26.1'

}
repositories {
  mavenCentral()
  maven {  // Apache Snapshots repository
    url "https://repository.apache.org/content/repositories/snapshots/"
  }
}
application {
  mainClass = "${javaPackagePath}.ClickstreamPubSubToBq"
  version = appVersion
}
test {
  // JUnit 4.
  useJUnit()
  dependsOn cleanTest
  testLogging.showStandardStreams = true
}
compileJava {
  options.compilerArgs.addAll(['-Xlint:deprecation', '-Xlint:unchecked'])
}
run {
  if (project.hasProperty('args')) {
    args project.args.split('\\s')
  }
}
dependencies {
  // App dependencies.
  implementation "org.apache.beam:beam-sdks-java-core:${beamVersion}"
  runtimeOnly "org.apache.beam:beam-runners-direct-java:${beamVersion}"
  implementation "org.apache.beam:beam-runners-google-cloud-dataflow-java:${beamVersion}"
  implementation "org.apache.beam:beam-sdks-java-io-google-cloud-platform:${beamVersion}"
  // Tests dependencies.
  testImplementation "junit:junit:${junitVersion}"
  testImplementation "org.hamcrest:hamcrest:${hamcrestVersion}"
  testImplementation "org.apache.beam:beam-sdks-java-test-utils:${beamVersion}"
  implementation 'org.checkerframework:checker-qual:3.49.1'
  errorprone "com.google.errorprone:error_prone_core:${errorProneCoreVersion}"

  // Google Java format for Gradle
  implementation "com.google.googlejavaformat:google-java-format:${googleJavaFormat}"
}

// Package a self-contained jar file.
jar {
  archiveBaseName = packageName
  destinationDirectory = file('build')
  manifest {
    attributes 'Main-Class': "${javaPackagePath}.ClickstreamPubSubToBq"
  }
  exclude 'META-INF/*.SF'
  exclude 'META-INF/*.DSA'
  exclude 'META-INF/*.RSA'
  duplicatesStrategy = DuplicatesStrategy.INCLUDE
  from {
    configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
  }
  zip64 true
}
spotless {
  format 'misc', {
    // define the files to apply `misc` to
    target '*.gradle', '*.md', '.gitignore'
    // define the steps to apply to those files
    trimTrailingWhitespace()
    leadingTabsToSpaces(2)
    endWithNewline()
  }
  java {
    target project.fileTree(project.rootDir) {
      include '**/*.java'
      exclude 'build/*'
    }
    // apply a specific flavor of google-java-format
    googleJavaFormat("${googleJavaFormat}").aosp().reflowLongStrings()
    // fix formatting of type annotations
    formatAnnotations()
    // make sure every file has the following copyright header.
    licenseHeader '''/*
 * Copyright $YEAR Google.
 *
 * 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.
 */
'''
  }
}
