Skip to content

Refactor sbt build #539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 11 additions & 154 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,154 +1,11 @@
import _root_.scalafix.sbt.BuildInfo.scalafixVersion
import scalatex.ScalatexReadme

ThisBuild / shellPrompt := ((s: State) => Project.extract(s).currentRef.project + "> ")

lazy val scalafixRules = project
.in(file("scalafix"))
.settings(
libraryDependencies += ("ch.epfl.scala" %% "scalafix-core" % scalafixVersion).cross(CrossVersion.for3Use2_13),
)

def sourceMapsToGithub: Project => Project =
p => p.settings(
scalacOptions ++= {
val isDotty = scalaVersion.value startsWith "3"
val ver = version.value
if (isSnapshot.value)
Nil
else {
val a = p.base.toURI.toString.replaceFirst("[^/]+/?$", "")
val g = s"https://raw.githubusercontent.com/scala-js/scala-js-dom"
val flag = if (isDotty) "-scalajs-mapSourceURI" else "-P:scalajs:mapSourceURI"
s"$flag:$a->$g/v$ver/" :: Nil
}
}
)

lazy val root = project
.in(file("."))
.enablePlugins(ScalaJSPlugin)
.enablePlugins(ScalafixPlugin)
.dependsOn(scalafixRules % ScalafixConfig)
.configure(sourceMapsToGithub)

name := "Scala.js DOM"

ThisBuild / crossScalaVersions := Seq("2.11.12", "2.12.14", "2.13.6", "3.0.1")
ThisBuild / scalaVersion := crossScalaVersions.value.find(_.startsWith("2.13.")).get

val inCI = sys.props.get("CI").exists(_.contains("1"))

val commonSettings = Seq(
organization := "org.scala-js",
scalacOptions ++= Seq(
"-deprecation",
"-feature",
),
scalacOptions ++= (if (!inCI) Seq.empty else Seq(
"-Xfatal-warnings",
)),
scalacOptions ++= (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, 11)) => "-Ywarn-unused-import" :: Nil
case Some((2, 12)) => "-Ywarn-unused:imports,patvars,locals,implicits" :: Nil
case Some((2, 13)) => "-Wunused:imports,patvars,locals,implicits" :: Nil
case _ => Nil
}),
)

val noPublishSettings = Seq(
publish / skip := true
)

normalizedName := "scalajs-dom"

commonSettings

homepage := Some(url("http://scala-js.org/"))

licenses += ("MIT", url("http://opensource.org/licenses/mit-license.php"))

def hasNewCollections(version: String): Boolean = {
!version.startsWith("2.11.") &&
!version.startsWith("2.12.")
}

/** Returns the appropriate subdirectory of `sourceDir` depending on whether
* the `scalaV` uses the new collections (introduced in 2.13) or not.
*/
def collectionsEraDependentDirectory(scalaV: String, sourceDir: File): File =
if (hasNewCollections(scalaV)) sourceDir / "scala-new-collections"
else sourceDir / "scala-old-collections"

inConfig(Compile)(Def.settings(
unmanagedSourceDirectories +=
collectionsEraDependentDirectory(scalaVersion.value, sourceDirectory.value)
))

versionScheme := Some("early-semver")

pomExtra := (
<developers>
<developer>
<id>japgolly</id>
<name>David Barri</name>
<url>https://github.com/japgolly</url>
</developer>
<developer>
<id>armanbilge</id>
<name>Arman Bilge</name>
<url>https://github.com/armanbilge</url>
</developer>
<developer>
<id>sjrd</id>
<name>Sébastien Doeraene</name>
<url>https://github.com/sjrd/</url>
</developer>
<developer>
<id>gzm0</id>
<name>Tobias Schlatter</name>
<url>https://github.com/gzm0/</url>
</developer>
<developer>
<id>lihaoyi</id>
<name>Li Haoyi</name>
<url>https://github.com/lihaoyi/</url>
</developer>
</developers>
)

pomIncludeRepository := { _ => false }

