module Commands::Init::P4Helpers

Public Instance Methods

open_client(options) { |dir, name| ... } click to toggle source

Intended to be a block helper that creates a ‘temporary client’

Your block should take the client name and path.

Example:

open_client(p4) do |path, name|
  puts "my client #{name}'s root is #{path}"
end
# File lib/commands/init/p4_helpers.rb, line 18
def open_client(options)
  p4 = options[:p4]

  # Switch user only if specified
  user = nil
  password = nil
  olduser = nil
  oldpass = nil
  if (options[:user])
    user = options[:user]
    password = options[:password] if options.key?(:password)
    olduser = options[:olduser]
    oldpass = options[:oldpass] if options.key?(:oldpass)
    p4.user = user
    p4.password = password
    if password
      results = p4.run_login('-p')
      p4.password = results.first
    end
  end

  name = RandomUtil.randstr
  dir = File.join(Conventions.client_root_dir, name)

  if !Dir.exist?(dir)
    FileUtils.mkpath(dir)
  end

  spec = p4.fetch_client
  spec._root = dir
  spec._client = name
  spec._description = 'p4util init temp client'
  if options.key?(:view)
    spec._view = options[:view].map { |x| x.gsub("${name}", name) }
  else
    spec._view = ["//depot/... //#{name}/depot/..."]
  end

  p4.save_client(spec)

  p4.client = name

  p4.run_sync('-f', '//...')

  if block_given?
    yield dir, name
  else
    return dir, name
  end
ensure
  if block_given?
    if (user)
      p4.user = olduser
      p4.password = oldpass
      if oldpass
        results = p4.run_login('-p')
        p4.password = results.first
      end
    end

    p4.run_client('-d', '-f', name)
    p4.client = 'invalid'
    FileUtils.rmtree(dir)
  end
end