Skip to content

Commit

Permalink
generate website workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
khajavi committed Dec 5, 2022
1 parent e97e35a commit 0ea62ee
Show file tree
Hide file tree
Showing 7 changed files with 618 additions and 74 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/site.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file was autogenerated using `zio-sbt` via `sbt generateGithubWorkflow`
# task and should be included in the git repository. Please do not edit
# it manually.

name: Website
'on':
release:
types:
- published
jobs:
publish-docs:
runs-on: ubuntu-latest
steps:
- name: Git Checkout
with:
fetch-depth: '0'
uses: actions/[email protected]
- name: Setup Scala
with:
node-version: 16.x
registry-url: https://registry.npmjs.org
uses: actions/[email protected]
- env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
name: Publish Docs to NPM Registry
run: sbt docs/publishToNpm
name: Publish Docs to The NPM Registry

1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.6.0")

libraryDependencies += "org.snakeyaml" % "snakeyaml-engine" % "2.5"
libraryDependencies += "dev.zio" %% "zio" % "2.0.2"
libraryDependencies += "io.circe" %% "circe-yaml" % "0.14.2"
3 changes: 2 additions & 1 deletion zio-sbt-website/build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.6")

libraryDependencies += "dev.zio" %% "zio" % "2.0.2"
libraryDependencies += "dev.zio" %% "zio" % "2.0.4"
libraryDependencies += "io.circe" %% "circe-yaml" % "0.14.2"
77 changes: 4 additions & 73 deletions zio-sbt-website/src/main/scala/zio/sbt/WebsitePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package zio.sbt

import java.nio.file.{Files, Path, Paths}
import java.nio.file.{Path, Paths}

import scala.sys.process.*

Expand Down Expand Up @@ -207,7 +207,7 @@ object WebsitePlugin extends sbt.AutoPlugin {

Unsafe.unsafe { implicit unsafe =>
Runtime.default.unsafe
.run(WebsitePluginUtils.generateReadme(websiteDir.value.resolve("docs/index.md").toString))
.run(WebsiteUtils.generateReadme(websiteDir.value.resolve("docs/index.md").toString))
.getOrThrowFiberFailure()
}
val logger = streams.value.log
Expand All @@ -221,81 +221,12 @@ object WebsitePlugin extends sbt.AutoPlugin {
val template =
s"""|# This file was autogenerated using `zio-sbt` via `sbt generateGithubWorkflow`
|# task and should be included in the git repository. Please do not edit
|# it manually.
|# it manually.
|
|name: website
|
|on:
| release:
| types: [ published ]
|
|jobs:
| publish-docs:
| runs-on: ubuntu-20.04
| timeout-minutes: 30
| steps:
| - uses: actions/[email protected]
| with:
| fetch-depth: 0
| - name: Setup Scala and Java
| uses: actions/[email protected]
| with:
| distribution: temurin
| java-version: 17
| check-latest: true
| - uses: actions/setup-node@v3
| with:
| node-version: '16.x'
| registry-url: 'https://registry.npmjs.org'
| - name: Publishing Docs to NPM Registry
| run: sbt docs/publishToNpm
| env:
| NODE_AUTH_TOKEN: $${{ secrets.NPM_TOKEN }}
|${WebsiteUtils.websiteWorkflow}
|""".stripMargin

IO.write(new File(".github/workflows/site.yml"), template)
}

}

object WebsitePluginUtils {
import zio.*

import java.nio.charset.StandardCharsets

def removeYamlHeader(markdown: String): String =
markdown
.split("\n")
.dropWhile(_ == "---")
.dropWhile(_ != "---")
.dropWhile(_ == "---")
.mkString("\n")

def readFile(pathname: String): Task[String] =
ZIO.attemptBlocking {
val source = scala.io.Source.fromFile(new File(pathname))
val result = source.getLines().mkString("\n")
source.close()
result
}

def generateReadme(sourcePath: String): Task[Unit] =
for {
template <- readFile("README.template.md")
mainContent <- readFile(sourcePath).map(md => removeYamlHeader(md))
comment =
"""|[//]: # (This file was autogenerated using `zio-sbt-website` plugin via `sbt generateReadme` command.)
|[//]: # (So please do not edit it manually. Instead, edit `README.template.md` file. This command will replace any)
|[//]: # ({{ main_content }} template tag inside the `README.template.md` file with the main content of the)
|[//]: # ("docs/index.md" file.)
|""".stripMargin
readme <- ZIO.succeed(comment + '\n' + template.replaceFirst("\\{\\{.*main_content.*}}", mainContent))
_ <- ZIO.attemptBlocking(
Files.write(
Paths.get("README.md"),
readme.getBytes(StandardCharsets.UTF_8)
)
)
} yield ()

}
92 changes: 92 additions & 0 deletions zio-sbt-website/src/main/scala/zio/sbt/WebsiteUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package zio.sbt

import java.nio.file.{Files, Paths}

import io.circe.syntax.*
import sbt.File

import zio.*
import zio.sbt.githubactions.*

object WebsiteUtils {

import java.nio.charset.StandardCharsets

def removeYamlHeader(markdown: String): String =
markdown
.split("\n")
.dropWhile(_ == "---")
.dropWhile(_ != "---")
.dropWhile(_ == "---")
.mkString("\n")

def readFile(pathname: String): Task[String] =
ZIO.attemptBlocking {
val source = scala.io.Source.fromFile(new File(pathname))
val result = source.getLines().mkString("\n")
source.close()
result
}

def generateReadme(sourcePath: String): Task[Unit] =
for {
template <- readFile("README.template.md")
mainContent <- readFile(sourcePath).map(md => removeYamlHeader(md))
comment =
"""|[//]: # (This file was autogenerated using `zio-sbt-website` plugin via `sbt generateReadme` command.)
|[//]: # (So please do not edit it manually. Instead, edit `README.template.md` file. This command will replace any)
|[//]: # ({{ main_content }} template tag inside the `README.template.md` file with the main content of the)
|[//]: # ("docs/index.md" file.)
|""".stripMargin
readme <- ZIO.succeed(comment + '\n' + template.replaceFirst("\\{\\{.*main_content.*}}", mainContent))
_ <- ZIO.attemptBlocking(
Files.write(
Paths.get("README.md"),
readme.getBytes(StandardCharsets.UTF_8)
)
)
} yield ()

val websiteWorkflow: String =
io.circe.yaml
.Printer(dropNullKeys = true)
.pretty(
Workflow(
name = "Website",
triggers = Seq(Trigger.Release(Seq("published"))),
jobs = Seq(
Job(
id = "publish-docs",
name = "Publish Docs to The NPM Registry",
steps = Seq(
Step.StepSequence(
Seq(
Step.SingleStep(
name = "Git Checkout",
uses = Some(ActionRef("actions/[email protected]")),
parameters = Map("fetch-depth" -> "0".asJson)
),
Step.SingleStep(
name = "Setup Scala",
uses = Some(ActionRef("actions/[email protected]")),
parameters = Map(
"node-version" -> "16.x".asJson,
"registry-url" -> "https://registry.npmjs.org".asJson
)
),
Step.SingleStep(
name = "Publish Docs to NPM Registry",
run = Some("sbt docs/publishToNpm"),
env = Map(
"NODE_AUTH_TOKEN" -> "${{ secrets.NPM_TOKEN }}"
)
)
)
)
)
)
)
).asJson
)

}
Loading

0 comments on commit 0ea62ee

Please sign in to comment.