Sha256: 306234d2c80d345a9f1d4462b5fbc3c67b60d33c1eb439c3551e03445fe202b4

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

module BuildMaster

class Release
  def initialize
    @tasks = []
  end

  def task(name, &callback)
    raise "#{name} task already exists in #{@tasks}" if task_by_name(name)
    @tasks.push ReleaseTask.new(name, &callback)
  end
  
  def command(arguments)
    if arguments.include? '-p'
      print
    else
      start = arguments.shift
      finish = arguments.shift
      execute(start, finish)
    end
  end
  
  def print
    @tasks.each {|task| puts "#{task.name} "}
  end
  
  def execute(start = nil, finish = nil)
    start_index = select_with_default(start, 0)
    finish_index = select_with_default(finish, @tasks.size - 1)
    raise "No tasks from #{start} to #{finish} in #{@tasks}" if (start_index > finish_index)
    start_index.upto(finish_index) do |i| 
      puts "[#{i}] #{@tasks[i].name}:"
      @tasks[i].execute
    end
  end
  
  private
  def select_with_default(name, default_if_nil)
    return default_if_nil unless name
    match = task_by_name(name)
    raise "Task not found: #{name}" unless match
    @tasks.index(match)
  end
  
  def task_by_name(name)
    @tasks.find {|task| task.name == name}
  end
end

class ReleaseTask
  attr_reader :name

  def initialize(name, &callback)
    @name = name
    @callback = callback
  end
  
  def execute
    @callback.call
  end
  
  def to_s
    "[#{name}]"
  end
end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
BuildMaster-1.0.6 lib/buildmaster/project/release.rb
BuildMaster-1.0.9 lib/buildmaster/project/release.rb
BuildMaster-1.1.12 lib/buildmaster/project/release.rb
BuildMaster-1.1.9 lib/buildmaster/project/release.rb