Programming with P4Ruby

The following example shows how to create a new client workspace based on an existing template:

require "P4"
template = "my-client-template"
client_root = 'c:\p4-work'
p4 = P4.new
p4.connect

begin

    # Run a "p4 client -t template -o" and convert it into a Ruby hash
    spec = p4.fetch_client( "-t", template, "my-new-client")

    # Now edit the fields in the form
    spec[ "Root" ]  = client_root
    spec[ "Options" ] = spec[ "Options" ].sub( "normdir", "rmdir" )

    # Now save the updated spec
    p4.save_client( spec )

    # Point to the newly-created client
    p4.client="my-new-client"

    # And sync it.
    p4.run_sync

rescue P4Exception
    # If any errors occur, we'll jump in here. Just log them
    # and raise the exception up to the higher level

    p4.errors.each { |e| $stderr.puts( e ) }
    raise

end

Connecting to SSL-enabled servers

Scripts written with P4Ruby use any existing P4TRUST file present in their operating environment (by default, .p4trust in the home directory of the user that runs the script).

If the fingerprint returned by the server fails to match the one installed in the P4TRUST file associated with the script’s run-time environment, your script will (and should!) fail to connect to the server.