lazy val readme = ScalatexReadme(
projectId = "readme",
wd = file(""),
url = "https://github.com/scala-js/scala-js-dom/tree/master",
source = "Index",
autoResources = Seq("example-opt.js")
).settings(
scalaVersion := "2.12.10",
scalacOptions ++= Seq("-deprecation", "-feature", "-Xfatal-warnings"),
(Compile / resources) += (example / Compile / fullOptJS).value.data
).settings(noPublishSettings: _*)

lazy val example = project.
enablePlugins(ScalaJSPlugin).
settings(commonSettings: _*).
settings(noPublishSettings: _*).
dependsOn(root)

addCommandAlias("prePR", "+prePR_nonCross")

val prePR_nonCross = taskKey[Unit]("Performs all necessary work required before submitting a PR, for a single version of Scala.")

ThisBuild / prePR_nonCross := Def.sequential(
root / clean,
root / Compile / scalafmt,
Def.taskDyn {
if (scalaVersion.value.startsWith("2."))
(root / Compile / scalafix).toTask("")
else
Def.task[Unit]((root / Compile / compile).value)
},
example / Compile / compile,
).value
ThisBuild / homepage := Some(url("https://scala-js.org"))
ThisBuild / licenses += ("MIT", url("https://opensource.org/licenses/mit-license.php"))
ThisBuild / organization := "org.scala-js"
ThisBuild / shellPrompt := ((s: State) => Project.extract(s).currentRef.project + "> ")
ThisBuild / versionScheme := Some("early-semver")

val root = Build.root
val scalafixRules = Build.scalafixRules
val dom = Build.dom
val example = Build.example
val readme = Build.readme
17 changes: 17 additions & 0 deletions prePR.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Build._

addCommandAlias("prePR", "+prePR_nonCross")

val prePR_nonCross = taskKey[Unit]("Performs all necessary work required before submitting a PR, for a single version of Scala.")

ThisBuild / prePR_nonCross := Def.sequential(
root / clean,
dom / Compile / scalafmt,
Def.taskDyn {
if (scalaVersion.value.startsWith("2."))
(dom / Compile / scalafix).toTask("")
else
Def.task[Unit]((dom / Compile / compile).value)
},
root / Compile / compile,
).value
65 changes: 65 additions & 0 deletions project/Build.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import sbt._
import sbt.Keys._
import org.scalajs.sbtplugin.ScalaJSPlugin
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
import scalafix.sbt.ScalafixPlugin
import scalafix.sbt.ScalafixPlugin.autoImport._
import scalatex.ScalatexReadme
import Dependencies._
import Lib._

object Build {

// TODO: Change root from {.root => .} and dom from {. => dom}

lazy val root = project
.in(file(".root"))
.configure(commonSettings, crossScala, preventPublication)
.settings(
name := "Scala.js DOM",
)
.aggregate(
scalafixRules,
dom,
example,
// readme, // This is a Scala 2.12 only module
)

lazy val dom = project
.in(file("."))
.dependsOn(scalafixRules % ScalafixConfig)
.enablePlugins(ScalaJSPlugin, ScalafixPlugin)
.configure(commonSettings, crossScala, publicationSetttings)
.settings(
moduleName := "scalajs-dom",
Compile / unmanagedSourceDirectories +=
collectionsEraDependentDirectory(scalaVersion.value, (Compile / sourceDirectory).value),
)

lazy val scalafixRules = project
.in(file("scalafix"))
.configure(commonSettings, crossScala, preventPublication)
.settings(
libraryDependencies += Dep.scalafixCore.value,
)

lazy val example = project
.dependsOn(dom)
.enablePlugins(ScalaJSPlugin)
.configure(commonSettings, crossScala, preventPublication)

lazy val readme =
ScalatexReadme(
projectId = "readme",
wd = file(""),
url = "https://github.com/scala-js/scala-js-dom/tree/master",
source = "Index",
autoResources = Seq("example-opt.js"),
)
.configure(commonSettings, preventPublication)
.settings(
scalaVersion := Ver.scala212,
Compile / resources += (example / Compile / fullOptJS).value.data,
)

}
17 changes: 17 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sbt._
import sbt.Keys._
import scalafix.sbt.BuildInfo.scalafixVersion

