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

Provide includeFilter in cleanSite and excludeFilter in cleanSite #17

Merged
merged 1 commit into from
Sep 16, 2016
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ Here's an example `src/site/index.html` you can use as a starting point:
</html>
```

## Protecting Existing Files
The default behaviour of sbt-ghpages is to remove all existing files in the Github Pages repository
prior to publishing current pages. sbt-ghpages supports customisation of this behaviour via the provided
`includeFilter in cleanSite` and/or `excludeFilter in cleanSite` setting keys.

sbt-ghpages will only delete files which are matched by the FileFilter specified by the `includeFilter in cleanSite`
setting key AND are not matched by the FileFilter specified by the `excludeFilter in cleanSite` key.

For example, to prevent sbt-ghpages from deleting the "CNAME" file located at the root of your site, and any file
named "versions.html", add the following to your build.sbt:

```scala
excludeFilter in GhPagesKeys.cleanSite :=
new FileFilter{
def accept(f: File) = (GhPagesKeys.repository.value / "CNAME").getCanonicalPath == f.getCanonicalPath
} || "versions.html"
```

For more information on creating more complex filters, please refer to the [sbt FileFilter documentation](http://www.scala-sbt.org/0.13/docs/Paths.html#File+Filters).


## LICENSE ##

Copyright (c) 2008, 2009, 2010, 2011 Josh Suereth, Steven Blundy, Josh Cough, Mark Harrah, Stuart Roebuck, Tony Sloane, Vesa Vilhonen, Jason Zaugg
Expand Down
19 changes: 11 additions & 8 deletions src/main/scala/com/typesafe/sbt/SbtGhPages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,33 @@ object SbtGhPages extends Plugin {
pushSite <<= pushSite0,
privateMappings <<= siteMappings,
synchLocal <<= synchLocal0,
cleanSite <<= cleanSite0
)
cleanSite <<= cleanSite0,
includeFilter in cleanSite := AllPassFilter,
excludeFilter in cleanSite := NothingFilter)
private def updatedRepo(repo: SettingKey[File], remote: SettingKey[String], branch: SettingKey[Option[String]]) =
(repo, remote, branch, GitKeys.gitRunner, streams) map { (local, uri, branch, git, s) =>
git.updated(remote = uri, cwd = local, branch = branch, log = s.log);
local
}

private def synchLocal0 = (privateMappings, updatedRepository, ghpagesNoJekyll, GitKeys.gitRunner, streams) map { (mappings, repo, noJekyll, git, s) =>
private def synchLocal0 = (privateMappings, updatedRepository, ghpagesNoJekyll, GitKeys.gitRunner, streams, includeFilter in cleanSite, excludeFilter in cleanSite) map {
(mappings, repo, noJekyll, git, s, incl, excl) =>
// TODO - an sbt.Synch with cache of previous mappings to make this more efficient. */
val betterMappings = mappings map { case (file, target) => (file, repo / target) }
// First, remove 'stale' files.
cleanSiteForRealz(repo, git, s)
cleanSiteForRealz(repo, git, s, incl, excl)
// Now copy files.
IO.copy(betterMappings)
if(noJekyll) IO.touch(repo / ".nojekyll")
repo
}

private def cleanSite0 = (updatedRepository, GitKeys.gitRunner, streams) map { (dir, git, s) =>
cleanSiteForRealz(dir, git, s)
private def cleanSite0 = (updatedRepository, GitKeys.gitRunner, streams, includeFilter in cleanSite, excludeFilter in cleanSite) map { (dir, git, s, incl, excl) =>
cleanSiteForRealz(dir, git, s, incl, excl)
}
private def cleanSiteForRealz(dir: File, git: GitRunner, s: TaskStreams): Unit = {
val toClean = IO.listFiles(dir).filterNot(_.getName == ".git").map(_.getAbsolutePath).toList
private def cleanSiteForRealz(dir: File, git: GitRunner, s: TaskStreams, incl: FileFilter, excl: FileFilter): Unit = {
val toClean = IO.listFiles(dir)
.filter(f ⇒ f.getName != ".git" && incl.accept(f) && !excl.accept(f)).map(_.getAbsolutePath).toList
if(!toClean.isEmpty)
git(("rm" :: "-r" :: "-f" :: "--ignore-unmatch" :: toClean) :_*)(dir, s.log)
()
Expand Down