class Parcels::WidgetTree

Constants

ALL_EXTENSIONS
EXTENSION_TO_PARCEL_CLASS_MAP
PARCELS_LOGICAL_PATH_PREFIXES

Attributes

parcels_environment[R]
root[R]

Public Class Methods

new(parcels_environment, root) click to toggle source
# File lib/parcels/widget_tree.rb, line 15
def initialize(parcels_environment, root)
  @parcels_environment = parcels_environment
  @root = File.expand_path(root, parcels_environment.root)

  @sprockets_environments_added_to = { }
end

Public Instance Methods

add_all_widgets_to_sprockets_context!(sprockets_context, set_names) click to toggle source
# File lib/parcels/widget_tree.rb, line 59
def add_all_widgets_to_sprockets_context!(sprockets_context, set_names)
  return unless root_exists?
  sprockets_context.depend_on(root)

  all_parcels = [ ]

  Find.find(root) do |path|
    full_path = File.expand_path(path, root)
    stat = File.stat(full_path)

    sprockets_context.depend_on(path) if stat.directory?
    next unless stat.file?

    extension = File.extname(full_path).strip.downcase
    if (klass = EXTENSION_TO_PARCEL_CLASS_MAP[extension])
      parcel = klass.new(self, full_path)
      if parcel.usable? && parcel.included_in_any_set?(set_names)
        if all_parcels.length == 0
          ensure_workaround_directory_is_set_up!
        end

        all_parcels << parcel
      end
    end
  end

  parcel_list = ::Parcels::DependencyParcelList.new
  parcel_list.add_parcels!(all_parcels)
  parcel_list.parcels_in_order.each do |parcel|
    parcel.add_to_sprockets_context!(sprockets_context)
  end
end
add_workaround_directory_to_sprockets!(sprockets_environment) click to toggle source
# File lib/parcels/widget_tree.rb, line 26
def add_workaround_directory_to_sprockets!(sprockets_environment)
  return if (! root_exists?)

  @sprockets_environments_added_to[sprockets_environment] ||= begin
    sprockets_environment.prepend_path(workaround_directory)
    true
  end
end
remove_workaround_directory_from(full_path) click to toggle source
# File lib/parcels/widget_tree.rb, line 39
def remove_workaround_directory_from(full_path)
  if (path_under_workaround = ::Parcels::Utils::PathUtils.maybe_path_under(full_path, workaround_directory))
    if (separator_index = path_under_workaround.index(File::SEPARATOR))
      if (separator_index > 0 && separator_index < (path_under_workaround.length - 1))
        link_name = path_under_workaround[0..(separator_index - 1)]
        after_link = path_under_workaround[(separator_index + 1)..-1]

        if File.symlink?(File.join(workaround_directory, link_name))
          link_value = File.readlink(File.join(workaround_directory, link_name))
          resolved_link = File.expand_path(File.join(workaround_directory, link_value))
          final_path = File.join(resolved_link, after_link)
          return final_path
        end
      end
    end
  end

  nil
end
subpath_to(full_path) click to toggle source
# File lib/parcels/widget_tree.rb, line 35
def subpath_to(full_path)
  ::Parcels::Utils::PathUtils.path_under(full_path, root)
end
widget_naming_root_dirs() click to toggle source
# File lib/parcels/widget_tree.rb, line 22
def widget_naming_root_dirs
  @widget_naming_root_dirs ||= [ root, File.dirname(root) ]
end

Private Instance Methods

ensure_nothing_else_is_in_workaround_directory!() click to toggle source
# File lib/parcels/widget_tree.rb, line 132
    def ensure_nothing_else_is_in_workaround_directory!
      entries = Dir.entries(workaround_directory).reject { |e| e =~ /^\./ }
      extra = entries - PARCELS_LOGICAL_PATH_PREFIXES

      if extra.length > 0
        raise Errno::EEXIST, %{Parcels uses the directory '#{workaround_directory}' internally
(to allow us to safely add assets to Sprockets without treading on the global asset namespace);
it should either be empty, or contain at most symlinks named any of
#{PARCELS_LOGICAL_PATH_PREFIXES.map { |p| "'#{p}'" }.join(", ")}.
(Parcels will create those symlinks automatically; you should not manage them yourself.)

However, this directory currently contains other file(s) that we weren't expecting:

#{extra.join("\n")}}
      end
    end
ensure_workaround_directory_exists!() click to toggle source
# File lib/parcels/widget_tree.rb, line 120
  def ensure_workaround_directory_exists!
    unless File.directory?(workaround_directory)
      if File.exist?(workaround_directory)
        raise Errno::EEXIST, %{Parcels uses the directory '#{workaround_directory}' internally
(to allow us to safely add assets to Sprockets without treading on the global asset namespace);
however, there is already something at that path that is not a directory.}
      end

      FileUtils.mkdir_p(workaround_directory)
    end
  end
ensure_workaround_directory_is_set_up!() click to toggle source
# File lib/parcels/widget_tree.rb, line 110
def ensure_workaround_directory_is_set_up!
  @workaround_directory_exists ||= begin
    ensure_workaround_directory_exists!
    ensure_nothing_else_is_in_workaround_directory!
    ensure_symlinks_point_to_the_right_place!

    true
  end
end
root_exists?() click to toggle source
# File lib/parcels/widget_tree.rb, line 102
def root_exists?
  File.exist?(root)
end
workaround_directory() click to toggle source
# File lib/parcels/widget_tree.rb, line 106
def workaround_directory
  @workaround_directory ||= parcels_environment.workaround_directory_root_for_widget_tree(self)
end