Class: Dock0::Base
- Inherits:
-
Object
- Object
- Dock0::Base
- Defined in:
- lib/dock0.rb
Overview
Base class for common methods
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #cleanup(list) ⇒ Object
- #default_config ⇒ Object
-
#initialize(*configs) ⇒ Base
constructor
A new instance of Base.
- #render_templates(prefix) ⇒ Object
- #run(cmd) ⇒ Object
- #run_script(script) ⇒ Object
- #run_scripts ⇒ Object
- #templates ⇒ Object
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
#config ⇒ Object (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_config ⇒ Object
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_scripts ⇒ Object
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 |
#templates ⇒ Object
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 |