93 lines
3.8 KiB
C#
93 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
|
|
namespace Sarand.Net.Standard.Base.App
|
|
{
|
|
//=======================================================================
|
|
/// <summary>
|
|
/// Process definitions found on command line.
|
|
/// <para>Used to automatically run process on start up, and then close application.</para>
|
|
/// <para>Command line examples:</para>
|
|
/// <para>app /Process:ExportData</para>
|
|
/// <para>app /SaProc:ExportData</para>
|
|
/// <para>app /Proc:ProcId=ExportData;User=uat</para>
|
|
/// <para>app /SaProcess:Proc=ExportData;User=uat</para>
|
|
/// </summary>
|
|
public class SacsProcessDefinition
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private static List<String> validProcessPrefix = StringListUtil.Split("Proc,Process,SaProc,SaProcess",",");
|
|
private static List<String> validProcessNameParam = StringListUtil.Split("Proc,Process,ProcId,ProcessId",",");
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SacsProcessDefinition (String commandLineArg)
|
|
{
|
|
// First field should be a valid process prefix.
|
|
if (!IsValidProcessDefinition(commandLineArg)) throw new NotSupportedException("SacsProcessDefinition Invalid process command line arg: '" + commandLineArg + "'");
|
|
|
|
// A single colon is expected.
|
|
String[] fields = commandLineArg.Split(':');
|
|
if (fields.Length != 2) throw new NotSupportedException("SacsProcessDefinition Colon expected in command line arg: '" + commandLineArg + "'");
|
|
|
|
// Extract process params.
|
|
String[] processParams = fields[1].Split(';');
|
|
foreach (String processParam in processParams)
|
|
{
|
|
// Each parameter is either delimited as 'name=value' or is interpretted as the process name if no equals.
|
|
String[] paramValues = processParam.Split('=');
|
|
if (paramValues.Length == 1 || (paramValues.Length == 2 && validProcessNameParam.ContainsCI(paramValues[0])))
|
|
{
|
|
// Parameter is the process name.
|
|
String processName = (paramValues.Length == 1 ? paramValues[0] : paramValues[1]);
|
|
if (!StringUtil.IsEmpty(ProcessName)) throw new NotSupportedException("SacsProcessDefinition Multiple process names in command line arg: '" + commandLineArg + "'");
|
|
ProcessName = processName;
|
|
}
|
|
else
|
|
{
|
|
if (Params.HasValue(paramValues[0])) throw new NotSupportedException("SacsProcessDefinition Duplicate param name '"+ paramValues[0] + "' in command line arg: '" + commandLineArg + "'");
|
|
Params.AddValue(paramValues[0], new DataValue(paramValues[1]));
|
|
}
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public String ProcessName { get; }
|
|
public NamedValue.Map Params { get; } = new NamedValue.Map();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Utilities.
|
|
public static bool IsValidProcessDefinition (String commandLineArg)
|
|
{
|
|
if (StringUtil.IsEmpty(commandLineArg)) return false;
|
|
foreach (String validPrefix in validProcessPrefix)
|
|
{
|
|
if (commandLineArg.StartsWithCI(validPrefix + ":"))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
|
|
//=======================================================================
|
|
public static class SacsProcessDefinitionList
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Locate process in the list.
|
|
public static SacsProcessDefinition GetProcessDefinition (this List<SacsProcessDefinition> values, String name)
|
|
{
|
|
return values.Find(item => item.ProcessName.EqualsCI(name));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
|
|
//=======================================================================
|
|
}
|