Skip to content

Commit

Permalink
Merge pull request #287 from japgolly/topic/scala3
Browse files Browse the repository at this point in the history
Scala 3
  • Loading branch information
japgolly authored Jul 31, 2021
2 parents d68c301 + 5680a82 commit 3664637
Show file tree
Hide file tree
Showing 46 changed files with 1,088 additions and 260 deletions.
16 changes: 5 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ jobs:
fail-fast: false
matrix:
include:
- java: 8
scala: 2.13.6
- java: 11
scala: 2.13.6
- java: 16
scala: 2.13.6
scala: 2
- java: 8
scala: 3

steps:
- name: Git checkout
Expand All @@ -42,12 +40,8 @@ jobs:

- name: Build and test
shell: bash
run: sbt ++${{ matrix.scala }} test

- name: Test fullOptJS
shell: bash
run: sbt ++${{ matrix.scala }} 'set ThisBuild / scalaJSStage := FullOptStage' JS/test
run: bin/ci ${{ matrix.scala }}

- name: Show JS sizes
shell: bash
run: sbt ++${{ matrix.scala }} cmpJsSize jsSizes
run: sbt "++$(bin/get_scala_version ${{ matrix.scala }})" cmpJsSize jsSizes
57 changes: 57 additions & 0 deletions bin/ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")/.."

if [ $# -gt 1 ]; then
echo "Usage: $0 [<scala ver>]"
echo ""
echo "Where"
echo " <scala ver> = 2 or 3"
echo
exit 1
fi

case "${1:-}" in
"")
"$0" 2
echo
echo
"$0" 3
exit 0
;;
2|3)
SCALA_VER="$(bin/get_scala_version $1)"
echo "Scala version: $SCALA_VER"
;;
*)
echo "Unrecognised version: $1" >&2
exit 2
;;
esac

dryrun=
# dryrun=-n

# See how much memory is available
free -h
echo

# Test upstream
rm -rf */target/scala-*/{,test-}classes
cmd=(
sbt
-J-Xmx3G
-J-XX:+UseG1GC
++$SCALA_VER
'set ThisBuild / parallelExecution := false'
'set Global / concurrentRestrictions += Tags.limit(ScalaJSTags.Link, 1)'
test # Test development-mode
'set ThisBuild / scalaJSStage := FullOptStage' test # Test production-mode
publishLocal # For downstream tests
)
echo "> $(printf "%s\n " "${cmd[@]}")"
[[ $dryrun == "-n" ]] || "${cmd[@]}"

# Test downstream
SCALA_MAJOR_VER="${SCALA_VER:0:1}"
downstream-tests/run -$SCALA_MAJOR_VER $dryrun
11 changes: 11 additions & 0 deletions bin/get_scala_version
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")/.."
tmp="$(fgrep " scala$1 " project/Dependencies.scala)"
r='[^"]*"([^"]*)"[^"]*'
if [[ "$tmp" =~ $r ]]; then
echo "${BASH_REMATCH[1]}"
else
echo "Unable to determine Scala $1 version" >&2
exit 3
fi
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name := "ScalaCSS"
startYear := Some(2015)

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

// Mutability & NameGen causes occasional test failures
ThisBuild / parallelExecution := false

Expand All @@ -10,8 +12,6 @@ val rootJS = ScalaCssBuild.rootJS

val coreJVM = ScalaCssBuild.coreJVM
val coreJS = ScalaCssBuild.coreJS
val elisionTestJVM = ScalaCssBuild.elisionTestJVM
val elisionTestJS = ScalaCssBuild.elisionTestJS
val extScalatagsJVM = ScalaCssBuild.extScalatagsJVM
val extScalatagsJS = ScalaCssBuild.extScalatagsJS
val extReact = ScalaCssBuild.extReact
Expand Down
52 changes: 52 additions & 0 deletions core/js/src/main/scala-2/scalacss/internal/ModeMacros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package scalacss.internal

import scala.reflect.macros.whitebox.Context
import scalacss.defaults._

// ==========================
// ==== ====
// ==== Scala 2 / JS ====
// ==== ====
// ==========================

object ModeMacros {

private def readConfig(key: String): Option[String] =
(Option(System.getProperty(key, null)) orElse Option(System.getenv(key)))
.map(_.trim.toLowerCase)
.filter(_.nonEmpty)

trait DevOrProdDefaults {
def devOrProdDefaults: Exports with mutable.Settings =
macro ModeMacros.devOrProdDefaults
}

def devOrProdDefaults(c: Context): c.Expr[Exports with mutable.Settings] = {
import c.universe._

def fail(msg: String): Nothing =
c.abort(c.enclosingPosition, msg)

c.Expr[Exports with mutable.Settings](
readConfig("scalacss.mode") match {

case None =>
q"""
if (_root_.scala.scalajs.LinkingInfo.developmentMode)
_root_.scalacss.DevDefaults
else
_root_.scalacss.ProdDefaults
"""

case Some("dev") =>
q"_root_.scalacss.DevDefaults"

case Some("prod") =>
q"_root_.scalacss.ProdDefaults"

case Some(x) =>
fail(s"Unrecognise option for scalacss.mode: $x. Legal values are 'dev' and 'prod'.")
}
)
}
}
55 changes: 55 additions & 0 deletions core/js/src/main/scala-3/scalacss/internal/ModeMacros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package scalacss.internal

import scalacss.defaults._
import scala.language.`3.0`
import scala.quoted._

// ==========================
// ==== ====
// ==== Scala 3 / JS ====
// ==== ====
// ==========================

