eerohele / saxon-gradle

A Gradle plugin for running XSLT transformations with Saxon
MIT License
16 stars 6 forks source link

Handle missing stylesheets #19

Closed ndw closed 3 years ago

ndw commented 3 years ago

This PR fixes a bug where the task is too aggressive about parsing stylesheets during the configuration phase.

My use case is a complex build (xslTNG) where some stylesheets are downloaded during the build. Attempting to use these stylesheets with SaxonXsltTask always fails because the stylesheets don't exist during the configuration phase.

I've tried to make the changes minimal.

(I'm going to publish my fork of this project in order to quickly unblock myself, but I'll happily switch back to the "official" release if you can apply this, or a similar, patch.)

eerohele commented 3 years ago

Thanks for the PR! The change looks good to me. I tried writing a test for it, but I couldn't figure out how to coax GradleRunner to just run the configuration phase of the build.

I just cut v0.9.0-beta2, which includes this change. I tried updating some of the dependencies, too, but, well... when I tried updating to Gradle 7.0, I got one error, and if I tried updating just the plugin dependencies, I got another error, and if I tried updating both, I think I got a third, different error, so, yeah... had to give up in the end. Might revisit it later.

ndw commented 3 years ago

Here's an example Gradle script that demonstrates the problem, FYI:

plugins {
  id 'com.github.eerohele.saxon-gradle' version '0.9.0-beta1'
}

import com.github.eerohele.SaxonXsltTask

repositories {
  mavenLocal()
  mavenCentral()
  maven {
    url "https://dev.saxonica.com/maven"
  }
}

task first() {
  outputs.file "build/identity.xsl"
  doLast {
    new File("build/identity.xsl").withWriter("utf-8") { writer ->
      writer.writeLine("<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
      writer.writeLine("                xmlns:xs='http://www.w3.org/2001/XMLSchema'");
      writer.writeLine("                exclude-result-prefixes='xs'");
      writer.writeLine("                version='3.0'>");

      writer.writeLine("<xsl:output method='xml' encoding='utf-8' indent='no'/>");

      writer.writeLine("<xsl:template match='/'>");
      writer.writeLine("  <xsl:apply-templates/>");
      writer.writeLine("</xsl:template>");

      writer.writeLine("<xsl:template match='element()'>");
      writer.writeLine("  <xsl:copy>");
      writer.writeLine("    <xsl:apply-templates select='@*,node()'/>");
      writer.writeLine("  </xsl:copy>");
      writer.writeLine("</xsl:template>");

      writer.writeLine("<xsl:template match='attribute()|text()|comment()|processing-instruction()'>");
      writer.writeLine("  <xsl:copy/>");
      writer.writeLine("</xsl:template>");

      writer.writeLine("</xsl:stylesheet>");
    }
  }
}

task second(type: SaxonXsltTask, dependsOn: ['first']) {
  inputs.files first.outputs.files
  outputs.file "build/copied.xml"

  input "build/identity.xsl"
  stylesheet first.outputs.files.singleFile
  output "build/copied.xml"
}

If you start with an empty build directory, this will fail. Happily, it succeds with beta2! Thank you for the quick turnaround as I've been utterly unable to work out how to get my version to publish on Maven Central.

eerohele commented 3 years ago

Thanks for the example — I do see the problem. It seems clear to me we should only expect the stylesheet to exist in the execution phase. I imagine the same is true for inputs, the Saxon config file, etc. It would be nice to come up with a more complete solution to this issue, I think.

I've been utterly unable to work out how to get my version to publish on Maven Central.

I'm not sure you can publish Gradle plugins into Maven Central. This plugin, for instance, is published into the Gradle plugin portal.

Anyway, glad to hear it's working!

ndw commented 3 years ago

As far as I can tell, the plugin doesn't attempt to access the (document) input until the execution phase. I'm not sure about configuration files and other things. I'll keep my eyes open.

ndw commented 3 years ago

Thanks for the tip about Maven Central. I persuaded myself that I'd done it with another plugin, but closer inspection reveals I installed it by hand on my own web server. :-/