Class: RSCM::P4Admin

Inherits:
Object
  • Object
show all
Defined in:
lib/rscm/scm/perforce.rb

Overview

Understands p4 administrative operations (not specific to a client)

Instance Method Summary collapse

Constructor Details

#initialize(port, user, pwd) ⇒ P4Admin

Returns a new instance of P4Admin.



156
157
158
# File 'lib/rscm/scm/perforce.rb', line 156

def initialize(port, user, pwd)
  @port, @user, @pwd = port, user, pwd
end

Instance Method Details

#central_exists?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/rscm/scm/perforce.rb', line 199

def central_exists?
  execute("info").split.join(" ") !~ /Connect to server failed/
end

#client_exists?(rootdir, clientname) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
172
# File 'lib/rscm/scm/perforce.rb', line 168

def client_exists?(rootdir, clientname)
  dir_regex = Regexp.new(rootdir)
  name_regex = Regexp.new(clientname)
  execute("clients").split("\n").find {|c| c =~ dir_regex && c =~ name_regex}
end

#clientspec(name, rootdir) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rscm/scm/perforce.rb', line 203

def clientspec(name, rootdir)
  s = StringIO.new
  s.puts "Client: #{name}"
  s.puts "Owner: #{ENV["LOGNAME"]}"
  s.puts "Host: #{ENV["HOSTNAME"]}"
  s.puts "Description: another one"
  s.puts "Root: #{rootdir}"
  s.puts "Options: noallwrite noclobber nocompress unlocked nomodtime normdir"
  s.puts "LineEnd: local"
  s.puts "View: //depot/... //#{name}/..."
  s.string
end

#create_client(rootdir, clientname) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/rscm/scm/perforce.rb', line 160

def create_client(rootdir, clientname)
  rootdir = File.expand_path(rootdir) if rootdir =~ /\.\./
  unless client_exists?(rootdir, clientname)
    execute_popen("client -i", "w+", clientspec(clientname, rootdir))
  end
  P4Client.new(rootdir, clientname, @port, @user, @pwd)
end

#delete_client(client) ⇒ Object



174
175
176
# File 'lib/rscm/scm/perforce.rb', line 174

def delete_client(client)
  execute("client -d #{client.name}")
end

#execute(cmd) ⇒ Object



228
229
230
231
232
# File 'lib/rscm/scm/perforce.rb', line 228

def execute(cmd)
  cmd = format_cmd(cmd)
  $stderr.puts "> executing: #{cmd}"
  `#{cmd}`
end

#execute_popen(cmd, mode, input) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/rscm/scm/perforce.rb', line 220

def execute_popen(cmd, mode, input)
  IO.popen(format_cmd(cmd), mode) do |io|
    io.puts(input)
    io.close_write
    io.each_line {|line| debug(line)}
  end
end

#format_cmd(cmd) ⇒ Object



234
235
236
# File 'lib/rscm/scm/perforce.rb', line 234

def format_cmd(cmd)
  "p4 -p #{@port} -u '#{@user}' -P '#{@pwd}' #{cmd} 2>&1"
end

#install_trigger(trigger_command) ⇒ Object



182
183
184
# File 'lib/rscm/scm/perforce.rb', line 182

def install_trigger(trigger_command)
  execute_popen("triggers -i", "a+", triggerspec_append(trigger_command))
end

#trigger_installed?(trigger_command) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/rscm/scm/perforce.rb', line 178

def trigger_installed?(trigger_command)
  triggers.any? {|line| line =~ /#{trigger_command}/}
end

#triggersObject



216
217
218
# File 'lib/rscm/scm/perforce.rb', line 216

def triggers
  execute("triggers -o")
end

#triggerspec_append(trigger_command) ⇒ Object



190
191
192
193
# File 'lib/rscm/scm/perforce.rb', line 190

def triggerspec_append(trigger_command)
  new_trigger = " damagecontrol commit //depot/... \"#{trigger_command}\" "
  triggers + $/ + new_trigger
end

#triggerspec_remove(trigger_command) ⇒ Object



195
196
197
# File 'lib/rscm/scm/perforce.rb', line 195

def triggerspec_remove(trigger_command)
  triggers.reject {|line| line =~ /#{trigger_command}/}.join
end

#uninstall_trigger(trigger_command) ⇒ Object



186
187
188
# File 'lib/rscm/scm/perforce.rb', line 186

def uninstall_trigger(trigger_command)
  execute_popen("triggers -i", "a+", triggerspec_remove(trigger_command))
end