Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.

Commit 06c26bc

Browse files
author
Simeon H.K. Fitch
committed
Incremental commit toward autoplugin conversion.
1 parent 41021ca commit 06c26bc

12 files changed

+420
-313
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
.idea
2-
target/
2+
target
+43-72
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,59 @@
11
package com.typesafe.sbt
22

3-
import com.typesafe.sbt.site.AsciidoctorSupport
43
import sbt._
54
import Keys._
65
import com.typesafe.sbt.site._
76

8-
9-
object SbtSite extends Plugin {
10-
object SiteKeys {
7+
object SbtSite extends AutoPlugin {
8+
override def trigger = allRequirements
9+
object autoImport {
1110
val makeSite = TaskKey[File]("make-site", "Generates a static website for a project.")
1211
val packageSite = TaskKey[File]("package-site", "Create a zip file of the website.")
13-
14-
// Helper to point at mappings for the site.
15-
val siteMappings = mappings in makeSite
16-
val siteDirectory = target in makeSite
17-
val siteSources = sources in makeSite
18-
val siteSourceDirectory = sourceDirectory in makeSite
19-
20-
val previewSite = TaskKey[Unit]("preview-site", "Launches a jetty server that serves your generated site from the target directory")
21-
val previewFixedPort = SettingKey[Option[Int]]("previewFixedPort") in previewSite
22-
val previewLaunchBrowser = SettingKey[Boolean]("previewLaunchBrowser") in previewSite
2312
}
13+
import autoImport._
14+
15+
// Helper to point at mappings for the site.
16+
private[sbt] val siteMappings = mappings in makeSite
17+
private[sbt] val siteDirectory = target in makeSite
18+
private[sbt] val siteSources = sources in makeSite
19+
private[sbt] val siteSourceDirectory = sourceDirectory in makeSite
20+
21+
override lazy val projectSettings = Seq(
22+
siteMappings := Seq.empty,
23+
siteMappings <<= siteMappings ?? Seq.empty,
24+
siteDirectory := target.value / "site",
25+
siteSourceDirectory := sourceDirectory.value / "site",
26+
includeFilter in makeSite := "*.html" | "*.css" | "*.png" | "*.jpg" | "*.gif" | "*.js" | "*.swf",
27+
siteMappings ++= SiteHelpers.selectSubpaths(siteSourceDirectory.value, (includeFilter in makeSite).value),
28+
makeSite := SiteHelpers.copySite(siteDirectory.value, streams.value.cacheDirectory, siteMappings.value),
29+
artifact in packageSite := SiteHelpers.siteArtifact(moduleName.value),
30+
artifactPath in packageSite <<= Defaults.artifactPathSetting(artifact in packageSite),
31+
packageSite := SiteHelpers.createSiteZip(makeSite.value, (artifactPath in packageSite).value, streams.value)
32+
)
2433

2534
object site {
26-
import SiteKeys._
2735

28-
val settings = Seq(
29-
siteMappings := Seq.empty,
30-
siteDirectory := target.value / "site",
31-
siteSourceDirectory := sourceDirectory.value / "site",
32-
includeFilter in makeSite := "*.html" | "*.css" | "*.png" | "*.jpg" | "*.gif" | "*.js" | "*.swf",
33-
siteMappings ++= selectSubpaths(siteSourceDirectory.value, (includeFilter in makeSite).value),
34-
makeSite := copySite(siteDirectory.value, streams.value.cacheDirectory, siteMappings.value),
35-
artifact in packageSite := siteArtifact(moduleName.value),
36-
artifactPath in packageSite <<= Defaults.artifactPathSetting(artifact in packageSite),
37-
packageSite := createSiteZip(makeSite.value, (artifactPath in packageSite).value, streams.value)
38-
) ++ Preview.settings
39-
40-
/** Convenience functions to add a task of mappings to a site under a nested directory. */
41-
def addMappingsToSiteDir(mappings: TaskKey[Seq[(File,String)]], nestedDirectory: String): Setting[_] =
42-
siteMappings <++= mappings map { m =>
43-
for((f, d) <- m) yield (f, nestedDirectory + "/" + d)
44-
}
45-
46-
/** Includes scaladoc APIS in site under a "latest/api" directory. */
47-
def includeScaladoc(alias: String = "latest/api"): Seq[Setting[_]] =
48-
Seq(addMappingsToSiteDir(mappings in packageDoc in Compile, alias))
49-
/** Includes Jekyll generated site under the root directory. */
50-
def jekyllSupport(alias: String = ""): Seq[Setting[_]] =
51-
JekyllSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in JekyllSupport.Jekyll, alias))
52-
/** Includes Sphinx generated site under the root directory. */
53-
def sphinxSupport(alias: String = ""): Seq[Setting[_]] =
54-
SphinxSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in SphinxSupport.Sphinx, alias))
55-
/** Includes Pamflet generate site under the root directory. */
56-
def pamfletSupport(alias: String = ""): Seq[Setting[_]] =
57-
PamfletSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in PamfletSupport.Pamflet, alias))
58-
/** Includes Nanoc generated site under the root directory. */
59-
def nanocSupport(alias: String = ""): Seq[Setting[_]] =
60-
NanocSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in NanocSupport.Nanoc, alias))
61-
def asciidoctorSupport(alias: String = ""): Seq[Setting[_]] =
62-
AsciidoctorSupport.settings ++ Seq(addMappingsToSiteDir(mappings in AsciidoctorSupport.Asciidoctor, alias))
63-
def preprocessSite(alias: String = ""): Seq[Setting[_]] =
64-
PreprocessSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in PreprocessSupport.Preprocess, alias))
36+
// def preprocessSite(alias: String = ""): Seq[Setting[_]] =
37+
// PreprocessSupport.settings() ++ Seq(addMappingsToSiteDir(mappings in PreprocessSupport.Preprocess, alias))
38+
//
39+
// /** Includes scaladoc APIS in site under a "latest/api" directory. */
40+
// def includeScaladoc(alias: String = "latest/api"): Seq[Setting[_]] =
41+
// Seq(SiteHelpers.addMappingsToSiteDir(mappings in packageDoc in Compile, alias))
42+
// /** Includes Jekyll generated site under the root directory. */
43+
// def jekyllSupport(alias: String = ""): Seq[Setting[_]] =
44+
// JekyllSupport.settings() ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in JekyllSupport.autoImports.Jekyll, alias))
45+
// /** Includes Sphinx generated site under the root directory. */
46+
// def sphinxSupport(alias: String = ""): Seq[Setting[_]] =
47+
// SphinxSupport.settings() ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in SphinxSupport.Sphinx, alias))
48+
// /** Includes Pamflet generate site under the root directory. */
49+
// def pamfletSupport(alias: String = ""): Seq[Setting[_]] =
50+
// PamfletSupport.settings() ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in PamfletSupport.Pamflet, alias))
51+
// /** Includes Nanoc generated site under the root directory. */
52+
// def nanocSupport(alias: String = ""): Seq[Setting[_]] =
53+
// NanocSupport.settings() ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in NanocSupport.Nanoc, alias))
54+
// def asciidoctorSupport(alias: String = ""): Seq[Setting[_]] =
55+
// AsciidoctorSupport.settings ++ Seq(SiteHelpers.addMappingsToSiteDir(mappings in AsciidoctorSupport.Asciidoctor, alias))
6556
def publishSite(): SettingsDefinition = addArtifact(artifact in packageSite, packageSite)
6657
}
6758

