From 048b1489b384bddfc2e58f77807a8c978208a1d9 Mon Sep 17 00:00:00 2001 From: Ali Salim Rashid Date: Wed, 30 Mar 2016 12:05:21 +0300 Subject: [PATCH] Provide `includeFilter in cleanSite` and `excludeFilter in cleanSite` settings to allow user specified FileFilters defining files which may be deleted. Update documentation with guide to protecting files. --- README.md | 21 +++++++++++++++++++ .../scala/com/typesafe/sbt/SbtGhPages.scala | 19 ++++++++++------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a737253..5a1627e 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,27 @@ Here's an example `src/site/index.html` you can use as a starting point: ``` +## 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 diff --git a/src/main/scala/com/typesafe/sbt/SbtGhPages.scala b/src/main/scala/com/typesafe/sbt/SbtGhPages.scala index c9fc646..15a17db 100644 --- a/src/main/scala/com/typesafe/sbt/SbtGhPages.scala +++ b/src/main/scala/com/typesafe/sbt/SbtGhPages.scala @@ -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) ()