Sha256: 75698e9cf86e98024b72cc01c63e986ecd6210682938e4a606be391ce282388f

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'erb'

module Blazing
  class Commands

    include Logger

    class << self
      def run(command, options = {})
        self.new(options.merge({ :command => command })).send(command)
      end
    end

    def initialize(options = {})
      warn 'The :default Target option has been deprecated and will be ignored' if options.has_key?(:default)

      @target_name = options[:target_name]
      @config_file = options[:file]
      @command = options[:command]
      @targets = []

      if command_requires_config?
        @config ||= Config.parse(@config_file)
        @targets = determine_targets
        error 'no target given or found' if @targets.empty?
      end
    end

    def init
      info "Creating an example config file in #{Blazing::DEFAULT_CONFIG_LOCATION}"
      info "Customize it to your needs"
      create_config_directory
      write_config_file
    end

    def setup
      @targets.each { |target| target.setup }
      update
    end

    def update
      @targets.each { |target| target.update }
    end

    def recipes
      @config.recipes.each { |recipe| recipe.run({:target_name => @target_name}) }
    end

    def list
      Blazing::Recipe.pretty_list
    end

    private

    def create_config_directory
      Dir.mkdir 'config' unless File.exists? 'config'
    end

    def write_config_file
      config = ERB.new(File.read("#{Blazing::TEMPLATE_ROOT}/config.erb")).result
      File.open(Blazing::DEFAULT_CONFIG_LOCATION,"wb") { |f| f.puts config }
    end

    def determine_targets
      if @target_name == 'all'
        @config.targets
      else
        targets = []
        targets << @config.find_target(@target_name)
        targets.compact
      end
    end

    def command_requires_config?
      [:setup, :update, :recipes].include? @command
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blazing-0.3.0 lib/blazing/commands.rb