/
UnusedCodePlugin.scala
209 lines (196 loc) · 6.72 KB
/
UnusedCodePlugin.scala
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
package unused_code
import java.nio.charset.StandardCharsets
import sbt.*
import sbt.Keys.*
import sbt.plugins.JvmPlugin
import sjsonnew.Builder
import sjsonnew.JsonFormat
import sjsonnew.Unbuilder
import sjsonnew.support.scalajson.unsafe.CompactPrinter
import scalafix.sbt.ScalafixPlugin
import scala.concurrent.duration.*
object UnusedCodePlugin extends AutoPlugin {
object autoImport {
val unusedCode = taskKey[Unit]("analyze code and output intermediate file")
val unusedCodeConfig = taskKey[UnusedCodeConfig]("config for UnusedCode")
}
import autoImport.*
private[this] implicit val instance: JsonFormat[UnusedCodeConfig] = {
import sjsonnew.BasicJsonProtocol.*
val strFormat = implicitly[JsonFormat[String]]
def from[A, B](f: JsonFormat[A])(f1: A => B, f2: B => A): JsonFormat[B] =
new JsonFormat[B] {
override def write[J](obj: B, builder: Builder[J]) =
f.write(f2(obj), builder)
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]) =
f1(f.read(jsOpt, unbuilder))
}
implicit val durationInstance: JsonFormat[Duration] =
from(strFormat)(Duration.apply, _.toString)
implicit val dialectInstance: JsonFormat[Dialect] =
from(strFormat)(Dialect.map, _.value)
caseClass9(UnusedCodeConfig, UnusedCodeConfig.unapply)(
"files",
"scalafixConfigPath",
"excludeNameRegex",
"excludePath",
"excludeGitLastCommit",
"excludeMainMethod",
"dialect",
"excludeMethodRegex",
"baseDir",
)
}
private[unused_code] implicit class UnusedCodeConfigOps(private val self: UnusedCodeConfig) extends AnyVal {
def toJsonString: String = {
val builder = new Builder(sjsonnew.support.scalajson.unsafe.Converter.facade)
implicitly[JsonFormat[UnusedCodeConfig]].write(self, builder)
CompactPrinter.apply(
builder.result.getOrElse(sys.error("invalid json"))
)
}
}
private def sbtLauncher: Def.Initialize[Task[File]] = Def.task {
val Seq(launcher) = (LocalRootProject / dependencyResolution).value
.retrieve(
dependencyId = "org.scala-sbt" % "sbt-launch" % (unusedCode / sbtVersion).value,
scalaModuleInfo = None,
retrieveDirectory = (ThisBuild / csrCacheDirectory).value,
log = streams.value.log
)
.left
.map(e => throw e.resolveException)
.merge
.distinct
launcher
}
// avoid extraProjects and derivedProjects
// https://github.com/sbt/sbt/issues/6860
// https://github.com/sbt/sbt/issues/4947
private[this] def runUnusedCode(
base: File,
config: UnusedCodeConfig,
launcher: File,
forkOptions: ForkOptions,
): Either[Int, Unit] = {
val buildSbt =
s"""|name := "tmp-unused-code"
|logLevel := Level.Warn
|scalaVersion := "2.13.12"
|libraryDependencies ++= Seq(
| "com.github.xuwei-k" %% "unused-code-scalafix" % "${UnusedCodeBuildInfo.version}"
|)
|Compile / sources := Nil
|""".stripMargin
IO.withTemporaryDirectory { dir =>
val forkOpt = forkOptions.withWorkingDirectory(dir)
val in = dir / "in.json"
IO.write(dir / "build.sbt", buildSbt.getBytes(StandardCharsets.UTF_8))
IO.write(in, config.toJsonString.getBytes(StandardCharsets.UTF_8))
val ret = Fork.java.apply(
forkOpt,
Seq(
"-jar",
launcher.getCanonicalPath,
Seq(
"runMain",
"unused_code.UnusedCode",
s"--input=${in.getCanonicalPath}"
).mkString(" ")
)
)
if (ret == 0) {
Right(())
} else {
Left(ret)
}
}
}
override def trigger: PluginTrigger = allRequirements
override def requires: Plugins = JvmPlugin && ScalafixPlugin
override def projectSettings: Seq[Def.Setting[?]] = Seq(
unusedCode / sources := ((Compile / sources).value ** "*.scala").get,
)
override def buildSettings: Seq[Def.Setting[?]] = Def.settings(
ScalafixPlugin.autoImport.scalafixDependencies += {
"com.github.xuwei-k" %% "unused-code-scalafix" % UnusedCodeBuildInfo.version
},
unusedCode / forkOptions := ForkOptions(),
unusedCodeConfig := Def.taskDyn {
val s = state.value
val dialect = (LocalRootProject / scalaBinaryVersion).value match {
case "2.10" =>
Dialect.Scala210
case "2.11" =>
Dialect.Scala211
case "2.12" =>
if (scalacOptions.value.contains("-Xsource:3")) {
Dialect.Scala212Source3
} else {
Dialect.Scala212
}
case "2.13" =>
if (scalacOptions.value.contains("-Xsource:3")) {
Dialect.Scala213Source3
} else {
Dialect.Scala213
}
case "3" =>
Dialect.Scala3
case _ =>
Dialect.Scala213Source3
}
val extracted = Project.extract(s)
val currentBuildUri = extracted.currentRef.build
val projects = extracted.structure.units
.apply(currentBuildUri)
.defined
.values
.filter(
_.autoPlugins.contains(UnusedCodePlugin)
)
.toList
val baseDir = (LocalRootProject / baseDirectory).value
val sourcesTask: Def.Initialize[Task[Seq[File]]] = projects.map { p =>
LocalProject(p.id) / unusedCode / sources
}.join.map(_.flatten)
sourcesTask.map { files =>
UnusedCodeConfig(
files = files.map { f =>
IO.relativize(baseDir, f).getOrElse(sys.error("invalid file " + f.getCanonicalFile))
}.toList,
scalafixConfigPath = ScalafixPlugin.autoImport.scalafixConfig.?.value.flatten.map { f =>
IO.relativize(baseDir, f).getOrElse(sys.error("invalid file " + f.getCanonicalFile))
},
excludeNameRegex = Set.empty,
excludePath = Set(
"glob:**/target/**",
"glob:**/src_managed/**",
),
excludeGitLastCommit = Some(60.days),
excludeMainMethod = true,
dialect = dialect,
excludeMethodRegex = Set(
"apply",
"unapply",
"unapplySeq",
"update",
),
baseDir = (LocalRootProject / baseDirectory).value.getCanonicalPath
)
}
}.value,
unusedCode := {
val conf = unusedCodeConfig.value
val _ = conf.pathMatchers // check syntax error
val jsonString = conf.toJsonString
streams.value.log.debug(jsonString)
runUnusedCode(
base = (LocalRootProject / baseDirectory).value,
config = conf,
launcher = sbtLauncher.value,
forkOptions = (unusedCode / forkOptions).value
).fold(e => sys.error(s"${unusedCode.key.label} failed ${e}"), x => x)
},
)
}