From 42f17fba0e945b13447da495d1fcbb6c2d6d90f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=A3o=20Martins?= Date: Thu, 30 Jan 2020 11:15:12 +0000 Subject: [PATCH] Introduce RPM / packageBin / artifactPath setting. (#1299) * Introduce RPM / packageBin / artifactPath setting. Closes #1287 * Fix setting cannot depend on a task. * Proper fix. * Now without breaking mima. * Add a little bit of documentation. And a sbt-test. * Fix test Co-authored-by: Nepomuk Seiler --- .../typesafe/sbt/packager/rpm/RpmHelper.scala | 8 ++-- .../typesafe/sbt/packager/rpm/RpmPlugin.scala | 10 ++++- src/sbt-test/rpm/test-artifactPath/build.sbt | 45 +++++++++++++++++++ .../rpm/test-artifactPath/project/plugins.sbt | 1 + src/sbt-test/rpm/test-artifactPath/test | 8 ++++ src/sphinx/formats/rpm.rst | 3 ++ 6 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/sbt-test/rpm/test-artifactPath/build.sbt create mode 100644 src/sbt-test/rpm/test-artifactPath/project/plugins.sbt create mode 100644 src/sbt-test/rpm/test-artifactPath/test diff --git a/src/main/scala/com/typesafe/sbt/packager/rpm/RpmHelper.scala b/src/main/scala/com/typesafe/sbt/packager/rpm/RpmHelper.scala index be5fbdaa3..224cc7d75 100644 --- a/src/main/scala/com/typesafe/sbt/packager/rpm/RpmHelper.scala +++ b/src/main/scala/com/typesafe/sbt/packager/rpm/RpmHelper.scala @@ -1,7 +1,6 @@ package com.typesafe.sbt.packager.rpm import sbt._ -import com.typesafe.sbt.packager.Compat._ import com.typesafe.sbt.packager.linux.LinuxSymlink object RpmHelper { @@ -26,6 +25,9 @@ object RpmHelper { workArea } + private[rpm] def defaultRpmArtifactPath(stagingArea: File, meta: RpmMetadata): File = + stagingArea / "RPMS" / meta.arch / s"${meta.name}-${meta.version}-${meta.release}.${meta.arch}.rpm" + /** * Build the rpm package * @@ -36,9 +38,7 @@ object RpmHelper { */ def buildRpm(spec: RpmSpec, stagingArea: File, log: sbt.Logger): File = { buildPackage(stagingArea, spec, log) - // We should probably return the File that was created. - val rpmname = "%s-%s-%s.%s.rpm" format (spec.meta.name, spec.meta.version, spec.meta.release, spec.meta.arch) - stagingArea / "RPMS" / spec.meta.arch / rpmname + defaultRpmArtifactPath(stagingArea, spec.meta) } private[this] def copyFiles(spec: RpmSpec, workArea: File, log: sbt.Logger): Unit = { diff --git a/src/main/scala/com/typesafe/sbt/packager/rpm/RpmPlugin.scala b/src/main/scala/com/typesafe/sbt/packager/rpm/RpmPlugin.scala index 4d209b4af..cffd3b63f 100644 --- a/src/main/scala/com/typesafe/sbt/packager/rpm/RpmPlugin.scala +++ b/src/main/scala/com/typesafe/sbt/packager/rpm/RpmPlugin.scala @@ -165,7 +165,15 @@ object RpmPlugin extends AutoPlugin { (defaultLinuxInstallLocation in Rpm).value ), stage in Rpm := RpmHelper.stage(rpmSpecConfig.value, (target in Rpm).value, streams.value.log), - packageBin in Rpm := RpmHelper.buildRpm(rpmSpecConfig.value, (stage in Rpm).value, streams.value.log), + artifactPath in (Rpm, packageBin) := RpmHelper.defaultRpmArtifactPath((target in Rpm).value, rpmMetadata.value), + packageBin in Rpm := { + val defaultPath = RpmHelper.buildRpm(rpmSpecConfig.value, (stage in Rpm).value, streams.value.log) + // `file` points to where buildRpm created the rpm. However we want it to be at `artifactPath`. + // If `artifactPath` is not the default value then we need to copy the file. + val path = (artifactPath in (Rpm, packageBin)).value + if (path.getCanonicalFile != defaultPath.getCanonicalFile) IO.copyFile(defaultPath, path) + path + }, rpmLint := { sys.process.Process(Seq("rpmlint", "-v", (packageBin in Rpm).value.getAbsolutePath)) ! streams.value.log match { case 0 => () diff --git a/src/sbt-test/rpm/test-artifactPath/build.sbt b/src/sbt-test/rpm/test-artifactPath/build.sbt new file mode 100644 index 000000000..5633cefa7 --- /dev/null +++ b/src/sbt-test/rpm/test-artifactPath/build.sbt @@ -0,0 +1,45 @@ +enablePlugins(JavaServerAppPackaging, SystemVPlugin) + +name := "rpm-test" + +version := "0.1.0" + +maintainer := "Josh Suereth " + +packageSummary := "Test rpm package" + +packageName in Linux := "rpm-package" + +artifactPath in (Rpm, packageBin) := target.value / s"${(packageName in Rpm).value}-${(version in Rpm).value}.rpm" + +packageDescription := """A fun package description of our software, + with multiple lines.""" + +rpmRelease := "1" + +rpmVendor := "typesafe" + +rpmUrl := Some("http://github.com/sbt/sbt-native-packager") + +rpmLicense := Some("BSD") + +TaskKey[Unit]("checkSpecFile") := { + val spec = IO.read(target.value / "rpm" / "SPECS" / "rpm-package.spec") + streams.value.log.success(spec) + assert( + spec contains "%attr(0644,root,root) /usr/share/rpm-package/lib/rpm-test.rpm-test-0.1.0.jar", + "Wrong installation path\n" + spec + ) + assert(spec contains "%attr(0755,root,root) /etc/init.d/rpm-package", "Wrong /etc/init.d path\n" + spec) + assert(spec contains "%config %attr(644,root,root) /etc/default/rpm-package", "Wrong /etc default file\n" + spec) + assert( + spec contains "%dir %attr(755,rpm-package,rpm-package) /var/log/rpm-package", + "Wrong logging dir path\n" + spec + ) + assert( + spec contains "%dir %attr(755,rpm-package,rpm-package) /var/run/rpm-package", + "Wrong /var/run dir path\n" + spec + ) + streams.value.log.success("Successfully tested rpm-package file") + () +} diff --git a/src/sbt-test/rpm/test-artifactPath/project/plugins.sbt b/src/sbt-test/rpm/test-artifactPath/project/plugins.sbt new file mode 100644 index 000000000..b53de154c --- /dev/null +++ b/src/sbt-test/rpm/test-artifactPath/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version")) diff --git a/src/sbt-test/rpm/test-artifactPath/test b/src/sbt-test/rpm/test-artifactPath/test new file mode 100644 index 000000000..5dd349653 --- /dev/null +++ b/src/sbt-test/rpm/test-artifactPath/test @@ -0,0 +1,8 @@ +# Run the debian packaging. +> rpm:packageBin +$ exists target/rpm-package-0.1.0.rpm +$ exists target/rpm/RPMS/noarch/rpm-package-0.1.0-1.noarch.rpm +$ exists target/rpm/SPECS/rpm-package.spec + +# Check files for defaults +> checkSpecFile diff --git a/src/sphinx/formats/rpm.rst b/src/sphinx/formats/rpm.rst index 44c55c5f1..e6bdb0541 100644 --- a/src/sphinx/formats/rpm.rst +++ b/src/sphinx/formats/rpm.rst @@ -129,6 +129,9 @@ Informational Settings used, and in the event of a tie it will fall back to comparing the version and release. + ``artifactPath in (Rpm, packageBin)`` + The location of the generated RPM. + Dependency Settings ~~~~~~~~~~~~~~~~~~~