class Molder::App

Attributes

command[RW]
command_name[RW]
commands[RW]
config[RW]
log_dir[RW]
options[RW]
templates[RW]

Public Class Methods

new(config:, options:, command_name:) click to toggle source
# File lib/molder/app.rb, line 11
def initialize(config:, options:, command_name:)
  self.config       = config
  self.options      = options
  self.command_name = command_name
  self.commands     = []
  self.log_dir      = options[:log_dir] || config.global.log_dir || './log'

  resolve_command!

  resolve_templates!
end

Public Instance Methods

execute!() click to toggle source
# File lib/molder/app.rb, line 23
def execute!
    colors = %i(yellow blue red green magenta cyan white)

    FileUtils.mkdir_p(log_dir)
    if options.dry_run
      puts "\nDry-run print of #{commands.size} commands:\n".bold.cyan.underlined
      commands.count.times do |i|
        color = colors[i % colors.size]
        cmd   = commands[i ]
        puts "#{cmd}\n".send(color).send(:bold)
      end
    else
      puts "Executing #{commands.size} commands using a pool of up to #{options.max_processes} processes:\n".bold.cyan.underlined
      ::Parallel.each((1..commands.size),
                      :in_processes => options.max_processes) do |i|

        color = colors[(i - 1) % colors.size]
        cmd   = commands[i - 1]
        printf('%s', "Worker: #{Parallel.worker_number}, command #{i}\n".send(color)) if options.verbose
        puts "#{cmd}\n".send(color)

        system %Q(( #{cmd} ) > #{log_dir}/#{command_name}.#{i}.log)
    end
  end
end

Private Instance Methods

puts(*args) click to toggle source
# File lib/molder/app.rb, line 51
def puts(*args)
  ::Molder::CLI.instance.stdout.puts(*args)
end
resolve_command!() click to toggle source
# File lib/molder/app.rb, line 85
def resolve_command!
  unless config.commands.include?(command_name)
    raise(::Molder::InvalidCommandError, "Command #{command_name} is not defined in the configuration file #{options[:config]}")
  end

  command_hash = Hashie::Extensions::SymbolizeKeys.symbolize_keys(config.commands[command_name].to_h)
  self.command = Molder::Command.new(name: command_name, config: config, **command_hash)
end
resolve_templates!() click to toggle source
# File lib/molder/app.rb, line 55
def resolve_templates!
  self.templates ||= []
  options.names.each_pair do |name, indexes|
    if config.templates[name]
      template_array = config.templates[name].is_a?(Array) ?
                         config.templates[name] :
                         [config.templates[name]]

      template_array.flatten.each do |attrs|
        attributes = attrs.dup
        attributes.merge!(options.override) if options.override
        self.templates << ::Molder::Template.new(config:     config,
                                                 options:    options,
                                                 name:       name,
                                                 indexes:    indexes,
                                                 command:    command,
                                                 attributes: attributes)
      end
    else
      raise ::Molder::InvalidTemplateName, "Template name #{name} is not valid."
    end
  end

  self.templates.each do |t|
    t.each_command do |cmd|
      self.commands << cmd
    end
  end
end