Class: Batali::Source::Path

Inherits:
Batali::Source show all
Includes:
Bogo::Memoization
Defined in:
lib/batali/source/path.rb

Overview

Path based source

Direct Known Subclasses

Git

Constant Summary collapse

DEFAULT_IGNORE =

Returns default ignore globs.

Returns:

  • (Array<String>)

    default ignore globs

['.git*']
IGNORE_FILE =

Returns valid ignore file names.

Returns:

  • (Array<String>)

    valid ignore file names

['chefignore', '.chefignore']

Instance Attribute Summary

Attributes inherited from Batali::Source

#cache_path

Instance Method Summary collapse

Methods inherited from Batali::Source

#==, build, #clean_asset, #diff, #initialize, #unit_dependencies, #unit_version

Constructor Details

This class inherits a constructor from Batali::Source

Instance Method Details

#assetString

Returns directory containing contents.

Returns:

  • (String)

    directory containing contents



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/batali/source/path.rb', line 21

def asset
  memoize(:asset) do
    dir = Dir.mktmpdir
    chefignore = IGNORE_FILE.map do |c_name|
      c_path = File.join(path, c_name)
      c_path if File.exist?(c_path)
    end.compact.first
    chefignore = chefignore ? File.readlines(chefignore) : []
    chefignore += DEFAULT_IGNORE
    chefignore.uniq!
    files_to_copy = Dir.glob(File.join(path, '{.[^.]*,**}', '**', '{*,*.*,.*}'))
    files_to_copy = files_to_copy.map do |file_path|
      next unless File.file?(file_path)
      relative_path = file_path.sub("#{path}/", '')
      relative_path unless chefignore.detect{|ig| File.fnmatch(ig, relative_path)}
    end.compact
    files_to_copy.each do |relative_path|
      new_path = File.join(dir, relative_path)
      FileUtils.mkdir_p(File.dirname(new_path))
      FileUtils.cp(File.join(path, relative_path), new_path)
    end
    dir
  end
end