// Copyright 2020 Google LLC
//
// 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.

// [START functions_build_gradle]
apply plugin: 'java'

repositories {
  jcenter()
  mavenCentral()
}

configurations {
  invoker
}

dependencies {
  // Every function needs this dependency to get the Functions Framework API.
  implementation 'com.google.cloud.functions:functions-framework-api:1.1.0'
  invoker 'com.google.cloud.functions.invoker:java-function-invoker:1.3.1'

  // Function implementations can have additional dependencies like this.
  implementation 'com.google.code.gson:gson:2.10.1'
  implementation 'io.github.resilience4j:resilience4j-retry:1.7.1'

  // These dependencies are only used by the tests.
  testImplementation 'com.google.cloud.functions:functions-framework-api:1.1.0'
  testImplementation 'junit:junit:4.13.2'
  testImplementation 'com.google.truth:truth:1.4.0'
  testImplementation 'org.mockito:mockito-core:5.10.0'
}

jar {
  // There must be a jar {} section, though the archiveBaseName does not have to be 'function'.
  archiveBaseName = 'function'
}

// Run your function locally:
// ./gradlew runFunction -Prun.functionTarget=functions.HelloHttp -Prun.port=8080
tasks.register("runFunction", JavaExec) {
  main = 'com.google.cloud.functions.invoker.runner.Invoker'
  classpath(configurations.invoker)
  inputs.files(configurations.runtimeClasspath, sourceSets.main.output)
  args(
          '--target', project.findProperty('run.functionTarget'),
          '--port', project.findProperty('run.port') ?: 8080
  )
  doFirst {
      args('--classpath', files(configurations.runtimeClasspath, sourceSets.main.output).asPath)
  }
}
// [END functions_build_gradle]
