-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathTpolecatPlugin.scala
229 lines (197 loc) · 8.49 KB
/
TpolecatPlugin.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
/*
* Copyright 2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.typelevel.sbt.tpolecat
import sbt.Keys._
import sbt.{ScalaVersion => _, _}
import scala.util.Try
import org.typelevel.scalacoptions._
object TpolecatPlugin extends AutoPlugin {
override def trigger: PluginTrigger = allRequirements
object autoImport {
private[TpolecatPlugin] def supportedOptionsFor(
version: String,
modeScalacOptions: Set[ScalacOption]
): Set[ScalacOption] = {
(CrossVersion.partialVersion(version), version.split('.')) match {
case (Some((maj, min)), Array(maj2, min2, patch))
if maj.toString == maj2 && min.toString == min2 =>
val patchVersion = patch.takeWhile(_.isDigit)
val binaryVersion = ScalaVersion(maj, min, Try(patchVersion.toLong).getOrElse(0))
ScalacOptions.optionsForVersion(binaryVersion, modeScalacOptions)
case (Some((maj, min)), _) =>
val binaryVersion = ScalaVersion(maj, min, 0)
ScalacOptions.optionsForVersion(binaryVersion, modeScalacOptions)
case (None, _) =>
Set.empty[ScalacOption]
}
}
def scalacOptionsFor(
version: String,
modeScalacOptions: Set[ScalacOption]
): Seq[String] = {
supportedOptionsFor(version, modeScalacOptions).toList.flatMap(opt => opt.option :: opt.args)
}
val tpolecatDefaultOptionsMode = settingKey[OptionsMode](
"The default mode to use for configuring scalac options via the sbt-tpolecat plugin."
)
val tpolecatOptionsMode = settingKey[OptionsMode](
"The mode to use for configuring scalac options via the sbt-tpolecat plugin."
)
val tpolecatVerboseModeEnvVar = settingKey[String](
"The environment variable to use to enable the sbt-tpolecat verbose mode."
)
val tpolecatDevModeEnvVar = settingKey[String](
"The environment variable to use to enable the sbt-tpolecat development mode."
)
val tpolecatCiModeEnvVar = settingKey[String](
"The environment variable to use to enable the sbt-tpolecat continuous integration mode."
)
val tpolecatReleaseModeEnvVar = settingKey[String](
"The environment variable to use to enable the sbt-tpolecat release mode."
)
val tpolecatVerboseModeOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be applied by the sbt-tpolecat plugin in the verbose mode."
)
val tpolecatDevModeOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be applied by the sbt-tpolecat plugin in the development mode."
)
val tpolecatCiModeOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be applied by the sbt-tpolecat plugin in the continuous integration mode."
)
val tpolecatReleaseModeOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be applied by the sbt-tpolecat plugin in the release mode."
)
val tpolecatScalacOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be applied by the sbt-tpolecat plugin."
)
val tpolecatExcludeOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will be excluded."
)
val tpolecatEffectiveScalacOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that will effectively be applied by the sbt-tpolecat. For internal use only."
).withRank(sbt.KeyRanks.Invisible)
val tpolecatManagedScalacOptions = settingKey[Set[ScalacOption]](
"The set of scalac options that sbt-tpolecat owns and manages. Defaults to anything it ever adds in any scope delegation chain."
).withRank(sbt.KeyRanks.DSetting)
}
import autoImport._
val commandAliases =
addCommandAlias(
"tpolecatVerboseMode",
"set every tpolecatOptionsMode := _root_.org.typelevel.sbt.tpolecat.VerboseMode"
) ++
addCommandAlias(
"tpolecatDevMode",
"set every tpolecatOptionsMode := _root_.org.typelevel.sbt.tpolecat.DevMode"
) ++
addCommandAlias(
"tpolecatCiMode",
"set every tpolecatOptionsMode := _root_.org.typelevel.sbt.tpolecat.CiMode"
) ++
addCommandAlias(
"tpolecatReleaseMode",
"set every tpolecatOptionsMode := _root_.org.typelevel.sbt.tpolecat.ReleaseMode"
)
override def buildSettings: Seq[Setting[_]] = Seq(
tpolecatDefaultOptionsMode := CiMode,
tpolecatVerboseModeEnvVar := "SBT_TPOLECAT_VERBOSE",
tpolecatDevModeEnvVar := "SBT_TPOLECAT_DEV",
tpolecatCiModeEnvVar := "SBT_TPOLECAT_CI",
tpolecatReleaseModeEnvVar := "SBT_TPOLECAT_RELEASE",
tpolecatOptionsMode := {
if (sys.env.contains(tpolecatReleaseModeEnvVar.value)) ReleaseMode
else if (sys.env.contains(tpolecatCiModeEnvVar.value)) CiMode
else if (sys.env.contains(tpolecatDevModeEnvVar.value)) DevMode
else if (sys.env.contains(tpolecatVerboseModeEnvVar.value)) VerboseMode
else tpolecatDefaultOptionsMode.value
},
tpolecatDevModeOptions := ScalacOptions.default
) ++ commandAliases
private def removeOption(options: List[String], optionToRemove: ScalacOption): List[String] = {
val option = optionToRemove.option
val args = optionToRemove.args
if (args.isEmpty) {
// fast path
options.filterNot(_ == option)
} else {
def loop(options: List[String]): List[String] = options match {
case Nil => Nil
case `option` :: tail if tail.startsWith(args) => loop(tail.drop(args.size))
case head :: tail => head :: loop(tail)
}
loop(options)
}
}
private def addOption(options: List[String], optionToAdd: ScalacOption): List[String] = {
val option = optionToAdd.option
val args = optionToAdd.args
if (args.isEmpty) {
// fast path
if (options.contains(option)) options
else options ::: option :: Nil
} else {
if (options.containsSlice(option :: args)) options
else options ::: option :: args
}
}
override def projectSettings: Seq[Setting[_]] = Seq(
Def.derive(
scalacOptions := {
val prevOptions = scalacOptions.value.toList
val managedOptions = tpolecatManagedScalacOptions.value
val effectiveOptions = tpolecatEffectiveScalacOptions.value
val optionsToRemove = managedOptions.diff(effectiveOptions)
val optionsToAdd = effectiveOptions
val optionsWithRemovals = optionsToRemove.foldLeft(prevOptions)(removeOption(_, _))
optionsToAdd.foldLeft(optionsWithRemovals)(addOption(_, _))
}
),
Def.derive(
tpolecatManagedScalacOptions ++= tpolecatEffectiveScalacOptions.value
),
Def.derive(
tpolecatEffectiveScalacOptions := {
val pluginOptions = tpolecatScalacOptions.value
val pluginExcludes = tpolecatExcludeOptions.value
val selectedOptions = pluginOptions.diff(pluginExcludes)
supportedOptionsFor(scalaVersion.value, selectedOptions)
}
),
Def.derive(
tpolecatVerboseModeOptions := tpolecatDevModeOptions.value ++ ScalacOptions.verboseOptions
),
Def.derive(
tpolecatCiModeOptions := tpolecatDevModeOptions.value + ScalacOptions.fatalWarnings
),
Def.derive(
tpolecatReleaseModeOptions := tpolecatCiModeOptions.value + ScalacOptions.optimizerMethodLocal
),
Def.derive(tpolecatScalacOptions := {
tpolecatOptionsMode.value match {
case VerboseMode => tpolecatVerboseModeOptions.value
case DevMode => tpolecatDevModeOptions.value
case CiMode => tpolecatCiModeOptions.value
case ReleaseMode => tpolecatReleaseModeOptions.value
}
}),
Compile / console / tpolecatExcludeOptions ++= ScalacOptions.defaultConsoleExclude,
Test / console / tpolecatExcludeOptions ++= ScalacOptions.defaultConsoleExclude
)
override def globalSettings: Seq[Def.Setting[_]] = Seq(
tpolecatManagedScalacOptions := Set.empty,
tpolecatExcludeOptions := Set.empty
)
}