Module: Commands::Init::P4Helpers

Included in:
ChangelistModel, DepotModel
Defined in:
lib/commands/init/p4_helpers.rb

Instance Method Summary collapse

Instance Method Details

#open_client(options) ⇒ Object

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


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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.('-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.('-p')
        p4.password = results.first
      end
    end

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