Skip to content

Commit

Permalink
New plugin unidoc & serve api doc in demo
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed Nov 25, 2023
1 parent 2e6ef00 commit 5db8ef0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 59 deletions.
73 changes: 48 additions & 25 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,29 @@ lazy val publishConfigure: Project => Project = _.settings(
// )
// )

/** run with 'docs/clean' then 'docs/mdoc' and then 'docs/laikaSite' */
/** https://docs.scala-lang.org/scala3/guides/scaladoc/settings.html */
lazy val docConfigure: Project => Project =
_.settings(
autoAPIMappings := true,
Compile / doc / target := {
val path = rootPaths.value.apply("BASE").toFile() /
"docs" / "target" / "scaladoc" / "apis" / name.value / baseDirectory.value.getName
// REMOVE println(path.getAbsolutePath())
path
},
apiURL := Some(url(s"https://did.fmgp.app/apis/${name.value}/${baseDirectory.value.getName}")),
)

/** Custom commands:
* - doc
* - docs/mdoc
* - docs/laikaSite
* - docs/unidoc
* - docs/previewSite
*/
lazy val docs = project
.in(file("docs"))
.settings(skip / publish := true)
.enablePlugins(LaikaPlugin)
.enablePlugins(MdocPlugin)
.settings(
mdocVariables := Map("VERSION" -> version.value),
Expand All @@ -81,15 +99,31 @@ lazy val docs = project
disableUnusedWarningsForMdoc
}
)
.enablePlugins(LaikaPlugin)
.settings(
name := "scala-did-docs",
laikaTheme := ManualSettings.themeProvider(version.value),
laikaConfig := LaikaConfig.defaults, // ManualSettings.config,
laikaConfig := ManualSettings.config,
laikaExtensions := Seq(laika.format.Markdown.GitHubFlavor, laika.config.SyntaxHighlighting),
Laika / sourceDirectories := Seq(mdocOut.value),
Laika / target := baseDirectory.value / "target",
laikaSite / target := target.value / "site" / "did-doc",
laikaIncludeAPI := true,
laikaIncludeAPI := false,
)
.enablePlugins(ScalaUnidocPlugin)
.settings(
ScalaUnidoc / unidoc / target := target.value / "scaladoc" / "unidoc",
ScalaUnidoc / unidoc / unidocProjectFilter := {
inProjects(
did.jvm,
didFramework.jvm,
didImp.jvm,
multiformats.jvm,
didResolverPeer.jvm,
didResolverWeb.jvm,
didUniresolver.jvm,
), // or inAnyProject -- inProjects(...)
}
)
.dependsOn(did.jvm)

Expand Down Expand Up @@ -259,19 +293,6 @@ lazy val buildInfoConfigure: Project => Project = _.enablePlugins(BuildInfoPlugi
),
)

/** https://docs.scala-lang.org/scala3/guides/scaladoc/settings.html */
lazy val docConfigure: Project => Project =
_.settings(
autoAPIMappings := true,
Compile / doc / target := {
val path = rootPaths.value.apply("BASE").toFile() /
"docs" / "target" / "api" / name.value / baseDirectory.value.getName
println(path.getAbsolutePath())
path
},
apiURL := Some(url(s"https://did.fmgp.app/apis/${name.value}/${baseDirectory.value.getName}")),
)