object ModeMacros {

private def readConfig(key: String): Option[String] =
(Option(System.getProperty(key, null)) orElse Option(System.getenv(key)))
.map(_.trim.toLowerCase)
.filter(_.nonEmpty)

trait DevOrProdDefaults {
transparent inline def devOrProdDefaults: Exports with mutable.Settings =
${ ModeMacros.devOrProdDefaults }
}

def devOrProdDefaults(using Quotes): Expr[Exports with mutable.Settings] = {
import quotes.reflect._

def fail(msg: String): Nothing =
report.throwError(msg)

type S = Exports with mutable.Settings
val expr: Expr[S] =
readConfig("scalacss.mode") match {

case None =>
'{
if (_root_.scala.scalajs.LinkingInfo.developmentMode)
_root_.scalacss.DevDefaults
else
_root_.scalacss.ProdDefaults
}

case Some("dev") =>
'{ scalacss.DevDefaults }

case Some("prod") =>
'{ scalacss.ProdDefaults }

case Some(x) =>
fail(s"Unrecognise option for scalacss.mode: $x. Legal values are 'dev' and 'prod'.")
}

Inlined(None, Nil, expr.asTerm).asExprOf[S]
}
}
11 changes: 0 additions & 11 deletions core/js/src/main/scala/scalacss/internal/Platform.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package scalacss.internal

import scala.annotation.elidable

// ================
// ==== ====
// ==== JS ====
Expand All @@ -10,15 +8,6 @@ import scala.annotation.elidable

object Platform {

/**
* Use the scalac `-Xelide-below` flag to switch from development- to production-mode.
*/
@elidable(elidable.ASSERTION)
@inline def DevMode: Boolean =
true
// @inline def DevMode: Boolean =
// scalajs.LinkingInfo.developmentMode

implicit def env: Env =
Env.empty
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ final class Mutex {
}

object Mutex {
implicit val mutex = new Mutex
implicit val mutex: Mutex = new Mutex
}
42 changes: 21 additions & 21 deletions core/js/src/main/scala/scalacss/js/PlatformJs.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scalacss.js

import scala.annotation.nowarn
import scala.scalajs.js._
import scala.scalajs.js

object PlatformJs {

Expand All @@ -16,38 +16,38 @@ object PlatformJs {
/**
* Returns `undefined` in NodeJS unit tests.
*/
lazy val value: UndefOr[PlatformJs] =
lazy val value: js.UndefOr[PlatformJs] =
try {
eval(sourceCode)
Dynamic.global.platform.asInstanceOf[UndefOr[PlatformJs]]
js.eval(sourceCode)
js.Dynamic.global.platform.asInstanceOf[js.UndefOr[PlatformJs]]
} catch {
case e: Throwable =>
e.printStackTrace(System.err)
undefined
js.undefined
}
}

/**
* https://github.com/bestiejs/platform.js/blob/master/doc/README.md#readme
*/
@native
@js.native
@nowarn
trait PlatformJs extends Object {
val description : UndefOr[String] = native
val layout : UndefOr[String] = native
val manufacturer: UndefOr[String] = native
val name : UndefOr[String] = native
val prerelease : UndefOr[String] = native
val product : UndefOr[String] = native
val ua : UndefOr[String] = native
val version : UndefOr[String] = native
val os : UndefOr[PlatformJsOS] = native
trait PlatformJs extends js.Object {
val description : js.UndefOr[String] = js.native
val layout : js.UndefOr[String] = js.native
val manufacturer: js.UndefOr[String] = js.native
val name : js.UndefOr[String] = js.native
val prerelease : js.UndefOr[String] = js.native
val product : js.UndefOr[String] = js.native
val ua : js.UndefOr[String] = js.native
val version : js.UndefOr[String] = js.native
val os : js.UndefOr[PlatformJsOS] = js.native
}

@native
@js.native
@nowarn
trait PlatformJsOS extends Object {
val architecture: UndefOr[Int] = native
val family : UndefOr[String] = native
val version : UndefOr[String] = native
trait PlatformJsOS extends js.Object {
val architecture: js.UndefOr[Int] = js.native
val family : js.UndefOr[String] = js.native
val version : js.UndefOr[String] = js.native
}
44 changes: 44 additions & 0 deletions core/jvm/src/main/scala-2/scalacss/internal/ModeMacros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package scalacss.internal

import scala.reflect.macros.whitebox.Context
import scalacss.defaults._

// ===========================
// ==== ====
// ==== Scala 2 / JVM ====
// ==== ====
// ===========================

object ModeMacros {

private def readConfig(key: String): Option[String] =
(Option(System.getProperty(key, null)) orElse Option(System.getenv(key)))
.map(_.trim.toLowerCase)
.filter(_.nonEmpty)

trait DevOrProdDefaults {
def devOrProdDefaults: Exports with mutable.Settings =
macro ModeMacros.devOrProdDefaults
}

def devOrProdDefaults(c: Context): c.Expr[Exports with mutable.Settings] = {
import c.universe._

def fail(msg: String): Nothing =
c.abort(c.enclosingPosition, msg)

c.Expr[Exports with mutable.Settings](
readConfig("scalacss.mode") match {

case Some("dev") | None =>
q"_root_.scalacss.DevDefaults"

case Some("prod") =>
q"_root_.scalacss.ProdDefaults"

case Some(x) =>
fail(s"Unrecognise option for scalacss.mode: $x. Legal values are 'dev' and 'prod'.")
}
)
}
}
Loading

0 comments on commit 3664637

Please sign in to comment.