build.sc
author Tomas Zeman <tzeman@volny.cz>
Thu, 17 Jan 2019 18:26:38 +0100
changeset 22 35f19c3873e2
parent 21 175e7d7ce5b3
child 24 782f121133c7
permissions -rw-r--r--
fatags: 0.4-SNAPSHOT -> 0.4

import ammonite.ops._
import mill._
import mill.define.{Input, Sources, Target}
import mill.scalajslib._
import mill.scalalib._
import mill.scalalib.publish._
import mill.util.Loose

object V {
  val fatags = "0.4"
  val scalaJs = "0.6.26"
  val scala211 = "2.11.12"
  val scala212 = "2.12.8"
}

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

trait Common extends CrossSbtModule with PublishModule {

  def publishVersion: Target[String] = V.fatags

  def pomSettings = PomSettings(
    description = "FontAwesome Scala DSL (tags)",
    organization = "net.tz",
    url = "https://bitbucket.org/tzeman/fatags",
    licenses = Seq(License.`Apache-2.0`),
    versionControl = VersionControl(developerConnection = Some(
      "ssh://hg@bitbucket.org/tzeman/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.6.3/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 match {
      //case "clone" => "clone_"
      case x => x
    }
    d._2.map {
      case "brands" => s"""      val `fab-$sym`: T = fab("${d._1}")\n"""
      case "solid" => s"""      val `fas-$sym`: T = fas("${d._1}")\n"""
      case "regular" => s"""      val `far-$sym`: T = far("${d._1}")\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}
        |
        |   /*
        |   object fab {
        |     private def fa(w: String): T = FA(FaStyle.Brands, w)
        |     {icons.filter(_._2.contains("brands")).map(tpl).mkString}
        |   }
        |
        |   object fas {
        |     private def fa(w: String): T = FA(FaStyle.Solid, w)
        |     {icons.filter(_._2.contains("solid")).map(tpl).mkString}
        |   }
        |
        |   object far {
        |     private def fa(w: String): T = FA(FaStyle.Regular, w)
        |     {icons.filter(_._2.contains("regular")).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()()
}

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