Class: Monolith::Berksfile

Inherits:
Object
  • Object
show all
Defined in:
lib/monolith/berksfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Berksfile

Returns a new instance of Berksfile.



9
10
11
12
13
14
15
16
17
# File 'lib/monolith/berksfile.rb', line 9

def initialize(options)
  Berkshelf.ui.mute! if Monolith.formatter.quiet
  begin
    @berksfile = Berkshelf::Berksfile.from_options(options)
  rescue Berkshelf::BerksfileNotFound => e
    Monolith.formatter.error(e)
    exit(e.status_code)
  end
end

Instance Attribute Details

#berksfileObject (readonly)

Returns the value of attribute berksfile.



6
7
8
# File 'lib/monolith/berksfile.rb', line 6

def berksfile
  @berksfile
end

#cached_cookbooksObject (readonly)

Returns the value of attribute cached_cookbooks.



7
8
9
# File 'lib/monolith/berksfile.rb', line 7

def cached_cookbooks
  @cached_cookbooks
end

Instance Method Details

#cookbooks(path) ⇒ Object

Retrieve all cookbooks listed in the berksfile.

Can take a block to do something with each cookbook.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/monolith/berksfile.rb', line 30

def cookbooks(path)
  FileUtils.mkdir_p(path)
  cached_cookbooks = @cached_cookbooks
  if block_given?
    cached_cookbooks.each do |cookbook|
      destination = File.join(File.expand_path(path),
                              cookbook.cookbook_name)
      dep = berksfile.get_dependency(cookbook.cookbook_name)
      yield cookbook, dep, destination
    end
  end
  cached_cookbooks
end

#installObject

Runs berks install. This is needed before we install cookbooks ourselves to make sure berkshelf has a local copy of them for us to clone from or copy. However, it’s not needed for other commands, so it separated out here.



23
24
25
# File 'lib/monolith/berksfile.rb', line 23

def install
  @cached_cookbooks = @berksfile.install
end

#monolith_action(action, cookbook, dep, destination) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/monolith/berksfile.rb', line 44

def monolith_action(action, cookbook, dep, destination)
  obj = monolith_obj(cookbook, dep, destination)
  if obj.nil?
    Monolith.formatter.unsupported_location(cookbook, dep)
  else
    obj.send(action)
  end
end

#monolith_obj(cookbook, dep, destination) ⇒ Object

Feteches the appropriate monolith location object for a given cookbook dependency. I.e. Monolith::FooLocation.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/monolith/berksfile.rb', line 55

def monolith_obj(cookbook, dep, destination)
  if dep.nil? or dep.location.nil?
    Monolith::DefaultLocation.new(cookbook, dep, destination)
  else
    klass = dep.location.class.name.split('::')[-1]
    Monolith.formatter.debug("Location class for " \
                             "#{cookbook.cookbook_name} is #{klass}")
    if Monolith.const_defined?(klass)
      Monolith.const_get(klass).new(cookbook, dep, destination)
    end
  end
end