class Released::Runner

Public Class Methods

new(goals, dry_run: false) click to toggle source
# File lib/released/runner.rb, line 42
def initialize(goals, dry_run: false)
  @goals = goals
  @dry_run = dry_run

  @tui = TUI.new($stdout)
  @tui_mutex = Mutex.new
end

Public Instance Methods

run() click to toggle source
# File lib/released/runner.rb, line 50
def run
  assess_all
  try_achieve_all
end

Private Instance Methods

assess_all() click to toggle source
# File lib/released/runner.rb, line 103
def assess_all
  puts 'Assessing goals…'
  print_goals

  @goals.each.with_index do |_, idx|
    write_state(idx, left, 'waiting')
  end

  @goals.each.with_index do |goal, idx|
    if goal.assessable?
      write_state(idx, left, 'working…')

      begin
        goal.assess
        write_state(idx, left, 'ok (succeeded)')
      rescue => e
        write_state(idx, left, 'failed')
        handle_error(e)
        exit 1 # FIXME: eww
      end
    else
      write_state(idx, left, 'ok (skipped)')
    end
  end

  puts
end
handle_error(e) click to toggle source
# File lib/released/runner.rb, line 78
def handle_error(e)
  puts
  puts 'FAILURE!'
  puts '-----'
  puts e.message
  puts
  puts e.backtrace.join("\n")
  puts '-----'
  puts 'Aborting!'
end
left() click to toggle source
# File lib/released/runner.rb, line 99
def left
  @_left ||= @goals.map { |g| g.to_s.size }.max + 8
end
print_goals() click to toggle source
try_achieve_all() click to toggle source
# File lib/released/runner.rb, line 131
def try_achieve_all
  puts 'Achieving goals…'
  print_goals

  @goals.each.with_index do |_, idx|
    write_state(idx, left, 'waiting')
  end

  @goals.each.with_index do |goal, idx|
    if @dry_run
      if goal.achieved?
        write_state(idx, left, 'ok (already achieved)')
      else
        write_state(idx, left, 'pending: ' + goal.failure_reason)
      end
      next
    end

    if goal.achieved?
      write_state(idx, left, 'ok (already achieved)')
      next
    end

    begin
      write_state(idx, left, 'working…')
      goal.try_achieve
    rescue => e
      write_state(idx, left, 'errored')
      handle_error(e)
      exit 1 # FIXME: eww
    end

    if !goal.effectful?
      write_state(idx, left, 'ok (passed)')
      next
    elsif goal.achieved?
      write_state(idx, left, 'ok (newly achieved)')
      next
    else
      write_state(idx, left, 'failed: ' + goal.failure_reason)
      puts
      puts 'Failed!'
      exit 1 # FIXME: eww
    end
  end
end
write_state(idx, left, state) click to toggle source
# File lib/released/runner.rb, line 89
def write_state(idx, left, state)
  up = @goals.size - idx
  @tui.move_up(up)
  @tui.move_to_left(left)
  $stdout << state
  @tui.clear_to_end_of_line
  @tui.move_to_left
  @tui.move_down(up)
end