Skip to content

Commit

Permalink
Update mui to MUI5
Browse files Browse the repository at this point in the history
  • Loading branch information
nafg committed Dec 10, 2023
1 parent e1e2be7 commit 5e39f71
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 51 deletions.
64 changes: 32 additions & 32 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ lazy val reactWaypoint = project.configure(moduleConfig("react-waypoint", "10.3.
lazy val reactDatepicker = project.configure(moduleConfig("react-datepicker", "4.8.0"))


def materialUiCoreVersion = "4.12.4"
def materialUiCoreVersion = "5.7.0"

def commonPropInfoTransformer: PropInfoTransformer = {
case (_, p @ PropInfo("classes", _, _, _, true)) =>
Expand All @@ -92,12 +92,39 @@ def materialUiGitUrl = "https://github.com/mui-org/material-ui.git"

lazy val materialUiCore =
project
.configure(moduleConfig("@material-ui/core", materialUiCoreVersion))
.configure(moduleConfig("@mui/material", materialUiCoreVersion))
.enablePlugins(FacadeGeneratorPlugin)
.settings(
reactDocGenRepoUrl := materialUiGitUrl,
reactDocGenRepoRef := ("v" + materialUiCoreVersion),
propInfoTransformer := commonPropInfoTransformer.orElse {
case (ComponentInfo("Autocomplete", _, _), p) =>
p.name match {
case "filterOptions" => p.copy(propTypeInfo = PropTypeInfo("(Seq[js.Any], js.Object) => Seq[js.Any]"))
case "getOptionLabel" => p.copy(propTypeInfo = PropTypeInfo("js.Any => String"))
case "onChange" =>
p.copy(
propTypeInfo =
PropTypeInfo(
"(ReactEvent, js.Any) => Callback",
CommonImports.Callback ++ CommonImports.ReactEvent
)
)
case "onInputChange" =>
p.copy(
propTypeInfo =
PropTypeInfo(
"(ReactEvent, String, String) => Callback",
CommonImports.Callback ++ CommonImports.ReactEvent
)
)
case "renderInput" =>
p.copy(propTypeInfo = PropTypeInfo("js.Dictionary[js.Any] => VdomNode", CommonImports.VdomNode))
case "renderOption" =>
p.copy(propTypeInfo = PropTypeInfo("js.Any => VdomNode", CommonImports.VdomNode))
case _ =>
p
}
case (ComponentInfo("ClickAwayListener" | "Tooltip", _, _), p @ PropInfo("children", _, _, _, _)) =>
p.copy(
required = true,
Expand Down Expand Up @@ -174,12 +201,12 @@ lazy val materialUiCore =
case c @ ComponentCodeGenInfo(ComponentInfo("ButtonGroup" | "List", _, _), _, _) =>
c.copy(moduleTrait = "FacadeModule.ArrayChildren")
},
Compile / sourceGenerators += generateReactDocGenFacades("packages/material-ui/src", "@material-ui/core", "mui")
Compile / sourceGenerators += generateReactDocGenFacades("packages/mui-material/src", "@mui/material", "mui")
)

lazy val materialUiLab =
project
.configure(moduleConfig("@material-ui/lab", "4.0.0-alpha.60"))
.configure(moduleConfig("@mui/lab", "5.0.0-alpha.110"))
.enablePlugins(FacadeGeneratorPlugin)
.settings(
reactDocGenRepoUrl := materialUiGitUrl,
Expand All @@ -190,41 +217,14 @@ lazy val materialUiLab =
propTypeInfo =
PropTypeInfo("(ReactEvent, js.Any) => Callback", CommonImports.Callback ++ CommonImports.ReactEvent)
)
case (ComponentInfo("Autocomplete", _, _), p) =>
p.name match {
case "filterOptions" => p.copy(propTypeInfo = PropTypeInfo("(Seq[js.Any], js.Object) => Seq[js.Any]"))
case "getOptionLabel" => p.copy(propTypeInfo = PropTypeInfo("js.Any => String"))
case "onChange" =>
p.copy(
propTypeInfo =
PropTypeInfo(
"(ReactEvent, js.Any) => Callback",
CommonImports.Callback ++ CommonImports.ReactEvent
)
)
case "onInputChange" =>
p.copy(
propTypeInfo =
PropTypeInfo(
"(ReactEvent, String, String) => Callback",
CommonImports.Callback ++ CommonImports.ReactEvent
)
)
case "renderInput" =>
p.copy(propTypeInfo = PropTypeInfo("js.Dictionary[js.Any] => VdomNode", CommonImports.VdomNode))
case "renderOption" =>
p.copy(propTypeInfo = PropTypeInfo("js.Any => VdomNode", CommonImports.VdomNode))
case _ =>
p
}
},
componentInfoTransformer := commonComponentInfoTransformer,
componentCodeGenInfoTransformer := {
case c if Set("Breadcrumbs", "ToggleButtonGroup").contains(c.componentInfo.name) =>
c.copy(moduleTrait = "FacadeModule.ArrayChildren")
},
Compile / sourceGenerators +=
generateReactDocGenFacades("packages/material-ui-lab/src", "@material-ui/lab", "mui.lab")
generateReactDocGenFacades("packages/mui-lab/src", "@mui/lab", "mui.lab")
)

val generateInstallInstructions = taskKey[Unit]("Generate install instructions in README.md")
Expand Down
30 changes: 21 additions & 9 deletions project/FacadeGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ object FacadeGenerator {
doCached(CacheStore((base / "docgen-cache.json").toIO), repoDir, subDir) {
val docGenOutputFile = base / "react-docgen.json"
println(s"Writing react-docgen JSON for $subDir to $docGenOutputFile")
def logFile = base / "react-docgen.log"
val logToFile = BooleanProp.keyExists("docgen.log").value
val log: ProcessOutput =
if (BooleanProp.keyExists("docgen.log").value)
if (logToFile)
os.Inherit
else
base / "react-docgen.log"
os.proc("yarn", "react-docgen", "--out", docGenOutputFile.toString(), "--exclude", ".*\\.test\\.js$", subDir)
.call(cwd = repoDir, stderr = log, stdout = log)
else {
println(s"Writing react-docgen log for $subDir to $logFile")
logFile
}
val res =
os.proc("yarn", "react-docgen", "--out", docGenOutputFile.toString(), "--exclude", ".*\\.test\\.js$", "-x", "js", "-x", "tsx", subDir)
.call(cwd = repoDir, stderr = log, stdout = log, check = false)
if (res.exitCode != 0) {
if (logToFile)
println(os.read(logFile))
throw new Exception(s"react-docgen failed with exit code ${res.exitCode}")
}
println()
os.read(docGenOutputFile)
}
Expand Down Expand Up @@ -103,15 +113,17 @@ object FacadeGenerator {
.map(i => "PropTypes.WithChildren" -> Some(i.propTypeInfo.code))
.getOrElse("PropTypes" -> None)
val propDefs =
info.props.map { case PropInfo(_, ident, PropTypeInfo(code, _, presets), description, _) =>
info.props.map { case PropInfo(_, ident, PropTypeInfo(typeCode, _, presets), description, _) =>
s"${comment(description, " ")}\n" +
(if (presets.isEmpty)
s"def $ident = of[$code]"
s"def $ident = of[$typeCode]"
else {
s"""object $ident extends PropTypes.Prop[$code]("$ident") {
s"""object $ident extends PropTypes.Prop[$typeCode]("$ident") {
|${
presets
.map { case PropTypeInfo.Preset(name, code) => s" val $name = this := $code" }
.map { case PropTypeInfo.Preset(name, presetCode) =>
s" val $name = this := $presetCode.asInstanceOf[$typeCode]"
}
.mkString("\n")
}
|}""".stripMargin
Expand Down
23 changes: 13 additions & 10 deletions project/PropInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ object PropInfo {
.toSeq
.sortBy(_._1)
.flatMap { case (name, spec) =>
PropType.read(spec("type").obj)
.map { propType =>
PropInfo(
name = name,
ident = Identifier(name),
propTypeInfo = PropTypeInfo(propType),
description = spec("description").str,
required = spec("required").bool
)
}
if (!spec.obj.contains("type"))
None
else
PropType.read(spec("type").obj)
.map { propType =>
PropInfo(
name = name,
ident = Identifier(name),
propTypeInfo = PropTypeInfo(propType),
description = spec("description").str,
required = spec("required").bool
)
}
}
implicit val jf: JsonFormat[PropInfo] =
caseClass(new PropInfo(_, _, _, _, _), unapply)("name", "ident", "propTypeInfo", "description", "required")
Expand Down

0 comments on commit 5e39f71

Please sign in to comment.