Class: Dock0::Base

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

Overview

Base class for common methods

Direct Known Subclasses

Config, Image, Install

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*configs) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
37
38
# File 'lib/dock0.rb', line 31

def initialize(*configs)
  @config = configs.each_with_object(default_config) do |path, obj|
    new = YAML.safe_load(File.read(path))
    next unless new
    obj.deep_merge! Cymbal.symbolize(new)
  end
  @paths = @config[:paths]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/dock0.rb', line 29

def config
  @config
end

Instance Method Details

#cleanup(list) ⇒ Object



79
80
81
82
# File 'lib/dock0.rb', line 79

def cleanup(list)
  puts "Removing: #{list.join(', ')}"
  FileUtils.rm_rf list
end

#default_configObject



40
41
42
# File 'lib/dock0.rb', line 40

def default_config
  { paths: { scripts: './scripts' } }
end

#render_templates(prefix) ⇒ Object



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

def render_templates(prefix)
  templates.each do |path|
    puts "Templating #{path}"
    template = File.read "#{@paths[:templates]}/#{path}"
    parsed = ERB.new(template, nil, '<>').result(binding)

    target_path = "#{@paths[:build]}/#{prefix}/#{path}"
    FileUtils.mkdir_p File.dirname(target_path)
    File.open(target_path, 'w') { |fh| fh.write parsed }
  end
end

#run(cmd) ⇒ Object



44
45
46
47
48
# File 'lib/dock0.rb', line 44

def run(cmd)
  results = `#{cmd} 2>&1`
  return results if $CHILD_STATUS.success?
  raise "Failed running #{cmd}:\n#{results}"
end

#run_script(script) ⇒ Object



68
69
70
# File 'lib/dock0.rb', line 68

def run_script(script)
  Dir.chdir('.') { instance_eval File.read(script), script, 0 }
end

#run_scriptsObject



72
73
74
75
76
77
# File 'lib/dock0.rb', line 72

def run_scripts
  Dir.glob(@paths[:scripts] + '/*.rb').sort.each do |script|
    puts "Running #{script}"
    run_script script
  end
end

#templatesObject



50
51
52
53
54
# File 'lib/dock0.rb', line 50

def templates
  Dir.chdir(@paths[:templates]) do
    Dir.glob('**/*').select { |x| File.file? x }
  end
end