2014-03-14 14:13:41 -04:00
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using UnrealBuildTool ;
namespace AutomationTool
{
/// <summary>
/// Environment to allow access to commonly used environment variables.
/// </summary>
public class P4Environment
{
2014-04-23 20:07:00 -04:00
2014-03-14 14:13:41 -04:00
public string P4Port { get ; protected set ; }
public string ClientRoot { get ; protected set ; }
public string User { get ; protected set ; }
2014-04-23 20:07:00 -04:00
private string ChangelistStringInternal ;
public string ChangelistString
{
get
{
if ( String . IsNullOrEmpty ( ChangelistStringInternal ) )
{
throw new AutomationException ( "P4Environment.ChangelistString has not been initialized but is requested. Set uebp_CL env var or run UAT with -P4CL to automatically detect changelist." ) ;
}
return ChangelistStringInternal ;
}
protected set
{
ChangelistStringInternal = value ;
}
}
private int ChangelistInternal ;
public int Changelist
{
get
{
if ( ChangelistInternal < = 0 )
{
throw new AutomationException ( "P4Environment.Changelist has not been initialized but is requested. Set uebp_CL env var or run UAT with -P4CL to automatically detect changelist." ) ;
}
2014-04-23 20:10:20 -04:00
return ChangelistInternal ;
2014-04-23 20:07:00 -04:00
}
protected set
{
ChangelistInternal = value ;
}
}
2014-03-14 14:13:41 -04:00
public string Client { get ; protected set ; }
public string BuildRootP4 { get ; protected set ; }
public string BuildRootEscaped { get ; protected set ; }
public string LabelToSync { get ; protected set ; }
public string LabelPrefix { get ; protected set ; }
public string BranchName { get ; protected set ; }
2014-04-02 18:09:23 -04:00
internal P4Environment ( P4Connection Connection , CommandEnvironment CmdEnv )
2014-03-14 14:13:41 -04:00
{
2014-04-02 18:09:23 -04:00
InitEnvironment ( Connection , CmdEnv ) ;
2014-03-14 14:13:41 -04:00
}
2014-04-02 18:09:23 -04:00
protected virtual void InitEnvironment ( P4Connection Connection , CommandEnvironment CmdEnv )
2014-03-14 14:13:41 -04:00
{
//
// P4 Environment
//
P4Port = CommandUtils . GetEnvVar ( EnvVarNames . P4Port ) ;
ClientRoot = CommandUtils . GetEnvVar ( EnvVarNames . ClientRoot ) ;
User = CommandUtils . GetEnvVar ( EnvVarNames . User ) ;
2014-04-23 20:07:00 -04:00
ChangelistStringInternal = CommandUtils . GetEnvVar ( EnvVarNames . Changelist , null ) ;
2014-03-14 14:13:41 -04:00
Client = CommandUtils . GetEnvVar ( EnvVarNames . Client ) ;
BuildRootP4 = CommandUtils . GetEnvVar ( EnvVarNames . BuildRootP4 ) ;
if ( BuildRootP4 . EndsWith ( "/" , StringComparison . InvariantCultureIgnoreCase ) | | BuildRootP4 . EndsWith ( "\\" , StringComparison . InvariantCultureIgnoreCase ) )
{
// We expect the build root to not end with a path separator
BuildRootP4 = BuildRootP4 . Substring ( 0 , BuildRootP4 . Length - 1 ) ;
CommandUtils . SetEnvVar ( EnvVarNames . BuildRootP4 , BuildRootP4 ) ;
}
BuildRootEscaped = CommandUtils . GetEnvVar ( EnvVarNames . BuildRootEscaped ) ;
LabelToSync = CommandUtils . GetEnvVar ( EnvVarNames . LabelToSync ) ;
if ( ( ( CommandUtils . P4Enabled | | CommandUtils . IsBuildMachine ) & & ( ClientRoot = = String . Empty | | User = = String . Empty | |
2014-04-23 20:07:00 -04:00
( String . IsNullOrEmpty ( ChangelistStringInternal ) & & CommandUtils . IsBuildMachine ) | | Client = = String . Empty | | BuildRootP4 = = String . Empty ) ) )
2014-03-14 14:13:41 -04:00
{
2014-04-23 17:27:31 -04:00
Log . TraceInformation ( "P4Enabled={0}" , CommandUtils . P4Enabled ) ;
Log . TraceInformation ( "ClientRoot={0}" , ClientRoot ) ;
Log . TraceInformation ( "User={0}" , User ) ;
2014-04-23 20:07:00 -04:00
Log . TraceInformation ( "ChangelistString={0}" , ChangelistStringInternal ) ;
2014-04-23 17:27:31 -04:00
Log . TraceInformation ( "Client={0}" , Client ) ;
Log . TraceInformation ( "BuildRootP4={0}" , BuildRootP4 ) ;
2014-03-14 14:13:41 -04:00
throw new AutomationException ( "BUILD FAILED Perforce Environment is not set up correctly. Please check your environment variables." ) ;
}
LabelPrefix = BuildRootP4 + "/" ;
if ( CommandUtils . P4Enabled )
{
2014-04-23 20:07:00 -04:00
if ( CommandUtils . IsBuildMachine | | ChangelistStringInternal ! = null )
2014-03-14 14:13:41 -04:00
{
2014-04-23 20:07:00 -04:00
// We may not always need the changelist number if we're not a build machine.
// In local runs, changelist initialization can be really slow!
VerifyChangelistStringAndSetChangelistNumber ( ) ;
2014-03-14 14:13:41 -04:00
}
// Setup branch name
string DepotSuffix = "//depot/" ;
if ( BuildRootP4 . StartsWith ( DepotSuffix ) )
{
BranchName = BuildRootP4 . Substring ( DepotSuffix . Length ) ;
}
else
{
throw new AutomationException ( "Needs update to work with a stream" ) ;
}
if ( String . IsNullOrWhiteSpace ( BranchName ) )
{
throw new AutomationException ( "BUILD FAILED no branch name." ) ;
}
}
LogSettings ( ) ;
}
void LogSettings ( )
{
// Log all vars
const bool bQuiet = true ;
Log . TraceInformation ( "Perforce Environment settings:" ) ;
Log . TraceInformation ( "{0}={1}" , EnvVarNames . P4Port , InternalUtils . GetEnvironmentVariable ( EnvVarNames . P4Port , "" , bQuiet ) ) ;
Log . TraceInformation ( "{0}={1}" , EnvVarNames . User , InternalUtils . GetEnvironmentVariable ( EnvVarNames . User , "" , bQuiet ) ) ;
Log . TraceInformation ( "{0}={1}" , EnvVarNames . Client , InternalUtils . GetEnvironmentVariable ( EnvVarNames . Client , "" , bQuiet ) ) ;
Log . TraceInformation ( "{0}={1}" , EnvVarNames . BuildRootP4 , InternalUtils . GetEnvironmentVariable ( EnvVarNames . BuildRootP4 , "" , bQuiet ) ) ;
Log . TraceInformation ( "{0}={1}" , EnvVarNames . BuildRootEscaped , InternalUtils . GetEnvironmentVariable ( EnvVarNames . BuildRootEscaped , "" , bQuiet ) ) ;
Log . TraceInformation ( "{0}={1}" , EnvVarNames . ClientRoot , InternalUtils . GetEnvironmentVariable ( EnvVarNames . ClientRoot , "" , bQuiet ) ) ;
Log . TraceInformation ( "{0}={1}" , EnvVarNames . Changelist , InternalUtils . GetEnvironmentVariable ( EnvVarNames . Changelist , "" , bQuiet ) ) ;
Log . TraceInformation ( "{0}={1}" , EnvVarNames . LabelToSync , InternalUtils . GetEnvironmentVariable ( EnvVarNames . LabelToSync , "" , bQuiet ) ) ;
Log . TraceInformation ( "{0}={1}" , "P4USER" , InternalUtils . GetEnvironmentVariable ( "P4USER" , "" , bQuiet ) ) ;
Log . TraceInformation ( "{0}={1}" , "P4CLIENT" , InternalUtils . GetEnvironmentVariable ( "P4CLIENT" , "" , bQuiet ) ) ;
Log . TraceInformation ( "LabelPrefix={0}" , LabelPrefix ) ;
}
2014-04-23 20:07:00 -04:00
protected void VerifyChangelistStringAndSetChangelistNumber ( )
{
if ( String . IsNullOrEmpty ( ChangelistStringInternal ) )
{
throw new AutomationException ( "BUILD FAILED null CL " ) ;
}
if ( String . IsNullOrEmpty ( ChangelistStringInternal ) | | ChangelistStringInternal . Length < 2 )
{
2014-04-23 20:10:20 -04:00
throw new AutomationException ( "BUILD FAILED bad CL {0}." , ChangelistStringInternal ) ;
2014-04-23 20:07:00 -04:00
}
ChangelistInternal = Int32 . Parse ( ChangelistStringInternal ) ;
if ( ChangelistInternal < = 10 )
{
throw new AutomationException ( "BUILD FAILED bad CL {0} {1}." , ChangelistStringInternal , ChangelistInternal ) ;
}
ChangelistStringInternal = ChangelistInternal . ToString ( ) ; // Make sure they match
}
2014-03-14 14:13:41 -04:00
}
}