object Dependencies {

object Ver {
val scala211 = "2.11.12"
val scala212 = "2.12.14"
val scala213 = "2.13.6"
val scala3 = "3.0.1"
}

object Dep {
val scalafixCore = Def.setting("ch.epfl.scala" %% "scalafix-core" % scalafixVersion cross CrossVersion.for3Use2_13)
}
}
103 changes: 103 additions & 0 deletions project/Lib.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import sbt._
import sbt.Keys._
import Dependencies._

object Lib {

val inCI = sys.props.get("CI").exists(_.contains("1"))

def commonSettings: Project => Project = _
.configure(sourceMapsToGithub)
.settings(
scalacOptions ++= Seq(
"-deprecation",
"-feature",
),
scalacOptions ++= (if (!inCI) Seq.empty else Seq(
"-Xfatal-warnings",
)),
scalacOptions ++= (CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, 11)) => "-Ywarn-unused-import" :: Nil
case Some((2, 12)) => "-Ywarn-unused:imports,patvars,locals,implicits" :: Nil
case Some((2, 13)) => "-Wunused:imports,patvars,locals,implicits" :: Nil
case _ => Nil
}),
)

def crossScala: Project => Project = _
.settings(
crossScalaVersions := Seq(
Ver.scala211,
Ver.scala212,
Ver.scala213,
Ver.scala3,
),
scalaVersion := Ver.scala213,
)

val publicationSetttings: Project => Project =
_.settings(
pomExtra := (
<developers>
<developer>
<id>japgolly</id>
<name>David Barri</name>
<url>https://github.com/japgolly</url>
</developer>
<developer>
<id>armanbilge</id>
<name>Arman Bilge</name>
<url>https://github.com/armanbilge</url>
</developer>
<developer>
<id>sjrd</id>
<name>Sébastien Doeraene</name>
<url>https://github.com/sjrd/</url>
</developer>
<developer>
<id>gzm0</id>
<name>Tobias Schlatter</name>
<url>https://github.com/gzm0/</url>
</developer>
<developer>
<id>lihaoyi</id>
<name>Li Haoyi</name>
<url>https://github.com/lihaoyi/</url>
</developer>
</developers>
),
pomIncludeRepository := { _ => false },
)

val preventPublication: Project => Project =
_.settings(publish / skip := true)

private def sourceMapsToGithub: Project => Project =
p => p.settings(
scalacOptions ++= {
val isDotty = scalaVersion.value startsWith "3"
val ver = version.value
if (isSnapshot.value)
Nil
else {
val a = p.base.toURI.toString.replaceFirst("[^/]+/?$", "")
val g = s"https://raw.githubusercontent.com/scala-js/scala-js-dom"
val flag = if (isDotty) "-scalajs-mapSourceURI" else "-P:scalajs:mapSourceURI"
s"$flag:$a->$g/v$ver/" :: Nil
}
}
)

def hasNewCollections(version: String): Boolean =
!version.startsWith("2.11.") && !version.startsWith("2.12.")

/** Returns the appropriate subdirectory of `sourceDir` depending on whether
* the `scalaVer` uses the new collections (introduced in 2.13) or not.
*/
def collectionsEraDependentDirectory(scalaVer: String, sourceDir: File): File =
if (hasNewCollections(scalaVer))
sourceDir / "scala-new-collections"
else
sourceDir / "scala-old-collections"

}
14 changes: 5 additions & 9 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.5.1")

addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.3.11")

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

addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7")

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.30")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.30")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7")
addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.3.11")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.5.1")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.0.7")
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.scalajs.dom.scalafix
import scala.annotation.tailrec
import scala.collection.immutable.SortedSet
import scala.collection.mutable
import scala.meta._
import scalafix.v1._

final class MutableState {
Expand Down