Class: Slinky::ManifestDir

Inherits:
Object
  • Object
show all
Defined in:
lib/slinky/manifest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, parent, build_dir, manifest) ⇒ ManifestDir

Returns a new instance of ManifestDir.



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/slinky/manifest.rb', line 379

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

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



378
379
380
# File 'lib/slinky/manifest.rb', line 378

def children
  @children
end

#dirObject

Returns the value of attribute dir.



378
379
380
# File 'lib/slinky/manifest.rb', line 378

def dir
  @dir
end

#filesObject

Returns the value of attribute files.



378
379
380
# File 'lib/slinky/manifest.rb', line 378

def files
  @files
end

#parentObject

Returns the value of attribute parent.



378
379
380
# File 'lib/slinky/manifest.rb', line 378

def parent
  @parent
end

Instance Method Details

#add_child(path) ⇒ Object

Adds a child directory



447
448
449
450
451
452
453
454
# File 'lib/slinky/manifest.rb', line 447

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) ⇒ Object

Adds a file on the filesystem to the manifest

Parameters:

  • String

    path The path of the file



459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/slinky/manifest.rb', line 459

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

#buildObject



481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/slinky/manifest.rb', line 481

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) ⇒ ManifestFile

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

Parameters:

  • String

    path the path of the file relative to the directory

  • Boolean

    allow_multiple if enabled, can return multiple paths according to glob rules

Returns:

  • (ManifestFile)

    the manifest file at that path if one exists



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/slinky/manifest.rb', line 406

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) ⇒ Object

Removes a file from the manifest

Parameters:

  • ManifestFile

    mf The file to be deleted



477
478
479
# File 'lib/slinky/manifest.rb', line 477

def remove_file mf
  @files.delete(mf)
end

#to_sObject



494
495
496
# File 'lib/slinky/manifest.rb', line 494

def to_s
  "<ManifestDir:'#{@dir}'>"
end