Class: Chef::ChefFS::FileSystem::ChefRepositoryFileSystemCookbookDir

Inherits:
ChefRepositoryFileSystemCookbookEntry show all
Defined in:
lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb

Instance Attribute Summary

Attributes inherited from ChefRepositoryFileSystemCookbookEntry

#recursive, #ruby_only

Attributes inherited from FileSystemEntry

#file_path

Attributes inherited from BaseFSObject

#name, #parent, #path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ChefRepositoryFileSystemCookbookEntry

#write_pretty_json

Methods inherited from ChefRepositoryFileSystemEntry

#data_handler, #minimize, #write, #write_pretty_json

Methods inherited from FileSystemEntry

#create_child, #delete, #dir?, #path_for_printing, #read, #write

Methods inherited from BaseFSDir

#child, #dir?

Methods inherited from BaseFSObject

#child, #compare_to, #create_child, #delete, #dir?, #exists?, #path_for_printing, #read, #root, #write

Constructor Details

#initialize(name, parent, file_path = nil) ⇒ ChefRepositoryFileSystemCookbookDir

Returns a new instance of ChefRepositoryFileSystemCookbookDir.



29
30
31
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb', line 29

def initialize(name, parent, file_path = nil)
  super(name, parent, file_path)
end

Class Method Details

.canonical_cookbook_name(entry_name) ⇒ Object

Exposed as a class method so that it can be used elsewhere



74
75
76
77
78
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb', line 74

def self.canonical_cookbook_name(entry_name)
  name_match = Chef::ChefFS::FileSystem::CookbookDir::VALID_VERSIONED_COOKBOOK_NAME.match(entry_name)
  return nil if name_match.nil?
  return name_match[1]
end

Instance Method Details

#can_have_child?(name, is_dir) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb', line 65

def can_have_child?(name, is_dir)
  if is_dir
    # Only the given directories will be uploaded.
    return CookbookDir::COOKBOOK_SEGMENT_INFO.keys.include?(name.to_sym) && name != 'root_files'
  end
  super(name, is_dir)
end

#canonical_cookbook_name(entry_name) ⇒ Object



80
81
82
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb', line 80

def canonical_cookbook_name(entry_name)
  self.class.canonical_cookbook_name(entry_name)
end

#chef_objectObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb', line 33

def chef_object
  begin
    loader = Chef::Cookbook::CookbookVersionLoader.new(file_path, parent.chefignore)
    # We need the canonical cookbook name if we are using versioned cookbooks, but we don't
    # want to spend a lot of time adding code to the main Chef libraries
    if Chef::Config[:versioned_cookbooks]
      _canonical_name = canonical_cookbook_name(File.basename(file_path))
      fail "When versioned_cookbooks mode is on, cookbook #{file_path} must match format <cookbook_name>-x.y.z"  unless _canonical_name

      # KLUDGE: We shouldn't have to use instance_variable_set
      loader.instance_variable_set(:@cookbook_name, _canonical_name)
    end

    loader.load_cookbooks
    return loader.cookbook_version
  rescue
    Chef::Log.error("Could not read #{path_for_printing} into a Chef object: #{$!}")
  end
  nil
end

#childrenObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/chef/chef_fs/file_system/chef_repository_file_system_cookbook_dir.rb', line 54

def children
  begin
    Dir.entries(file_path).sort.
        select { |child_name| can_have_child?(child_name, File.directory?(File.join(file_path, child_name))) }.
        map { |child_name| make_child(child_name) }.
        select { |entry| !(entry.dir? && entry.children.size == 0) }
  rescue Errno::ENOENT
    raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!)
  end
end