2019-12-26 23:01:54 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2019-07-15 20:50:02 -04:00
2019-07-15 06:43:14 -04:00
using System ;
using System.Text ;
using System.IO ;
2020-12-21 23:07:37 -04:00
using EpicGames.Core ;
2019-07-15 06:43:14 -04:00
using System.Collections.Generic ;
using System.Linq ;
2021-06-11 18:20:44 -04:00
using UnrealBuildBase ;
2019-07-15 06:43:14 -04:00
namespace AutomationTool
{
[Help("Auto-detects P4 settings based on the current path and creates a p4config file with the relevant settings.")]
[Help("setignore", "Adds a P4IGNORE to the default file (Engine/Extras/Perforce/p4ignore)")]
[Help("path=<path>", "Write to a path other than the current directory")]
[Help("p4port=<server:port>", "Optional hint/override of the server to use during lookup")]
[Help("p4user=<server:port>", "Optional hint/override of the username to use during lookup")]
2021-08-02 17:35:28 -04:00
public class P4WriteConfig : BuildCommand
2019-07-15 06:43:14 -04:00
{
public override ExitCode Execute ( )
{
Log . TraceInformation ( "Setting up Perforce environment." ) ;
// User can specify these to help auto detection
string Port = ParseParamValue ( "p4port" , "" ) ;
string User = ParseParamValue ( "p4user" , "" ) ;
bool SetIgnore = ParseParam ( "setignore" ) ;
bool ListOnly = ParseParam ( "listonly" ) ;
string OutputPath = ParseParamValue ( "path" , "" ) ;
// apply any hints
if ( ! string . IsNullOrEmpty ( Port ) )
{
Environment . SetEnvironmentVariable ( EnvVarNames . P4Port , Port ) ;
}
if ( ! string . IsNullOrEmpty ( User ) )
{
Environment . SetEnvironmentVariable ( EnvVarNames . User , User ) ;
}
// try to init P4
try
{
CommandUtils . InitP4Environment ( ) ;
CommandUtils . InitDefaultP4Connection ( ) ;
}
catch ( Exception Ex )
2021-08-02 17:35:28 -04:00
{
Log . TraceError (
2022-03-02 13:56:42 -05:00
"Unable to find matching Perforce info. If the below does not help try P4WriteConfig -p4port=<server:port> and -p4user=<username> to supply more info" ) ;
2019-07-15 06:43:14 -04:00
Log . TraceError ( "{0}" , Ex . Message ) ;
return ExitCode . Error_Arguments ;
}
// store all our settings
2021-08-02 17:35:28 -04:00
StringBuilder P4Config = new StringBuilder ( ) ;
2019-07-15 06:43:14 -04:00
2021-08-02 17:35:28 -04:00
P4Config . AppendLine ( $"P4PORT={P4Env.ServerAndPort}" ) ;
P4Config . AppendLine ( $"P4USER={P4Env.User}" ) ;
P4Config . AppendLine ( $"P4CLIENT={P4Env.Client}" ) ;
2019-07-15 06:43:14 -04:00
if ( SetIgnore )
{
2021-06-11 18:20:44 -04:00
string IgnorePath = Path . Combine ( Unreal . EngineDirectory . ToString ( ) , "Extras" , "Perforce" , "p4ignore" ) ;
2021-08-02 17:35:28 -04:00
P4Config . AppendLine ( $"P4IGNORE={IgnorePath}" ) ;
2019-07-15 06:43:14 -04:00
}
2021-08-02 17:35:28 -04:00
string P4Settings = P4Config . ToString ( ) ;
2019-07-15 06:43:14 -04:00
if ( ! string . IsNullOrEmpty ( OutputPath ) )
{
if ( ! DirectoryExists ( OutputPath ) & & ! FileExists ( OutputPath ) )
{
throw new AutomationException ( "Path {0} does not exist." , OutputPath ) ;
}
}
else
{
OutputPath = Environment . CurrentDirectory ;
}
if ( ! File . Exists ( OutputPath ) )
{
OutputPath = Path . Combine ( OutputPath , "p4config.txt" ) ;
}
2021-08-02 17:35:28 -04:00
Console . WriteLine ( "***\nWriting\n{0}to - {1}\n***" , P4Settings , OutputPath ) ;
2019-07-15 06:43:14 -04:00
if ( ! ListOnly )
{
File . WriteAllText ( OutputPath , P4Settings ) ;
string OutputFile = Path . GetFileName ( OutputPath ) ;
Log . TraceInformation ( "Wrote P4 settings to {0}" , OutputPath ) ;
P4 . P4 ( string . Format ( "set P4CONFIG={0}" , OutputFile ) ) ;
Log . TraceInformation ( "set P4CONFIG={0}" , OutputFile ) ;
}
else
{
Log . TraceInformation ( "Skipped write" ) ;
}
return ExitCode . Success ;
2021-08-02 17:35:28 -04:00
}
2019-07-15 06:43:14 -04:00
}
}