Skip to content

Commit

Permalink
Incremental commit toward autoplugin conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simeon H.K. Fitch committed Oct 5, 2015
1 parent 41021ca commit 06c26bc
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 313 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea
target/
target
115 changes: 43 additions & 72 deletions src/main/scala/com/typesafe/sbt/SbtSite.scala
Original file line number Diff line number Diff line change
@@ -1,88 +1,59 @@
package com.typesafe.sbt

import com.typesafe.sbt.site.AsciidoctorSupport
import sbt._
import Keys._
import com.typesafe.sbt.site._


object SbtSite extends Plugin {
object SiteKeys {
object SbtSite extends AutoPlugin {
override def trigger = allRequirements
object autoImport {
val makeSite = TaskKey[File]("make-site", "Generates a static website for a project.")
val packageSite = TaskKey[File]("package-site", "Create a zip file of the website.")

// Helper to point at mappings for the site.
val siteMappings = mappings in makeSite
val siteDirectory = target in makeSite
val siteSources = sources in makeSite
val siteSourceDirectory = sourceDirectory in makeSite

val previewSite = TaskKey[Unit]("preview-site", "Launches a jetty server that serves your generated site from the target directory")
val previewFixedPort = SettingKey[Option[Int]]("previewFixedPort") in previewSite
val previewLaunchBrowser = SettingKey[Boolean]("previewLaunchBrowser") in previewSite
}
import autoImport._

// Helper to point at mappings for the site.
private[sbt] val siteMappings = mappings in makeSite
private[sbt] val siteDirectory = target in makeSite
private[sbt] val siteSources = sources in makeSite
private[sbt] val siteSourceDirectory = sourceDirectory in makeSite

override lazy val projectSettings = Seq(
siteMappings := Seq.empty,
siteMappings <<= siteMappings ?? Seq.empty,
siteDirectory := target.value / "site",
siteSourceDirectory := sourceDirectory.value / "site",
includeFilter in makeSite := "*.html" | "*.css" | "*.png" | "*.jpg" | "*.gif" | "*.js" | "*.swf",
siteMappings ++= SiteHelpers.selectSubpaths(siteSourceDirectory.value, (includeFilter in makeSite).value),
makeSite := SiteHelpers.copySite(siteDirectory.value, streams.value.cacheDirectory, siteMappings.value),
artifact in packageSite := SiteHelpers.siteArtifact(moduleName.value),
artifactPath in packageSite <<= Defaults.artifactPathSetting(artifact in packageSite),
packageSite := SiteHelpers.createSiteZip(makeSite.value, (artifactPath in packageSite).value, streams.value)
)

object site {
import SiteKeys._

val settings = Seq(
siteMappings := Seq.empty,
siteDirectory := target.value / "site",
siteSourceDirectory := sourceDirectory.value / "site",
includeFilter in makeSite := "*.html" | "*.css" | "*.png" | "*.jpg" | "*.gif" | "*.js" | "*.swf",
siteMappings ++= selectSubpaths(siteSourceDirectory.value, (includeFilter in makeSite).value),
makeSite := copySite(siteDirectory.value, streams.value.cacheDirectory, siteMappings.value),
artifact in packageSite := siteArtifact(moduleName.value),
artifactPath in packageSite <<= Defaults.artifactPathSetting(artifact in packageSite),
packageSite := createSiteZip(makeSite.value, (artifactPath in packageSite).value, streams.value)
) ++ Preview.settings

/** Convenience functions to add a task of mappings to a site under a nested directory. */
def addMappingsToSiteDir(mappings: TaskKey[Seq[(File,String)]], nestedDirectory: String): Setting[_] =
siteMappings <++= mappings map { m =>
for((f, d) <- m) yield (f, nestedDirectory + "/" + d)
}

/** Includes scaladoc APIS in site under a "latest/api" directory. */
def includeScaladoc(alias: String = "latest/api"): Seq[Setting[_]] =
Seq(addMappingsToSiteDir(mappings in packageDoc in Compile, alias))
/** Includes Jekyll generated site under the root directory. */
def jekyllSupport(alias: String = ""): Seq[Setting[_]] =
JekyllSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in JekyllSupport.Jekyll, alias))
/** Includes Sphinx generated site under the root directory. */
def sphinxSupport(alias: String = ""): Seq[Setting[_]] =
SphinxSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in SphinxSupport.Sphinx, alias))
/** Includes Pamflet generate site under the root directory. */
def pamfletSupport(alias: String = ""): Seq[Setting[_]] =
PamfletSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in PamfletSupport.Pamflet, alias))
/** Includes Nanoc generated site under the root directory. */
def nanocSupport(alias: String = ""): Seq[Setting[_]] =
NanocSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in NanocSupport.Nanoc, alias))
def asciidoctorSupport(alias: String = ""): Seq[Setting[_]] =
AsciidoctorSupport.settings ++ Seq(addMappingsToSiteDir(mappings in AsciidoctorSupport.Asciidoctor, alias))
def preprocessSite(alias: String = ""): Seq[Setting[_]] =
PreprocessSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in PreprocessSupport.Preprocess, alias))
// def preprocessSite(alias: String = ""): Seq[Setting[_]] =
// PreprocessSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in PreprocessSupport.Preprocess, alias))
//
// /** Includes scaladoc APIS in site under a "latest/api" directory. */
// def includeScaladoc(alias: String = "latest/api"): Seq[Setting[_]] =
// Seq(SiteHelpers.addMappingsToSiteDir(mappings in packageDoc in Compile, alias))
// /** Includes Jekyll generated site under the root directory. */
// def jekyllSupport(alias: String = ""): Seq[Setting[_]] =
// JekyllSupport.settings() ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in JekyllSupport.autoImports.Jekyll, alias))
// /** Includes Sphinx generated site under the root directory. */
// def sphinxSupport(alias: String = ""): Seq[Setting[_]] =
// SphinxSupport.settings() ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in SphinxSupport.Sphinx, alias))
// /** Includes Pamflet generate site under the root directory. */
// def pamfletSupport(alias: String = ""): Seq[Setting[_]] =
// PamfletSupport.settings() ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in PamfletSupport.Pamflet, alias))
// /** Includes Nanoc generated site under the root directory. */
// def nanocSupport(alias: String = ""): Seq[Setting[_]] =
// NanocSupport.settings() ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in NanocSupport.Nanoc, alias))
// def asciidoctorSupport(alias: String = ""): Seq[Setting[_]] =
// AsciidoctorSupport.settings ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in AsciidoctorSupport.Asciidoctor, alias))
def publishSite(): SettingsDefinition = addArtifact(artifact in packageSite, packageSite)
}

