Class: Basketcase::AutoSyncCommand

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

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.



875
876
877
878
879
# File 'lib/basketcase.rb', line 875

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

Instance Method Details

#apply_actionsObject



934
935
936
937
938
939
940
941
# File 'lib/basketcase.rb', line 934

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

#collect_actionsObject



899
900
901
902
903
904
905
906
907
908
909
910
# File 'lib/basketcase.rb', line 899

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



943
944
945
946
947
948
949
950
951
# File 'lib/basketcase.rb', line 943

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

#helpObject



885
886
887
888
889
890
891
# File 'lib/basketcase.rb', line 885

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



893
894
895
# File 'lib/basketcase.rb', line 893

def option_noprompt
  @noprompt = true
end

#prompt_for_confirmationObject



912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
# File 'lib/basketcase.rb', line 912

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



881
882
883
# File 'lib/basketcase.rb', line 881

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