build.sc
author Tomas Zeman <tomas@functionals.cz>
Thu, 24 Oct 2019 08:52:17 +0200
changeset 30 8933b942e89f
parent 29 bf5bf2dd51db
child 32 7cc486dc94cd
permissions -rw-r--r--
fatags: 0.5-SNAPSHOT -> 0.5

import ammonite.ops._
import mill._
import mill.api.Loose
import mill.define.{Input, Sources, Target}
import mill.scalajslib._
import mill.scalalib._
import mill.scalalib.publish._
import $ivy.`de.tototec::de.tobiasroeser.mill.publishM2:0.1.0`
import de.tobiasroeser.mill.publishM2._

import scala.language.postfixOps

object V {
  val fatags = "0.5"
  val scalaJs = "0.6.29"
  val scala211 = "2.11.12"
  val scala212 = "2.12.10"
}

object D {
  val scalatags = ivy"com.lihaoyi::scalatags::0.6.7"
}

def camelCase(s: String): String = {
  def inner(l: List[Char]): List[Char] = l match {
    case '-' :: '-' :: c :: rest => c.toUpper :: inner(rest)
    case '-' :: c :: rest => c.toUpper :: inner(rest)
    case c :: rest => c :: inner(rest)
    case Nil => Nil
  }
  inner(s toList) mkString ""
}

trait Common extends CrossSbtModule with PublishM2Module {

  override def artifactName = "fatags"

  def publishVersion: Target[String] = V.fatags

  def pomSettings = PomSettings(
    description = "FontAwesome Scala DSL (tags)",
    organization = "net.tz",
    url = "https://hg.functionals.cz/fatags",
    licenses = Seq(License.`Apache-2.0`),
    versionControl = VersionControl(developerConnection = Some(
      "ssh://tzeman@hg.functionals.cz/repos/public/fatags")),
    developers = Seq(
      Developer("tzeman", "Tomas Zeman", "")
    )
  )

  override def scalacOptions = T{Seq(
    "-deprecation",                      // Emit warning and location for usages of deprecated APIs.
    "-encoding", "utf-8",                // Specify character encoding used by source files.
    "-explaintypes",                     // Explain type errors in more detail.
    "-feature",                          // Emit warning and location for usages of features that should be imported explicitly.
    "-language:higherKinds",             // Allow higher-kinded types
    "-language:implicitConversions",     // Allow definition of implicit functions called views
    "-language:reflectiveCalls",
    "-language:postfixOps",
    "-unchecked",                        // Enable additional warnings where generated code depends on assumptions.
    "-Xcheckinit",                       // Wrap field accessors to throw an exception on uninitialized access.
    "-Xfuture",                          // Turn on future language features.
    "-target:jvm-1.8",
  )}

  def hgId: Input[String] = T.input {
    os.proc("hg", "id", "-i").call().out.string.trim
  }

  def hgNum: Input[String] = T.input {
    os.proc("hg", "id", "-n").call().out.string.trim
  }

  def hgTag: Input[Option[String]] = T.input {
    os.proc("hg", "id", "-t").call().out.string.trim.split(' ').headOption
  }

  override def ivyDeps: Target[Loose.Agg[Dep]] = Agg(D.scalatags)

  override def sources: Sources = T.sources{
    super.sources() :+ PathRef(millSourcePath / 'shared / 'src / 'main / 'scala)
  }

  // https://github.com/FortAwesome/Font-Awesome/raw/5.11.2/metadata/icons.json
  type IcoDef = (String, Set[String])
  def parseIcons: Target[List[IcoDef]] = T{
    ujson.read(os.read! pwd / "icons.json").obj.map(e => (e._1, e._2.obj("styles").arr.map(_.str).toSet)).toList
  }

  private def tpl(d: IcoDef): String = {
    val sym = d._1
    d._2.map {
      case "brands" =>  s"""      val ${camelCase(s"fab-$sym")}: T = fab("$sym")\n"""
      case "solid" =>   s"""      val ${camelCase(s"fas-$sym")}: T = fas("$sym")\n"""
      case "regular" => s"""      val ${camelCase(s"far-$sym")}: T = far("$sym")\n"""
    }.mkString
  }

  override def generatedSources: Sources = T.sources{
    val d = T.ctx().dest
    val icons = parseIcons()
    write(d / "generated.scala",
      s"""
        | package fontawesome.generic
        | import scalatags.generic._
        | class GenericFA[Builder, Output <: FragT, FragT](
        |   val bun: Bundle[Builder, Output, FragT]) {
        |
        |   import bun.all._
        |
        |   type T = FA[Builder, Output, FragT]
        |
        |   val faStack: Modifier = cls := "fa-stack fa-lg"
        |
        |   implicit val bundle: Bundle[Builder, Output, FragT] = bun
        |   implicit def fa2tag(x: T): TypedTag[Builder, Output, FragT] = x()
        |
        |   private def fab(w: String): T = FA(FaStyle.Brands, w)
        |   private def far(w: String): T = FA(FaStyle.Regular, w)
        |   private def fas(w: String): T = FA(FaStyle.Solid, w)
        |
        |   ${icons map tpl mkString}
        |
        | }
      """.stripMargin)
    d
  }
}

class JvmModule(val crossScalaVersion: String) extends Common
class JsModule(val crossScalaVersion: String) extends ScalaJSModule
  with Common {
  override def scalaJSVersion: Target[String] = V.scalaJs
}

object jvm extends Cross[JvmModule](V.scala211, V.scala212)
object js extends Cross[JsModule](V.scala211, V.scala212)

def publishLocal(): define.Command[Unit] = T.command{
  jvm(V.scala212).publishLocal()()
  js(V.scala212).publishLocal()()
  jvm(V.scala211).publishLocal()()
  js(V.scala211).publishLocal()()
}

def publishM2Local(p: os.Path): define.Command[Unit] = T.command{
  jvm(V.scala212).publishM2Local(p)()
  js(V.scala212).publishM2Local(p)()
  jvm(V.scala211).publishM2Local(p)()
  js(V.scala211).publishM2Local(p)()
  ()
}

object example extends ScalaModule with ScalaJSModule {
  override def scalaVersion: Target[String] = T{V.scala212}
  override def scalaJSVersion: Target[String] = V.scalaJs


  /* Ugly hack to prevent
   * "Referring to non-existent method fontawesome.generic.FA... error"
   */
  //override def moduleDeps: Seq[PublishModule] = Seq(js(V.scala212))
  override def ivyDeps: Target[Loose.Agg[Dep]] = Agg(D.scalatags)
  override def sources: Sources = T.sources{super.sources() ++ js(V.scala212).sources()}
  override def generatedSources = T{js(V.scala212).generatedSources()}
}


// vim: et ts=2 sw=2 syn=scala