Skip to content

Commit

Permalink
Merge pull request #54 from DavidGregory084/modes
Browse files Browse the repository at this point in the history
Add dev, CI, release mode functionality to the plugin
  • Loading branch information
DavidGregory084 authored Mar 29, 2022
2 parents 5d5688f + fb85ad1 commit 6a13e0d
Show file tree
Hide file tree
Showing 15 changed files with 1,257 additions and 301 deletions.
7 changes: 7 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rules = [
OrganizeImports
]

OrganizeImports {
preset = INTELLIJ_2020_3
}
13 changes: 13 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version = 3.4.3

runner.dialect = scala212

preset = defaultWithAlign

maxColumn = 100

indent {
callSite = 2
defnSite = 2
extendSite = 2
}
75 changes: 72 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,83 @@ Add the following to your project's `project/plugins.sbt`:
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.1.22")
```

If necessary you can filter out scalac options that are unhelpful in the REPL from user-defined tasks or scopes.
To filter out scala compiler options that don't work well in the REPL, use the `tpolecatConsoleOptionsFilter`.

By default the plugin only applies this filtering to the `console` task in the `Compile` and `Test` configurations.
By default the plugin only applies this filter to the `console` task in the `Compile` and `Test` configurations.

For example, to apply this filter to the `console` task in the `IntegrationTest` configuration you can do the following:

```scala
IntegrationTest / console / tpolecatScalacOptions ~= tpolecatConsoleOptionsFilter
```

### Modes

This plugin can be used in several modes. The default mode is the continous integration mode.

Modes can selected by using mode switching commands, or by setting environment variables.

When multiple mode-setting environment variables are defined, the most restrictive mode is selected. For example, if the `SBT_TPOLECAT_DEV` and `SBT_TPOLECAT_CI` variables are both defined, continuous integration mode will be enabled.

You can customise the default mode by modifying the `ThisBuild / tpolecatDefaultOptionsMode` key. Default: `CiMode`.

#### Development mode

To enable the development mode, use the `tpolecatDevMode` command or define the environment variable `SBT_TPOLECAT_DEV`.

In this mode a baseline set of scala compiler options are enabled.

You can customise the options that are enabled in this mode by modifying the `tpolecatDevModeOptions` key. Default: `ScalacOptions.default`.

For example, to disable macros you could customise the development mode options as follows:

```scala
tpolecatDevModeOptions ~= { opts =>
opts.filterNot(Set(ScalacOptions.languageExperimentalMacros))
}
```

You can customise the environment variable that is used to enable this mode by modifying the `ThisBuild / tpolecatDevModeEnvVar` key. Default: `"SBT_TPOLECAT_DEV"`.

#### Continuous integration mode

To enable the continuous integration mode, use the `tpolecatCiMode` command or define the environment variable `SBT_TPOLECAT_CI`.

In this mode all development mode options are enabled, and the fatal warning options (`-Xfatal-warnings` / `-Werror`) are added as appropriate for your Scala version.

You can customise the options that are enabled in this mode by modifying the `tpolecatCiModeOptions` key. Default: `tpolecatDevModeOptions.value ++ ScalacOptions.fatalWarningOptions`.

For example, to disable unused linting you could customise the CI options as follows:

```scala
scalacOptions.in(Tut) ~= filterConsoleScalacOptions
tpolecatCiModeOptions ~= { opts =>
opts.filterNot(
ScalacOptions.privateWarnUnusedOptions ++
ScalacOptions.warnUnusedOptions
)
}
```

You can customise the environment variable that is used to enable this mode by modifying the `ThisBuild / tpolecatCiModeEnvVar` key. Default: `"SBT_TPOLECAT_CI"`.

#### Release mode

To enable the release mode, use the `tpolecatReleaseMode` command or define the environment variable `SBT_TPOLECAT_RELEASE`.

In this mode all CI mode options are enabled, and the method-local optimisation option (`-opt:l:method`) is enabled if available for your Scala version.

You can customise the options that are enabled in this mode by modifying the `tpolecatReleaseModeOptions` key. Default: `tpolecatCiModeOptions.value + ScalacOptions.optimizerMethodLocal`.

For example, to enable inlining within your library or application's packages you could customise the release options as follows:

```scala
tpolecatReleaseModeOptions ++= ScalacOptions.optimizerOptions("your.library.**")
```

To understand more about the Scala optimizer read [The Scala 2.12 / 2.13 Inliner and Optimizer](https://docs.scala-lang.org/overviews/compiler-options/optimizer.html).

You can customise the environment variable that is used to enable this mode by modifying the `ThisBuild / tpolecatReleaseModeEnvVar` key. Default: `"SBT_TPOLECAT_RELEASE"`.

### Caveat

I can't promise this plugin will work for old minor releases of Scala.
Expand Down
21 changes: 14 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Common settings

name := "sbt-tpolecat"
description := "scalac options for the enlightened"
name := "sbt-tpolecat"
description := "scalac options for the enlightened"
organization := "io.github.davidgregory084"

organizationName := "David Gregory"
startYear := Some(2022)
startYear := Some(2022)
licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html"))
scmInfo := Some(
ScmInfo(
Expand Down Expand Up @@ -34,20 +34,27 @@ addSbtPlugin("org.lyranthe.sbt" % "partial-unification" % "1.1.2")
// License headers

Compile / headerCreate := { (Compile / headerCreate).triggeredBy(Compile / compile).value }
Test / headerCreate := { (Test / headerCreate).triggeredBy(Test / compile).value }
Test / headerCreate := { (Test / headerCreate).triggeredBy(Test / compile).value }

scalacOptions += "-Xlint:unused"

libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.11" % Test,
"org.scalacheck" %% "scalacheck" % "1.15.4" % Test,
"org.scalatest" %% "scalatest" % "3.2.11" % Test,
"org.scalacheck" %% "scalacheck" % "1.15.4" % Test,
"org.scalatestplus" %% "scalacheck-1-15" % "3.2.11.0" % Test
)

ThisBuild / semanticdbEnabled := true
ThisBuild / semanticdbVersion := scalafixSemanticdb.revision
ThisBuild / scalafixDependencies += "com.github.liancheng" %% "organize-imports" % "0.6.0"

// Testing

scriptedBufferLog := false

scriptedLaunchOpts := scriptedLaunchOpts.value ++ Seq(
"-Xmx1024M", "-Dplugin.version=" + version.value
"-Xmx1024M",
"-Dplugin.version=" + version.value
)

test := {
Expand Down
4 changes: 4 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.6.5")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7")

addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.6.2")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.34")
23 changes: 23 additions & 0 deletions src/main/scala/io/github/davidgregory084/OptionsMode.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2022 David Gregory
*
* 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 io.github.davidgregory084

sealed abstract class OptionsMode extends Product with Serializable

case object DevMode extends OptionsMode
case object CiMode extends OptionsMode
case object ReleaseMode extends OptionsMode
38 changes: 38 additions & 0 deletions src/main/scala/io/github/davidgregory084/ScalaVersion.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2022 David Gregory
*
* 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 io.github.davidgregory084

import scala.Ordering.Implicits._

case class ScalaVersion(major: Long, minor: Long, patch: Long) {
def isBetween(addedVersion: ScalaVersion, removedVersion: ScalaVersion) =
this >= addedVersion && this < removedVersion
}

object ScalaVersion {
val V2_11_0 = ScalaVersion(2, 11, 0)
val V2_12_0 = ScalaVersion(2, 12, 0)
val V2_13_0 = ScalaVersion(2, 13, 0)
val V2_13_3 = ScalaVersion(2, 13, 3)
val V2_13_4 = ScalaVersion(2, 13, 4)
val V2_13_5 = ScalaVersion(2, 13, 5)
val V2_13_6 = ScalaVersion(2, 13, 6)
val V3_0_0 = ScalaVersion(3, 0, 0)

implicit val scalaVersionOrdering: Ordering[ScalaVersion] =
Ordering.by(version => (version.major, version.minor, version.patch))
}
39 changes: 39 additions & 0 deletions src/main/scala/io/github/davidgregory084/ScalacOption.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2022 David Gregory
*
* 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 io.github.davidgregory084

/** A Scala compiler option.
*
* @param tokens
* The tokens which must be provided to declare this option.
* @param isSupported
* A predicate function determining whether the provided Scala compiler version supports this
* option.
*/
class ScalacOption(
val tokens: List[String],
val isSupported: ScalaVersion => Boolean = _ => true
) {
override def hashCode(): Int =
41 * tokens.hashCode

override def equals(other: Any): Boolean =
other match {
case that: ScalacOption => this.tokens == that.tokens
case _ => false
}
}
Loading

0 comments on commit 6a13e0d

Please sign in to comment.