Class: Kamaze::Project::Tools::Vagrant::Writer
- Defined in:
- lib/kamaze/project/tools/vagrant/writer.rb,
lib/kamaze/project/tools/vagrant/writer.rb
Overview
Responsible to write a Vagrantfile
Vagrantfile
is written as a standalone, i. e. boxes
variable
is set as a base64 string. Vagrantfile
defines the necessary methods
to setup VMs from boxes
.
Instance Attribute Summary collapse
-
#output_file ⇒ Pathname
readonly
Path to Vagrantfile.
-
#template ⇒ ::Pathname
readonly
Template used to generate
Vagrantfile
.
Instance Method Summary collapse
-
#initialize(template, output_file = 'Vagrantfile') ⇒ Writer
constructor
A new instance of Writer.
-
#templatize(yaml) ⇒ String
protected
Make content (used to write
Vagrantfile
). -
#word_wrap(text, width = 80) ⇒ Array<String>
protected
Wrap text into small chunks.
-
#write(boxes) ⇒ Object
Write
Vagrantfile
based on givenboxes
.
Constructor Details
#initialize(template, output_file = 'Vagrantfile') ⇒ Writer
Returns a new instance of Writer.
36 37 38 39 40 41 42 |
# File 'lib/kamaze/project/tools/vagrant/writer.rb', line 36 def initialize(template, output_file = 'Vagrantfile') template = ::Pathname.new(template) @template = template @template = template.join('Vagrantfile') if template.directory? @output_file = output_file end |
Instance Attribute Details
#output_file ⇒ Pathname (readonly)
Path to Vagrantfile
32 33 34 |
# File 'lib/kamaze/project/tools/vagrant/writer.rb', line 32 def output_file @output_file end |
#template ⇒ ::Pathname (readonly)
Template used to generate Vagrantfile
27 28 29 |
# File 'lib/kamaze/project/tools/vagrant/writer.rb', line 27 def template @template end |
Instance Method Details
#templatize(yaml) ⇒ String (protected)
Make content (used to write Vagrantfile
)
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/kamaze/project/tools/vagrant/writer.rb', line 59 def templatize(yaml) boxes64 = Base64.strict_encode64(yaml).yield_self do |text| word_wrap(text, 70).map { |s| "\s\s'#{s}'\\" }.join("\n").chomp('\\') end ['# frozen_string_literal: true', '# vim: ai ts=2 sts=2 et sw=2 ft=ruby', nil, '[:base64, :yaml, :pp].each { |req| require req.to_s }', nil, "cnf64 = \\\n#{boxes64}", nil, 'boxes = YAML.safe_load(Base64.strict_decode64(cnf64), [Symbol])', nil, template.read.gsub(/^#.*\n/, '')] .map(&:to_s).join("\n").gsub(/\n\n+/, "\n\n") end |
#word_wrap(text, width = 80) ⇒ Array<String> (protected)
Wrap text into small chunks
78 79 80 |
# File 'lib/kamaze/project/tools/vagrant/writer.rb', line 78 def word_wrap(text, width = 80) text.each_char.each_slice(width).to_a.map(&:join) end |
#write(boxes) ⇒ Object
Write Vagrantfile
based on given boxes
47 48 49 50 51 |
# File 'lib/kamaze/project/tools/vagrant/writer.rb', line 47 def write(boxes) ::YAML.dump(boxes) .yield_self { |yaml| templatize(yaml) } .yield_self { |content| output_file.write(content) } end |