-
Notifications
You must be signed in to change notification settings - Fork 182
/
ScalafixArgumentsImpl.scala
204 lines (176 loc) · 6.25 KB
/
ScalafixArgumentsImpl.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
package scalafix.internal.interfaces
import java.io.PrintStream
import java.net.{URL, URLClassLoader}
import java.nio.charset.Charset
import java.nio.file.Path
import java.nio.file.PathMatcher
import java.util
import java.util.Optional
import coursierapi.Repository
import metaconfig.Conf
import metaconfig.Configured
import scala.jdk.CollectionConverters._
import scala.meta.io.AbsolutePath
import scala.meta.io.Classpath
import scala.util.control.NoStackTrace
import scalafix.interfaces.ScalafixArguments
import scalafix.interfaces.ScalafixError
import scalafix.interfaces.ScalafixException
import scalafix.interfaces.ScalafixMainCallback
import scalafix.interfaces.ScalafixMainMode
import scalafix.interfaces.ScalafixRule
import scalafix.internal.v1.Args
import scalafix.internal.v1.MainOps
import scalafix.internal.v1.Rules
import scalafix.v1.RuleDecoder
import scalafix.Versions
final case class ScalafixArgumentsImpl(args: Args = Args.default)
extends ScalafixArguments {
override def run(): Array[ScalafixError] = {
val exit = MainOps.run(Array(), args)
ScalafixErrorImpl.fromScala(exit)
}
override def withRules(rules: util.List[String]): ScalafixArguments =
copy(args = args.copy(rules = rules.asScala.toList))
override def withToolClasspath(
customURLs: util.List[URL]
): ScalafixArguments =
withToolClasspath(
new URLClassLoader(customURLs.asScala.toArray, getClass.getClassLoader)
)
override def withToolClasspath(
customURLs: util.List[URL],
customDependenciesCoordinates: util.List[String]
): ScalafixArguments =
withToolClasspath(
customURLs,
customDependenciesCoordinates,
Repository.defaults()
)
override def withToolClasspath(
customURLs: util.List[URL],
customDependenciesCoordinates: util.List[String],
repositories: util.List[Repository]
): ScalafixArguments = {
val customDependenciesJARs = ScalafixCoursier.toolClasspath(
repositories,
customDependenciesCoordinates,
Versions.scalaVersion
)
val extraURLs = customURLs.asScala ++ customDependenciesJARs.asScala
val classLoader = new URLClassLoader(
extraURLs.toArray,
getClass.getClassLoader
)
withToolClasspath(classLoader)
}
override def withToolClasspath(
classLoader: URLClassLoader
): ScalafixArguments =
copy(args = args.copy(toolClasspath = classLoader))
override def withPaths(paths: util.List[Path]): ScalafixArguments = {
copy(
args = args.copy(
files = paths.asScala.iterator.map(AbsolutePath(_)(args.cwd)).toList
)
)
}
override def withExcludedPaths(
matchers: util.List[PathMatcher]
): ScalafixArguments =
copy(args = args.copy(exclude = matchers.asScala.toList))
override def withWorkingDirectory(path: Path): ScalafixArguments = {
require(path.isAbsolute, s"working directory must be relative: $path")
copy(args = args.copy(cwd = AbsolutePath(path)))
}
override def withConfig(path: Optional[Path]): ScalafixArguments = {
val abspath = Option(path.orElse(null)).map(p => AbsolutePath(p)(args.cwd))
copy(args = args.copy(config = abspath))
}
override def withMode(mode: ScalafixMainMode): ScalafixArguments =
mode match {
case ScalafixMainMode.CHECK =>
copy(args = args.copy(check = true))
case ScalafixMainMode.IN_PLACE =>
copy(args = args.copy(stdout = false))
case ScalafixMainMode.STDOUT =>
copy(args = args.copy(stdout = true))
case ScalafixMainMode.AUTO_SUPPRESS_LINTER_ERRORS =>
copy(args = args.copy(autoSuppressLinterErrors = true))
}
override def withParsedArguments(
args: util.List[String]
): ScalafixArguments = {
if (args.isEmpty) this
else {
val decoder = Args.decoder(this.args)
val newArgs = Conf
.parseCliArgs[Args](args.asScala.toList)
.andThen(c => c.as[Args](decoder)) match {
case Configured.Ok(value) =>
value
case Configured.NotOk(error) =>
throw new IllegalArgumentException(error.toString())
}
copy(args = newArgs)
}
}
override def withPrintStream(out: PrintStream): ScalafixArguments =
copy(args = args.copy(out = out))
override def withClasspath(path: util.List[Path]): ScalafixArguments =
copy(
args = args.copy(
classpath =
Classpath(path.asScala.iterator.map(AbsolutePath(_)(args.cwd)).toList)
)
)
override def withSourceroot(path: Path): ScalafixArguments = {
require(path.isAbsolute, s"sourceroot must be relative: $path")
copy(args = args.copy(sourceroot = Some(AbsolutePath(path)(args.cwd))))
}
override def withMainCallback(
callback: ScalafixMainCallback
): ScalafixArguments =
copy(args = args.copy(callback = callback))
override def withCharset(charset: Charset): ScalafixArguments =
copy(args = args.copy(charset = charset))
override def availableRules(): util.List[ScalafixRule] = {
Rules
.all(args.toolClasspath)
.map(rule => ScalafixRuleImpl(rule))
.asJava
}
override def rulesThatWillRun(): util.List[ScalafixRule] = {
val decoder = RuleDecoder.decoder(args.ruleDecoderSettings)
val rules = decoder
.read(args.rulesConf(() => args.fileConfig.getOrException))
.getOrException
rules.rules.map(rule => ScalafixRuleImpl(rule)).asJava
}
override def withScalacOptions(
options: util.List[String]
): ScalafixArguments =
copy(args = args.copy(scalacOptions = options.asScala.toList))
override def withScalaVersion(version: String): ScalafixArguments =
copy(args = args.copy(scalaVersion = version))
override def validate(): Optional[ScalafixException] = {
args.validate match {
case Configured.Ok(_) =>
Optional.empty()
case Configured.NotOk(error) =>
Optional.of(new ScalafixMainArgsException(error.toString()))
}
}
implicit class XtensionConfigured[T](c: Configured[T]) {
def getOrException: T = c match {
case Configured.Ok(value) => value
case Configured.NotOk(error) =>
throw new ScalafixMainArgsException(error.toString())
}
}
}
class ScalafixMainArgsException(msg: String, cause: Throwable)
extends ScalafixException(msg, cause)
with NoStackTrace {
def this(msg: String) = this(msg, null)
}