forked from wartremover/wartremover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sbt
414 lines (395 loc) · 12.4 KB
/
build.sbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import ReleaseTransformations._
import com.jsuereth.sbtpgp.PgpKeys
import xsbti.api.ClassLike
import xsbti.api.DefinitionType
import scala.collection.compat._
import scala.reflect.NameTransformer
import java.lang.reflect.Modifier
Global / onChangedBuildSource := ReloadOnSourceChanges
// compiler plugin should be fully cross-versioned. e.g.
// - https://github.com/ghik/silencer/issues/31
// - https://github.com/typelevel/kind-projector/issues/15
//
// add more scala versions when found binary and/or source incompatibilities in scala-compiler
lazy val allScalaVersions = Seq(
"2.12.13",
"2.12.14",
"2.12.15",
"2.12.16",
"2.12.17",
"2.12.18-M2",
"2.13.6",
"2.13.7",
"2.13.8",
"2.13.9",
"2.13.10",
"2.13.11-M2",
"3.1.1",
"3.1.2",
"3.1.3",
"3.2.0",
"3.2.1",
"3.2.2",
"3.3.0-RC6",
)
def latestScala212 = latest(12, allScalaVersions)
def latestScala213 = latest(13, allScalaVersions)
def latestScala3 = allScalaVersions.filterNot(_ contains "-RC").last // TODO more better way
addCommandAlias("SetLatestStableScala3", s"""++ ${latestScala3}! -v""")
def latest(n: Int, versions: Seq[String]) = {
val prefix = "2." + n + "."
prefix + versions
.filter(_ startsWith prefix)
.flatMap(_.drop(prefix.length).toLongOption)
.reduceLeftOption(_ max _)
.getOrElse(
sys.error(s"not found Scala ${prefix}x version ${versions}")
)
}
val scalaCompilerDependency = Def.settings(
libraryDependencies += {
if (scalaBinaryVersion.value == "3") {
"org.scala-lang" %% "scala3-compiler" % scalaVersion.value
} else {
"org.scala-lang" % "scala-compiler" % scalaVersion.value
}
},
)
lazy val baseSettings = Def.settings(
scalacOptions ++= Seq(
"-deprecation"
),
scalacOptions ++= {
scalaBinaryVersion.value match {
case "2.12" =>
Seq("-language:higherKinds")
case _ =>
Nil
}
},
Test / javaOptions ++= Seq("-Xmx5G"),
run / fork := true,
scalaVersion := latestScala212,
)
lazy val commonSettings = Def.settings(
baseSettings,
libraryDependencies ++= {
Seq("org.scalatest" %% "scalatest-funsuite" % "3.2.16" % "test")
},
Seq(packageBin, packageDoc, packageSrc).flatMap {
// include LICENSE file in all packaged artifacts
inTask(_)(
Seq(
(Compile / mappings) += ((ThisBuild / baseDirectory).value / "LICENSE") -> "LICENSE"
)
)
},
organization := "org.wartremover",
licenses := Seq(
"The Apache Software License, Version 2.0" ->
url("http://www.apache.org/licenses/LICENSE-2.0.txt")
),
publishMavenStyle := true,
Test / publishArtifact := false,
(Compile / doc / scalacOptions) ++= {
if (scalaBinaryVersion.value == "3") {
Nil // TODO
} else {
val base = (LocalRootProject / baseDirectory).value.getAbsolutePath
val t = sys.process.Process("git rev-parse HEAD").lineStream_!.head
Seq(
"-sourcepath",
base,
"-doc-source-url",
"https://github.com/wartremover/wartremover/tree/" + t + "€{FILE_PATH}.scala"
)
}
},
publishTo := sonatypePublishToBundle.value,
homepage := Some(url("https://github.com/wartremover/wartremover")),
pomExtra :=
<scm>
<url>[email protected]:wartremover/wartremover.git</url>
<connection>scm:git:[email protected]:wartremover/wartremover.git</connection>
</scm>
<developers>
<developer>
<id>puffnfresh</id>
<name>Brian McKenna</name>
<url>http://brianmckenna.org/</url>
</developer>
<developer>
<name>Chris Neveu</name>
<url>http://chrisneveu.com</url>
</developer>
</developers>
)
commonSettings
publishArtifact := false
releaseCrossBuild := true
releaseProcess := Seq[ReleaseStep](
checkSnapshotDependencies,
inquireVersions,
setReleaseVersion,
commitReleaseVersion,
tagRelease,
releaseStepCommandAndRemaining("+publishSigned"),
releaseStepCommand("sonatypeBundleRelease"),
setNextVersion,
commitNextVersion,
pushChanges
)
val coreId = "core"
def crossSrcSetting(c: Configuration) = {
c / unmanagedSourceDirectories ++= {
val dir = (LocalProject(coreId) / baseDirectory).value / "src" / Defaults.nameForSrc(c.name)
PartialFunction
.condOpt(CrossVersion.partialVersion(scalaVersion.value)) {
case Some((2, v)) if v <= 12 =>
dir / s"scala-2.13-"
case _ =>
dir / s"scala-2.13+"
}
.toList
}
}
val coreSettings = Def.settings(
commonSettings,
name := "wartremover",
Test / fork := true,
crossScalaVersions := allScalaVersions,
Test / scalacOptions += {
val hash = (Compile / sources).value.map { f =>
sbt.internal.inc.HashUtil.farmHash(f.toPath)
}.sum
s"-Dplease-recompile-because-main-source-files-changed-${hash}"
},
Test / scalacOptions ++= {
if (scalaBinaryVersion.value == "3") {
Seq("-source:3.0-migration")
} else {
Nil
}
},
libraryDependencies ++= {
Seq(
"org.scala-lang.modules" %% "scala-xml" % "2.1.0" % "test",
"org.mockito" % "mockito-core" % "4.11.0" % "test",
)
},
scalaCompilerDependency,
pomPostProcess := { node =>
import scala.xml._
import scala.xml.transform._
val strip = new RewriteRule {
override def transform(n: Node) =
if ((n \ "groupId").text == "test-macros" && (n \ "artifactId").text.startsWith("test-macros_"))
NodeSeq.Empty
else
n
}
new RuleTransformer(strip).transform(node)(0)
},
assembly / assemblyOutputPath := file("./wartremover-assembly.jar")
)
lazy val coreCrossBinary = Project(
id = "core-cross-binary",
base = file("core-cross-binary")
).settings(
coreSettings,
crossSrcSetting(Compile),
Compile / scalaSource := (core / Compile / scalaSource).value,
Compile / resourceDirectory := (core / Compile / resourceDirectory).value,
crossScalaVersions := Seq(latestScala212, latestScala213, latestScala3),
crossVersion := CrossVersion.binary
).dependsOn(testMacros % "test->compile")
lazy val inspectorCommon = Project(
id = "inspector-common",
base = file("inspector-common")
).settings(
commonSettings,
publish / skip := (scalaBinaryVersion.value == "2.13"),
crossScalaVersions := Seq(latestScala3, latestScala212),
name := "wartremover-inspector-common",
)
lazy val inspector = Project(
id = "inspector",
base = file("inspector")
).settings(
commonSettings,
name := "wartremover-inspector",
crossScalaVersions := Seq(latestScala3),
publish / skip := (scalaBinaryVersion.value != "3"),
Test / fork := true,
Test / baseDirectory := target.value / "test-base-dir",
Test / testOptions ++= List(
Tests.Setup { () =>
val dir = (Test / baseDirectory).value
IO.delete(dir)
dir.mkdirs()
},
Tests.Cleanup { () =>
IO.delete((Test / baseDirectory).value)
}
),
libraryDependencies ++= {
if (scalaBinaryVersion.value == "3") {
Seq(
"org.scala-sbt" %% "io" % "1.8.1" % Test,
"io.get-coursier" % "coursier" % "2.1.1" % Test cross CrossVersion.for3Use2_13 exclude ("io.argonaut", "*") exclude ("org.scala-lang.modules", "scala-xml_2.13"),
"io.argonaut" %% "argonaut" % "6.3.8",
"org.scala-lang" %% "scala3-tasty-inspector" % scalaVersion.value % Provided,
)
} else {
Nil
}
}
).dependsOn(
coreCrossBinary,
inspectorCommon,
)
lazy val core = Project(
id = coreId,
base = file("core")
).settings(
coreSettings,
crossSrcSetting(Compile),
crossSrcSetting(Test),
crossScalaVersions := allScalaVersions,
crossVersion := CrossVersion.full,
commands += {
object ToInt {
def unapply(s: String): Option[Int] = Some(s.toInt)
}
Command.args("testPartial", "") { case (state, Seq(ToInt(total), ToInt(index))) =>
assert(0 <= index, index)
assert(0 < total, total)
assert(index < total, (total, index))
val allVersions = Project.extract(state).get(crossScalaVersions)
val eachSize = math.ceil(allVersions.size / total.toDouble).toInt
val values = allVersions.sliding(eachSize, eachSize).toList
assert(values.size == total, values.toString)
assert(values.flatten == allVersions, values.toString)
val result = values(index).flatMap { v =>
s"++ ${v}!" :: "test" :: Nil
}.toList
println(result)
result ::: state
}
},
Test / sources := {
val src = (Test / sources).value
if (scalaBinaryVersion.value != "3") {
src
} else if (SemanticSelector(">=3.3.0-RC1").matches(VersionNumber(scalaVersion.value))) {
// maybe https://github.com/lampepfl/dotty/pull/15642
val exclude = Set[String](
"Matchable",
"AnyVal",
).map(_ + "Test.scala")
src.filterNot(f => exclude(f.getName))
} else {
val exclude = Set[String](
"OrTypeLeastUpperBound",
).map(_ + "Test.scala")
src.filterNot(f => exclude(f.getName))
}
},
crossTarget := {
// workaround for https://github.com/sbt/sbt/issues/5097
target.value / s"scala-${scalaVersion.value}"
},
assembly / assemblyOutputPath := file("./wartremover-assembly.jar")
).dependsOn(testMacros % "test->compile")
val wartClasses = Def.task {
val loader = (core / Test / testLoader).value
val wartTraverserClass = Class.forName("org.wartremover.WartTraverser", false, loader)
Tests
.allDefs((core / Compile / compile).value)
.collect { case c: ClassLike =>
val decoded = c.name.split('.').map(NameTransformer.decode).mkString(".")
c.definitionType match {
case DefinitionType.Module =>
decoded + "$"
case _ =>
decoded
}
}
.flatMap(c =>
try {
List[Class[_]](Class.forName(c, false, loader))
} catch {
case _: ClassNotFoundException =>
Nil
}
)
.filter(c => !Modifier.isAbstract(c.getModifiers) && wartTraverserClass.isAssignableFrom(c))
.map(_.getSimpleName.replace("$", ""))
.filterNot(Set("Unsafe", "ForbidInference"))
.sorted
}
lazy val sbtPlug: Project = Project(
id = "sbt-plugin",
base = file("sbt-plugin")
).settings(
commonSettings,
name := "sbt-wartremover",
sbtPlugin := true,
scriptedBufferLog := false,
scriptedLaunchOpts ++= {
val javaVmArgs = {
import scala.collection.JavaConverters._
java.lang.management.ManagementFactory.getRuntimeMXBean.getInputArguments.asScala.toList
}
javaVmArgs.filter(a => Seq("-Xmx", "-Xms", "-XX", "-Dsbt.log.noformat").exists(a.startsWith))
},
libraryDependencies += "io.get-coursier" %% "coursier" % "2.1.4" % Test,
scriptedLaunchOpts += ("-Dplugin.version=" + version.value),
crossScalaVersions := Seq(latestScala212),
(Compile / sourceGenerators) += Def.task {
val base = (Compile / sourceManaged).value
val file = base / "wartremover" / "Wart.scala"
val warts = wartClasses.value
val expectCount = 70
assert(
warts.size == expectCount,
s"${warts.size} != ${expectCount}. please update build.sbt when add or remove wart"
)
val wartsDir = core.base / "src" / "main" / "scala" / "org" / "wartremover" / "warts"
val unsafeSource = IO.read(wartsDir / "Unsafe.scala")
val unsafe = warts.filter(unsafeSource contains _)
assert(unsafe.nonEmpty)
val content =
s"""package wartremover
|// Autogenerated code, see build.sbt.
|final class Wart private[wartremover](val clazz: String) {
| override def toString: String = clazz
|}
|object Wart {
| val PluginVersion: String = "${version.value}"
| private[wartremover] lazy val AllWarts: List[Wart] = List(${warts mkString ", "})
| private[wartremover] lazy val UnsafeWarts: List[Wart] = List(${unsafe mkString ", "})
| /** A fully-qualified class name of a custom Wart implementing `org.wartremover.WartTraverser`. */
| def custom(clazz: String): Wart = new Wart(clazz)
| private[this] def w(nm: String): Wart = new Wart(s"org.wartremover.warts.$$nm")
|""".stripMargin +
warts.map(w => s""" val $w = w("${w}")""").mkString("\n") + "\n}\n"
IO.write(file, content)
Seq(file)
}
).enablePlugins(ScriptedPlugin)
.dependsOn(inspectorCommon)
lazy val testMacros: Project = Project(
id = "test-macros",
base = file("test-macros")
).settings(
baseSettings,
crossScalaVersions := allScalaVersions,
publish / skip := true,
publishArtifact := false,
publish := {},
publishLocal := {},
PgpKeys.publishSigned := {},
PgpKeys.publishLocalSigned := {},
scalaCompilerDependency,
)