From 108df67a759552d7a186ff254d510c2ec1b17ccb Mon Sep 17 00:00:00 2001 From: Matthew de Detrich Date: Wed, 24 Jan 2024 16:27:53 +1100 Subject: [PATCH] Prefix pom.xml with apache --- .scalafix.conf | 1 - README.md | 2 + build.sbt | 4 + .../apache/sonatype/ApacheSonatypeKeys.scala | 3 +- .../sonatype/ApacheSonatypePlugin.scala | 27 ++++++- .../sbt-apache-sonatype/simple/build.sbt | 9 +++ src/sbt-test/sbt-apache-sonatype/simple/test | 2 + src/test/resources/after.pom.xml | 73 +++++++++++++++++++ src/test/resources/before.pom.xml | 73 +++++++++++++++++++ .../sonatype/ApacheSonatypePluginSpec.scala | 15 ++++ 10 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/after.pom.xml create mode 100644 src/test/resources/before.pom.xml create mode 100644 src/test/scala/org/mdedetrich/apache/sonatype/ApacheSonatypePluginSpec.scala diff --git a/.scalafix.conf b/.scalafix.conf index c1a95fc..f65f708 100644 --- a/.scalafix.conf +++ b/.scalafix.conf @@ -16,7 +16,6 @@ DisableSyntax.noNulls = true DisableSyntax.noReturns = true DisableSyntax.noWhileLoops = true DisableSyntax.noIsInstanceOf = true -DisableSyntax.noXml = true DisableSyntax.noFinalVal = true DisableSyntax.noFinalize = true DisableSyntax.noValPatterns = true diff --git a/README.md b/README.md index 1de06b7..ea8a808 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ if you want to set `apacheSonatypeDisclaimerFile` to point to a `DISCLAIMER` fil * `apacheSonatypeCredentialsLogLevel`: The log level to be used when logging about potential problems in resolving credentials, defaults to `Level.Debug`. If you are trying to diagnose issues with resolving credentials then increase the log level to either `Level.Warn` or `Level.Error`. +* `apacheSonatypeArtifactNamePrefix`: Apache guidelines mandate that the `` field in the POM xml has to have a + specific prefix. ### Keys for Github Actions diff --git a/build.sbt b/build.sbt index 35eca55..b1a1ed0 100644 --- a/build.sbt +++ b/build.sbt @@ -77,3 +77,7 @@ inThisBuild( ) ) ) + +libraryDependencies ++= Seq( + "org.scalatest" %% "scalatest" % "3.2.17" % Test +) diff --git a/src/main/scala/org/mdedetrich/apache/sonatype/ApacheSonatypeKeys.scala b/src/main/scala/org/mdedetrich/apache/sonatype/ApacheSonatypeKeys.scala index 7eeccad..272ab8d 100644 --- a/src/main/scala/org/mdedetrich/apache/sonatype/ApacheSonatypeKeys.scala +++ b/src/main/scala/org/mdedetrich/apache/sonatype/ApacheSonatypeKeys.scala @@ -30,5 +30,6 @@ trait ApacheSonatypeKeys { settingKey[File]("The NOTICE file which needs to be included in published artifact") lazy val apacheSonatypeDisclaimerFile: SettingKey[Option[File]] = settingKey[Option[File]]("The DISCLAIMER file which can optionally be included in published artifact") - + lazy val apacheSonatypeArtifactNamePrefix: SettingKey[Option[String]] = + settingKey[Option[String]]("A prefix that will be added before the artifact name") } diff --git a/src/main/scala/org/mdedetrich/apache/sonatype/ApacheSonatypePlugin.scala b/src/main/scala/org/mdedetrich/apache/sonatype/ApacheSonatypePlugin.scala index 7648712..d2150b4 100644 --- a/src/main/scala/org/mdedetrich/apache/sonatype/ApacheSonatypePlugin.scala +++ b/src/main/scala/org/mdedetrich/apache/sonatype/ApacheSonatypePlugin.scala @@ -44,7 +44,8 @@ object ApacheSonatypePlugin extends AutoPlugin { apacheSonatypeCredentialsPasswordEnvVar.value ) }, - apacheSonatypeCredentialsLogLevel := Level.Debug + apacheSonatypeCredentialsLogLevel := Level.Debug, + apacheSonatypeArtifactNamePrefix := Some("apache") ) private[sonatype] lazy val sbtSonatypeBuildSettings: Seq[Setting[_]] = Seq( @@ -54,6 +55,21 @@ object ApacheSonatypePlugin extends AutoPlugin { private[sonatype] lazy val baseDir = LocalRootProject / baseDirectory + import scala.xml._ + + private[sonatype] def prefixNameInPom(prefix: String): Node => Node = (pomXml: Node) => + new transform.RewriteRule { + override def transform(n: Node): Seq[Node] = n match { + case node @ projectNode if projectNode.label == "project" => + val modifiedChildren = projectNode.child.collect { + case {currentName} => {s"$prefix-$currentName"} + case other => other + } + Elem(node.prefix, node.label, node.attributes, node.scope, false, modifiedChildren: _*) + case other => other + } + }.transform(pomXml).head + private[sonatype] lazy val sbtMavenBuildSettings: Seq[Setting[_]] = Seq( credentials ++= { val log = sLog.value @@ -68,7 +84,14 @@ object ApacheSonatypePlugin extends AutoPlugin { }, organization := s"org.apache.${apacheSonatypeProjectProfile.value}", organizationName := apacheSonatypeOrganizationName.value, - organizationHomepage := Some(apacheSonatypeOrganizationHomePage.value) + organizationHomepage := Some(apacheSonatypeOrganizationHomePage.value), + pomPostProcess := { + apacheSonatypeArtifactNamePrefix.value match { + case Some(prefix) => + pomPostProcess.value andThen prefixNameInPom(prefix) + case None => pomPostProcess.value + } + } ) private[sonatype] lazy val sbtMavenProjectSettings: Seq[Setting[_]] = Seq( diff --git a/src/sbt-test/sbt-apache-sonatype/simple/build.sbt b/src/sbt-test/sbt-apache-sonatype/simple/build.sbt index e01a689..9b02424 100644 --- a/src/sbt-test/sbt-apache-sonatype/simple/build.sbt +++ b/src/sbt-test/sbt-apache-sonatype/simple/build.sbt @@ -1,5 +1,6 @@ ThisBuild / scalaVersion := "2.13.10" ThisBuild / apacheSonatypeProjectProfile := "project" +name := "some-name" TaskKey[Unit]("check-organization") := { val org = "org.apache.project" @@ -60,3 +61,11 @@ TaskKey[Unit]("extract-packageDoc-contents") := { IO.unzip(sourcesJar, targetDir) } + +TaskKey[Unit]("check-artifact-name") := { + val file = makePom.value + val xml = scala.xml.XML.loadFile(file) + val nameNode = (xml \ "name").text + if (nameNode != "apache-some-name") + sys.error(s"expected artifact name to be apache-some-name, instead got ${nameNode}") +} diff --git a/src/sbt-test/sbt-apache-sonatype/simple/test b/src/sbt-test/sbt-apache-sonatype/simple/test index a43a8c5..cfbb24e 100644 --- a/src/sbt-test/sbt-apache-sonatype/simple/test +++ b/src/sbt-test/sbt-apache-sonatype/simple/test @@ -15,3 +15,5 @@ $ must-mirror target/scala-2.13/packageDoc/META-INF/NOTICE NOTICE > checkPomIncludeRepository > checkPomLicenseField > checkPublishToField +> checkArtifactName + diff --git a/src/test/resources/after.pom.xml b/src/test/resources/after.pom.xml new file mode 100644 index 0000000..0163acf --- /dev/null +++ b/src/test/resources/after.pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + org.apache.pekko + pekko-stream_3 + jar + Apache Pekko is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala. + https://pekko.apache.org/ + 1.0.2 + + + Apache-2.0 + https://www.apache.org/licenses/LICENSE-2.0.html + repo + + + apache-pekko-stream + 2022 + + Apache Software Foundation + https://www.apache.org + + + https://github.com/apache/incubator-pekko + scm:git:https://github.com/apache/incubator-pekko.git + scm:git:git@github.com:apache/incubator-pekko.git + + + + pekko-contributors + Apache Pekko Contributors + https://github.com/apache/incubator-pekko/graphs/contributors + dev@pekko.apache.org + + + + https://pekko.apache.org/api/pekko/1.0.2 + semver-spec + + + + org.apache.pekko + pekko-actor_3 + 1.0.2 + + + org.apache.pekko + pekko-protobuf-v3_3 + 1.0.2 + + + org.scala-lang + scala3-library_3 + 3.3.1 + + + org.reactivestreams + reactive-streams + 1.0.4 + + + com.typesafe + ssl-config-core_3 + 0.6.1 + + + org.scalatest + scalatest_3 + 3.2.14 + test + + + diff --git a/src/test/resources/before.pom.xml b/src/test/resources/before.pom.xml new file mode 100644 index 0000000..a8e44ee --- /dev/null +++ b/src/test/resources/before.pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + org.apache.pekko + pekko-stream_3 + jar + Apache Pekko is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala. + https://pekko.apache.org/ + 1.0.2 + + + Apache-2.0 + https://www.apache.org/licenses/LICENSE-2.0.html + repo + + + pekko-stream + 2022 + + Apache Software Foundation + https://www.apache.org + + + https://github.com/apache/incubator-pekko + scm:git:https://github.com/apache/incubator-pekko.git + scm:git:git@github.com:apache/incubator-pekko.git + + + + pekko-contributors + Apache Pekko Contributors + https://github.com/apache/incubator-pekko/graphs/contributors + dev@pekko.apache.org + + + + https://pekko.apache.org/api/pekko/1.0.2 + semver-spec + + + + org.apache.pekko + pekko-actor_3 + 1.0.2 + + + org.apache.pekko + pekko-protobuf-v3_3 + 1.0.2 + + + org.scala-lang + scala3-library_3 + 3.3.1 + + + org.reactivestreams + reactive-streams + 1.0.4 + + + com.typesafe + ssl-config-core_3 + 0.6.1 + + + org.scalatest + scalatest_3 + 3.2.14 + test + + + diff --git a/src/test/scala/org/mdedetrich/apache/sonatype/ApacheSonatypePluginSpec.scala b/src/test/scala/org/mdedetrich/apache/sonatype/ApacheSonatypePluginSpec.scala new file mode 100644 index 0000000..8a9ae82 --- /dev/null +++ b/src/test/scala/org/mdedetrich/apache/sonatype/ApacheSonatypePluginSpec.scala @@ -0,0 +1,15 @@ +package org.mdedetrich.apache.sonatype + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.must.Matchers + +import scala.xml._ + +class ApacheSonatypePluginSpec extends AnyFlatSpec with Matchers { + "ApacheSonatypePlugin" must "replace node in prefixNameInPom" in { + val before = XML.load(getClass.getResource("/before.pom.xml").getPath) + val after = XML.load(getClass.getResource("/after.pom.xml").getPath) + + ApacheSonatypePlugin.prefixNameInPom("apache")(before).text mustEqual after.text + } +}