class Kamaze::Project::Tools::Vagrant::Writer
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“.
Attributes
output_file[R]
Path to Vagrantfile
@return [Pathname]
template[R]
Template used to generate “Vagrantfile“
@return [::Pathname]
Public Class Methods
new(template, output_file = 'Vagrantfile')
click to toggle source
@param [String] template @param [String] output_file
# 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
Public Instance Methods
write(boxes)
click to toggle source
Write “Vagrantfile“ based on given “boxes“
@param [Hash] boxes
# 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
Protected Instance Methods
templatize(yaml)
click to toggle source
Make content (used to write “Vagrantfile“)
@param [String] yaml @return [String]
# 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)
click to toggle source
Wrap text into small chunks
@param [String] text @param [Fixnum] width @return [Array<String>]
# 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