addCommandAlias(
"testJVM",
";didJVM/test; didFrameworkJVM/test; didImpJVM/test; " +
Expand All @@ -285,12 +306,13 @@ addCommandAlias(
"multiformatsJS/test"
)
addCommandAlias("testAll", ";testJVM;testJS")
addCommandAlias("docsAll", "docs/mdoc;doc;docs/laikaSite")
addCommandAlias("fastPackAll", "compile;docsAll;serviceworker/fastLinkJS;webapp/fastLinkJS")
addCommandAlias("fullPackAll", "compile;docsAll;serviceworker/fullLinkJS;webapp/fullLinkJS")
addCommandAlias("assemblyAll", "installFrontend;fullPackAll;buildFrontend;demoJVM/assembly")
addCommandAlias("docAll", "doc;docs/unidoc")
addCommandAlias("siteAll", "docs/mdoc;docs/laikaSite")
addCommandAlias("fastPackAll", "compile;serviceworker/fastLinkJS;webapp/fastLinkJS")
addCommandAlias("fullPackAll", "compile;serviceworker/fullLinkJS;webapp/fullLinkJS")
addCommandAlias("assemblyAll", "docAll;siteAll;installFrontend;fullPackAll;buildFrontend;demoJVM/assembly")
addCommandAlias("live", "fastPackAll;~demoJVM/reStart")
addCommandAlias("ciJob", "installFrontend;fullPackAll;buildFrontend;testAll")
addCommandAlias("ciJob", "docAll;siteAll;installFrontend;fullPackAll;buildFrontend;testAll")

lazy val installFrontend = taskKey[Unit]("Install all NPM package")
installFrontend := {
Expand All @@ -314,16 +336,17 @@ lazy val root = project
.settings(publish / skip := true)
.aggregate(did.js, did.jvm) // publish
.aggregate(didFramework.js, didFramework.jvm) // publish
.aggregate(didExperiments.js, didExperiments.jvm) // NOT publish
.aggregate(didImp.js, didImp.jvm) // publish
.aggregate(multiformats.js, multiformats.jvm) // publish
.aggregate(didResolverPeer.js, didResolverPeer.jvm) // publish
.aggregate(didResolverWeb.js, didResolverWeb.jvm) // publish
.aggregate(didUniresolver.js, didUniresolver.jvm) // NOT publish
.aggregate(docs) // just to aggregate the command clean
// Move to a new repository
.aggregate(didExperiments.js, didExperiments.jvm) // NOT publish
.aggregate(didExample.js, didExample.jvm)
.aggregate(webapp, serviceworker)
.aggregate(serviceworker)
.aggregate(webapp)
.aggregate(demo.jvm, demo.js)

lazy val did = crossProject(JSPlatform, JVMPlatform)
Expand Down Expand Up @@ -557,7 +580,7 @@ lazy val demo = crossProject(JSPlatform, JVMPlatform)
assembly / assemblyJarName := "scala-did-demo-server.jar",
libraryDependencies += D.ziohttp.value,
Compile / unmanagedResourceDirectories += baseDirectory.value / "src" / "main" / "extra-resources",
Compile / unmanagedResourceDirectories += rootPaths.value.apply("BASE").toFile() / "docs" / "target" / "api",
Compile / unmanagedResourceDirectories += rootPaths.value.apply("BASE").toFile() / "docs" / "target" / "scaladoc",
// Compile / unmanagedResourceDirectories += rootPaths.value.apply("BASE").toFile() / "docs" / "target" / "mdoc"
Compile / unmanagedResourceDirectories += rootPaths.value.apply("BASE").toFile() / "docs" / "target" / "site",
Compile / unmanagedResourceDirectories += rootPaths.value.apply("BASE").toFile() / "vite" / "dist",
Expand Down
77 changes: 56 additions & 21 deletions demo/jvm/src/main/scala/fmgp/did/demo/DocsApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,19 @@ object DocsApp {
for {
path <- extractPath
file <- Handler.getResourceAsFile("did-doc/" + path.dropLeadingSlash.encode)
http <-
// Rendering a custom UI to list all the files in the directory
extractRequest >>> {
http <- extractRequest >>> {
if (file.isDirectory) {
// Accessing the files in the directory
val files = file.listFiles.toList.sortBy(_.getName)
val base = "/doc/"
val rest = path.dropTrailingSlash

println("11-" + path.encode)
println("22-" + rest)

// Custom UI to list all the files in the directory
Handler.template(s"File Explorer ~$base$rest") {
ul(
li(a(href := s"$base$rest", "..")),
files.map { file => li(a(href := s"$base${rest}${file.getName}", file.getName)) },
val auxPath = path./:("/doc")
Handler.template(s"File Explorer ~$auxPath") {
ul( // Custom UI to list all the files in the directory
li(a(href := s"${auxPath.dropRight(1)}", "..")),
files.map { file => li(a(href := s"${auxPath.addTrailingSlash}${file.getName}", file.getName)) },
)
}
}

// Return the file if it's a static resource
else if (file.isFile) Handler.fromFile(file)

// Return a 404 if the file doesn't exist
else Handler.notFound
} else if (file.isFile) Handler.fromFile(file) // Return the file if it's a static resource
else Handler.notFound // Return a 404 if the file doesn't exist
}
} yield http

Expand All @@ -95,6 +82,54 @@ object DocsApp {
// case s => Header.ContentType(MediaType.text.plain)
// Handler.fromResource(fullPath).map(_.addHeader(headerContentType))
},
Method.GET / "apis" / trailing -> handler {
val extractPath = Handler.param[(Path, Request)](_._1)
val extractRequest = Handler.param[(Path, Request)](_._2)
import zio.http.template._

for {
path <- extractPath
file <- Handler.getResourceAsFile("apis/" + path.dropLeadingSlash.encode)
http <- extractRequest >>> {
if (file.isDirectory) {
// Accessing the files in the directory
val files = file.listFiles.toList.sortBy(_.getName)
val auxPath = path./:("/apis")
Handler.template(s"File Explorer ~$auxPath") {
ul( // Custom UI to list all the files in the directory
li(a(href := s"${auxPath.dropRight(1)}", "..")),
files.map { file => li(a(href := s"${auxPath.addTrailingSlash}${file.getName}", file.getName)) },
)
}
} else if (file.isFile) Handler.fromFile(file) // Return the file if it's a static resource
else Handler.notFound // Return a 404 if the file doesn't exist
}
} yield http
},
Method.GET / "api" / trailing -> handler {
val extractPath = Handler.param[(Path, Request)](_._1)
val extractRequest = Handler.param[(Path, Request)](_._2)
import zio.http.template._

for {
path <- extractPath
file <- Handler.getResourceAsFile("unidoc/" + path.dropLeadingSlash.encode)
http <- extractRequest >>> {
if (file.isDirectory) {
// Accessing the files in the directory
val files = file.listFiles.toList.sortBy(_.getName)
val auxPath = path./:("/api")
Handler.template(s"File Explorer ~$auxPath") {
ul( // Custom UI to list all the files in the directory
li(a(href := s"${auxPath.dropRight(1)}", "..")),
files.map { file => li(a(href := s"${auxPath.addTrailingSlash}${file.getName}", file.getName)) },
)
}
} else if (file.isFile) Handler.fromFile(file) // Return the file if it's a static resource
else Handler.notFound // Return a 404 if the file doesn't exist
}
} yield http
},
).sandbox.toHttpApp

}
18 changes: 6 additions & 12 deletions project/ManualSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,16 @@ object ManualSettings {

private object links {
val github = "https://github.com/FabioPinheiro/scala-did"
val scaladoc = "https://did.fmgp.app/api/index.html"
val javadoc = "https://javadoc.io/doc/app.fmgp"
val demo = "https://did.fmgp.app"
val discord = "https://discord.gg/atala"
}

// val config: LaikaConfig = LaikaConfig.defaults
// .withConfigValue(LinkConfig.empty.addApiLinks(ApiLinks(paths.apiURL)))
val config: LaikaConfig = LaikaConfig.defaults
.withConfigValue(LaikaKeys.site.apiPath, "api")
.withConfigValue(LinkConfig.empty.addApiLinks(ApiLinks(links.javadoc + "/did_3/0.1.0-M15/index.html")))
// .withConfigValue(LinkValidation.Global(Seq(Root / "api")))
// .withConfigValue(
// Selections(
// SelectionConfig(
// "config",
// ChoiceConfig("sbt", "sbt Plugin"),
// ChoiceConfig("library", "Library API")
// ).withSeparateEbooks
// )
// )
// .withConfigValue(LaikaKeys.artifactBaseName, s"laika-${versions.current.displayValue}")
// .withConfigValue(LaikaKeys.versioned, true)

Expand Down Expand Up @@ -90,8 +83,9 @@ object ManualSettings {
),
projectLinks = Seq(
TextLink.external(links.github, "Source on GitHub"),
TextLink.external(links.demo, "Live Demo"),
TextLink.external(links.scaladoc, "Merged Scaladoc"),
TextLink.external(links.javadoc, "API Javadoc"),
TextLink.external(links.demo, "Live Demo"),
),
teasers = Seq(
Teaser(
Expand Down
4 changes: 3 additions & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.11.1")

// mdoc - https://github.com/scalameta/mdoc/tags
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.5.1")
addSbtPlugin("org.typelevel" % "laika-sbt" % "1.0.0")
addSbtPlugin("org.typelevel" % "laika-sbt" % "1.0.0") // https://typelevel.org/Laika/
addSbtPlugin("com.github.sbt" % "sbt-unidoc" % "0.5.0") // https://github.com/sbt/sbt-unidoc
// addSbtPlugin("org.typelevel" % "sbt-typelevel-site" % "0.6.2") // https://typelevel.org/sbt-typelevel/site.html

// Deploy demo - https://github.com/sbt/sbt-assembly/tags
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.3")
Expand Down

0 comments on commit 5db8ef0

Please sign in to comment.