Skip to content

Commit

Permalink
model: add strict option for scala
Browse files Browse the repository at this point in the history
when true, add all scalac options for sbt-tpolecat to the given project
  • Loading branch information
oyvindberg committed Jul 26, 2022
1 parent d9248fe commit e729b20
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 55 deletions.
6 changes: 3 additions & 3 deletions bleep-cli-test/src/scala/bleep/TemplateTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.time.Instant

class TemplateTest extends SnapshotTest {
override val outFolder = Paths.get("snapshot-tests").resolve("templates").toAbsolutePath
val scala = model.Scala(Some(Versions.Scala213), Options.empty, None, JsonSet.empty)
val scala = model.Scala(Some(Versions.Scala213), Options.empty, None, JsonSet.empty, None)
val a = noCross("a")
val aTest = noCross("aTest")
val b = noCross("b")
Expand All @@ -33,7 +33,7 @@ class TemplateTest extends SnapshotTest {
}

test("should extract common template and a test template") {
val scala = model.Scala(Some(Versions.Scala213), Options.empty, None, JsonSet.empty)
val scala = model.Scala(Some(Versions.Scala213), Options.empty, None, JsonSet.empty, None)
val projects = Map(
a -> p.copy(scala = Some(scala)),
aTest -> p.copy(scala = Some(scala.copy(options = fooOpt)), isTestProject = Some(true), dependsOn = JsonSet(a.name)),
Expand All @@ -48,7 +48,7 @@ class TemplateTest extends SnapshotTest {
}

test("should heed ignoreWhenInferringTemplates") {
val scala = model.Scala(Some(Versions.Scala213), Options.empty, None, JsonSet.empty)
val scala = model.Scala(Some(Versions.Scala213), Options.empty, None, JsonSet.empty, None)
val projects = Map(
a -> p.copy(scala = Some(scala)),
b -> p.copy(dependsOn = JsonSet(a.name)),
Expand Down
4 changes: 2 additions & 2 deletions bleep-cli/src/scala/bleep/commands/BuildCreateNew.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ object BuildCreateNew {
resources = JsonSet.empty,
dependencies = JsonSet(exampleFiles.fansi),
java = None,
scala = Some(model.Scala(version = Some(scala), options = defaultOpts, setup = None, compilerPlugins = JsonSet.empty)),
scala = Some(model.Scala(version = Some(scala), options = defaultOpts, setup = None, compilerPlugins = JsonSet.empty, strict = Some(true))),
platform = Some(
platformId match {
case model.PlatformId.Jvm =>
Expand Down Expand Up @@ -163,7 +163,7 @@ object BuildCreateNew {
resources = JsonSet.empty,
dependencies = JsonSet(exampleFiles.scalatest),
java = None,
scala = Some(model.Scala(version = Some(scala), options = defaultOpts, setup = None, compilerPlugins = JsonSet.empty)),
scala = Some(model.Scala(version = Some(scala), options = defaultOpts, setup = None, compilerPlugins = JsonSet.empty, strict = Some(true))),
platform = Some(
platformId match {
case model.PlatformId.Jvm => model.Platform.Jvm(Options.empty, jvmMainClass = None, jvmRuntimeOptions = Options.empty)
Expand Down
2 changes: 1 addition & 1 deletion bleep-cli/src/scala/bleep/commands/Import.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ addSbtPlugin("build.bleep" % "sbt-export-dependencies" % "0.1.0")
if (hackDropBleepDependency) JsonSet.empty
else JsonSet(Dep.Scala("build.bleep", "bleep-tasks", constants.BleepVersionTemplate)),
java = None,
scala = Some(model.Scala(scalaVersion, Options.empty, None, JsonSet.empty)),
scala = Some(model.Scala(scalaVersion, Options.empty, None, JsonSet.empty, strict = None)),
platform = Some(model.Platform.Jvm(Options.empty, None, Options.empty)),
isTestProject = None,
testFrameworks = JsonSet.empty
Expand Down
22 changes: 15 additions & 7 deletions bleep-cli/src/scala/bleep/internal/importBloopFilesFromSbt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import bleep.internal.ImportInputProjects.ProjectType
import bleep.logging.Logger
import bloop.config.Config
import coursier.core.Configuration
import io.github.davidgregory084.{DevMode, TpolecatPlugin}

import java.net.URI
import java.nio.file.{Path, Paths}
Expand Down Expand Up @@ -320,11 +321,7 @@ object importBloopFilesFromSbt {
translatedPlatform
}

def translateScala(
compilerPlugins: Seq[Dep],
replacementsDirs: Replacements,
scalaVersions: ScalaVersions
)(s: Config.Scala): model.Scala = {
def translateScala(compilerPlugins: Seq[Dep], replacementsDirs: Replacements, scalaVersions: ScalaVersions)(s: Config.Scala): model.Scala = {
val options = parseOptionsDropSemanticDb(s.options, Some(replacementsDirs))

val notCompilerPlugins = options.values.filter {
Expand All @@ -337,9 +334,19 @@ object importBloopFilesFromSbt {
scalaVersions.compilerPlugin.foldLeft(compilerPlugins) { case (all, fromPlatform) => all.filterNot(_ == fromPlatform) }
}

val (strict, remainingOptions) = {
val tpolecat = new TpolecatPlugin(DevMode)

val tpolecatOptions = Options.parse(tpolecat.scalacOptions(s.version).toList, None)
if (tpolecatOptions.values.forall(notCompilerPlugins.contains))
(Some(true), new Options(notCompilerPlugins -- tpolecatOptions.values))
else
(None, new Options(notCompilerPlugins))
}

model.Scala(
version = Some(Versions.Scala(s.version)),
options = new Options(notCompilerPlugins),
options = remainingOptions,
setup = s.setup.map(setup =>
model.CompileSetup(
order = Some(setup.order),
Expand All @@ -350,7 +357,8 @@ object importBloopFilesFromSbt {
filterLibraryFromClasspath = Some(setup.filterLibraryFromClasspath)
)
),
compilerPlugins = JsonSet.fromIterable(filteredCompilerPlugins)
compilerPlugins = JsonSet.fromIterable(filteredCompilerPlugins),
strict = strict
)
}

Expand Down
2 changes: 1 addition & 1 deletion bleep-cli/src/scala/bleep/rewrites/semanticDb.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ case class semanticDb(buildPaths: BuildPaths) extends Rewrite {

def apply(name: model.CrossProjectName, explodedProject: model.Project): model.Project =
explodedProject.scala match {
case Some(s @ model.Scala(Some(version), _, _, _)) =>
case Some(s @ model.Scala(Some(version), _, _, _, _)) =>
val projectPaths = buildPaths.project(name, explodedProject)
val addedScalacOptions = List(Some(compilerOption(version)), targetRootOptions(version, projectPaths), sourceRootOptions(version)).flatten
val compilerPlugins: JsonSet[Dep] =
Expand Down
11 changes: 10 additions & 1 deletion bleep-core/src/scala/bleep/GenBloopFiles.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import bloop.config.{Config, ConfigCodecs}
import com.github.plokhotnyuk.jsoniter_scala.core.{readFromString, writeToString, WriterConfig}
import coursier.core.{Configuration, Extension}
import coursier.{Classifier, Dependency}
import io.github.davidgregory084.{DevMode, TpolecatPlugin}

import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.{Files, Path}
Expand Down Expand Up @@ -325,7 +326,15 @@ object GenBloopFiles {
}

val scalacOptions: Options =
maybeScala.fold(Options.empty)(_.options).union(compilerPlugins)
maybeScala match {
case Some(scala) =>
val base = scala.options.union(compilerPlugins)
if (scala.strict.getOrElse(false)) {
val tpolecat = Options.parse(new TpolecatPlugin(DevMode).scalacOptions(scalaVersion.scalaVersion).toList, None)
base.union(tpolecat)
} else base
case None => Options.empty
}

Config.Scala(
organization = scalaCompiler.module.organization.value,
Expand Down
16 changes: 10 additions & 6 deletions bleep-core/src/scala/bleep/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,40 @@ object model {
version: Option[Versions.Scala],
options: Options,
setup: Option[CompileSetup],
compilerPlugins: JsonSet[Dep]
compilerPlugins: JsonSet[Dep],
strict: Option[Boolean]
) extends SetLike[Scala] {
override def intersect(other: Scala): Scala =
Scala(
version = if (`version` == other.`version`) `version` else None,
options = options.intersect(other.options),
setup = setup.zipCompat(other.setup).map { case (_1, _2) => _1.intersect(_2) },
compilerPlugins = compilerPlugins.intersect(other.compilerPlugins)
compilerPlugins = compilerPlugins.intersect(other.compilerPlugins),
strict = if (strict == other.strict) strict else None
)

override def removeAll(other: Scala): Scala =
Scala(
version = if (`version` == other.`version`) None else `version`,
options = options.removeAll(other.options),
setup = List(setup, other.setup).flatten.reduceOption(_ removeAll _),
compilerPlugins = compilerPlugins.removeAll(other.compilerPlugins)
compilerPlugins = compilerPlugins.removeAll(other.compilerPlugins),
strict = if (strict == other.strict) None else strict
)

override def union(other: Scala): Scala =
Scala(
version = version.orElse(other.version),
options = options.union(other.options),
setup = List(setup, other.setup).flatten.reduceOption(_ union _),
compilerPlugins = compilerPlugins.union(other.compilerPlugins)
compilerPlugins = compilerPlugins.union(other.compilerPlugins),
strict = strict.orElse(other.strict)
)

override def isEmpty: Boolean =
this match {
case Scala(version, options, setup, compilerPlugins) =>
version.isEmpty && options.isEmpty && setup.fold(true)(_.isEmpty) && compilerPlugins.isEmpty
case Scala(version, options, setup, compilerPlugins, strict) =>
version.isEmpty && options.isEmpty && setup.fold(true)(_.isEmpty) && compilerPlugins.isEmpty && strict.isEmpty
}
}

Expand Down
9 changes: 5 additions & 4 deletions bleep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ projects:
- com.lihaoyi::pprint:0.7.3
- com.monovore::decline:2.3.0
- org.graalvm.nativeimage:svm:22.1.0.1
- exclusions:
org.scala-sbt: util-logging_2.13
module: org.scala-sbt::librarymanagement-core:1.6.1
- org.scalameta:svm-subs:101.0.0
dependsOn: bleep-core
extends:
- template-common
- template-scala-2.13
platform:
mainClass: bleep.Main
sources: ../liberated/sbt-tpolecat/src/main/scala
bleep-cli-test:
dependsOn: bleep-cli
extends:
Expand All @@ -33,6 +29,10 @@ projects:
- io.circe::circe-generic:0.14.2
- io.circe::circe-parser:0.14.2
- io.circe::circe-yaml:0.14.1
- exclusions:
org.scala-sbt: util-logging_2.13
module: org.scala-sbt::librarymanagement-core:1.6.1
for3Use213: true
- exclusions:
org.scala-lang.modules: scala-collection-compat_2.13
for3Use213: true
Expand All @@ -44,6 +44,7 @@ projects:
- org.snakeyaml:snakeyaml-engine:2.3
- org.virtuslab.scala-cli::bloop-rifle:0.1.9
extends: template-cross-all
sources: ../liberated/sbt-tpolecat/src/main/scala
bleep-tasks:
dependsOn: bleep-core
extends: template-cross-all
Expand Down
5 changes: 4 additions & 1 deletion schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"RelPath": {
"type": "string",
"pattern": "^\\./.+$"
"pattern": "^\\.\\.?/.+$"
},
"Dependency": {
"anyOf": [
Expand Down Expand Up @@ -178,6 +178,9 @@
"setup": {
"$ref": "#/$defs/CompileSetup"
},
"strict": {
"type": "boolean"
},
"compilerPlugins": {
"oneOf": [
{
Expand Down
35 changes: 35 additions & 0 deletions snapshot-tests/create-new-build/.bleep/.bloop/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,45 @@
"name": "scala-compiler",
"version": "2.13.6",
"options": [
"-Wdead-code",
"-Wextra-implicit",
"-Wnumeric-widen",
"-Wunused:explicits",
"-Wunused:implicits",
"-Wunused:imports",
"-Wunused:locals",
"-Wunused:nowarn",
"-Wunused:params",
"-Wunused:patvars",
"-Wunused:privates",
"-Wvalue-discard",
"-Xlint:-byname-implicit",
"-Xlint:adapted-args",
"-Xlint:constant",
"-Xlint:delayedinit-select",
"-Xlint:deprecation",
"-Xlint:doc-detached",
"-Xlint:implicit-not-found",
"-Xlint:implicit-recursion",
"-Xlint:inaccessible",
"-Xlint:infer-any",
"-Xlint:missing-interpolator",
"-Xlint:nullary-unit",
"-Xlint:option-implicit",
"-Xlint:package-object-classes",
"-Xlint:poly-implicit-overload",
"-Xlint:private-shadow",
"-Xlint:stars-align",
"-Xlint:strict-unsealed-patmat",
"-Xlint:type-parameter-shadow",
"-Xplugin:<COURSIER>/https/repo1.maven.org/maven2/org/scala-js/scalajs-compiler_2.13.6/1.9.0/scalajs-compiler_2.13.6-1.9.0.jar",
"-encoding",
"utf8",
"-feature",
"-language:existentials",
"-language:experimental.macros",
"-language:higherKinds",
"-language:implicitConversions",
"-unchecked"
],
"jars": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@
"name": "scala3-compiler_3",
"version": "3.1.1",
"options": [
"-Ykind-projector",
"-deprecation",
"-encoding",
"utf8",
"-feature",
"-language:experimental.macros",
"-language:higherKinds",
"-language:implicitConversions",
"-unchecked"
],
"jars": [
Expand Down
35 changes: 35 additions & 0 deletions snapshot-tests/create-new-build/.bleep/.bloop/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,44 @@
"name": "scala-compiler",
"version": "2.13.6",
"options": [
"-Wdead-code",
"-Wextra-implicit",
"-Wnumeric-widen",
"-Wunused:explicits",
"-Wunused:implicits",
"-Wunused:imports",
"-Wunused:locals",
"-Wunused:nowarn",
"-Wunused:params",
"-Wunused:patvars",
"-Wunused:privates",
"-Wvalue-discard",
"-Xlint:-byname-implicit",
"-Xlint:adapted-args",
"-Xlint:constant",
"-Xlint:delayedinit-select",
"-Xlint:deprecation",
"-Xlint:doc-detached",
"-Xlint:implicit-not-found",
"-Xlint:implicit-recursion",
"-Xlint:inaccessible",
"-Xlint:infer-any",
"-Xlint:missing-interpolator",
"-Xlint:nullary-unit",
"-Xlint:option-implicit",
"-Xlint:package-object-classes",
"-Xlint:poly-implicit-overload",
"-Xlint:private-shadow",
"-Xlint:stars-align",
"-Xlint:strict-unsealed-patmat",
"-Xlint:type-parameter-shadow",
"-encoding",
"utf8",
"-feature",
"-language:existentials",
"-language:experimental.macros",
"-language:higherKinds",
"-language:implicitConversions",
"-unchecked"
],
"jars": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@
"name": "scala3-compiler_3",
"version": "3.1.1",
"options": [
"-Ykind-projector",
"-deprecation",
"-encoding",
"utf8",
"-feature",
"-language:experimental.macros",
"-language:higherKinds",
"-language:implicitConversions",
"-unchecked"
],
"jars": [
Expand Down
Loading

0 comments on commit e729b20

Please sign in to comment.