From fd37a528362bf332e82fcb33a65c9d6ca6a2959c Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Mon, 7 Oct 2024 10:34:03 +0900 Subject: [PATCH] sbt 2 --- .github/workflows/ci.yml | 2 +- .scalafmt.conf | 20 +++++++---- build.sbt | 27 +++++++++----- project/build.properties | 2 +- src/main/{scala => scala-2}/NoComma.scala | 2 +- .../{scala => scala-2}/NoCommaPlugin.scala | 0 src/main/scala-3/NoComma.scala | 36 +++++++++++++++++++ src/main/scala-3/NoCommaPlugin.scala | 11 ++++++ src/test/scala/NoCommaTest.scala | 10 +++--- 9 files changed, 88 insertions(+), 22 deletions(-) rename src/main/{scala => scala-2}/NoComma.scala (99%) rename src/main/{scala => scala-2}/NoCommaPlugin.scala (100%) create mode 100644 src/main/scala-3/NoComma.scala create mode 100644 src/main/scala-3/NoCommaPlugin.scala diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3bb33c6..a857514 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: uses: coursier/cache-action@v6 - name: Build and test run: | - sbt -v clean scalafmt::test test:scalafmt::test sbt:scalafmt::test test scripted + sbt -v clean scalafmtSbtCheck "+ scalafmtCheckAll" "+ test" "+ scripted" rm -rf "$HOME/.ivy2/local" || true find $HOME/Library/Caches/Coursier/v1 -name "ivydata-*.properties" -delete || true find $HOME/.ivy2/cache -name "ivydata-*.properties" -delete || true diff --git a/.scalafmt.conf b/.scalafmt.conf index 9437871..37b57f3 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,12 +1,11 @@ -version = 2.3.2 -edition = 2019-10 +version = 3.8.3 maxColumn = 100 project.git = true lineEndings = preserve # https://docs.scala-lang.org/style/scaladoc.html recommends the JavaDoc style. # scala/scala is written that way too https://github.com/scala/scala/blob/v2.12.2/src/library/scala/Predef.scala -docstrings = JavaDoc +docstrings.style = Asterisk # This also seems more idiomatic to include whitespace in import x.{ yyy } spaces.inImportCurlyBraces = true @@ -16,7 +15,16 @@ spaces.inImportCurlyBraces = true align.openParenCallSite = false align.openParenDefnSite = false -# For better code clarity -danglingParentheses = true - trailingCommas = preserve + +align.preset = none +align.tokens = [ + caseArrow + + { + code = "=" + } +] + +project.layout = StandardConvention +runner.dialect = scala212source3 diff --git a/build.sbt b/build.sbt index b2b779b..2dbdbb8 100644 --- a/build.sbt +++ b/build.sbt @@ -1,4 +1,4 @@ -lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.4" +lazy val scalaTest = "org.scalatest" %% "scalatest-flatspec" % "3.2.19" ThisBuild / organization := "com.eed3si9n" ThisBuild / scalaVersion := "2.12.8" @@ -11,14 +11,24 @@ lazy val root = (project in file(".")) .settings( name := "sbt-nocomma", libraryDependencies ++= Vector(scalaTest % Test), - libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaVersion.value, - scriptedLaunchOpts := { scriptedLaunchOpts.value ++ - Seq("-Xmx1024M", "-Dplugin.version=" + version.value) + libraryDependencies ++= { + scalaBinaryVersion.value match { + case "3" => + Nil + case _ => + Seq("org.scala-lang" % "scala-compiler" % scalaVersion.value) + } + }, + scriptedLaunchOpts := { + scriptedLaunchOpts.value ++ + Seq("-Xmx1024M", "-Dplugin.version=" + version.value) }, scriptedBufferLog := false, + crossScalaVersions += "3.3.4", (pluginCrossBuild / sbtVersion) := { scalaBinaryVersion.value match { case "2.12" => "1.2.8" + case _ => "2.0.0-M2" } }, ) @@ -39,7 +49,9 @@ ThisBuild / developers := List( ) ThisBuild / description := "sbt plugin to reduce commas from your build.sbt" ThisBuild / homepage := Some(url("https://github.com/sbt/sbt-nocomma")) -ThisBuild / licenses := Seq("Apache-2.0" -> url("https://github.com/sbt/sbt-nocomma/blob/master/LICENSE")) +ThisBuild / licenses := Seq( + "Apache-2.0" -> url("https://github.com/sbt/sbt-nocomma/blob/master/LICENSE") +) ThisBuild / pomIncludeRepository := { _ => false } ThisBuild / publishTo := { val nexus = "https://oss.sonatype.org/" @@ -58,8 +70,7 @@ def pomConsistency2021DraftSettings: Seq[Setting[_]] = Seq( pomConsistency2021Draft := Set("true", "1")(sys.env.get("POM_CONSISTENCY").getOrElse("false")), moduleName := { if (pomConsistency2021Draft.value) - sbtPluginModuleName2021Draft(moduleName.value, - (pluginCrossBuild / sbtBinaryVersion).value) + sbtPluginModuleName2021Draft(moduleName.value, (pluginCrossBuild / sbtBinaryVersion).value) else moduleName.value }, projectID := { @@ -73,4 +84,4 @@ def sbtPluginModuleName2021Draft(n: String, sbtV: String): String = def sbtPluginExtra2021Draft(m: ModuleID): ModuleID = m.withExtraAttributes(Map.empty) - .withCrossVersion(CrossVersion.binary) + .withCrossVersion(CrossVersion.binary) diff --git a/project/build.properties b/project/build.properties index 6a9f038..0b699c3 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.7.3 +sbt.version=1.10.2 diff --git a/src/main/scala/NoComma.scala b/src/main/scala-2/NoComma.scala similarity index 99% rename from src/main/scala/NoComma.scala rename to src/main/scala-2/NoComma.scala index 3c765b6..14f9737 100644 --- a/src/main/scala/NoComma.scala +++ b/src/main/scala-2/NoComma.scala @@ -31,5 +31,5 @@ object NoComma { Select(reify(Vector).tree, TermName("apply")), items)) } - */ + */ } diff --git a/src/main/scala/NoCommaPlugin.scala b/src/main/scala-2/NoCommaPlugin.scala similarity index 100% rename from src/main/scala/NoCommaPlugin.scala rename to src/main/scala-2/NoCommaPlugin.scala diff --git a/src/main/scala-3/NoComma.scala b/src/main/scala-3/NoComma.scala new file mode 100644 index 0000000..eb5bf2b --- /dev/null +++ b/src/main/scala-3/NoComma.scala @@ -0,0 +1,36 @@ +package sbtnocomma + +import sbt.* +import scala.quoted.Expr +import scala.quoted.Quotes +import scala.quoted.Varargs + +object NoComma { + inline def nocomma(inline a: SettingsDefinition): Vector[Setting[?]] = + ${ nocommaImpl('a) } + + def nocommaImpl(a: Expr[SettingsDefinition])(using q: Quotes): Expr[Vector[Setting[?]]] = { + import q.reflect.* + a.asTerm match { + case Inlined(_, _, t: Block) => + val values = (t.statements :+ t.expr).map { + case t: Term => + t.asExpr match { + case '{ $x: Seq[Setting[?]] } => + '{ Def.SettingList($x) } + case '{ $x: Setting[?] } => + '{ Def.SettingList($x :: Nil) } + case '{ $x: SettingsDefinition } => + x + case _ => + report.errorAndAbort("unexpected") + } + case _ => + report.errorAndAbort("unexpected") + } + '{ Vector[SettingsDefinition](${ Varargs(values) }*).flatMap(_.settings) } + case other => + report.errorAndAbort("unexpected") + } + } +} diff --git a/src/main/scala-3/NoCommaPlugin.scala b/src/main/scala-3/NoCommaPlugin.scala new file mode 100644 index 0000000..6a6a92d --- /dev/null +++ b/src/main/scala-3/NoCommaPlugin.scala @@ -0,0 +1,11 @@ +package sbtnocomma + +import sbt.* + +object NoCommaPlugin extends AutoPlugin { + override def trigger = allRequirements + object autoImport { + inline def nocomma(inline a: SettingsDefinition): Vector[Setting[?]] = + ${ NoComma.nocommaImpl('a) } + } +} diff --git a/src/test/scala/NoCommaTest.scala b/src/test/scala/NoCommaTest.scala index d5905c7..98aa4e7 100644 --- a/src/test/scala/NoCommaTest.scala +++ b/src/test/scala/NoCommaTest.scala @@ -1,9 +1,9 @@ -import org.scalatest._ +import org.scalatest.flatspec._ import sbtnocomma.NoComma._ import sbt._, Keys._ -class NoCommaSpec extends FlatSpec with Matchers { +class NoCommaSpec extends AnyFlatSpec { "nocomma" should "expand to a Vector" in { val seq = Seq( scalacOptions += "-deprecation" @@ -11,12 +11,12 @@ class NoCommaSpec extends FlatSpec with Matchers { val xs = nocomma { seq name := "something" - organization in ThisBuild := "com.example" + ThisBuild / organization := "com.example" } val ys = Vector[SettingsDefinition]( seq, name := "something", - organization in ThisBuild := "com.example", + ThisBuild / organization := "com.example", ) assert((xs map { _.key }) === (ys flatMap { _.settings.map(_.key) })) } @@ -42,5 +42,5 @@ class NoCommaSpec extends FlatSpec with Matchers { } def f1 = 1 - */ + */ }