/*
 * 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.
 */

// Add "help" tasks which display plain text files under 'help' folder.

configure(rootProject) {
  def helpFiles = [
      ["Workflow", "dev-docs/gradle-help/workflow.txt", "Typical workflow commands."],
      ["Ant", "dev-docs/gradle-help/ant.txt", "Ant-gradle migration help."],
      ["Tests", "dev-docs/gradle-help/tests.txt", "Tests, filtering, beasting, etc."],
      ["Formatting", "dev-docs/gradle-help/formatting.txt", "Code formatting conventions."],
      ["Deps", "dev-docs/gradle-help/dependencies.txt", "Declaring, inspecting and excluding dependencies."],
      ["ForbiddenApis", "dev-docs/gradle-help/forbiddenApis.txt", "How to add/apply rules for forbidden APIs."],
      ["LocalSettings", "dev-docs/gradle-help/localSettings.txt", "Local settings, overrides and build performance tweaks."],
      ["Git", "dev-docs/gradle-help/git.txt", "Git assistance and guides."],
      ["ValidateLogCalls", "dev-docs/gradle-help/validateLogCalls.txt", "How to use logging calls efficiently."],
      ["Publishing", "dev-docs/gradle-help/publishing.txt", "Release publishing publishing, signing, etc."],
      ["Docker", "dev-docs/gradle-help/docker.txt", "Building Solr Docker images."],
  ]

  helpFiles.each { section, path, sectionInfo ->
    task "help${section}" {
      group = 'Help (developer guides and hints)'
      description = sectionInfo
      doFirst {
        println "\n" + rootProject.file(path).getText("UTF-8")
      }
    }
  }

  help {
    doLast {
      println ""
      println "This is the Solr gradle build. See some guidelines, "
      println "ant-equivalent commands, etc. under help/*; or type:"
      println ""
      helpFiles.each { section, path, sectionInfo ->
        println String.format(Locale.ROOT,
            "  gradlew :help%-16s # %s", section, sectionInfo)
      }
      println ""
      println "For the impatient, build the project with 'gradlew assemble' or 'gradlew dev' "
    }
  }

  task allHelpFilesExit() {
    doFirst {
      helpFiles.each { section, path, sectionInfo ->
        if (!rootProject.file(path).exists()) {
          throw new GradleException("Help file missing: ${path} (correct help.gradle)")
        }
      }
    }
  }

  check.dependsOn allHelpFilesExit
}
