Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

3.0 KB
Raw Blame
/*
 * 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.
 */

import org.gradle.process.ExecOperations
import javax.inject.Inject

def resources = scriptResources(buildscript)

configure(project(':solr:documentation')) {
  task changesToHtml(type: ChangesToHtmlTask) {
    siteDir = resources
    script = file("${resources}/changes2html.py")
  }

  // Make the rendered HTML of changes available as a separate
  // artifact for the distribution.
  configurations {
    changesHtml
  }

  artifacts {
    changesHtml changesToHtml.targetDir, {
      builtBy changesToHtml
    }
  }
}

// compile CHANGELOG.md into an html file
abstract class ChangesToHtmlTask extends DefaultTask {

  @Inject
  abstract ExecOperations getExecOperations()

  @Internal
  Project productProject = project.parent

  @Internal
  String productName = productProject.name

  @InputFile
  File changesFile = project.rootProject.file('CHANGELOG.md')

  @InputDirectory
  File siteDir

  @OutputDirectory
  final DirectoryProperty targetDir = project.objects.directoryProperty()
      .fileProvider(project.providers.provider {project.file("${project.docroot}/changes")})

  @InputFile
  def script

  def toHtml() {
    def output = new ByteArrayOutputStream()

    // Check if Python is available
    if (!pythonExists()) {
      logger.warn("WARNING: Python is not installed, skipping creating Changes.html")
      return
    }

    def result = execOperations.exec {
      executable "python3"
      standardOutput project.file("${targetDir.get().getAsFile()}/Changes.html").newOutputStream()
      errorOutput = output
      ignoreExitValue = true

      args += [
          script.toString(),
          changesFile.toString()
      ]
    }

    if (result.getExitValue() != 0) {
      throw new GradleException("Changes generation failed:\n${output}")
    }
  }

  @TaskAction
  def convert() {
    project.mkdir targetDir
    if (changesFile.exists()) {
      toHtml()
      project.copy {
        from siteDir
        into targetDir
        include "*.css"
      }
    } else {
      throw new GradleException("Changes file ${changesFile} not found.")
    }
  }

  def pythonExists() {
    try {
      def process = "python3 --version".execute()
      process.waitFor()
      return process.exitValue() == 0
    } catch (Exception e) {
      return false
    }
  }
}