Skip to content

Commit

Permalink
Fix up dev/prod mode selection
Browse files Browse the repository at this point in the history
  • Loading branch information
japgolly committed Jul 31, 2021
1 parent c27bc56 commit 5680a82
Show file tree
Hide file tree
Showing 32 changed files with 713 additions and 184 deletions.
12 changes: 4 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
matrix:
include:
- java: 16
scala: 2.13.6
scala: 2
- java: 8
scala: 3.0.1
scala: 3

steps:
- name: Git checkout
Expand All @@ -40,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
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,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
}
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'.")
}
)
}
}
47 changes: 47 additions & 0 deletions core/jvm/src/main/scala-3/scalacss/internal/ModeMacros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package scalacss.internal

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

// ===========================
// ==== ====
// ==== Scala 3 / 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 {
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 Some("dev") | None =>
'{ 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]
}
}
10 changes: 0 additions & 10 deletions core/jvm/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

// =================
// ==== ====
// ==== JVM ====
Expand All @@ -10,14 +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

implicit def env: Env =
Env.empty

}
18 changes: 0 additions & 18 deletions core/shared/src/main/scala-2/scalacss/internal/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scalacss.internal
import java.util.regex.Pattern
import scala.reflect.NameTransformer
import scala.reflect.macros.blackbox.Context
import scalacss.defaults.Exports

object Macros {

Expand Down Expand Up @@ -181,21 +180,4 @@ object Macros {
/** c"#fc6" provides a validates Color */
def c(args: Any*): Color = macro ColorLiteral.impl
}

// ===================================================================================================================

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

def devOrProdDefaults(c: Context): c.Expr[Exports with mutable.Settings] = {
import c.universe._
c.Expr[Exports with mutable.Settings](q"""
if (_root_.scalacss.internal.Platform.DevMode)
_root_.scalacss.DevDefaults
else
_root_.scalacss.ProdDefaults
""")
}
}
Loading

0 comments on commit 5680a82

Please sign in to comment.