-
Notifications
You must be signed in to change notification settings - Fork 185
/
Args.scala
453 lines (428 loc) · 15.6 KB
/
Args.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package scalafix.internal.v1
import java.io.File
import java.io.PrintStream
import java.net.URI
import java.net.URLClassLoader
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.nio.file.FileSystems
import java.nio.file.PathMatcher
import java.nio.file.Paths
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException
import metaconfig.Configured._
import metaconfig._
import metaconfig.annotation._
import metaconfig.generic.Surface
import metaconfig.internal.ConfGet
import metaconfig.typesafeconfig.typesafeConfigMetaconfigParser
import pprint.TPrint
import scala.annotation.StaticAnnotation
import scala.language.higherKinds
import scala.meta.internal.io.PathIO
import scala.meta.internal.symtab.SymbolTable
import scala.meta.io.AbsolutePath
import scala.meta.io.Classpath
import scala.util.Failure
import scala.util.Success
import scala.util.Try
import scalafix.interfaces.ScalafixMainCallback
import scalafix.internal.config.FilterMatcher
import scalafix.internal.config.PrintStreamReporter
import scalafix.internal.config.ScalafixConfig
import scalafix.internal.diff.DiffDisable
import scalafix.internal.interfaces.MainCallbackImpl
import scalafix.internal.jgit.JGitDiff
import scalafix.internal.reflect.ClasspathOps
import scalafix.v1.Configuration
import scalafix.v1.RuleDecoder
class Section(val name: String) extends StaticAnnotation
case class Args(
@Section("Common options")
@Description(
"Scalafix rules to run, for example ExplicitResultTypes. " +
"The syntax for rules is documented in https://scalacenter.github.io/scalafix/docs/users/configuration#rules"
)
@ExtraName("r")
@Repeated
rules: List[String] = Nil,
@Description("Files or directories (recursively visited) to fix.")
@ExtraName("remainingArgs")
@ExtraName("f")
files: List[AbsolutePath] = Nil,
@Description(
"File path to a .scalafix.conf configuration file. " +
"Defaults to .scalafix.conf in the current working directory, if any."
)
config: Option[AbsolutePath] = None,
@Description(
"Check that all files have been fixed with scalafix, exiting with non-zero code on violations. " +
"Won't write to files."
)
@ExtraName("test")
check: Boolean = false,
@Description("Print fixed output to stdout instead of writing in-place.")
stdout: Boolean = false,
@Description(
"If set, only apply scalafix to added and edited files in git diff against the master branch."
)
diff: Boolean = false,
@Description(
"If set, only apply scalafix to added and edited files in git diff against a provided branch, commit or tag."
)
diffBase: Option[String] = None,
@Description(
"Run only syntactic rules, ignore semantic rules even if they are explicitly " +
"configured in .scalafix.conf or via --rules"
)
syntactic: Boolean = false,
@Description("Print out additional diagnostics while running scalafix.")
verbose: Boolean = false,
@Description("Print out this help message and exit")
@ExtraName("h")
help: Boolean = false,
@Description("Print out version number and exit")
@ExtraName("v")
version: Boolean = false,
@Section("Semantic options")
@Description(
"Full classpath of the files to fix, required for semantic rules. " +
"The source files that should be fixed must be compiled with semanticdb-scalac. " +
"Dependencies are required by rules like ExplicitResultTypes, but the dependencies do not " +
"need to be compiled with semanticdb-scalac."
)
classpath: Classpath = Classpath(Nil),
@Description(
"Absolute path passed to semanticdb with -P:semanticdb:sourceroot:<path>. " +
"Relative filenames persisted in the Semantic DB are absolutized by the " +
"sourceroot. Defaults to current working directory if not provided."
)
sourceroot: Option[AbsolutePath] = None,
@Description(
"If set, automatically infer the --classpath flag by scanning for directories with META-INF/semanticdb"
)
autoClasspath: Boolean = false,
@Description("Additional directories to scan for --auto-classpath")
@ExtraName("classpathAutoRoots")
autoClasspathRoots: List[AbsolutePath] = Nil,
@Description(
"The scala compiler options used to compile this --classpath, for example -Ywarn-unused-import"
)
scalacOptions: List[String] = Nil,
@Description(
"The Scala compiler version that was used to compile this project."
)
scalaVersion: String = scala.util.Properties.versionNumberString,
@Section("Tab completions")
@Description(
"""|Print out bash tab completions. To install:
|```
|# macOS, requires "brew install bash-completion"
|scalafix --bash > /usr/local/etc/bash_completion.d/scalafix
|# Linux
|scalafix --bash > /etc/bash_completion.d/scalafix
|```
|""".stripMargin
)
bash: Boolean = false,
@Description(
"""|Print out zsh tab completions. To install:
|```
|scalafix --zsh > /usr/local/share/zsh/site-functions/_scalafix
|unfunction _scalafix
|autoload -U _scalafix
|```
|""".stripMargin
)
zsh: Boolean = false,
// TODO: --sbt
@Section("Less common options")
@Description(
"Unix-style glob for files to exclude from fixing. " +
"The glob syntax is defined by `nio.FileSystem.getPathMatcher`."
)
exclude: List[PathMatcher] = Nil,
@Description(
"Additional classpath for compiling and classloading custom rules."
)
toolClasspath: URLClassLoader = ClasspathOps.thisClassLoader,
@Description("The encoding to use for reading/writing files")
charset: Charset = StandardCharsets.UTF_8,
@Description("If set, throw exception in the end instead of System.exit")
noSysExit: Boolean = false,
@Description("Don't error on stale semanticdb files.")
noStaleSemanticdb: Boolean = false,
@Description("Custom settings to override .scalafix.conf")
settings: Conf = Conf.Obj.empty,
@Description(
"Write fixed output to custom location instead of in-place. Regex is passed as first argument to file.replaceAll(--out-from, --out-to), requires --out-to."
)
outFrom: Option[String] = None,
@Description(
"Companion of --out-from, string that is passed as second argument to fileToFix.replaceAll(--out-from, --out-to)"
)
outTo: Option[String] = None,
@Description(
"Insert /* scalafix:ok */ suppressions instead of reporting linter errors."
)
autoSuppressLinterErrors: Boolean = false,
@Description("The current working directory")
cwd: AbsolutePath,
@Hidden
@Description("No longer used")
nonInteractive: Boolean = false,
@Hidden
out: PrintStream,
@Hidden
ls: Ls = Ls.Find,
@Hidden
callback: ScalafixMainCallback
) {
override def toString: String = ConfEncoder[Args].write(this).toString()
def configuredSymtab: Configured[SymbolTable] = {
Try(
ClasspathOps.newSymbolTable(
classpath = classpath,
out = out
)
) match {
case Success(symtab) =>
Configured.ok(symtab)
case Failure(e) =>
ConfError.message(s"Unable to load symbol table: ${e.getMessage}").notOk
}
}
def baseConfig: Configured[(Conf, ScalafixConfig, DelegatingMainCallback)] = {
fileConfig.andThen { b =>
val applied = Conf.applyPatch(b, settings)
applied.as[ScalafixConfig].map { scalafixConfig =>
val delegator = new DelegatingMainCallback(callback)
val reporter = MainCallbackImpl.fromJava(delegator)
(applied, scalafixConfig.copy(reporter = reporter), delegator)
}
}
}
def fileConfig: Configured[Conf] = {
val toRead: Option[AbsolutePath] = config.orElse {
val defaultPath = cwd.resolve(".scalafix.conf")
if (defaultPath.isFile) Some(defaultPath)
else None
}
toRead match {
case Some(file) =>
if (file.isFile) {
val input = metaconfig.Input.File(file.toNIO)
Conf.parseInput(input)
} else {
ConfError.fileDoesNotExist(file.toNIO).notOk
}
case _ =>
Configured.ok(Conf.Obj.empty)
}
}
def ruleDecoderSettings: RuleDecoder.Settings = {
RuleDecoder
.Settings()
.withToolClasspath(toolClasspath)
.withCwd(cwd)
.withSyntactic(syntactic)
}
def ruleDecoder(scalafixConfig: ScalafixConfig): ConfDecoder[Rules] = {
RuleDecoder.decoder(ruleDecoderSettings.withConfig(scalafixConfig))
}
def rulesConf(base: () => Conf): Conf = {
if (rules.isEmpty) {
ConfGet.getKey(base(), "rules" :: "rule" :: Nil) match {
case Some(c) => c
case _ => Conf.Lst(Nil)
}
} else {
Conf.Lst(rules.map(Conf.fromString))
}
}
def configuredRules(
base: Conf,
scalafixConfig: ScalafixConfig
): Configured[Rules] = {
val rulesConf = this.rulesConf(() => base)
val decoder = ruleDecoder(scalafixConfig)
val configuration = Configuration()
.withConf(base)
.withScalaVersion(scalaVersion)
.withScalacOptions(scalacOptions)
decoder.read(rulesConf).andThen(_.withConfiguration(configuration))
}
def resolvedPathReplace: Configured[AbsolutePath => AbsolutePath] =
(outFrom, outTo) match {
case (None, None) => Ok(identity[AbsolutePath])
case (Some(from), Some(to)) =>
try {
val outFromPattern = Pattern.compile(from)
def replacePath(file: AbsolutePath): AbsolutePath = AbsolutePath(
Paths.get(
URI.create(
"file:" +
outFromPattern.matcher(file.toURI.getPath).replaceAll(to)
)
)
)
Ok(replacePath _)
} catch {
case e: PatternSyntaxException =>
ConfError
.message(s"Invalid regex '$outFrom'! ${e.getMessage}")
.notOk
}
case (Some(from), _) =>
ConfError
.message(s"--out-from $from must be accompanied with --out-to")
.notOk
case (_, Some(to)) =>
ConfError
.message(s"--out-to $to must be accompanied with --out-from")
.notOk
}
def configuredDiffDisable: Configured[DiffDisable] = {
if (diff || diffBase.nonEmpty) {
val diffBase = this.diffBase.getOrElse("master")
JGitDiff(cwd.toNIO, diffBase)
} else {
Configured.Ok(DiffDisable.empty)
}
}
def configuredSourceroot: Configured[AbsolutePath] = {
val path = sourceroot.getOrElse(cwd)
if (path.isDirectory) Configured.ok(path)
else Configured.error(s"--sourceroot $path is not a directory")
}
def validatedClasspath: Classpath = {
val targetroot = semanticdbOption("targetroot")
.map(option => Classpath(option))
.getOrElse(Classpath(Nil))
val baseClasspath =
if (autoClasspath && classpath.entries.isEmpty) {
val roots =
if (autoClasspathRoots.isEmpty) cwd :: Nil
else autoClasspathRoots
ClasspathOps.autoClasspath(roots)
} else {
classpath
}
baseClasspath ++ targetroot
}
def classLoader: ClassLoader =
ClasspathOps.toOrphanClassLoader(validatedClasspath)
def semanticdbOption(name: String): Option[String] = {
val flag = s"-P:semanticdb:$name:"
scalacOptions
.find(_.startsWith(flag))
.map(_.stripPrefix(flag))
}
def semanticdbFilterMatcher: FilterMatcher = {
val include = semanticdbOption("include")
val exclude = semanticdbOption("exclude")
(include, exclude) match {
case (None, None) => FilterMatcher.matchEverything
case (Some(in), None) => FilterMatcher.include(in)
case (None, Some(ex)) => FilterMatcher.exclude(ex)
case (Some(in), Some(ex)) => FilterMatcher(List(in), List(ex))
}
}
def validate: Configured[ValidatedArgs] = {
baseConfig.andThen {
case (base, scalafixConfig, delegator) =>
(
configuredSourceroot |@|
configuredSymtab |@|
configuredRules(base, scalafixConfig) |@|
resolvedPathReplace |@|
configuredDiffDisable
).map {
case ((((root, symtab), rulez), pathReplace), diffDisable) =>
ValidatedArgs(
this,
symtab,
rulez,
scalafixConfig,
classLoader,
root,
pathReplace,
diffDisable,
delegator,
semanticdbFilterMatcher
)
}
}
}
}
object Args {
val baseMatcher: PathMatcher =
FileSystems.getDefault.getPathMatcher("glob:**.{scala,sbt}")
val default: Args = default(PathIO.workingDirectory, System.out)
def default(cwd: AbsolutePath, out: PrintStream): Args = {
val callback = MainCallbackImpl.fromScala(PrintStreamReporter(out))
new Args(cwd = cwd, out = out, callback = callback)
}
def decoder(base: Args): ConfDecoder[Args] = {
implicit val classpathDecoder: ConfDecoder[Classpath] =
ConfDecoder.stringConfDecoder.map { cp =>
Classpath(
cp.split(File.pathSeparator)
.iterator
.map(path => AbsolutePath(path)(base.cwd))
.toList
)
}
implicit val classLoaderDecoder: ConfDecoder[URLClassLoader] =
ConfDecoder[Classpath].map(ClasspathOps.toClassLoader)
implicit val absolutePathDecoder: ConfDecoder[AbsolutePath] =
ConfDecoder.stringConfDecoder.map(AbsolutePath(_)(base.cwd))
generic.deriveDecoder(base)
}
implicit val charsetDecoder: ConfDecoder[Charset] =
ConfDecoder.stringConfDecoder.map(name => Charset.forName(name))
implicit val printStreamDecoder: ConfDecoder[PrintStream] =
ConfDecoder.stringConfDecoder.map(_ => System.out)
implicit val pathMatcherDecoder: ConfDecoder[PathMatcher] =
ConfDecoder.stringConfDecoder.map(
glob => FileSystems.getDefault.getPathMatcher("glob:" + glob)
)
implicit val callbackDecoder: ConfDecoder[ScalafixMainCallback] =
ConfDecoder.stringConfDecoder.map(_ => MainCallbackImpl.default)
implicit val confEncoder: ConfEncoder[Conf] =
ConfEncoder.ConfEncoder
implicit val pathEncoder: ConfEncoder[AbsolutePath] =
ConfEncoder.StringEncoder.contramap(_.toString())
implicit val classpathEncoder: ConfEncoder[Classpath] =
ConfEncoder.StringEncoder.contramap(_ => "<classpath>")
implicit val classLoaderEncoder: ConfEncoder[URLClassLoader] =
ConfEncoder.StringEncoder.contramap(_ => "<classloader>")
implicit val charsetEncoder: ConfEncoder[Charset] =
ConfEncoder.StringEncoder.contramap(_.name())
implicit val printStreamEncoder: ConfEncoder[PrintStream] =
ConfEncoder.StringEncoder.contramap(_ => "<stdout>")
implicit val pathMatcherEncoder: ConfEncoder[PathMatcher] =
ConfEncoder.StringEncoder.contramap(_.toString)
implicit val callbackEncoder: ConfEncoder[ScalafixMainCallback] =
ConfEncoder.StringEncoder.contramap(_.toString)
implicit val argsEncoder: ConfEncoder[Args] = generic.deriveEncoder
implicit val absolutePathPrint: TPrint[AbsolutePath] =
TPrint.make[AbsolutePath](_ => "<path>")
implicit val pathMatcherPrint: TPrint[PathMatcher] =
TPrint.make[PathMatcher](_ => "<glob>")
implicit val confPrint: TPrint[Conf] =
TPrint.make[Conf](implicit cfg => TPrint.implicitly[ScalafixConfig].render)
implicit def optionPrint[T](
implicit ev: pprint.TPrint[T]
): TPrint[Option[T]] =
TPrint.make { implicit cfg =>
ev.render
}
implicit def iterablePrint[C[x] <: Iterable[x], T](
implicit ev: pprint.TPrint[T]
): TPrint[C[T]] =
TPrint.make { implicit cfg =>
s"[${ev.render} ...]"
}
implicit val argsSurface: Surface[Args] = generic.deriveSurface
}