static int Main(string[] args)
{
try
{
//Parse cmd-line args
List<XPathRegexReplacement> xPathRegexReplacements = new List<XPathRegexReplacement>();
string masterConfigFile = null;
string mergeFromConfigFile = null;
string optionalCode = null;
bool mWrittenSpecified = false;
int i = 0;
while (i < args.Length)
{
string arg = args[i];
switch (arg)
{
case MergeFromConfigFileFlag:
if (mergeFromConfigFile != null)
{
throw new ApplicationException("More than one -m arg specified");
}
i++;
if (i >= args.Length)
{
throw new ApplicationException(MergeFromConfigFileFlag + " requires following arg");
}
mergeFromConfigFile = args[i];
i++;
break;
//option for specifying a custom selector
case OptionalFlag:
if (optionalCode != null)
{
throw new ApplicationException("More than one -o arg specified");
}
i++;
if (i >= args.Length)
{
throw new ApplicationException(OptionalFlag + " requires following arg");
}
optionalCode = args[i];
i++;
break;
case XPathRegexReplacementFlag:
i++;
if (i >= args.Length)
{
throw new ApplicationException(XPathRegexReplacementFlag + " requires at least 2 following args");
}
string xPath = args[i];
i++;
if (i >= args.Length)
{
throw new ApplicationException(XPathRegexReplacementFlag + " requires at least 2 following args");
}
string replaceWith = args[i];
i++;
Regex replacePattern = null;
if (i < args.Length)
{
if (!IsFlag(args[i]))
{
replacePattern = new Regex(args[i], RegexOptions.IgnoreCase);
i++;
}
}
xPathRegexReplacements.Add(new XPathRegexReplacement(xPath, replaceWith, replacePattern));
break;
//the replacement pattern is specified
case XPathRegexReplacementFileFlag:
i++;
if (i >= args.Length)
{
throw new ApplicationException(XPathRegexReplacementFileFlag + " requires 1 following args");
}
string filename = args[i];
xPathRegexReplacements.AddRange(XPathFile.Load(filename));
i++;
break;
case MWrittenFlag:
mWrittenSpecified = true;
i++;
break;
default:
if (masterConfigFile != null)
{
throw new ApplicationException("More than one masterConfigFile specified");
}
masterConfigFile = args[i];
i++;
break;
}
}
if (masterConfigFile == null)
{
throw new ApplicationException("Required masterConfigFile param not specified");
}
if (mWrittenSpecified && mergeFromConfigFile == null)
{
throw new ApplicationException("mergeFromConfigFile is required when " + MWrittenFlag + " is specified.");
}
//Merge mergeFromConfigFile if one specified
bool mergedFromExists = (mergeFromConfigFile != null && File.Exists(mergeFromConfigFile));
ConfigFileManager config = new ConfigFileManager(masterConfigFile, mergeFromConfigFile,
mWrittenSpecified, optionalCode);
if (mergedFromExists)
{
Console.WriteLine("Merged existing '" + mergeFromConfigFile + "' settings with '" + masterConfigFile + "'");
}
//Process each specified XPathRegexReplacement
foreach (XPathRegexReplacement repl in xPathRegexReplacements)
{
string[] newValues = null;
try
{
newValues = config.ReplaceXPathValues(repl.XPath, repl.ReplaceWith, repl.ReplacePattern);
}
catch (Exception ex)
{
Exception newEx = new Exception(string.Format("XPath {0} failed with message: {1}",
repl.XPath, ex.Message), ex);
}
if (newValues != null) LogToConsole(newValues);
}
//Save changes
config.Save();
if (mWrittenSpecified)
{
Console.WriteLine("Saved changes to '" + mergeFromConfigFile);
}
else
{
Console.WriteLine("Saved changes to '" + masterConfigFile);
}
return 0; //ok
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("**ERROR: " + ex);
Console.ResetColor();
return 1; //error
}
}