Class: Basketcase::AutoSyncCommand

Inherits:
AutoCommand show all
Defined in:
lib/basketcase.rb

Constant Summary collapse

NUM_ELEMENTS_PER_COMMAND =
10

Instance Attribute Summary

Attributes inherited from Command

#comment, #listener, #targets

Instance Method Summary collapse

Methods inherited from AutoCommand

#each_element, #find_checkouts

Methods inherited from Command

#accept_args, #effective_targets, #option_comment, #option_graphical, #option_recurse, #report, #specified_targets

Methods included from Utils

#mkpath

Constructor Details

#initialize(*args) ⇒ AutoSyncCommand

Returns a new instance of AutoSyncCommand.



597
598
599
600
601
# File 'lib/basketcase.rb', line 597

def initialize(*args)
  super(*args)
  @control_file = Pathname.new("basketcase-autosync.tmp")
  @actions = []
end

Instance Method Details

#apply_actionsObject



658
659
660
661
662
663
664
665
# File 'lib/basketcase.rb', line 658

def apply_actions
  ['add', 'rm', 'co -h'].each do |command|
    elements = @actions.map { |a| a[1] if a[0] == command }.compact
    unless elements.empty?
      elements.each_slice(NUM_ELEMENTS_PER_COMMAND) {|subelements| run(*(command.split(' ') + subelements)) }
    end
  end
end

#collect_actionsObject



621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/basketcase.rb', line 621

def collect_actions
  each_element do |e|
    case e.status
    when :LOCAL
      @actions << ['add', e.path]
    when :MISSING
      @actions << ['rm', e.path]
    when :HIJACK
      @actions << ['co -h', e.path]
    end
  end
end

#executeObject



667
668
669
670
671
672
673
674
675
# File 'lib/basketcase.rb', line 667

def execute
  collect_actions
  if @actions.empty?
    puts "No changes required"
    return
  end
  prompt_for_confirmation unless @noprompt
  apply_actions
end

#helpObject



607
608
609
610
611
612
613
# File 'lib/basketcase.rb', line 607

def help
  <<EOF
Bulk add/remove: offer to add new elements, and remove missing ones.

-n          Don\'t prompt to confirm actions.
EOF
end

#option_nopromptObject Also known as: option_n



615
616
617
# File 'lib/basketcase.rb', line 615

def option_noprompt
  @noprompt = true
end

#prompt_for_confirmationObject



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/basketcase.rb', line 634

def prompt_for_confirmation
  @control_file.open('w') do |control|
    control.puts <<EOF
# basketcase proposes the actions listed below.
# Delete any that you don't wish to occur, then save this file.
#
EOF
    @actions.each do |a|
      control.puts a.join("\t")
    end
  end
  edit(@control_file)
  @actions = []
  @control_file.open('r') do |control|
    control.each_line do |line|
      if line =~ /^(add|rm|co -h)\s+(.*)/
        @actions << [$1, $2]
      end
    end
  end
end

#synopsisObject



603
604
605
# File 'lib/basketcase.rb', line 603

def synopsis
  "[<element> ...]"
end