Skip to content
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

SBT AutoPlugins #104

Closed
wants to merge 4 commits into from
Closed
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
target
js/yarn.lock
.bsp

.idea

.DS_Store
*.bloop
*.metals
*metals.sbt
3 changes: 3 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = "3.7.15"
runner.dialect = scala213
maxColumn = 160
63 changes: 52 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import VersionHelper.{versionFmt, fallbackVersion}
import VersionHelper.fallbackVersion
import VersionHelper.versionFmt
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for formatting noise. I'm an Emacs user and I have it configured to auto-format via scalafmt on save. I was too lazy to turn it off for this PR, which unfortunately caused some noise.


// Lets me depend on Maven Central artifacts immediately without waiting
resolvers ++= Resolver.sonatypeOssRepos("public")
Expand All @@ -25,7 +26,6 @@ ThisBuild / dynver := {
lazy val commonSettings = Seq(
name := "Scala DOM Types",
organization := "com.raquo",
normalizedName := "domtypes",
homepage := Some(url("https://github.com/raquo/scala-dom-types")),
licenses += ("MIT", url("https://github.com/raquo/scala-dom-types/blob/master/LICENSE.md")),
scmInfo := Some(
Expand All @@ -46,29 +46,40 @@ lazy val noPublish = Seq(
publish / skip := true
)

lazy val root = project.in(file("."))
.aggregate(domtypesJVM, domtypesJS) // order is important: first, generate sample traits, then try to compile them on the frontend
lazy val root = project
.in(file("."))
.aggregate(
domtypesJVM,
domtypesJS,
sbtDomtypes
) // order is important: first, generate sample traits, then try to compile them on the frontend
.settings(commonSettings)
.settings(noPublish)

lazy val domtypes = crossProject(JSPlatform, JVMPlatform).in(file("."))
lazy val domtypes = crossProject(JSPlatform, JVMPlatform)
.in(file("."))
.settings(commonSettings)
.settings(
libraryDependencies ++= Seq(
"org.scalatest" %%% "scalatest" % Versions.ScalaTest % Test
)
)
.settings(
normalizedName := "domtypes",
scalacOptions ++= Seq(
"-feature",
"-language:higherKinds"
),
scalacOptions ~= (_.filterNot(Set(
"-Wunused:params",
"-Ywarn-unused:params",
"-Wunused:explicits"
))),
(Compile / doc / scalacOptions) ~= (_.filter(_.startsWith("-Xplugin"))), // https://github.com/DavidGregory084/sbt-tpolecat/issues/36
scalacOptions ~= (_.filterNot(
Set(
"-Wunused:params",
"-Ywarn-unused:params",
"-Wunused:explicits"
)
)),
(Compile / doc / scalacOptions) ~= (_.filter(
_.startsWith("-Xplugin")
)), // https://github.com/DavidGregory084/sbt-tpolecat/issues/36
(Compile / doc / scalacOptions) ++= Seq(
"-no-link-warnings" // Suppress scaladoc "Could not find any member to link for" warnings
),
Expand Down Expand Up @@ -96,3 +107,33 @@ lazy val domtypes = crossProject(JSPlatform, JVMPlatform).in(file("."))

lazy val domtypesJS = domtypes.js
lazy val domtypesJVM = domtypes.jvm

lazy val sbtDomtypes = project
.in(file("sbt-domtypes"))
.enablePlugins(SbtPlugin)
.dependsOn(domtypesJVM)
.settings(commonSettings: _*)
.settings(
name := "sbt-domtypes",
normalizedName := "sbt-domtypes",
sbtPlugin := true,
scalaVersion := Versions.Scala_2_12,
scriptedLaunchOpts := {
scriptedLaunchOpts.value ++ Seq(
"-Xmx1024M",
"-Dplugin.version=" + version.value
)
},
scriptedBufferLog := false,
addSbtPlugin("org.scala-js" % "sbt-scalajs" % Versions.ScalaJs),
libraryDependencies ++= Seq(
"com.lihaoyi" %% "upickle" % "3.1.3", // for parsing custom-elements.json
"com.lihaoyi" %% "pprint" % "0.7.0"
),
(Compile / resourceGenerators) += Def.task {
val file =
(Compile / resourceManaged).value / "domtypes-version.properties"
IO.write(file, s"domtypes.version=${version.value}")
Seq(file)
}
)
2 changes: 2 additions & 0 deletions project/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ object Versions {

// -- Dependencies --

val ScalaJs = "1.16.0"

val ScalaJsDom = "2.8.0"

// -- Test --
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package com.raquo.sbt.domtypes

import com.raquo.domtypes.codegen.CanonicalDefGroups
import com.raquo.domtypes.codegen.CanonicalGenerator
import com.raquo.domtypes.codegen.CodeFormatting
import com.raquo.domtypes.codegen.DefType.LazyVal
import com.raquo.domtypes.codegen.SourceRepr
import com.raquo.domtypes.common.AttrDef
import com.raquo.domtypes.common.HtmlTagType
import com.raquo.domtypes.common.PropDef
import com.raquo.domtypes.common.TagDef
import com.raquo.domtypes.defs.styles.StyleTraitDefs

import java.io.File

// This is a rough draft of a wrapper around CanonicalGenerator that provides a more user-friendly API.
// In its current form, it is essentially the Laminator project's /project/DomDefsGenerator.scala file.
// The `typeDefsPackage` is the only required argument for the generator. The Laminar project
// overrides some methods in the CanonicalGenerator as a means to customize generator settings.
// For this proof-of-concept, I've simply made those overrides configuration options. You can imagine
// how this would extend to meet the needs of other client projects (by adding more options).
//
// This is essentially a port of the Laminar project's /project/DomDefsGenerator.scala.
// See: https://github.com/raquo/Laminar/blob/2ca18da3d8f66daf850af820746fef933ba0c2f9/project/DomDefsGenerator.scala
//
// There is no SBT dependency, so the final draft could be added to the core library if desired so that
// it would be available to non-SBT clients as well (such as Mill).
final case class ConfiguredGenerator(
typeDefsPackage: String,
settersPackagePath: Option[String] = None,
scalaJsElementTypeParam: Option[String] = None
) { config =>
def generateDomTypeDefs(
managedSourceDir: File
): Seq[File] = {
val packageName = config.typeDefsPackage
val targetDir = new File(managedSourceDir, typeDefsPackage.replace(".", File.separator))
object generator
extends CanonicalGenerator(
baseOutputDirectoryPath = targetDir.getAbsolutePath,
basePackagePath = packageName,
standardTraitCommentLines = Nil,
format = CodeFormatting()
) {
override def settersPackagePath: String = config.settersPackagePath.getOrElse(super.settersPackagePath)
override def scalaJsElementTypeParam: String = config.scalaJsElementTypeParam.getOrElse(super.scalaJsElementTypeParam)
// TODO: more overrides from config
}
val defGroups = new CanonicalDefGroups()

def mkTagsFile(
traitName: String,
groups: CanonicalDefGroups => List[(String, List[TagDef])],
keyImplName: String,
packagePath: CanonicalGenerator => String
): File = {
val content = generator.generateTagsTrait(
tagType = HtmlTagType,
defGroups = groups(defGroups),
printDefGroupComments = true,
traitCommentLines = Nil,
traitModifiers = Nil,
traitName = traitName,
keyKind = traitName,
baseImplDefComments = Nil,
keyImplName = keyImplName,
defType = LazyVal
)
generator.writeToFile(packagePath = packagePath(generator), fileName = traitName, fileContent = content)
}

// made the following helpers in haste to simplify this proof-of-concept. There could be some errors here.
// TODO(markschaake): Final draft should probably remove the helpers and carefully set each param for each group
// according to configuration.
def mkAttrsFile(
traitName: String,
groups: CanonicalDefGroups => List[(String, List[AttrDef])],
keyImplName: String,
packagePath: CanonicalGenerator => String,
nameSpaceImpl: String => String,
transformAttrDomName: String => String = identity
): File = {
val content = generator.generateAttrsTrait(
defGroups = groups(defGroups),
printDefGroupComments = true,
traitCommentLines = Nil,
traitModifiers = Nil,
traitName = traitName,
keyKind = traitName,
implNameSuffix = traitName,
baseImplDefComments = Nil,
baseImplName = keyImplName,
namespaceImports = Nil,
namespaceImpl = nameSpaceImpl,
transformAttrDomName = transformAttrDomName,
defType = LazyVal
)
generator.writeToFile(packagePath = packagePath(generator), fileName = traitName, fileContent = content)
}

def mkPropsFile(
traitName: String,
groups: CanonicalDefGroups => List[(String, List[PropDef])],
keyImplName: String,
packagePath: CanonicalGenerator => String
): File = {
val content = generator.generatePropsTrait(
defGroups = groups(defGroups),
printDefGroupComments = true,
traitCommentLines = Nil,
traitModifiers = Nil,
traitName = traitName,
keyKind = traitName,
implNameSuffix = traitName,
baseImplDefComments = Nil,
baseImplName = keyImplName,
defType = LazyVal
)
generator.writeToFile(packagePath = packagePath(generator), fileName = traitName, fileContent = content)
}

val htmlTags = mkTagsFile("HtmlTags", _.htmlTagsDefGroups, "htmlTag", _.tagDefsPackagePath)

val svgTags = mkTagsFile("SvgTags", _.svgTagsDefGroups, "svgTag", _.tagDefsPackagePath)
val htmlAttrs = mkAttrsFile("HtmlAttrs", _.htmlAttrDefGroups, "htmlAttr", _.attrDefsPackagePath, identity)
val svgAttrs = mkAttrsFile("SvgAttrs", _.svgAttrDefGroups, "svgAttr", _.attrDefsPackagePath, SourceRepr(_))
val ariaAttrs = mkAttrsFile(
"AriaAttrs",
_.ariaAttrDefGroups,
"ariaAttr",
_.attrDefsPackagePath,
identity,
ariaAttrName => if (ariaAttrName.startsWith("aria-")) ariaAttrName.substring(5) else throw new Exception(s"Aria attribute does not start with aria")
)
val htmlProps = mkPropsFile("HtmlProps", _.propDefGroups, "htmlProp", _.propDefsPackagePath)
val globalEventProps = {
val baseTraitName = "GlobalEventProps"

val subTraits = List(
"WindowEventProps" -> defGroups.windowEventPropDefGroups,
"DocumentEventProps" -> defGroups.documentEventPropDefGroups
)
val content = generator.generateEventPropsTrait(
defSources = defGroups.globalEventPropDefGroups,
printDefGroupComments = true,
traitCommentLines = Nil,
traitModifiers = Nil,
traitName = baseTraitName,
traitExtends = Nil,
traitThisType = None,
baseImplDefComments = Nil,
outputBaseImpl = true,
keyKind = "EventProp",
keyImplName = "eventProp",
defType = LazyVal
)
val mainTrait = generator.writeToFile(packagePath = generator.eventPropDefsPackagePath, fileName = baseTraitName, fileContent = content)
val subTraitFiles = subTraits.map { case (traitName, groups) =>
val fileContent = generator.generateEventPropsTrait(
defSources = groups,
printDefGroupComments = true,
traitCommentLines = List(groups.head._1),
traitModifiers = Nil,
traitName = traitName,
traitExtends = Nil,
traitThisType = Some(baseTraitName),
baseImplDefComments = Nil,
outputBaseImpl = false,
keyKind = "EventProp",
keyImplName = "eventProp",
defType = LazyVal
)

generator.writeToFile(
packagePath = generator.eventPropDefsPackagePath,
fileName = traitName,
fileContent = fileContent
)
}
mainTrait :: subTraitFiles
}
val styleProps = {
val traitName = "StyleProps"
val baseTraitName = "StyleProps"
val content = generator.generateStylePropsTrait(
defSources = defGroups.stylePropDefGroups,
printDefGroupComments = true,
traitCommentLines = Nil,
traitModifiers = Nil,
traitName = traitName,
keyKind = "StyleProp",
keyKindAlias = "StyleProp",
setterType = "StyleSetter",
setterTypeAlias = "SS",
derivedKeyKind = "DerivedStyleProp",
derivedKeyKindAlias = "DSP",
baseImplDefComments = Nil,
baseImplName = "styleProp",
defType = LazyVal,
lengthUnitsNumType = "Int",
outputUnitTraits = true
)
generator.writeToFile(packagePath = generator.stylePropDefsPackagePath, fileName = baseTraitName, fileContent = content)
}
val styleTraits = StyleTraitDefs.defs.map { styleTrait =>
val fileContent = generator.generateStyleKeywordsTrait(
defSources = styleTrait.keywordDefGroups,
printDefGroupComments = styleTrait.keywordDefGroups.length > 1,
traitCommentLines = Nil,
traitModifiers = Nil,
traitName = styleTrait.scalaName.replace("[_]", ""),
extendsTraits = styleTrait.extendsTraits.map(_.replace("[_]", "")),
extendsUnitTraits = styleTrait.extendsUnits,
propKind = "StyleProp",
keywordType = "StyleSetter",
derivedKeyKind = "DerivedStyleProp",
lengthUnitsNumType = "Int",
defType = LazyVal,
outputUnitTypes = true,
allowSuperCallInOverride = false // can't access lazy val from `super`
)

generator.writeToFile(
packagePath = generator.styleTraitsPackagePath(),
fileName = styleTrait.scalaName.replace("[_]", ""),
fileContent = fileContent
)
}
Seq(htmlTags, svgTags, htmlAttrs, svgAttrs, ariaAttrs, htmlProps, styleProps) ++ globalEventProps ++ styleTraits
}
}
Loading