From 2d41468ad429c230501f1858f21b4fe88a24f2c1 Mon Sep 17 00:00:00 2001 From: Arron Mabrey Date: Fri, 14 Oct 2011 14:36:46 -0400 Subject: [PATCH 1/4] Adding: CLI -C --clean build option. --- lib/middleman/builder.rb | 39 ++++++++++++++++++++++++++++++++++++++- lib/middleman/cli.rb | 1 + 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index e8865679..914c557d 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -1,6 +1,7 @@ require "thor" require "thor/group" require 'rack/test' +require 'find' SHARED_SERVER = Middleman.server SHARED_SERVER.set :environment, :build @@ -182,11 +183,13 @@ module Middleman next end - handle_path(file_source) + dequeue_file handle_path(file_source) end end def execute! + queue_current_paths @destination + handle_directory(source) do |path| file_name = path.gsub(SHARED_SERVER.views + "/", "") if file_name == "layouts" @@ -197,6 +200,40 @@ module Middleman true end end + + clean_up_queue if base.options.has_key?("clean") && base.options["clean"] + + end + + def clean_up_queue + files = @cleaning_queue.select { |q| File.file? q } + directories = @cleaning_queue.select { |q| File.directory? q } + + files.each { |f| base.remove_file f, config } + + directories.sort_by! {|d| d.length }.reverse! + + directories.each do |d| + base.remove_file(d, config) if directory_empty? d + end + end + + def directory_empty?(directory) + Dir["#{directory}/*"].empty? + end + + def queue_current_paths(destination) + @cleaning_queue = [] + Find.find(destination) do |path| + unless path == destination + @cleaning_queue << @relative_destination + path.sub(destination,'') + end + end + end + + def dequeue_file(path) + @cleaning_queue.delete_if {|q| q == path } + return path end end end \ No newline at end of file diff --git a/lib/middleman/cli.rb b/lib/middleman/cli.rb index 73c63464..8448b714 100644 --- a/lib/middleman/cli.rb +++ b/lib/middleman/cli.rb @@ -50,6 +50,7 @@ module Middleman desc "build", "Builds the static site for deployment" method_option :relative, :type => :boolean, :aliases => "-r", :default => false, :desc => 'Override the config.rb file and force relative urls' + method_option :clean, :type => :boolean, :aliases => "-c", :default => false, :desc => 'Builds a clean project removing any orpahand files or directories' method_option :glob, :type => :string, :aliases => "-g", :default => nil, :desc => 'Build a subset of the project' def build v1_check From e4bbed33c55e5dacce0c461964ddc6f2ff8346cb Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 15 Oct 2011 11:21:56 -0700 Subject: [PATCH 2/4] add some build cleaning tests --- CHANGELOG | 4 ++++ features/clean_build.feature | 10 ++++++++++ features/step_definitions/builder_steps.rb | 8 ++++++++ fixtures/clean-app/config-complications.rb | 11 +++++++++++ fixtures/clean-app/config-empty.rb | 0 fixtures/clean-app/config.rb | 11 +++++++++++ fixtures/clean-app/source/index.html.haml | 6 ++++++ fixtures/clean-app/source/layout.haml | 6 ++++++ fixtures/clean-app/source/layouts/custom.haml | 5 +++++ fixtures/clean-app/source/real.html | 1 + fixtures/clean-app/source/real/index.html.erb | 5 +++++ fixtures/clean-app/source/should_be_ignored.html | 1 + fixtures/clean-app/source/should_be_ignored2.html | 1 + fixtures/clean-app/source/should_be_ignored3.html | 1 + fixtures/clean-app/source/static.html | 1 + lib/middleman/builder.rb | 2 +- lib/middleman/version.rb | 2 +- 17 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 features/clean_build.feature create mode 100644 fixtures/clean-app/config-complications.rb create mode 100644 fixtures/clean-app/config-empty.rb create mode 100644 fixtures/clean-app/config.rb create mode 100755 fixtures/clean-app/source/index.html.haml create mode 100644 fixtures/clean-app/source/layout.haml create mode 100755 fixtures/clean-app/source/layouts/custom.haml create mode 100644 fixtures/clean-app/source/real.html create mode 100644 fixtures/clean-app/source/real/index.html.erb create mode 100644 fixtures/clean-app/source/should_be_ignored.html create mode 100644 fixtures/clean-app/source/should_be_ignored2.html create mode 100644 fixtures/clean-app/source/should_be_ignored3.html create mode 100755 fixtures/clean-app/source/static.html diff --git a/CHANGELOG b/CHANGELOG index 0fa72041..98a83d98 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +2.0.13 +==== +middleman build --clean keeps the build directory clean of leftover files + 2.0.12 ==== Sinatra 1.3.1 and Padrino 0.10.4 diff --git a/features/clean_build.feature b/features/clean_build.feature new file mode 100644 index 00000000..9298c4bd --- /dev/null +++ b/features/clean_build.feature @@ -0,0 +1,10 @@ +Feature: Build Clean + Scenario: Build and Clean an app + Given app "clean-app" is using config "empty" + And a built app at "clean-app" + And app "clean-app" is using config "complications" + And a built app at "clean-app" with flags "--clean" + Then "should_be_ignored.html" should not exist at "clean-app" + And "should_be_ignored2.html" should not exist at "clean-app" + And "should_be_ignored3.html" should not exist at "clean-app" + And cleanup built app at "clean-app" \ No newline at end of file diff --git a/features/step_definitions/builder_steps.rb b/features/step_definitions/builder_steps.rb index 2050627b..69bc3d8e 100644 --- a/features/step_definitions/builder_steps.rb +++ b/features/step_definitions/builder_steps.rb @@ -1,5 +1,13 @@ require 'fileutils' +Given /^app "([^"]*)" is using config "([^"]*)"$/ do |path, config_name| + root = File.dirname(File.dirname(File.dirname(__FILE__))) + target = File.join(root, "fixtures", path) + config_path = File.join(target, "config-#{config_name}.rb") + config_dest = File.join(target, "config.rb") + FileUtils.cp(config_path, config_dest) +end + Given /^a built app at "([^"]*)"$/ do |path| root = File.dirname(File.dirname(File.dirname(__FILE__))) target = File.join(root, "fixtures", path) diff --git a/fixtures/clean-app/config-complications.rb b/fixtures/clean-app/config-complications.rb new file mode 100644 index 00000000..828aa09f --- /dev/null +++ b/fixtures/clean-app/config-complications.rb @@ -0,0 +1,11 @@ +page "/fake.html", :proxy => "/real.html", :layout => false + +ignore "/should_be_ignored.html" +page "/should_be_ignored2.html", :ignore => true +page "/target_ignore.html", :proxy => "/should_be_ignored3.html", :ignore => true + +%w(one two).each do |num| + page "/fake/#{num}.html", :proxy => "/real/index.html" do + @num = num + end +end \ No newline at end of file diff --git a/fixtures/clean-app/config-empty.rb b/fixtures/clean-app/config-empty.rb new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/clean-app/config.rb b/fixtures/clean-app/config.rb new file mode 100644 index 00000000..828aa09f --- /dev/null +++ b/fixtures/clean-app/config.rb @@ -0,0 +1,11 @@ +page "/fake.html", :proxy => "/real.html", :layout => false + +ignore "/should_be_ignored.html" +page "/should_be_ignored2.html", :ignore => true +page "/target_ignore.html", :proxy => "/should_be_ignored3.html", :ignore => true + +%w(one two).each do |num| + page "/fake/#{num}.html", :proxy => "/real/index.html" do + @num = num + end +end \ No newline at end of file diff --git a/fixtures/clean-app/source/index.html.haml b/fixtures/clean-app/source/index.html.haml new file mode 100755 index 00000000..6ef080c6 --- /dev/null +++ b/fixtures/clean-app/source/index.html.haml @@ -0,0 +1,6 @@ +%h1 Welcome + +:markdown + ## H2 + + Paragraph \ No newline at end of file diff --git a/fixtures/clean-app/source/layout.haml b/fixtures/clean-app/source/layout.haml new file mode 100644 index 00000000..e5a7a094 --- /dev/null +++ b/fixtures/clean-app/source/layout.haml @@ -0,0 +1,6 @@ +%html + %head + %title My Sample Site + / Comment in layout + %body + = yield \ No newline at end of file diff --git a/fixtures/clean-app/source/layouts/custom.haml b/fixtures/clean-app/source/layouts/custom.haml new file mode 100755 index 00000000..87de0ed3 --- /dev/null +++ b/fixtures/clean-app/source/layouts/custom.haml @@ -0,0 +1,5 @@ +%html + %head + %title Custom Layout + %body + = yield \ No newline at end of file diff --git a/fixtures/clean-app/source/real.html b/fixtures/clean-app/source/real.html new file mode 100644 index 00000000..cb312952 --- /dev/null +++ b/fixtures/clean-app/source/real.html @@ -0,0 +1 @@ +I am real \ No newline at end of file diff --git a/fixtures/clean-app/source/real/index.html.erb b/fixtures/clean-app/source/real/index.html.erb new file mode 100644 index 00000000..190d84ec --- /dev/null +++ b/fixtures/clean-app/source/real/index.html.erb @@ -0,0 +1,5 @@ +--- +layout: false +--- + +I am real: <%= @num %> \ No newline at end of file diff --git a/fixtures/clean-app/source/should_be_ignored.html b/fixtures/clean-app/source/should_be_ignored.html new file mode 100644 index 00000000..fb81d5c0 --- /dev/null +++ b/fixtures/clean-app/source/should_be_ignored.html @@ -0,0 +1 @@ +

