From e8fac8aeb57c4ccf67d75953170d973a411f7bf2 Mon Sep 17 00:00:00 2001 From: jules Ivanic Date: Sun, 16 Jul 2023 17:17:50 +0400 Subject: [PATCH] Give some love to the project - Update Scala version - Update sbt and sbt plugins - Remove deprecated sbt syntax usages - Update Scalafix and improve its configuration - Install Scala Steward to help us maintain this project - Fix Scalafix issues in code --- .github/workflows/scala-steward.yml | 16 ++++ .scalafix.conf | 25 +++++- build.sbt | 15 ++-- .../sbtnativeimage/NativeImagePlugin.scala | 87 +++++++++---------- project/build.properties | 2 +- project/plugins.sbt | 16 ++-- 6 files changed, 99 insertions(+), 62 deletions(-) create mode 100644 .github/workflows/scala-steward.yml diff --git a/.github/workflows/scala-steward.yml b/.github/workflows/scala-steward.yml new file mode 100644 index 0000000..83edb99 --- /dev/null +++ b/.github/workflows/scala-steward.yml @@ -0,0 +1,16 @@ +name: Scala Steward + +# This workflow will launch everyday at 00:00 +on: + schedule: + - cron: '0 0 * * *' + workflow_dispatch: {} + +jobs: + scala-steward: + timeout-minutes: 30 + runs-on: ubuntu-latest + name: Scala Steward + steps: + - name: Scala Steward + uses: scala-steward-org/scala-steward-action@v2.59.0 diff --git a/.scalafix.conf b/.scalafix.conf index b6b7aab..84a4444 100644 --- a/.scalafix.conf +++ b/.scalafix.conf @@ -1,7 +1,30 @@ rules = [ - OrganizeImports, + DisableSyntax + ExplicitResultTypes + LeakingImplicitClassVal + NoAutoTupling + NoValInForComprehension + ProcedureSyntax + RemoveUnused + OrganizeImports ] +RemoveUnused { + imports = false // See https://github.com/scalacenter/scalafix/blob/v0.11.0/docs/rules/OrganizeImports.md#configuration +} + +Disable { + ifSynthetic = [ + "scala/Option.option2Iterable" + "scala/Predef.any2stringadd" + ] +} + +DisableSyntax.noReturns = true +DisableSyntax.noXml = true +DisableSyntax.noFinalize = true +DisableSyntax.noValPatterns = true + ExplicitResultTypes.rewriteStructuralTypesToNamedSubclass = false OrganizeImports.groupedImports = Explode diff --git a/build.sbt b/build.sbt index c8a40c7..10191d4 100644 --- a/build.sbt +++ b/build.sbt @@ -1,4 +1,7 @@ -def scala212 = "2.12.12" +def scala212 = "2.12.18" + +Global / onChangedBuildSource := ReloadOnSourceChanges + inThisBuild( List( organization := "org.scalameta", @@ -15,8 +18,6 @@ inThisBuild( ) ), scalaVersion := scala212, - scalafixDependencies += - "com.github.liancheng" %% "organize-imports" % "0.5.0", scalacOptions ++= List("-Ywarn-unused-import"), scalafixCaching := true, semanticdbEnabled := true @@ -24,7 +25,7 @@ inThisBuild( ) crossScalaVersions := Nil -skip.in(publish) := true +publish / skip := true commands += Command.command("fixAll") { s => @@ -42,7 +43,7 @@ lazy val plugin = project .settings( moduleName := "sbt-native-image", sbtPlugin := true, - sbtVersion.in(pluginCrossBuild) := "1.0.0", + pluginCrossBuild / sbtVersion := "1.0.0", crossScalaVersions := List(scala212), buildInfoPackage := "sbtnativeimage", buildInfoKeys := Seq[BuildInfoKey](version), @@ -55,8 +56,8 @@ lazy val plugin = project lazy val example = project .in(file("example")) .settings( - skip.in(publish) := true, - mainClass.in(Compile) := Some("example.Hello"), + publish / skip := true, + Compile / mainClass := Some("example.Hello"), test := { val binary = nativeImage.value val output = scala.sys.process.Process(List(binary.toString)).!!.trim diff --git a/plugin/src/main/scala/sbtnativeimage/NativeImagePlugin.scala b/plugin/src/main/scala/sbtnativeimage/NativeImagePlugin.scala index 815cfb9..7134d03 100644 --- a/plugin/src/main/scala/sbtnativeimage/NativeImagePlugin.scala +++ b/plugin/src/main/scala/sbtnativeimage/NativeImagePlugin.scala @@ -140,12 +140,10 @@ object NativeImagePlugin extends AutoPlugin { override lazy val projectSettings: Seq[Def.Setting[_]] = List( libraryDependencies += "org.scalameta" % "svm-subs" % "101.0.0", - target.in(NativeImage) := target.in(Compile).value / "native-image", - target.in(NativeImageTest) := target.in(Test).value / "native-image-test", - target.in(NativeImageInternal) := - target.in(Compile).value / "native-image-internal", - target.in(NativeImageTestInternal) := - target.in(Test).value / "native-image-test-internal", + NativeImage / target := (Compile / target).value / "native-image", + NativeImageTest / target := (Test / target).value / "native-image-test", + NativeImageInternal / target := (Compile / target).value / "native-image-internal", + NativeImageTestInternal / target := (Test / target).value / "native-image-test-internal", nativeImageReady := { val s = streams.value @@ -163,15 +161,15 @@ object NativeImagePlugin extends AutoPlugin { nativeImageJvm := "graalvm-java11", nativeImageJvmIndex := "cs", nativeImageVersion := "20.2.0", - name.in(NativeImage) := name.value, - name.in(NativeImageTest) := name.in(Test).value, - mainClass.in(NativeImage) := mainClass.in(Compile).value, - mainClass.in(NativeImageTest) := mainClass.in(Test).value, - nativeImageOptions := List(), + NativeImage / name := name.value, + NativeImageTest / name := (Test / name).value, + NativeImage / mainClass := (Compile / mainClass).value, + NativeImageTest / mainClass := (Test / mainClass).value, + nativeImageOptions := List.empty, nativeImageTestOptions := nativeImageOptions.value, - nativeImageTestRunOptions := List(), + nativeImageTestRunOptions := List.empty, nativeImageCoursier := { - val dir = target.in(NativeImageInternal).value + val dir = (NativeImageInternal / target).value val out = copyResource("coursier", dir) if (Properties.isWin) { copyResource("coursier.bat", dir) @@ -297,7 +295,7 @@ object NativeImagePlugin extends AutoPlugin { arguments.mkString(" ") Project .extract(newState) - .runInputTask(run in (tpr, Compile), input, newState) + .runInputTask(tpr / Compile / run, input, newState) }, nativeImageTestRunAgent := { val _ = nativeImageTestCommand.value @@ -313,11 +311,11 @@ object NativeImagePlugin extends AutoPlugin { val options = (javaOptions in (Test, run)).value ++ Seq(agentOption) - val __ = compile.in(Test).value - val main = mainClass.in(NativeImageTest).value - val cp = fullClasspath.in(Test).value.map(_.data) - val manifest = target.in(NativeImageTestInternal).value / "manifest.jar" - manifest.getParentFile().mkdirs() + val __ = (Test / compile).value + val main = (NativeImageTest / mainClass).value + val cp = (Test / fullClasspath).value.map(_.data) + val manifest = (NativeImageTestInternal / target).value / "manifest.jar" + manifest.getParentFile.mkdirs() createManifestJar(manifest, cp) val nativeClasspath = manifest.absolutePath @@ -331,7 +329,7 @@ object NativeImagePlugin extends AutoPlugin { throw new MessageOnlyException( "no mainClass is specified for tests. " + "To fix this problem, update build.sbt to include the settings " + - "`mainClass.in(Test) := Some(\"com.MainTestClass\")`" + "`Test / mainClass := Some(\"com.MainTestClass\")`" ) ) command ++= nativeImageTestRunOptions.value @@ -344,12 +342,12 @@ object NativeImagePlugin extends AutoPlugin { } }, nativeImageOutput := - target.in(NativeImage).value / name.in(NativeImage).value, + (NativeImage / target).value / (NativeImage / name).value, nativeImageTestOutput := - target.in(NativeImageTest).value / name.in(NativeImageTest).value, + (NativeImageTest / target).value / (NativeImageTest / name).value, nativeImageCopy := { val binary = nativeImage.value - val out = fileParser(baseDirectory.in(ThisBuild).value).parsed + val out = fileParser((ThisBuild / baseDirectory).value).parsed Files.copy( binary.toPath(), out.toPath(), @@ -384,17 +382,17 @@ object NativeImagePlugin extends AutoPlugin { } }, nativeImage := { - val _ = compile.in(Compile).value - val main = mainClass.in(NativeImage).value + val _ = (Compile / compile).value + val main = (NativeImage / mainClass).value val binaryName = nativeImageOutput.value - val cp = fullClasspath.in(Compile).value.map(_.data) + val cp = (Compile / fullClasspath).value.map(_.data) // NOTE(olafur): we pass in a manifest jar instead of the full classpath // for two reasons: // * large classpaths quickly hit on the "argument list too large" // error, especially on Windows. // * we print the full command to the console and the manifest jar makes // it more readable and easier to copy-paste. - val manifest = target.in(NativeImageInternal).value / "manifest.jar" + val manifest = (NativeImageInternal / target).value / "manifest.jar" manifest.getParentFile().mkdirs() createManifestJar(manifest, cp) val nativeClasspath = manifest.absolutePath @@ -410,14 +408,14 @@ object NativeImagePlugin extends AutoPlugin { throw new MessageOnlyException( "no mainClass is specified. " + "To fix this problem, update build.sbt to include the settings " + - "`mainClass.in(Compile) := Some(\"com.MainClass\")`" + "`Compile / mainClass := Some(\"com.MainClass\")`" ) ) command += binaryName.absolutePath // Start native-image linker. streams.value.log.info(command.mkString(" ")) - val cwd = target.in(NativeImage).value + val cwd = (NativeImage / target).value cwd.mkdirs() val exit = Process(command, cwd = Some(cwd)).! if (exit != 0) { @@ -431,17 +429,17 @@ object NativeImagePlugin extends AutoPlugin { binaryName }, nativeImageTest := { - val _ = compile.in(Test).value - val main = mainClass.in(NativeImageTest).value + val _ = (Test / compile).value + val main = (NativeImageTest / mainClass).value val binaryName = nativeImageTestOutput.value - val cp = fullClasspath.in(Test).value.map(_.data) + val cp = (Test / fullClasspath).value.map(_.data) // NOTE(olafur): we pass in a manifest jar instead of the full classpath // for two reasons: // * large classpaths quickly hit on the "argument list too large" // error, especially on Windows. // * we print the full command to the console and the manifest jar makes // it more readable and easier to copy-paste. - val manifest = target.in(NativeImageTestInternal).value / "manifest.jar" + val manifest = (NativeImageTestInternal / target).value / "manifest.jar" manifest.getParentFile().mkdirs() createManifestJar(manifest, cp) val nativeClasspath = manifest.absolutePath @@ -457,14 +455,14 @@ object NativeImagePlugin extends AutoPlugin { throw new MessageOnlyException( "no mainClass is specified for tests. " + "To fix this problem, update build.sbt to include the settings " + - "`mainClass.in(Test) := Some(\"com.MainTestClass\")`" + "`Test / mainClass := Some(\"com.MainTestClass\")`" ) ) command += binaryName.absolutePath // Start native-image linker. streams.value.log.info(command.mkString(" ")) - val cwd = target.in(NativeImageTest).value + val cwd = (NativeImageTest / target).value cwd.mkdirs() val exit = Process(command, cwd = Some(cwd)).! if (exit != 0) { @@ -519,8 +517,7 @@ object NativeImagePlugin extends AutoPlugin { //this happens if the dependency jar resides on a different drive then the manifest, i.e. C:\Coursier\Cache and D:\myapp\target //copy dependency next to manifest as fallback case _: IllegalArgumentException => - import java.nio.file.Files - import java.nio.file.StandardCopyOption + import java.nio.file.{Files, StandardCopyOption} Files.copy( dependencyPath, manifestPath.resolve(path.getName), @@ -546,15 +543,15 @@ object NativeImagePlugin extends AutoPlugin { private def alertUser(streams: std.TaskStreams[_], message: String): Unit = { streams.log.info(message) - if (isCI) - return - try { - if (Properties.isMac) { - Process(List("say", message)).! + if (!isCI) { + try { + if (Properties.isMac) { + Process(List("say", message)).! + } + // NOTE(olafur): feel free to add support for Linux/Windows. + } catch { + case NonFatal(_) => } - // NOTE(olafur): feel free to add support for Linux/Windows. - } catch { - case NonFatal(_) => } } } diff --git a/project/build.properties b/project/build.properties index 7de0a93..875b706 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.4.4 +sbt.version=1.9.2 diff --git a/project/plugins.sbt b/project/plugins.sbt index 96148c6..ce65de8 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,13 +1,13 @@ -addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.11") -addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0") -addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.3.4") -addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.27") +addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12") +addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0") +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.0") libraryDependencies += "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value -unmanagedSourceDirectories.in(Compile) += - baseDirectory.in(ThisBuild).value.getParentFile / "plugin" / "src" / "main" / +Compile / unmanagedSourceDirectories += + (ThisBuild / baseDirectory).value.getParentFile / "plugin" / "src" / "main" / "scala" -unmanagedResourceDirectories.in(Compile) += - baseDirectory.in(ThisBuild).value.getParentFile / "plugin" / "src" / "main" / +Compile / unmanagedResourceDirectories += + (ThisBuild / baseDirectory).value.getParentFile / "plugin" / "src" / "main" / "resources"