Skip to content

Commit

Permalink
Merge pull request #183 from danicheg/tlSitePublishTags
Browse files Browse the repository at this point in the history
Add the `tlSitePublishTags` setting
  • Loading branch information
armanbilge authored Mar 1, 2022
2 parents 2113388 + 687cf51 commit 6b942f9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
1 change: 1 addition & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Instead of using the super-plugins, for finer-grained control you can always add
### sbt-typelevel-site
- `TypelevelSitePlugin`: Sets up an [mdoc](https://scalameta.org/mdoc/)/[Laika](https://planet42.github.io/Laika/)-generated microsite, automatically published to GitHub pages in CI.
- `tlSitePublishBranch` (setting): The branch to publish the site from on every push. Set this to `None` if you only want to update the site on tag releases. (default: `main`)
- `tlSitePublishTags` (setting): Defines whether the site should be published on tag releases. Note on setting this to `true` requires the `tlSitePublishBranch` setting to be set to `None`. (default: `false`)
- `tlSiteApiUrl` (setting): URL to the API docs. (default: `None`)
- `tlSiteHeliumConfig` (setting): the Laika Helium config. (default: how the sbt-typelevel site looks)

Expand Down
71 changes: 47 additions & 24 deletions site/src/main/scala/org/typelevel/sbt/TypelevelSitePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ object TypelevelSitePlugin extends AutoPlugin {
"A sequence of workflow steps which publishes the site (default: peaceiris/actions-gh-pages)")
lazy val tlSitePublishBranch = settingKey[Option[String]](
"The branch to publish the site from on every push. Set this to None if you only want to update the site on tag releases. (default: main)")
lazy val tlSitePublishTags = settingKey[Boolean](
"Defines whether the site should be published on tag releases. Note on setting this to true requires the 'tlSitePublishBranch' setting to be set to None. (default: false)")
lazy val tlSite = taskKey[Unit]("Generate the site (default: runs mdoc then laika)")
lazy val tlSitePreview = taskKey[Unit](
"Start a live-reload preview server (combines mdoc --watch with laikaPreview)")
Expand All @@ -73,6 +75,7 @@ object TypelevelSitePlugin extends AutoPlugin {

override def buildSettings = Seq(
tlSitePublishBranch := Some("main"),
tlSitePublishTags := tlSitePublishBranch.value.isEmpty,
tlSiteApiUrl := None,
tlSiteRelatedProjects := Seq(TypelevelProject.Cats),
tlSiteKeepFiles := true,
Expand Down Expand Up @@ -160,30 +163,50 @@ object TypelevelSitePlugin extends AutoPlugin {
name = Some("Generate site")
)
),
tlSitePublish := List(
WorkflowStep.Use(
UseRef.Public("peaceiris", "actions-gh-pages", "v3.8.0"),
Map(
"github_token" -> s"$${{ secrets.GITHUB_TOKEN }}",
"publish_dir" -> (ThisBuild / baseDirectory)
.value
.toPath
.toAbsolutePath
.relativize((laikaSite / target).value.toPath)
.toString,
"keep_files" -> tlSiteKeepFiles.value.toString
),
name = Some("Publish site"),
cond = {
val predicate = tlSitePublishBranch
.value // Either publish from branch or on tags, not both
.fold[RefPredicate](RefPredicate.StartsWith(Ref.Tag("v")))(b =>
RefPredicate.Equals(Ref.Branch(b)))
val publicationCond = GenerativePlugin.compileBranchPredicate("github.ref", predicate)
Some(s"github.event_name != 'pull_request' && $publicationCond")
}
)
),
tlSitePublish := {
def publishSiteWorkflowStep(publishPredicate: RefPredicate) =
List(
WorkflowStep.Use(
UseRef.Public("peaceiris", "actions-gh-pages", "v3.8.0"),
Map(
"github_token" -> s"$${{ secrets.GITHUB_TOKEN }}",
"publish_dir" -> (ThisBuild / baseDirectory)
.value
.toPath
.toAbsolutePath
.relativize((laikaSite / target).value.toPath)
.toString,
"keep_files" -> tlSiteKeepFiles.value.toString
),
name = Some("Publish site"),
cond = {
val predicate = publishPredicate
val publicationCond =
GenerativePlugin.compileBranchPredicate("github.ref", predicate)
Some(s"github.event_name != 'pull_request' && $publicationCond")
}
)
)

val tlSitePublishTagsV = tlSitePublishTags.value
val tlSitePublishBranchV = tlSitePublishBranch.value

(tlSitePublishTagsV, tlSitePublishBranchV) match {
case (true, Some(_)) =>
sys.error(
s"'tlSitePublishTags' setting is set to 'true' which conflicts with 'tlSitePublishBranch' which is non-empty. " +
s"Site publishing is available from tags or a particular branch, not from both.")

case (true, None) =>
publishSiteWorkflowStep(RefPredicate.StartsWith(Ref.Tag("v")))

case (false, Some(branch)) =>
publishSiteWorkflowStep(RefPredicate.Equals(Ref.Branch(branch)))

case (false, None) =>
List.empty
}
},
ThisBuild / githubWorkflowAddedJobs +=
WorkflowJob(
"site",
Expand Down

0 comments on commit 6b942f9

Please sign in to comment.