// Note: We include helpers so other plugins can 'plug in' to this one without requiring users to use/configure the site plugin.
override val settings = Seq(
SiteKeys.siteMappings <<= SiteKeys.siteMappings ?? Seq.empty
)

def selectSubpaths(dir: File, filter: FileFilter): Seq[(File, String)] = Path.selectSubpaths(dir, filter).toSeq

def copySite(dir: File, cacheDir: File, maps: Seq[(File, String)]): File = {
val concrete = maps map { case (file, dest) => (file, dir / dest) }
Sync(cacheDir / "make-site")(concrete)
dir
}

def siteArtifact(name: String) = Artifact(name, Artifact.DocType, "zip", "site")

def createSiteZip(siteDir: File, zipPath: File, s: TaskStreams): File = {
IO.zip(Path.allSubpaths(siteDir), zipPath)
s.log.info("Site packaged: " + zipPath)
zipPath
}
}
40 changes: 24 additions & 16 deletions src/main/scala/com/typesafe/sbt/site/AsciidoctorSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@ package com.typesafe.sbt.site

import java.util

import com.typesafe.sbt.SbtSite
import org.asciidoctor.Asciidoctor.Factory
import org.asciidoctor.{AsciiDocDirectoryWalker, Options, SafeMode}
import sbt.Keys._
import sbt._
object AsciidoctorSupport extends AutoPlugin {
override def requires = SbtSite
override def trigger = noTrigger

object AsciidoctorSupport {

val Asciidoctor = config("asciidoctor")

val settings: Seq[Setting[_]] =
Seq(
sourceDirectory in Asciidoctor <<= sourceDirectory(_ / "asciidoctor"),
target in Asciidoctor <<= target(_ / "asciidoctor"),
includeFilter in Asciidoctor := AllPassFilter) ++ inConfig(Asciidoctor)(Seq(
mappings <<= (sourceDirectory, target, includeFilter, version) map AsciidoctorRunner.run))
}

object AsciidoctorRunner {

def run(input: File, output: File, includeFilter: FileFilter, version: String): Seq[(File, String)] = {
object autoImport {
val Asciidoctor = config("asciidoctor")
}
import autoImport._
override def projectSettings: Seq[Setting[_]] = Seq(
sourceDirectory in Asciidoctor <<= sourceDirectory(_ / "asciidoctor"),
target in Asciidoctor <<= target(_ / "asciidoctor"),
includeFilter in Asciidoctor := AllPassFilter) ++
inConfig(Asciidoctor)(
Seq(
mappings <<= (sourceDirectory, target, includeFilter, version) map run,
SiteHelpers.addMappingsToSiteDir(mappings, "TODO")
)
)

private def run(
input: File,
output: File,
includeFilter: FileFilter,
version: String): Seq[(File, String)] = {
val oldContextClassLoader = Thread.currentThread().getContextClassLoader
Thread.currentThread().setContextClassLoader(this.getClass.getClassLoader)
val asciidoctor = Factory.create()
Expand All @@ -44,5 +53,4 @@ object AsciidoctorRunner {
Thread.currentThread().setContextClassLoader(oldContextClassLoader)
output ** includeFilter --- output pair relativeTo(output)
}

}
40 changes: 0 additions & 40 deletions src/main/scala/com/typesafe/sbt/site/Generator.scala

This file was deleted.

59 changes: 32 additions & 27 deletions src/main/scala/com/typesafe/sbt/site/JekyllSupport.scala
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
package com.typesafe.sbt
package site

import sbt.Keys._
import sbt._
import Keys._
import SbtSite.SiteKeys.siteMappings
object JekyllSupport extends AutoPlugin {
override def requires = SbtSite
override def trigger = noTrigger

object JekyllSupport {
val Jekyll = config("jekyll")
object autoImport {
val Jekyll = config("jekyll")
val requiredGems = SettingKey[Map[String, String]](
"jekyll-required-gems", "Required gem + versions for this build.")
val checkGems = TaskKey[Unit](
"jekyll-check-gems", "Tests whether or not all required gems are available.")
}
import autoImport._
override def projectSettings: Seq[Setting[_]] =
SiteHelpers.directorySettings(Jekyll) ++
Seq(
includeFilter in Jekyll := ("*.html" | "*.png" | "*.js" | "*.css" | "*.gif" | "CNAME" | ".nojekyll"),
requiredGems := Map.empty
) ++ inConfig(Jekyll)(
Seq(
checkGems := SiteHelpers.checkGems(requiredGems.value, streams.value),
mappings := {
val cg = checkGems.value
generate(sourceDirectory.value, target.value, includeFilter.value, streams.value)
},
SiteHelpers.addMappingsToSiteDir(mappings, "TODO")
)) ++ SiteHelpers.watchSettings(Jekyll)

val requiredGems = SettingKey[Map[String,String]]("jekyll-required-gems", "Required gem + versions for this build.")
val checkGems = TaskKey[Unit]("jekyll-check-gems", "Tests whether or not all required gems are available.")
def settings(config: Configuration = Jekyll): Seq[Setting[_]] =
Generator.directorySettings(config) ++
Seq(
includeFilter in config := ("*.html" | "*.png" | "*.js" | "*.css" | "*.gif" | "CNAME" | ".nojekyll"),
requiredGems := Map.empty
//(mappings in SiteKeys.siteMappings) <++= (mappings in Jekyll),
) ++ inConfig(config)(Seq(
checkGems := Generator.checkGems(requiredGems.value, streams.value),
mappings := {
val cg = checkGems.value
JekyllImpl.generate(sourceDirectory.value, target.value, includeFilter.value, streams.value)
}
)) ++ Seq(
siteMappings ++= (mappings in config).value
) ++
Generator.watchSettings(config) // TODO - this may need to be optional.
}

/** Helper class with implementations of tasks. */
object JekyllImpl {
// TODO - Add command line args and the like.
final def generate(src: File, target: File, inc: FileFilter, s: TaskStreams): Seq[(File, String)] = {
final def generate(
src: File,
target: File,
inc: FileFilter,
s: TaskStreams): Seq[(File, String)] = {
// Run Jekyll
sbt.Process(Seq("jekyll", "build", "-d", target.getAbsolutePath), Some(src)) ! s.log match {
case 0 => ()
case n => sys.error("Could not run jekyll, error: " + n)
}
// Figure out what was generated.
for {
(file, name) <- (target ** inc --- target pair relativeTo(target))
(file, name) <- target ** inc --- target pair relativeTo(target)
} yield file -> name
}
}
Loading

0 comments on commit 06c26bc

Please sign in to comment.