Class: Fragrant::VagrantfileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/fragrant/vagrantfile_generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_directory, opts = {}) ⇒ VagrantfileGenerator

Returns a new instance of VagrantfileGenerator.



13
14
15
16
17
18
19
20
# File 'lib/fragrant/vagrantfile_generator.rb', line 13

def initialize(target_directory, opts={})
  @target_directory = target_directory
  @scripts          = opts[:scripts] || []
  @addresses        = opts[:addresses] || []
  @box_name         = opts[:box_name]
  @box_url          = opts[:box_url]
  @contents         = opts[:contents]
end

Instance Attribute Details

#addressesObject

Returns the value of attribute addresses.



7
8
9
# File 'lib/fragrant/vagrantfile_generator.rb', line 7

def addresses
  @addresses
end

Class Method Details

.template_pathObject



9
10
11
# File 'lib/fragrant/vagrantfile_generator.rb', line 9

def self.template_path
  File.expand_path('../../../templates/Vagrantfile', __FILE__)
end

Instance Method Details

#add_script(contents) ⇒ Object



30
31
32
33
34
# File 'lib/fragrant/vagrantfile_generator.rb', line 30

def add_script(contents)
  raise "body set, adding a script not supported" if @contents
  @scripts << contents
  true
end

#box_nameObject



22
23
24
# File 'lib/fragrant/vagrantfile_generator.rb', line 22

def box_name
  @box_name || "precise32"
end

#box_urlObject



26
27
28
# File 'lib/fragrant/vagrantfile_generator.rb', line 26

def box_url
  @box_url || "http://files.vagrantup.com/precise32.box"
end

#writeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fragrant/vagrantfile_generator.rb', line 44

def write
  FileUtils.mkdir_p(@target_directory)

  writeable_scripts.each do |filename, script_body|
    File.open(File.join(@target_directory, filename), 'w') do |f|
      f.print script_body
      f.chmod 0755
    end
  end

  @contents ||= Vagrant::Util::TemplateRenderer.render(self.class.template_path,
                                                       :box_name => box_name,
                                                       :box_url => box_url,
                                                       :provision_script_paths => writeable_scripts.keys.sort,
                                                       :addresses => addresses)
  File.open(File.join(@target_directory, 'Vagrantfile'), 'w') { |f| f.print @contents }

  true
end

#writeable_scriptsObject



36
37
38
39
40
41
42
# File 'lib/fragrant/vagrantfile_generator.rb', line 36

def writeable_scripts
  retval = {}
  @scripts.each_with_index do |contents, i|
    retval[sprintf("script%03d", i + 1)] = contents
  end
  retval
end