move_file as a replacement for import_asset

This commit is contained in:
Thomas Reynolds 2015-10-22 20:23:39 -07:00
parent 62d7fa0676
commit 3cd84eb4d1
3 changed files with 118 additions and 0 deletions

View file

@ -0,0 +1,44 @@
Feature: Move files
Scenario: Move one path to another
Given a fixture app "large-build-app"
And a file named "config.rb" with:
"""
move_file "/static.html", "/static2.html"
"""
And the Server is running at "large-build-app"
When I go to "/static.html"
Then I should see 'Not Found'
When I go to "/static2.html"
Then I should see 'Static, no code!'
Scenario: Move one path to another with directory indexes
Given a fixture app "large-build-app"
And a file named "config.rb" with:
"""
activate :directory_indexes
move_file "/static.html", "/static2.html"
"""
And the Server is running at "large-build-app"
When I go to "/static.html"
Then I should see 'Not Found'
When I go to "/static/index.html"
Then I should see 'Not Found'
When I go to "/static2.html"
Then I should see 'Static, no code!'
Scenario: Move one path to another with directory indexes (using dest path)
Given a fixture app "large-build-app"
And a file named "config.rb" with:
"""
activate :directory_indexes
move_file "/static/index.html", "/static2.html"
"""
And the Server is running at "large-build-app"
When I go to "/static.html"
Then I should see 'Not Found'
When I go to "/static/index.html"
Then I should see 'Not Found'
When I go to "/static2.html"
Then I should see 'Static, no code!'

View file

@ -0,0 +1,68 @@
require 'middleman-core/sitemap/resource'
require 'middleman-core/core_extensions/collections/step_context'
module Middleman
module Sitemap
module Extensions
# Manages the list of proxy configurations and manipulates the sitemap
# to include new resources based on those configurations
class MoveFile < Extension
MoveFileDescriptor = Struct.new(:from, :to)
self.resource_list_manipulator_priority = 101
# Expose `create_move_file` to config as `move_file`
expose_to_config move_file: :create_move_file
def initialize(app, config={}, &block)
super
@move_configs = Set.new
end
def after_configuration
::Middleman::CoreExtensions::Collections::StepContext.add_to_context(:move_file, &method(:create_move_file))
end
# Setup a move from one path to another
# @param [String] from The original path.
# @param [String] to The new path.
# @return [void]
Contract String, String, Maybe[Hash] => Any
def create_move_file(from, to)
@move_configs << create_anonymous_move(from, to)
@app.sitemap.rebuild_resource_list!(:added_move_file)
end
# Setup a move from one path to another
# @param [String] from The original path.
# @param [String] to The new path.
# @return [MoveFileDescriptor]
Contract String, String => MoveFileDescriptor
def create_anonymous_move(from, to)
MoveFileDescriptor.new(
::Middleman::Util.normalize_path(from),
::Middleman::Util.normalize_path(to)
)
end
# Update the main sitemap resource list
# @return Array<Middleman::Sitemap::Resource>
Contract ResourceList => ResourceList
def manipulate_resource_list(resources)
resources.each do |r|
matches = @move_configs.select do |c|
c.from == r.path || c.from == r.destination_path
end
if c = matches.last
r.destination_path = c.to
end
end
resources
end
end
end
end
end

View file

@ -33,6 +33,12 @@ Middleman::Extensions.register :sitemap_redirects, auto_activate: :before_config
Middleman::Sitemap::Extensions::Redirects
end
# Move Files
Middleman::Extensions.register :sitemap_move_files, auto_activate: :before_configuration do
require 'middleman-core/sitemap/extensions/move_file'
Middleman::Sitemap::Extensions::MoveFile
end
require 'middleman-core/contracts'
module Middleman