-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.rb
138 lines (114 loc) · 3.98 KB
/
example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/ruby
require "P4"
#*******************************************************************************
# Construct your client
#
p4 = P4.new()
#*******************************************************************************
# Perforce client environment - getting the default settings
#
# p4.client - Get P4CLIENT
# p4.host - Get P4HOST
# p4.password - Get P4PASSWD
# p4.port - Get P4PORT
# p4.user - Get P4USER
#
print <<EOS
Perforce settings:
P4PORT = #{p4.port}
P4USER = #{p4.user}
P4CLIENT = #{p4.client}
EOS
#*******************************************************************************
# Perforce client environment - setting specific values
#
# Uncomment the settings below as required
#
# p4.client = "tonys_client"
# p4.host = "myhostname"
# p4.password = "ruby"
# p4.port = "localhost:1666"
# p4.user = "tony"
#
p4.port = "localhost:1666"
#*******************************************************************************
# Connect to Perforce
begin
p4.connect()
rescue P4Exception
puts( "Failed to connect to Perforce" )
raise
end
#*******************************************************************************
# Toggle tagged mode
p4.tagged = false
p4.tagged = true
#*******************************************************************************
# Running commands. All run* methods return an array. That can mean one line of
# output per array element, or in tagged mode, that can mean an array of hashes.
#
# By default a P4Exception is raised if any errors or warnings are encountered
# during command execution. You can also opt to have exceptions raised only
# for errors (and not warnings), or not at all by setting the exception level.
# The available levels are:
#
# 0 - Exceptions disabled
# 1 - Exceptions for errors
# 2 - Exceptions for errors and warnings
#
# For example:
#
# p4.exception_level( 1 )
#
# You can fetch the results of the command from within a rescue block
# by calling P4#output; the errors with P4#errors and the warnings with
# P4#warnings
#
#*******************************************************************************
#*******************************************************************************
# "p4 user -o" produces an array with a single hash entry.
#
begin
user_spec = p4.run( "user", "-o" ).shift
print <<EOS
User details:
User Name: #{user_spec[ "User" ]}
Full Name: #{user_spec[ "FullName" ]}
Email Address: #{user_spec[ "Email" ]}
EOS
#*******************************************************************************
# Now that we have the user's details, we can update them. Since this
# example is invasive, it's commented out by default.
#
# user_spec[ "Email" ].upcase!
# p4.input( user_spec )
# p4.run( "user", "-i" )
#
#*******************************************************************************
# You can also run Perforce commands by invoking the method "run_<command>"
# rather than passing the command name as an argument to the run method. For
# example
info = p4.run_info()
user_spec = p4.run_user( "-o" ).shift
user_list = p4.run_users()
protections = p4.run_protect( "-o" ).shift
#*******************************************************************************
# There are also shortcut methods to make form editing easy. Any method
# taking the form "fetch_<command>" is equivalent to running "p4 <command> -o"
# and likewise any method taking the form "save_<command>" is equivalent to
# running "p4 <command> -i". These methods do not return an array - they
# return only one element, since that's all that Perforce will return to you.
#
# Note that all of the "save*" methods require an argument. The argument
# can be either a string containing the edited form, or it can be the edited
# hash returned from a previous "fetch*" call.
#
client_spec = p4.fetch_client()
client_spec[ "Owner" ] = "tony"
# p4.save_client( client_spec )
rescue P4Exception => msg
puts( msg )
p4.warnings.each { |w| puts( w ) }
p4.errors.each { |e| puts( e ) }
p4.output.each { |o| puts( o ) }
end