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

Add the tlSitePublishTags setting #183

Merged
merged 4 commits into from
Mar 1, 2022
Merged
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
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 @@ -42,6 +42,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)")
}

Expand All @@ -53,6 +55,7 @@ object TypelevelSitePlugin extends AutoPlugin {

override def buildSettings = Seq(
tlSitePublishBranch := Some("main"),
tlSitePublishTags := tlSitePublishBranch.value.isEmpty,
tlSiteApiUrl := None,
tlSiteKeepFiles := true,
homepage := {
Expand Down Expand Up @@ -128,30 +131,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