68-
// Note: We include helpers so other plugins can 'plug in' to this one without requiring users to use/configure the site plugin.
69-
override val settings = Seq(
70-
SiteKeys.siteMappings <<= SiteKeys.siteMappings ?? Seq.empty
71-
)
72-
73-
def selectSubpaths(dir: File, filter: FileFilter): Seq[(File, String)] = Path.selectSubpaths(dir, filter).toSeq
74-
75-
def copySite(dir: File, cacheDir: File, maps: Seq[(File, String)]): File = {
76-
val concrete = maps map { case (file, dest) => (file, dir / dest) }
77-
Sync(cacheDir / "make-site")(concrete)
78-
dir
79-
}
80-
81-
def siteArtifact(name: String) = Artifact(name, Artifact.DocType, "zip", "site")
82-
83-
def createSiteZip(siteDir: File, zipPath: File, s: TaskStreams): File = {
84-
IO.zip(Path.allSubpaths(siteDir), zipPath)
85-
s.log.info("Site packaged: " + zipPath)
86-
zipPath
87-
}
8859
}

src/main/scala/com/typesafe/sbt/site/AsciidoctorSupport.scala

+24-16
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,35 @@ package com.typesafe.sbt.site
22

33
import java.util
44

5+
import com.typesafe.sbt.SbtSite
56
import org.asciidoctor.Asciidoctor.Factory
67
import org.asciidoctor.{AsciiDocDirectoryWalker, Options, SafeMode}
78
import sbt.Keys._
89
import sbt._
10+
object AsciidoctorSupport extends AutoPlugin {
11+
override def requires = SbtSite
12+
override def trigger = noTrigger
913

10-
object AsciidoctorSupport {
11-
12-
val Asciidoctor = config("asciidoctor")
13-
14-
val settings: Seq[Setting[_]] =
15-
Seq(
16-
sourceDirectory in Asciidoctor <<= sourceDirectory(_ / "asciidoctor"),
17-
target in Asciidoctor <<= target(_ / "asciidoctor"),
18-
includeFilter in Asciidoctor := AllPassFilter) ++ inConfig(Asciidoctor)(Seq(
19-
mappings <<= (sourceDirectory, target, includeFilter, version) map AsciidoctorRunner.run))
20-
}
21-
22-
object AsciidoctorRunner {
23-
24-
def run(input: File, output: File, includeFilter: FileFilter, version: String): Seq[(File, String)] = {
14+
object autoImport {
15+
val Asciidoctor = config("asciidoctor")
16+
}
17+
import autoImport._
18+
override def projectSettings: Seq[Setting[_]] = Seq(
19+
sourceDirectory in Asciidoctor <<= sourceDirectory(_ / "asciidoctor"),
20+
target in Asciidoctor <<= target(_ / "asciidoctor"),
21+
includeFilter in Asciidoctor := AllPassFilter) ++
22+
inConfig(Asciidoctor)(
23+
Seq(
24+
mappings <<= (sourceDirectory, target, includeFilter, version) map run,
25+
SiteHelpers.addMappingsToSiteDir(mappings, "TODO")
26+
)
27+
)
28+
29+
private def run(
30+
input: File,
31+
output: File,
32+
includeFilter: FileFilter,
33+
version: String): Seq[(File, String)] = {
2534
val oldContextClassLoader = Thread.currentThread().getContextClassLoader
2635
Thread.currentThread().setContextClassLoader(this.getClass.getClassLoader)
2736
val asciidoctor = Factory.create()
@@ -44,5 +53,4 @@ object AsciidoctorRunner {
4453
Thread.currentThread().setContextClassLoader(oldContextClassLoader)
4554
output ** includeFilter --- output pair relativeTo(output)
4655
}
47-
4856
}

src/main/scala/com/typesafe/sbt/site/Generator.scala

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
11
package com.typesafe.sbt
22
package site
33

4+
import sbt.Keys._
45
import sbt._
5-
import Keys._
6-
import SbtSite.SiteKeys.siteMappings
6+
object JekyllSupport extends AutoPlugin {
7+
override def requires = SbtSite
8+
override def trigger = noTrigger
79

8-
object JekyllSupport {
9-
val Jekyll = config("jekyll")
10+
object autoImport {
11+
val Jekyll = config("jekyll")
12+
val requiredGems = SettingKey[Map[String, String]](
13+
"jekyll-required-gems", "Required gem + versions for this build.")
14+
val checkGems = TaskKey[Unit](
15+
"jekyll-check-gems", "Tests whether or not all required gems are available.")
16+
}
17+
import autoImport._
18+
override def projectSettings: Seq[Setting[_]] =
19+
SiteHelpers.directorySettings(Jekyll) ++
20+
Seq(
21+
includeFilter in Jekyll := ("*.html" | "*.png" | "*.js" | "*.css" | "*.gif" | "CNAME" | ".nojekyll"),
22+
requiredGems := Map.empty
23+
) ++ inConfig(Jekyll)(
24+
Seq(
25+
checkGems := SiteHelpers.checkGems(requiredGems.value, streams.value),
26+
mappings := {
27+
val cg = checkGems.value
28+
generate(sourceDirectory.value, target.value, includeFilter.value, streams.value)
29+
},
30+
SiteHelpers.addMappingsToSiteDir(mappings, "TODO")
31+
)) ++ SiteHelpers.watchSettings(Jekyll)
1032

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

31-
/** Helper class with implementations of tasks. */
32-
object JekyllImpl {
3334
// TODO - Add command line args and the like.
34-
final def generate(src: File, target: File, inc: FileFilter, s: TaskStreams): Seq[(File, String)] = {
35+
final def generate(
36+
src: File,
37+
target: File,
38+
inc: FileFilter,
39+
s: TaskStreams): Seq[(File, String)] = {
3540
// Run Jekyll
3641
sbt.Process(Seq("jekyll", "build", "-d", target.getAbsolutePath), Some(src)) ! s.log match {
3742
case 0 => ()
3843
case n => sys.error("Could not run jekyll, error: " + n)
3944
}
4045
// Figure out what was generated.
4146
for {
42-
(file, name) <- (target ** inc --- target pair relativeTo(target))
47+
(file, name) <- target ** inc --- target pair relativeTo(target)
4348
} yield file -> name
4449
}
4550
}

0 commit comments

Comments
 (0)