class Slinky::ManifestDir

Attributes

children[RW]
dir[RW]
files[RW]
parent[RW]

Public Class Methods

new(dir, parent, build_dir, manifest) click to toggle source
# File lib/slinky/manifest.rb, line 378
def initialize dir, parent, build_dir, manifest
  @dir = dir
  @parent = parent
  @files = []
  @children = []
  @build_dir = Pathname.new(build_dir)
  @manifest = manifest

  Dir.glob("#{dir}/*").each do |path|
    # skip the build dir
    next if Pathname.new(File.expand_path(path)) == Pathname.new(build_dir)
    if File.directory? path
      add_child(path)
    else
      add_file(path)
    end
  end
end

Public Instance Methods

add_child(path) click to toggle source

Adds a child directory

# File lib/slinky/manifest.rb, line 446
def add_child path
  if File.directory? path
    build_dir = (@build_dir + File.basename(path)).cleanpath
    md = ManifestDir.new(path, self, build_dir, @manifest)
    @children << md
    md
  end
end
add_file(path) click to toggle source

Adds a file on the filesystem to the manifest

@param String path The path of the file

# File lib/slinky/manifest.rb, line 458
def add_file path
  file = File.basename(path)
  full_path = Pathname.new(@dir).join(file).to_s
  if File.exists?(full_path) && !file.start_with?(".")
    mf = ManifestFile.new(full_path, @build_dir, @manifest, self)
    # we don't want two files with the same source
    extant_file = @files.find{|f| f.source == mf.source}
    if extant_file
      @files.delete(extant_file)
    end
    @files << mf
    mf
  end
end
build() click to toggle source
# File lib/slinky/manifest.rb, line 480
def build
  unless File.directory?(@build_dir.to_s)
    FileUtils.mkdir(@build_dir.to_s)
  end

  if (@files + @children).map {|m| m.build}.any?
    @build_dir
  else
    FileUtils.rmdir(@build_dir.to_s)
    nil
  end
end
find_by_path(path, allow_multiple = false) click to toggle source

Finds the file at the given path in the directory if one exists, otherwise nil.

@param String path the path of the file relative to the directory @param Boolean allow_multiple if enabled, can return multiple paths

according to glob rules

@return [ManifestFile] the manifest file at that path if one exists

# File lib/slinky/manifest.rb, line 405
def find_by_path path, allow_multiple = false
  if path[0] == '/'
    # refer absolute paths to the manifest
    return @manifest.find_by_path(path[1..-1], allow_multiple)
  end

  components = path.to_s.split(File::SEPARATOR).reject{|x| x == ""}
  case components.size
  when 0
    [self]
  when 1
    path = [@dir, components[0]].join(File::SEPARATOR)
    if (File.directory?(path) rescue false)
      c = @children.find{|d|
        Pathname.new(d.dir).cleanpath == Pathname.new(path).cleanpath
      }
      unless c
        c = add_child(path)
      end
      [c]
    else
      @files.find_all{|f| f.matches? components[0], allow_multiple}
    end
  else
    if components[0] == ".."
      @parent.find_by_path components[1..-1].join(File::SEPARATOR)
    else
      child = @children.find{|d|
        Pathname.new(d.dir).basename.to_s == components[0]
      }
      if child
        child.find_by_path(components[1..-1].join(File::SEPARATOR),
                           allow_multiple)
      else
        []
      end
    end
  end
end
remove_file(mf) click to toggle source

Removes a file from the manifest

@param ManifestFile mf The file to be deleted

# File lib/slinky/manifest.rb, line 476
def remove_file mf
  @files.delete(mf)
end
to_s() click to toggle source
# File lib/slinky/manifest.rb, line 493
def to_s
  "<ManifestDir:'#{@dir}'>"
end