Skip to content

Commit

Permalink
improve templating
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed May 27, 2022
1 parent 03b0651 commit 1f6fdc4
Show file tree
Hide file tree
Showing 18 changed files with 1,747 additions and 761 deletions.
2 changes: 2 additions & 0 deletions bleep-core/src/main/scala/bleep/JsonList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ case class JsonList[T](values: List[T]) extends SetLike[JsonList[T]] {
override def removeAll(other: JsonList[T]): JsonList[T] = JsonList(values.filterNot(other.values.toSet))
override def union(other: JsonList[T]): JsonList[T] = JsonList((values ++ other.values).distinct)

def transform[U](f: List[T] => List[U]): JsonList[U] = JsonList(f(values))

def +(t: T): JsonList[T] =
JsonList(values :+ t)
override def toString(): String =
Expand Down
26 changes: 19 additions & 7 deletions bleep-core/src/main/scala/bleep/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ object model {
implicit val encodesScala: Encoder[Versions.Scala] = Encoder[String].contramap(_.scalaVersion)

implicit val decodes: Decoder[Scala] = deriveDecoder
implicit val Encodes: Encoder[Scala] = deriveEncoder
implicit val encodes: Encoder[Scala] = deriveEncoder
}

sealed abstract class PlatformId(val value: String)
Expand Down Expand Up @@ -164,6 +164,7 @@ object model {

implicit val ordering: Ordering[TemplateId] = Ordering.by(_.value)
implicit val encodes: Encoder[TemplateId] = Encoder[String].contramap(_.value)
implicit val formats: Formatter[TemplateId] = _.value
implicit val keyDecodes: KeyDecoder[TemplateId] = KeyDecoder[String].map(TemplateId.apply)
implicit val keyEncodes: KeyEncoder[TemplateId] = KeyEncoder[String].contramap(_.value)
}
Expand Down Expand Up @@ -355,6 +356,7 @@ object model {
implicit val encodes: Encoder[ProjectName] = Encoder[String].contramap(_.value)
implicit val keyDecodes: KeyDecoder[ProjectName] = KeyDecoder[String].map(ProjectName.apply)
implicit val keyEncodes: KeyEncoder[ProjectName] = KeyEncoder[String].contramap(_.value)
implicit val formatter: Formatter[ProjectName] = pn => fansi.Str(pn.value)

def decoder(legal: Iterable[ProjectName]): Decoder[ProjectName] = {
val byString: Map[String, ProjectName] = legal.map(t => t.value -> t).toMap
Expand Down Expand Up @@ -399,15 +401,18 @@ object model {
Decoder.instance(c =>
for {
str <- c.as[String]
crossName <- str.split("@") match {
case Array(name) => Right(CrossProjectName(ProjectName(name), None))
case Array(name, crossId) => Right(CrossProjectName(ProjectName(name), Some(CrossId(crossId))))
case _ => Left(DecodingFailure(s"more than one '@' encountered in CrossProjectName $str", c.history))
}
crossName <- keyDecodes(str).toRight(DecodingFailure(s"more than one '@' encountered in CrossProjectName $str", c.history))
} yield crossName
)

implicit val encodes: Encoder[CrossProjectName] = Encoder[String].contramap(_.value)
implicit val keyEncodes: KeyEncoder[CrossProjectName] = KeyEncoder[String].contramap(_.value)
implicit val keyDecodes: KeyDecoder[CrossProjectName] = KeyDecoder.instance(str =>
str.split("@") match {
case Array(name) => Some(CrossProjectName(ProjectName(name), None))
case Array(name, crossId) => Some(CrossProjectName(ProjectName(name), Some(CrossId(crossId))))
case _ => None
}
)
}

case class Project(
Expand Down Expand Up @@ -604,6 +609,13 @@ object model {
)

object Build {
val empty = Build(
constants.$schema,
JsonMap.empty,
JsonMap.empty,
resolvers = JsonList.empty,
projects = JsonMap.empty
)
implicit val decodes: Decoder[Build] =
Decoder.instance(c =>
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.nio.file.{Files, Path, Paths}
import scala.util.Properties

trait SnapshotTest extends AnyFunSuite with TripleEqualsSupport {
val isCi: Boolean =
val enforceUpToDate: Boolean =
sys.env.contains("BUILD_NUMBER") || sys.env.contains("CI") // from sbt

val outFolder: Path =
Expand All @@ -29,11 +29,11 @@ trait SnapshotTest extends AnyFunSuite with TripleEqualsSupport {
def writeAndCompare(in: Path, fileMap: Map[Path, String]): Assertion =
if (Properties.isWin) pending // let's deal with this later
else {
if (isCi) {
if (enforceUpToDate) {
fileMap.foreach { case (path, contents) =>
if (Files.exists(path)) {
val existingContents = Files.readString(path)
assert(existingContents === contents)
assert(existingContents === contents, path)
} else {
fail(s"Expected path $path to exist")
}
Expand All @@ -50,7 +50,7 @@ trait SnapshotTest extends AnyFunSuite with TripleEqualsSupport {
def writeAndCompareEarly(in: Path, fileMap: Map[Path, String]): Assertion =
if (Properties.isWin) succeed // let's deal with this later
else {
if (isCi) {
if (enforceUpToDate) {
fileMap.foreach { case (path, contents) =>
if (Files.exists(path)) {
val existingContents = Files.readString(path)
Expand Down
144 changes: 47 additions & 97 deletions bleep.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"module" : "ch.epfl.scala::bloop-config:1.5.0"
},
"com.lihaoyi::fansi:0.3.1",
"io.circe::circe-core:0.14.1",
"io.circe::circe-generic:0.14.1",
"io.circe::circe-parser:0.14.1",
"io.circe::circe-core:0.14.2",
"io.circe::circe-generic:0.14.2",
"io.circe::circe-parser:0.14.2",
{
"exclusions" : {
"org.scala-lang.modules" : "scala-collection-compat_2.13"
Expand All @@ -43,25 +43,34 @@
"org.gnieh::diffson-circe:4.1.1",
"org.virtuslab.scala-cli::bloop-rifle:0.1.6"
],
"extends" : "template-cross-all"
"extends" : [
"template-common-main",
"template-cross-all"
]
},
"bleep-tasks" : {
"dependsOn" : "bleep-core",
"extends" : "template-cross-all",
"extends" : [
"template-common-main",
"template-cross-all"
],
"resources" : "./../liberated/sbt-native-image/plugin/src/main/resources",
"sources" : "./../liberated/sbt-native-image/plugin/src/main/scala"
},
"bleep-tasks-publishing" : {
"dependencies" : [
"com.eed3si9n::gigahorse-okhttp:0.6.0",
"org.bouncycastle:bcpg-jdk15on:1.69",
"org.bouncycastle:bcpg-jdk15on:1.70",
"org.scala-lang.modules::scala-parser-combinators:2.1.1",
"org.sonatype.spice.zapper:spice-zapper:1.3",
"org.wvlet.airframe::airframe-http:22.4.2",
"se.sawano.java:alphanumeric-comparator:1.4.1"
],
"dependsOn" : "bleep-tasks",
"extends" : "template-cross-jvm-212-213-main",
"extends" : [
"template-common-main",
"template-cross-jvm-212-213"
],
"sources" : [
"./../liberated/sbt-ci-release/plugin/src/main/scala",
"./../liberated/sbt-dynver/dynver/src/main/scala",
Expand All @@ -73,35 +82,28 @@
]
},
"bleep-tasks-publishing-test" : {
"cross" : {
"jvm212" : {
"extends" : [
"template-common-test",
"template-scala-2.12"
]
},
"jvm213" : {
"extends" : [
"template-common-test",
"template-scala-2.13"
]
}
},
"dependsOn" : "bleep-tasks-publishing",
"extends" : [
"template-common-test",
"template-cross-jvm-212-213"
],
"folder" : "./bleep-tasks-publishing"
},
"bleep-test" : {
"dependsOn" : "bleep",
"extends" : [
"template-common-test",
"template-scala-2.13"
"template-scala-2.13",
"template-common-test"
],
"folder" : "./bleep"
},
"bleep-test-harness" : {
"dependencies" : "org.scalatest::scalatest:3.2.12",
"dependsOn" : "bleep-core",
"extends" : "template-cross-jvm-212-213-main"
"extends" : [
"template-common-main",
"template-cross-jvm-212-213"
]
},
"scripts" : {
"dependsOn" : [
Expand Down Expand Up @@ -145,87 +147,54 @@
"dependsOn" : "bleep-test-harness",
"extends" : "template-common",
"isTestProject" : true,
"sbt-scope" : "test",
"scala" : {
"options" : [
"-Xcheckinit",
"-Xlint:adapted-args",
"-Xlint:constant",
"-Xlint:delayedinit-select",
"-Xlint:doc-detached",
"-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:type-parameter-shadow",
"-language:existentials"
]
}
"sbt-scope" : "test"
},
"template-cross-all" : {
"cross" : {
"jvm212" : {
"extends" : [
"template-common-main",
"template-scala-2.12"
]
},
"jvm213" : {
"extends" : [
"template-common-main",
"template-scala-2.13"
]
},
"jvm3" : {
"extends" : [
"template-common-main",
"template-scala-3"
]
"extends" : "template-scala-3"
}
}
},
"extends" : "template-cross-jvm-212-213"
},
"template-cross-jvm-212-213-main" : {
"template-cross-jvm-212-213" : {
"cross" : {
"jvm212" : {
"extends" : [
"template-common-main",
"template-scala-2.12"
]
"extends" : "template-scala-2.12"
},
"jvm213" : {
"extends" : [
"template-common-main",
"template-scala-2.13"
]
"extends" : "template-scala-2.13"
}
}
},
"template-scala-2.12" : {
"extends" : "template-common",
"template-scala-2" : {
"scala" : {
"options" : [
"-Xcheckinit",
"-Xlint:adapted-args",
"-Xlint:by-name-right-associative",
"-Xlint:constant",
"-Xlint:delayedinit-select",
"-Xlint:doc-detached",
"-Xlint:inaccessible",
"-Xlint:infer-any",
"-Xlint:missing-interpolator",
"-Xlint:nullary-override",
"-Xlint:nullary-unit",
"-Xlint:option-implicit",
"-Xlint:package-object-classes",
"-Xlint:poly-implicit-overload",
"-Xlint:private-shadow",
"-Xlint:stars-align",
"-Xlint:type-parameter-shadow",
"-language:existentials"
]
}
},
"template-scala-2.12" : {
"extends" : "template-scala-2",
"scala" : {
"options" : [
"-Xlint:by-name-right-associative",
"-Xlint:nullary-override",
"-Xlint:unsound-match",
"-Yno-adapted-args",
"-Ypartial-unification",
Expand All @@ -241,14 +210,13 @@
"-Ywarn-unused:patvars",
"-Ywarn-unused:privates",
"-Ywarn-value-discard",
"-deprecation",
"-language:existentials"
"-deprecation"
],
"version" : "2.12.15"
}
},
"template-scala-2.13" : {
"extends" : "template-common",
"extends" : "template-scala-2",
"scala" : {
"options" : [
"-Wdead-code",
Expand All @@ -263,34 +231,16 @@
"-Wunused:patvars",
"-Wunused:privates",
"-Wvalue-discard",
"-Xcheckinit",
"-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",
"-language:existentials"
"-Xlint:strict-unsealed-patmat"
],
"version" : "2.13.8"
}
},
"template-scala-3" : {
"extends" : "template-common",
"sbt-scope" : "main",
"scala" : {
"options" : [
"-Ykind-projector",
Expand Down
12 changes: 9 additions & 3 deletions bleep/src/main/scala/bleep/commands/BuildCreateNew.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ case class BuildCreateNew(logger: Logger, cwd: Path, platforms: NonEmptyList[mod

def genAllFiles(buildPaths: BuildPaths): Map[Path, String] = {
val exampleFiles = new BuildCreateNew.ExampleFiles(name)
val build = BuildCreateNew.genBuild(exampleFiles, platforms, scalas, name)
val build = BuildCreateNew.genBuild(logger, exampleFiles, platforms, scalas, name)

val value = Map[Path, String](
buildPaths.bleepJsonFile -> build.asJson.foldWith(ShortenAndSortJson).spaces2,
Expand Down Expand Up @@ -100,7 +100,13 @@ object BuildCreateNew {
}
}

def genBuild(exampleFiles: ExampleFiles, platforms: NonEmptyList[model.PlatformId], scalas: NonEmptyList[Versions.Scala], name: String): model.Build = {
def genBuild(
logger: Logger,
exampleFiles: ExampleFiles,
platforms: NonEmptyList[model.PlatformId],
scalas: NonEmptyList[Versions.Scala],
name: String
): model.Build = {
val defaultOpts = Options(Set(Options.Opt.WithArgs("-encoding", List("utf8")), Options.Opt.Flag("-feature"), Options.Opt.Flag("-unchecked")))

def variants(name: String): NonEmptyList[(model.PlatformId, Versions.Scala, model.CrossProjectName)] =
Expand Down Expand Up @@ -174,6 +180,6 @@ object BuildCreateNew {
retainCrossTemplates = Map.empty
)

Templates.apply(explodedBuild, ignoreWhenInferringTemplates = _ => false)
Templates.apply(logger, explodedBuild, ignoreWhenInferringTemplates = _ => false)
}
}
Loading

0 comments on commit 1f6fdc4

Please sign in to comment.