From a25e9c638203386c4207bccd895a98e47739b22a Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sun, 17 May 2015 12:25:17 -0700 Subject: [PATCH] Normalize file path string encoding on darwin. For #1506 --- .../lib/middleman-core/application.rb | 4 +++- .../lib/middleman-core/cli/build.rb | 4 +++- .../lib/middleman-core/cli/server.rb | 2 +- .../core_extensions/external_helpers.rb | 3 ++- .../core_extensions/file_watcher.rb | 7 ++++++ .../core_extensions/rendering.rb | 2 +- .../lib/middleman-core/preview_server.rb | 8 ++++--- middleman-core/lib/middleman-core/util.rb | 24 +++++++++++++++++++ .../middleman-more/core_extensions/i18n.rb | 2 +- 9 files changed, 47 insertions(+), 9 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 7c0ddcfc..f509e943 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -57,7 +57,9 @@ module Middleman # Root project directory (overwritten in middleman build/server) # @return [String] def self.root - ENV['MM_ROOT'] || Dir.pwd + r = ENV['MM_ROOT'] ? ENV['MM_ROOT'].dup : ::Middleman::Util.current_directory + r.encode!('UTF-8', 'UTF-8-MAC') if RUBY_PLATFORM =~ /darwin/ + r end delegate :root, to: :"self.class" diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 0fa8d646..e32a5252 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -138,7 +138,9 @@ module Middleman::Cli base.remove_file f, force: true end - Dir[@build_dir.join('**', '*')].select { |d| File.directory?(d) }.each do |d| + ::Middleman::Util.glob_directory(@build_dir.join('**', '*')) + .select { |d| File.directory?(d) } + .each do |d| base.remove_file d, force: true if directory_empty? d end end diff --git a/middleman-core/lib/middleman-core/cli/server.rb b/middleman-core/lib/middleman-core/cli/server.rb index bf222dd2..47b61b11 100644 --- a/middleman-core/lib/middleman-core/cli/server.rb +++ b/middleman-core/lib/middleman-core/cli/server.rb @@ -62,7 +62,7 @@ module Middleman::Cli unless ENV['MM_ROOT'] puts '== Could not find a Middleman project config.rb' puts '== Treating directory as a static site to be served' - ENV['MM_ROOT'] = Dir.pwd + ENV['MM_ROOT'] = ::Middleman::Util.current_directory ENV['MM_SOURCE'] = '' end diff --git a/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb b/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb index b63d2a10..4d08a93c 100644 --- a/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb +++ b/middleman-core/lib/middleman-core/core_extensions/external_helpers.rb @@ -19,7 +19,8 @@ module Middleman helpers_path = File.join(root, config[:helpers_dir]) next unless File.exist?(helpers_path) - Dir[File.join(helpers_path, config[:helpers_filename_glob])].each do |filename| + glob = File.join(helpers_path, config[:helpers_filename_glob]) + ::Middleman::Util.glob_directory(glob).each do |filename| module_name = config[:helpers_filename_to_module_name_proc].call(filename) next unless module_name diff --git a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb index d13d6589..834a02db 100644 --- a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb +++ b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb @@ -95,6 +95,8 @@ module Middleman # @param [Pathname] path The file that changed # @return [void] def did_change(path) + path = path.to_s.encode!('UTF-8', 'UTF-8-MAC') if RUBY_PLATFORM =~ /darwin/ + path = Pathname(path) logger.debug "== File Change: #{path}" @known_paths << path @@ -106,6 +108,8 @@ module Middleman # @param [Pathname] path The file that was deleted # @return [void] def did_delete(path) + path = path.to_s.encode!('UTF-8', 'UTF-8-MAC') if RUBY_PLATFORM =~ /darwin/ + path = Pathname(path) logger.debug "== File Deletion: #{path}" @known_paths.delete(path) @@ -146,7 +150,10 @@ module Middleman end def exists?(path) + path = path.to_s.encode!('UTF-8', 'UTF-8-MAC') if RUBY_PLATFORM =~ /darwin/ + p = Pathname(path) + p = p.relative_path_from(Pathname(@app.root)) unless p.relative? @known_paths.include?(p) end diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index 205e1063..39cb4006 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -503,7 +503,7 @@ module Middleman found_path = nil search_paths.each do |path_with_ext| - found_path = Dir[path_with_ext].find do |path| + found_path = ::Middleman::Util.glob_directory(path_with_ext).find do |path| ::Tilt[path] end diff --git a/middleman-core/lib/middleman-core/preview_server.rb b/middleman-core/lib/middleman-core/preview_server.rb index 7ab5fff6..6a1d91e1 100644 --- a/middleman-core/lib/middleman-core/preview_server.rb +++ b/middleman-core/lib/middleman-core/preview_server.rb @@ -144,7 +144,7 @@ module Middleman options = { force_polling: @options[:force_polling] } options[:latency] = @options[:latency] if @options[:latency] - @listener = Listen.to(Dir.pwd, options) do |modified, added, removed| + @listener = Listen.to(::Middleman::Util.current_directory, options) do |modified, added, removed| added_and_modified = (modified + added) # See if the changed file is config.rb or lib/*.rb @@ -152,14 +152,16 @@ module Middleman $mm_reload = true @webrick.stop else + wd = Pathname(::Middleman::Util.current_directory) + added_and_modified.each do |path| - relative_path = Pathname(path).relative_path_from(Pathname(Dir.pwd)).to_s + relative_path = Pathname(path).relative_path_from(wd).to_s next if app.files.ignored?(relative_path) app.files.did_change(relative_path) end removed.each do |path| - relative_path = Pathname(path).relative_path_from(Pathname(Dir.pwd)).to_s + relative_path = Pathname(path).relative_path_from(wd).to_s next if app.files.ignored?(relative_path) app.files.did_delete(relative_path) end diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 1c4b74ed..d559a442 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -222,6 +222,30 @@ module Middleman end end + # Glob a directory and try to keep path encoding consistent. + # + # @param [String] path The glob path. + # @return [Array] + def glob_directory(path) + results = ::Dir[path] + + return results unless RUBY_PLATFORM =~ /darwin/ + + results.map { |r| r.encode('UTF-8', 'UTF-8-MAC') } + end + + # Get the PWD and try to keep path encoding consistent. + # + # @param [String] path The glob path. + # @return [Array] + def current_directory + result = ::Dir.pwd + + return result unless RUBY_PLATFORM =~ /darwin/ + + result.encode('UTF-8', 'UTF-8-MAC') + end + private # Is mime type known to be non-binary? diff --git a/middleman-core/lib/middleman-more/core_extensions/i18n.rb b/middleman-core/lib/middleman-more/core_extensions/i18n.rb index 554c8867..c8fb7786 100644 --- a/middleman-core/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-more/core_extensions/i18n.rb @@ -133,7 +133,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension end def configure_i18n - ::I18n.load_path += Dir[File.join(app.root, @locales_glob)] + ::I18n.load_path += ::Middleman::Util.glob_directory(File.join(app.root, @locales_glob)) ::I18n.reload! ::I18n.default_locale = @mount_at_root