From c6401f1cd9ac36b63cd0ee077239fe4828ef1ec0 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sun, 10 Jan 2016 17:14:41 -0800 Subject: [PATCH] Fixes #1716 --- CHANGELOG.md | 1 + middleman-core/features/clean_build.feature | 16 ++++++++++++++++ middleman-core/lib/middleman-core/application.rb | 2 ++ middleman-core/lib/middleman-core/builder.rb | 10 +++++++--- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0e80a88..370a5e81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ master * Fix new extension template * Don't parse frontmatter on ignored files. * Fix displaying frontmatter on `/__middleman/sitemap` +* Add `skip_build_clean` config which when set to a block, will avoid removing non-generated paths from build, like .git #1716 # 4.0.0 diff --git a/middleman-core/features/clean_build.feature b/middleman-core/features/clean_build.feature index eaa9942e..60c18578 100644 --- a/middleman-core/features/clean_build.feature +++ b/middleman-core/features/clean_build.feature @@ -16,6 +16,22 @@ Feature: Build Clean | build/should_be_ignored3.html | And the file "build/index.html" should contain "Comment in layout" + Scenario: Clean build has a whitelist + Given a fixture app "clean-app" + When a file named "build/.test" with: + """ + Hello + """ + When a file named "config.rb" with: + """ + set :skip_build_clean do |path| + path =~ /\.test/ + end + """ + Given a built app at "clean-app" + Then the following files should exist: + | build/.test | + Scenario: Clean build an app with newly ignored files and a nested output directory Given a fixture app "clean-nested-app" When a file named "config.rb" with: diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 5818548b..4efca949 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -191,6 +191,8 @@ module Middleman yaml: [%w(--- ---), %w(--- ...)] }, 'Allowed frontmatter delimiters' + define_setting :skip_build_clean, proc { |p| [/\.git/].any? { |r| r.match(p) } }, 'Whether some paths should not be removed during a clean build.' + define_setting :watcher_disable, false, 'If the Listen watcher should not run' define_setting :watcher_force_polling, false, 'If the Listen watcher should run in polling mode' define_setting :watcher_latency, nil, 'The Listen watcher latency' diff --git a/middleman-core/lib/middleman-core/builder.rb b/middleman-core/lib/middleman-core/builder.rb index 4166dcf5..02feb15b 100644 --- a/middleman-core/lib/middleman-core/builder.rb +++ b/middleman-core/lib/middleman-core/builder.rb @@ -58,7 +58,7 @@ module Middleman prerender_css output_files - clean if @cleaning + clean! if @cleaning ::Middleman::Profiling.report('build') @@ -219,8 +219,12 @@ module Middleman # Remove files which were not built in this cycle Contract ArrayOf[Pathname] - def clean - @to_clean.each do |f| + def clean! + to_remove = @to_clean.reject do |f| + app.config[:skip_build_clean].call(f.to_s) + end + + to_remove.each do |f| FileUtils.rm(f) trigger(:deleted, f) end