Ignore me!

\ No newline at end of file diff --git a/fixtures/clean-app/source/should_be_ignored2.html b/fixtures/clean-app/source/should_be_ignored2.html new file mode 100644 index 00000000..0940fd7c --- /dev/null +++ b/fixtures/clean-app/source/should_be_ignored2.html @@ -0,0 +1 @@ +

Ignore me! 2

\ No newline at end of file diff --git a/fixtures/clean-app/source/should_be_ignored3.html b/fixtures/clean-app/source/should_be_ignored3.html new file mode 100644 index 00000000..98007c81 --- /dev/null +++ b/fixtures/clean-app/source/should_be_ignored3.html @@ -0,0 +1 @@ +

Ignore me! 3

\ No newline at end of file diff --git a/fixtures/clean-app/source/static.html b/fixtures/clean-app/source/static.html new file mode 100755 index 00000000..7e50df4e --- /dev/null +++ b/fixtures/clean-app/source/static.html @@ -0,0 +1 @@ +Static, no code! \ No newline at end of file diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index ea79d53e..d6f6e4d8 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -211,7 +211,7 @@ module Middleman files.each { |f| base.remove_file f, config } - directories.sort_by! {|d| d.length }.reverse! + directories = directories.sort_by {|d| d.length }.reverse! directories.each do |d| base.remove_file(d, config) if directory_empty? d diff --git a/lib/middleman/version.rb b/lib/middleman/version.rb index 81ca3880..8522387a 100644 --- a/lib/middleman/version.rb +++ b/lib/middleman/version.rb @@ -1,3 +1,3 @@ module Middleman - VERSION = "2.0.12" + VERSION = "2.0.13" end From 11022cc175a7c06f241fb45971229ee4c792dfee Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 15 Oct 2011 14:15:33 -0700 Subject: [PATCH 3/4] moar padrino --- middleman-x86-mingw32.gemspec | 4 ++-- middleman.gemspec | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/middleman-x86-mingw32.gemspec b/middleman-x86-mingw32.gemspec index 7fd7881f..ebbef6a3 100644 --- a/middleman-x86-mingw32.gemspec +++ b/middleman-x86-mingw32.gemspec @@ -51,8 +51,8 @@ eos s.add_dependency("compass", ["~> 0.11.3"]) s.add_dependency("coffee-script", ["~> 2.2.0"]) s.add_dependency("sprockets", ["~> 2.0.0"]) - s.add_dependency("padrino-core", ["~> 0.10.4"]) - s.add_dependency("padrino-helpers", ["~> 0.10.4"]) + s.add_dependency("padrino-core", ["~> 0.10.5"]) + s.add_dependency("padrino-helpers", ["~> 0.10.5"]) s.add_dependency("eventmachine", ["1.0.0.beta.3"]) s.add_dependency("win32-process", ["~> 0.6.5"]) diff --git a/middleman.gemspec b/middleman.gemspec index 7c942122..7134be98 100644 --- a/middleman.gemspec +++ b/middleman.gemspec @@ -53,8 +53,8 @@ eos s.add_dependency("coffee-script", ["~> 2.2.0"]) s.add_dependency("execjs", ["~> 1.2.7"]) s.add_dependency("sprockets", ["~> 2.0.0"]) - s.add_dependency("padrino-core", ["~> 0.10.4"]) - s.add_dependency("padrino-helpers", ["~> 0.10.4"]) + s.add_dependency("padrino-core", ["~> 0.10.5"]) + s.add_dependency("padrino-helpers", ["~> 0.10.5"]) s.add_dependency("guard", ["~> 0.6.2"]) s.add_dependency("middleman-livereload", ["~> 0.2.0"]) From 6b47ca9ba8e8a034fb2f7dd13a77c175063348ca Mon Sep 17 00:00:00 2001 From: Arron Mabrey Date: Sat, 15 Oct 2011 21:24:19 -0400 Subject: [PATCH 4/4] Fixing: cli-clean-option. --- lib/middleman/builder.rb | 87 +++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index d6f6e4d8..757ca417 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -23,12 +23,61 @@ module Middleman request_path.gsub!(/\s/, "%20") response = Middleman::Builder.shared_rack.get(request_path) + dequeue_file_from destination if cleaning? + create_file destination, nil, config do response.body end if response.status == 200 rescue end end + + + def clean!(destination) + return unless cleaning? + queue_current_paths_from destination + add_clean_up_callback + end + + def cleaning? + options.has_key?("clean") && options["clean"] + end + + def add_clean_up_callback + clean_up_callback = lambda do + files = @cleaning_queue.select { |q| File.file? q } + directories = @cleaning_queue.select { |q| File.directory? q } + + files.each { |f| remove_file f, :force => true } + + directories = directories.sort_by {|d| d.length }.reverse! + + directories.each do |d| + remove_file d, :force => true if directory_empty? d + end + end + self.class.after_run :clean_up_callback do + clean_up_callback.call + end + end + + def directory_empty?(directory) + Dir["#{directory}/*"].empty? + end + + def queue_current_paths_from(destination) + @cleaning_queue = [] + Find.find(destination) do |path| + unless path == destination + @cleaning_queue << path.sub(destination, destination[/([^\/]+?)$/]) + end + end + end + + def dequeue_file_from(destination) + @cleaning_queue.delete_if {|q| q == destination } + end + end class Builder < Thor::Group @@ -103,6 +152,7 @@ module Middleman end def invoke! + base.clean! destination execute! end @@ -183,13 +233,11 @@ module Middleman next end - dequeue_file handle_path(file_source) + handle_path(file_source) end end def execute! - queue_current_paths @destination - handle_directory(source) do |path| file_name = path.gsub(SHARED_SERVER.views + "/", "") if file_name == "layouts" @@ -200,40 +248,7 @@ module Middleman true end end - - clean_up_queue if base.options.has_key?("clean") && base.options["clean"] - - end - - def clean_up_queue - files = @cleaning_queue.select { |q| File.file? q } - directories = @cleaning_queue.select { |q| File.directory? q } - - files.each { |f| base.remove_file f, config } - - directories = directories.sort_by {|d| d.length }.reverse! - - directories.each do |d| - base.remove_file(d, config) if directory_empty? d - end end - def directory_empty?(directory) - Dir["#{directory}/*"].empty? - end - - def queue_current_paths(destination) - @cleaning_queue = [] - Find.find(destination) do |path| - unless path == destination - @cleaning_queue << @relative_destination + path.sub(destination,'') - end - end - end - - def dequeue_file(path) - @cleaning_queue.delete_if {|q| q == path } - return path - end end end \ No newline at end of file