Last active
December 21, 2015 13:59
-
-
Save tkfm-yamaguchi/6316314 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PreserveSymlink < Middleman::Extension | |
def initialize(app, options_hash={}, &block) | |
super | |
setup_app! app, options_hash | |
ext = self | |
# callback | |
app.after_build do |builder| | |
ext.source_symlinks.each do |source_symlink_path| | |
target_symlink_path = | |
ext.convert_source_path_for_build(source_symlink_path) | |
if target_symlink_path.exist? | |
unless target_symlink_path.symlink? | |
raise "Collupsion: symbolick link in source/ but file instance in build/" | |
end | |
next | |
end | |
builder.run( | |
ext.symlink_command( | |
source_symlink_path, | |
target_symlink_path.dirname | |
) | |
) | |
end | |
end | |
end | |
# callback | |
def after_configuration | |
# Ignore symlinks on source/ and remove them on build/ | |
# only on build environment | |
if app.build? | |
source_symlinks.each do |source_symlink| | |
ignore_path = source_symlink.relative_path_from(@_source_path) | |
ignore_path = ignore_path.join("*") if source_symlink.directory? | |
app.ignore ignore_path.to_s | |
end | |
# Middleman's clean process searches cleaning(remove) target file | |
# regardless of it is in symlinked directory or not. | |
# And then, it removes a file which in a symlink directory, and | |
# deletes symlink itself becase symlink's directory is be empty. | |
# To avoid to be deleted the file which placed on a symlink | |
# referel directory, remove all symlink in a build dir manually. | |
# This may be safe because normally there must not be symlinks | |
# in Middleman's build directory. | |
if @_build_path.exist? | |
build_symlinks.each do |symlink_path| | |
symlink_path.delete | |
end | |
end | |
end | |
end | |
def setup_app!(app, options_hash) | |
@_root_path = app.root_path | |
@_source_path = @_root_path.join(app.source) | |
@_build_path = @_root_path.join(app.build_dir) | |
# if options_hash[:preserve_symlinks] | |
# @_source_symlinks_ = | |
# options_hash[:preserve_symlinks].map do |preserve_path| | |
# Pathname(preserve_path).relative_path_from(@_source_path) | |
# end | |
# | |
# @_build_symlinks_ = | |
# @_source_symlinks_.map{|path| convert_source_path_for_build(path) } | |
# end | |
end | |
def build_symlinks(relative: false) | |
@_build_symlinks_ ||= @_build_path.find.select(&:symlink?) | |
if relative | |
@_build_symlinks_.map{|t| t.relative_path_from(@_build_path) } | |
else | |
@_build_symlinks_ | |
end | |
end | |
def source_symlinks(relative: false) | |
@_source_symlinks_ ||= @_source_path.find.select(&:symlink?) | |
if relative | |
@_source_symlinks_.map{|t| t.relative_path_from(@_source_path) } | |
else | |
@_source_symlinks_ | |
end | |
end | |
def convert_source_path_for_build(path) | |
@_build_path.join(path.relative_path_from(@_source_path)) | |
end | |
def symlink_command(source_symlink_path, target_directory) | |
"ln -s #{source_symlink_path.readlink} #{target_directory}" | |
end | |
end | |
::Middleman::Extensions.register(:preserve_symlink, PreserveSymlink) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment