class P4Tools::Move

Public Class Methods

new(args) click to toggle source
# File lib/commands/move.rb, line 24
def initialize(args)
  @workspace = args[:workspace]
  @switch = args[:switch]
  @changelists = args[:changelists]
  @revert = args[:revert]
  @shelve = args[:shelve]

  @p4 = P4Tools.connection
end
run(arguments) click to toggle source
# File lib/commands/move.rb, line 4
def self.run(arguments)
  Move.new(arguments).run
end
set_options(opts) click to toggle source
# File lib/commands/move.rb, line 8
def self.set_options(opts)
  opts.set do
    help 'Move a pending changelist from one workspace to another, optionally shelve and delete them before.'
    help 'The changelist need to be empty to move(only shelved files allowed)!'
    help ''
    help 'Options:'
    help ''
    arg :workspace, "Name of the new workspace. If 'switch' option provided, this will be ignored.", :short => '-w', :type => :string
    arg :switch, 'Switch between workspaces, only 2 workspace name allowed.', :short => '-i', :type => :strings
    arg :changelists, 'Changelist numbers to move.', :short => '-c', :type => :ints, :required => true
    arg :revert, 'Revert before move.', :short => '-r'
    arg :shelve, 'Shelve before move.', :short => '-s'
  end
end

Public Instance Methods

get_workspace(changelist_spec) click to toggle source
# File lib/commands/move.rb, line 73
def get_workspace(changelist_spec)
  if @workspaces
    current = changelist_spec['Client']
    validate_current(current)

    @workspaces[current]
  else
    @workspace
  end
end
move_changelists() click to toggle source
# File lib/commands/move.rb, line 47
def move_changelists
  @changelists.each do |changelist|
    @changelist = changelist
    @is_not_empty = !CommandUtils.empty_changelist?(@changelist)

    shelve
    revert

    changelist_spec = @p4.fetch_change(@changelist)
    changelist_spec['Client'] = get_workspace(changelist_spec)
    @p4.save_change(changelist_spec)
  end
end
prepare_workspaces() click to toggle source
# File lib/commands/move.rb, line 39
def prepare_workspaces
  if @switch
    validate_workspaces(@switch)

    @workspaces = Hash[@switch.permutation.to_a]
  end
end
revert() click to toggle source
# File lib/commands/move.rb, line 67
def revert
  if @revert && @is_not_empty && CommandUtils.changelist_shelved?(@changelist)
    @p4.run_revert('-w', '-c', @changelist, '//...')
  end
end
run() click to toggle source
# File lib/commands/move.rb, line 34
def run
  prepare_workspaces
  move_changelists
end
shelve() click to toggle source
# File lib/commands/move.rb, line 61
def shelve
  if @shelve && @is_not_empty
    @p4.run_shelve('-f', '-c', @changelist)
  end
end
validate_current(current_workspace) click to toggle source
# File lib/commands/move.rb, line 84
def validate_current(current_workspace)
  unless @workspaces.include?(current_workspace)
    raise(ArgumentError, "The switch parameter does not contains the currently active workspace: #{current_workspace}!")
  end
end
validate_workspaces(workspaces) click to toggle source
# File lib/commands/move.rb, line 90
def validate_workspaces(workspaces)
  if workspaces.length != 2
    raise(ArgumentError, 'The switch parameter need to contains 2 workspace names exactly!')
  end
end