// Copyright Epic Games, Inc. All Rights Reserved.
// This software is provided "as-is," without any express or implied warranty.
// In no event shall the author, nor Epic Games, Inc. be held liable for any damages arising from the use of this software.
// This software will not be supported.
// Use at your own risk.
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Reflection;
using EpicGames.Core;
using System.IO;
using System.Collections.Generic;
using UnrealBuildBase;
using Microsoft.Extensions.Logging;
namespace AutomationToolDriver
{
///
/// Main entry point
///
public partial class Program
{
///
/// Parses command line parameter.
///
/// Parameter
/// Recently parsed command
/// Logger for output
/// True if the parameter has been successfully parsed.
private static void ParseParam(string CurrentParam, CommandInfo CurrentCommand, ILogger Logger)
{
if (AutomationToolCommandLine.IsParameterIgnored(CurrentParam))
{
return;
}
bool bGlobalParam = AutomationToolCommandLine.TrySetGlobal(CurrentParam);
// Global value parameters, handled explicitly
string Option_ScriptsForProject = "-ScriptsForProject";
string Option_ScriptDir = "-ScriptDir";
string Option_Telemetry = "-Telemetry";
string Option_WaitForStdStreams = "-WaitForStdStreams";
// The parameter was not found in the list of global parameters, continue looking...
if (CurrentParam.StartsWith(Option_ScriptsForProject + "=", StringComparison.InvariantCultureIgnoreCase))
{
if (AutomationToolCommandLine.IsSetUnchecked(Option_ScriptsForProject))
{
throw new Exception("The -ScriptsForProject argument may only be specified once");
}
string ProjectFileName = CurrentParam.Substring(CurrentParam.IndexOf('=') + 1).Replace("\"", "");
FileReference ProjectReference = NativeProjectsBase.FindProjectFile(ProjectFileName, Logger);
if (ProjectReference != null)
{
AutomationToolCommandLine.SetUnchecked(Option_ScriptsForProject, ProjectReference.FullName);
Logger.LogDebug("Found project file: {0}", ProjectReference.FullName);
}
else if (Path.IsPathFullyQualified(ProjectFileName))
{
throw new Exception($"Project '{ProjectFileName}' does not exist");
}
else
{
throw new Exception($"Project '{ProjectFileName}' does not exist relative to any entries in *.uprojectdirs");
}
}
else if (CurrentParam.StartsWith(Option_ScriptDir + "=", StringComparison.InvariantCultureIgnoreCase))
{
string ScriptDir = CurrentParam.Substring(CurrentParam.IndexOf('=') + 1);
if (Directory.Exists(ScriptDir))
{
List OutAdditionalScriptDirectories = (List)AutomationToolCommandLine.GetValueUnchecked(Option_ScriptDir) ?? new List();
OutAdditionalScriptDirectories.Add(Path.GetFullPath(ScriptDir));
AutomationToolCommandLine.SetUnchecked(Option_ScriptDir, OutAdditionalScriptDirectories);
Logger.LogDebug("Found additional script dir: {0}", Path.GetFullPath(ScriptDir));
}
else
{
DirectoryReference ScriptDirReference = NativeProjectsBase.FindRelativeDirectoryReference(ScriptDir, Logger);
if (ScriptDirReference != null)
{
List OutAdditionalScriptDirectories = (List)AutomationToolCommandLine.GetValueUnchecked(Option_ScriptDir) ?? new List();
OutAdditionalScriptDirectories.Add(ScriptDirReference.FullName);
AutomationToolCommandLine.SetUnchecked(Option_ScriptDir, OutAdditionalScriptDirectories);
Logger.LogDebug("Found additional script dir: {0}", ScriptDirReference.FullName);
}
else if (Path.IsPathFullyQualified(ScriptDir))
{
throw new Exception($"Specified ScriptDir doesn't exist: {ScriptDir}");
}
else
{
throw new Exception($"Specified ScriptDir doesn't exist relative to any entries in *.uprojectdirs: {ScriptDir}");
}
}
}
else if (CurrentParam.StartsWith(Option_Telemetry + "=", StringComparison.InvariantCultureIgnoreCase))
{
string TelemetryPath = CurrentParam.Substring(CurrentParam.IndexOf('=') + 1);
AutomationToolCommandLine.SetUnchecked(Option_Telemetry, TelemetryPath);
}
else if (CurrentParam.StartsWith(Option_WaitForStdStreams + "=", StringComparison.InvariantCultureIgnoreCase))
{
string WaitTime = CurrentParam.Substring(CurrentParam.IndexOf('=') + 1);
AutomationToolCommandLine.SetUnchecked(Option_WaitForStdStreams, WaitTime);
}
else if (CurrentParam.StartsWith("-"))
{
if (CurrentCommand != null)
{
CurrentCommand.Arguments.Add(CurrentParam.Substring(1));
}
else if (!bGlobalParam)
{
throw new Exception($"Unknown parameter {CurrentParam} in the command line that does not belong to any command.");
}
}
else if (CurrentParam.Contains("="))
{
// Environment variable
int ValueStartIndex = CurrentParam.IndexOf('=') + 1;
string EnvVarName = CurrentParam.Substring(0, ValueStartIndex - 1);
if (String.IsNullOrEmpty(EnvVarName))
{
throw new Exception($"Unable to parse environment variable that has no name. Error when parsing command line param {CurrentParam}");
}
string EnvVarValue = CurrentParam.Substring(ValueStartIndex);
Logger.LogDebug($"SetEnvVar {EnvVarName}={EnvVarValue}");
Environment.SetEnvironmentVariable(EnvVarName, EnvVarValue);
}
}
private static string ParseString(string Key, string Value)
{
if (!String.IsNullOrEmpty(Key))
{
if (Value == "true" || Value == "false")
{
return "-" + Key;
}
else
{
string param = "-" + Key + "=";
if (Value.Contains(" "))
{
param += "\"" + Value + "\"";
}
else
{
param += Value;
}
return param;
}
}
else
{
return Value;
}
}
private static string ParseList(string Key, List