Skip to content

Commit 2e90590

Browse files
committed
support sbt 2
1 parent dcb3c2f commit 2e90590

5 files changed

Lines changed: 92 additions & 9 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
java-version: ${{ matrix.java }}
2424
cache: sbt
2525
- uses: sbt/setup-sbt@v1
26-
- run: sbt -v test scripted
26+
- run: sbt -v "+ Test/compile" "+ test" scripted

build.sbt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@ val commonSettings = Seq(
2121
Developer("retronym", "Jason Zaugg", "<jzaugg@gmail.com>", url("https://github.com/retronym")),
2222
Developer("johanandren", "Johan Andrén", "<johan@markatta.com>", url("https://github.com/johanandren"))
2323
),
24-
crossSbtVersions := Vector("1.3.0"),
24+
25+
crossScalaVersions += "3.7.2",
26+
27+
pluginCrossBuild / sbtVersion := {
28+
scalaBinaryVersion.value match {
29+
case "2.12" =>
30+
(pluginCrossBuild / sbtVersion).value
31+
case _ =>
32+
"2.0.0-RC2"
33+
}
34+
},
2535

2636
scalacOptions ++= List(
2737
"-unchecked",
2838
"-deprecation",
29-
"-language:_",
3039
"-encoding", "UTF-8"
3140
),
3241

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2014 pl.project13.scala
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.project13.scala.sbt
18+
19+
import _root_.sbt.*
20+
import xsbti.FileConverter
21+
22+
private[sbt] object JmhPluginCompat {
23+
def toPath(a: File): File =
24+
a
25+
26+
def toAttributedFile(a: Attributed[File], converter: FileConverter): Attributed[File] =
27+
a
28+
29+
implicit class DefOps(private val self: _root_.sbt.Def.type) extends AnyVal {
30+
def uncached[A](a: A): A = a
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2014 pl.project13.scala
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.project13.scala.sbt
18+
19+
import java.nio.file.{ Path => NioPath }
20+
import _root_.sbt.*
21+
import xsbti.{ FileConverter, HashedVirtualFileRef }
22+
23+
private[sbt] object JmhPluginCompat {
24+
def toPath(a: File): NioPath =
25+
a.toPath
26+
27+
def toAttributedFile(a: Attributed[HashedVirtualFileRef], converter: FileConverter): Attributed[File] =
28+
a.map(converter.toPath(_).toFile)
29+
}

plugin/src/main/scala/pl/project13/scala/sbt/JmhPlugin.scala

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package pl.project13.scala.sbt
1818

1919
import sbt.*
2020
import sbt.Keys.*
21+
import sjsonnew.BasicJsonProtocol.*
22+
import pl.project13.scala.sbt.JmhPluginCompat.*
23+
import xsbti.FileConverter
2124

2225
import java.util.Properties
2326

@@ -51,13 +54,23 @@ object JmhPlugin extends AutoPlugin {
5154
run / fork := true, // makes sure that sbt manages classpath for JMH when forking
5255
// allow users to configure another classesDirectory like e.g. test:classDirectory
5356
classDirectory := (Compile / classDirectory).value,
54-
dependencyClasspath := (Compile / dependencyClasspath).value,
57+
dependencyClasspath := Def.uncached((Compile / dependencyClasspath).value),
5558

5659
resourceDirectory := (Compile / resourceDirectory).value,
5760
sourceGenerators := Seq(Def.task { generateJmhSourcesAndResources.value._1 }.taskValue),
5861
resourceGenerators := Seq(Def.task { generateJmhSourcesAndResources.value._2 }.taskValue),
59-
generateJmhSourcesAndResources := generateBenchmarkSourcesAndResources(streams.value, crossTarget.value / "jmh-cache", (Jmh / classDirectory).value, sourceManaged.value, resourceManaged.value, generatorType.value, (Jmh / dependencyClasspath).value, new ForkRun(ForkOptions())),
60-
generateJmhSourcesAndResources := (generateJmhSourcesAndResources dependsOn(Compile / compile)).value,
62+
generateJmhSourcesAndResources := Def.uncached {
63+
generateBenchmarkSourcesAndResources(
64+
streams.value,
65+
crossTarget.value / "jmh-cache",
66+
(Jmh / classDirectory).value, sourceManaged.value,
67+
resourceManaged.value,
68+
generatorType.value,
69+
(Jmh / dependencyClasspath).value.map(JmhPluginCompat.toAttributedFile(_, fileConverter.value)),
70+
new ForkRun(ForkOptions())
71+
)
72+
},
73+
generateJmhSourcesAndResources := generateJmhSourcesAndResources.dependsOn(Compile / compile).value,
6174

6275
// local copy of https://github.com/sbt/sbt/blob/e4231ac03903e174bc9975ee00d34064a1d1f373/main/src/main/scala/sbt/Keys.scala#L400
6376
// so that it does not break on sbt version below 1.4.0
@@ -90,7 +103,7 @@ object JmhPlugin extends AutoPlugin {
90103
}
91104

92105
private def generateBenchmarkSourcesAndResources(s: TaskStreams, cacheDir: File, bytecodeDir: File, sourceDir: File, resourceDir: File, generatorType: String, classpath: Seq[Attributed[File]], run: ScalaRun): (Seq[File], Seq[File]) = {
93-
val inputs: Set[File] = (bytecodeDir ** "*").filter(_.isFile).get.toSet
106+
val inputs: Set[File] = (bytecodeDir ** "*").filter(_.isFile).get().toSet
94107
val cachedGeneration = FileFunction.cached(cacheDir, FilesInfo.hash) { _ =>
95108
// ignore change report and rebuild it completely
96109
internalGenerateBenchmarkSourcesAndResources(s, bytecodeDir, sourceDir, resourceDir, generatorType, classpath, run, s.log)
@@ -109,7 +122,7 @@ object JmhPlugin extends AutoPlugin {
109122

110123
val mainClass = "org.openjdk.jmh.generators.bytecode.JmhBytecodeGenerator"
111124
val options = Seq(bytecodeDir, sourceDir, resourceDir, generatorType).map(_.toString)
112-
run.run(mainClass, classpath.map(_.data), options, log).get
113-
((sourceDir ** "*").filter(_.isFile) +++ (resourceDir ** "*").filter(_.isFile)).get.toSet
125+
run.run(mainClass, classpath.map(x => JmhPluginCompat.toPath(x.data)), options, log).get
126+
((sourceDir ** "*").filter(_.isFile) +++ (resourceDir ** "*").filter(_.isFile)).get().toSet
114127
}
115128
}

0 commit comments

Comments
 (0)