/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

plugins {
  id 'java'
  id 'war'
}

description = 'Solr webapp'

configurations {
  war {}
  serverLib
  solrCore
  generatedJSClient
  generatedJSClientBundle
}

ext {
  jsClientWorkspace = layout.buildDirectory.dir("jsClientWorkspace").get()
  jsClientBuildDir = layout.buildDirectory.dir("jsClientBuild").get()
  jsClientBundleDir = layout.buildDirectory.dir("jsClientBundle").get()
}

dependencies {
  permitUnusedDeclared project(":solr:core")
  serverLib project(path: ":solr:server", configuration: "libExt")
  serverLib project(path: ":solr:server", configuration: "serverLib")
  solrCore project(":solr:core")
  implementation(configurations.solrCore - configurations.serverLib)

  generatedJSClient project(path: ":solr:api", configuration: "jsClient")
  generatedJSClientBundle files(jsClientBundleDir) {
    builtBy "finalizeJsBundleDir"
  }
}

task syncJSClientSourceCode(type: Sync) {
  group = 'Solr JS Client'
  from configurations.generatedJSClient

  into jsClientWorkspace

  // Keep the node modules, so that they don't need to be re-downloaded
  preserve {
    include "node_modules/**"
  }
}

task jsClientDownloadDeps(type: NpmTask) {
  group = 'Solr JS Client'
  dependsOn tasks.syncJSClientSourceCode

  args = ["install"]
  workingDir = file(jsClientWorkspace)

  inputs.dir("${jsClientWorkspace}/src")
  inputs.file("${jsClientWorkspace}/package.json")
  outputs.dir("${jsClientWorkspace}/node_modules")
}

task jsClientBuild(type: NpmTask) {
  group = 'Solr JS Client'
  dependsOn tasks.jsClientDownloadDeps

  args = ["run", "build"]
  workingDir = file(jsClientWorkspace)

  inputs.dir("${jsClientWorkspace}/src")
  inputs.file("${jsClientWorkspace}/package.json")
  inputs.dir("${jsClientWorkspace}/node_modules")
  outputs.dir("${jsClientWorkspace}/dist")
}

task downloadBrowserify(type: NpmTask) {
  group = 'Build Dependency Download'
  args = ["install", "browserify@${libs.versions.browserify.get()}"]

  inputs.property("browserify version", libs.versions.browserify.get())
  outputs.dir("${nodeProjectDir}/node_modules/browserify")
}

if (gradle.ext.withUiModule) {
  tasks.register("generateUiDevFiles") {
    description = "Generate new UI for development and add files to outputs for later referencing."
    group = "build"
    dependsOn project(":solr:ui").tasks.wasmJsBrowserDevelopmentExecutableDistribution
    outputs.dir(project(":solr:ui").file("build/dist/wasmJs/developmentExecutable/"))
  }
}

if (gradle.ext.withUiModule) {
  tasks.register("generateUiProdFiles") {
    description = "Generate new UI for production and add files to outputs for later referencing"
    group = "build"

    // Production files are smaller in size but have longer compile times.
    dependsOn project(":solr:ui").tasks.wasmJsBrowserDistribution
    outputs.dir(project(":solr:ui").file("build/dist/wasmJs/productionExecutable/"))
  }
}

task generateJsClientBundle(type: NpxTask) {
  group = 'Solr JS Client'
  dependsOn tasks.downloadBrowserify
  dependsOn tasks.jsClientBuild

  command = 'browserify'
  args = ['dist/index.js', '-s', 'solrApi', '-o', "${jsClientBuildDir}/index.js"]
  workingDir = file(jsClientWorkspace)

  inputs.dir(jsClientWorkspace)
  inputs.property("browserify version", libs.versions.browserify.get())

  outputs.file("${jsClientBuildDir}/index.js")
}

task finalizeJsBundleDir(type: Sync) {
  group = 'Solr JS Client'

  from configurations.generatedJSClient {
    include "README.md"
    include "docs/**"
  }

  from tasks.generateJsClientBundle {
    include "index.js"
  }

  into jsClientBundleDir
}

war {
  webAppDirName = "web"

  from (configurations.generatedJSClientBundle, {
    into "libs/solr"
  })

  // Include new Admin UI files in webapp
  // By default, we always build dev that does not optimize generated files for release to reduce build times.
  if (gradle.ext.withUiModule) {
    if (rootProject.ext.development) {
      from (tasks.generateUiDevFiles) {
        into "ui"
      }
    } else {
      from (tasks.generateUiProdFiles) {
        into "ui"
      }
    }
  }
}

// Expose 'war' archive as an artifact so that it can be packaged in the distribution.
artifacts {
  war tasks.war
}
