jadams-tresys commented on code in PR #183:
URL: https://github.com/apache/daffodil-sbt/pull/183#discussion_r3236101655
##########
src/main/scala/org/apache/daffodil/DaffodilPlugin.scala:
##########
@@ -840,6 +854,179 @@ object DaffodilPlugin extends AutoPlugin {
}
}
+ def daffodilFlattenSettings: Seq[Setting[_]] = Seq(
+ daffodilFlattenTarget := target.value /
s"${name.value}-${version.value}-flat.zip",
+
+ /**
+ * Whether or not to publish the flattened schemas zip. Defaults to false.
+ *
+ * If projects want to publish flattened schemas then they must explicitly
enable it by
+ * setting 'daffodilFlattenSchemas / publishArtifact := true'.
+ *
+ * If false, flattened schemas will not be created unless you explicitly
run the
+ * daffodilFlattenSchemas.
+ */
+ daffodilFlattenSchemas / publishArtifact := false,
+
+ daffodilFlattenSchemas / artifact := Artifact(
+ name.value,
+ "flat",
+ "zip",
+ Some("flat"),
+ Vector(),
+ None
+ ),
+
+ /**
+ * Only grab the file types that need to be flattened. These files are only
+ * grabbed from the root project. Any referenced files will be pulled in
+ * from the classpath. Note that XML files are commonly used in XSLT for
+ * settings.
+ */
+ daffodilFlattenSchemas / includeFilter := "*.xsd" | "*.xsl" | "*.xslt" |
"*.xml",
+ daffodilFlattenSchemas / excludeFilter := HiddenFileFilter,
+
+ daffodilFlattenResourceReferencePatterns := List(
+ """schemaLocation=\"([^\"]*)\"""".r,
+ "href=\"([^\"]*)\"".r,
+ "document[(]'([^']*)'[)]".r),
+
+ daffodilFlattenSchemas / products := {
+
+ val logger = streams.value.log
+ val filter = (daffodilFlattenSchemas / includeFilter).value --
(daffodilFlattenSchemas / excludeFilter).value
+
+ val flatDir = target.value / "flatDir"
+ if (flatDir.exists())
+ IO.delete(flatDir)
+ IO.createDirectory(flatDir)
+
+ val projectResources = (Compile / resourceDirectories).value
+ .map { root => root.toURI.toURL -> (root **
filter).get.map(_.toURI.toURL).toList }
+ .toMap
+
+ /**
+ * Create a URLClassLoader object with URLs to all resources used by the
+ * project. The class loader will be used to resolve references made
+ * within the flattened files.
+ */
+ val projectURLs = (Compile /
resourceDirectories).value.map(_.toURI.toURL)
+ val allClasspathURLs = (Test /
externalDependencyClasspath).value.map(_.data.toURI.toURL)
+ val classLoader = new URLClassLoader((projectURLs ++
allClasspathURLs).toArray, null)
+
+ val referenceRegexes = daffodilFlattenResourceReferencePatterns.value
+
+ val processed = scala.collection.mutable.Set[URI]()
+ projectResources foreach { case (rootURL, urls) =>
+ val unprocessed = scala.collection.mutable.Stack[URI]()
+ val rootURI = rootURL.toURI
+ val rootPath = Paths.get(rootURI)
+ unprocessed.pushAll(urls.map(_.toURI))
+ while (!unprocessed.isEmpty) {
+ val contextURI = unprocessed.pop
+ val bytes = contextURI.toURL.openStream().readAllBytes()
+ val contextRelPath = contextURI.getScheme match {
+ case "jar" => Paths.get(contextURI.toString.split("!")(1).tail)
+ case "file" => Paths.get(rootURI.relativize(contextURI).getPath)
+ case _ => throw new IllegalArgumentException(s"Unrecognized URI
scheme: $contextURI")
+ }
+ val contextFlatPath = Paths.get(
+ flatDir.toString,
+ contextRelPath.toString.replaceAll("/", "__"))
+ val bw = Files.newBufferedWriter(contextFlatPath)
+ val fileAsString = new String(bytes, Charset.defaultCharset())
+
+ val references = referenceRegexes.flatMap { re =>
+ re.findAllIn(fileAsString).matchData.map(_.group(1))
+ }
+
+ val resolvedRefs = references.map { ref =>
+ val origLocation = if (ref contains " ") ref.split(" ")(1) else ref
+ if (origLocation.startsWith("/")) {
+ // Dealing with an absolute path
+ val url = Option(classLoader.findResource(origLocation.tail))
+ if (url.isDefined)
+ Some(origLocation -> url.get.toURI)
+ else {
+ logger.warn(s"Unable to resolve absolute reference to $ref
from source file $contextURI")
+ None
+ }
+ } else {
+ // Relative path
+ val relPath =
contextRelPath.resolveSibling(origLocation).normalize()
+ if (Files.exists(rootPath.resolve(relPath))) {
Review Comment:
What about instances where we have multiple payload types? For example,
using JREAP it defines it's own generic payload, but when combined with Link16
using the payload defined in JREAP would be incorrect as we would want to use
the one from Link16, which shares the same classpath name.
I realize that we typically have a 3rd schema (link16-with-jreap) which
means we are usually dealing with just a different jar file anyways, but I
could definitely see a project where it schema via a jar dependency, but then
overwrites an element with something defined in it's project.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]