191 lines
6.1 KiB
C#
191 lines
6.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using NLog;
|
|
using Raytheon.Common;
|
|
|
|
namespace ProgramPostBuild
|
|
{
|
|
/// <summary>
|
|
/// Perform any needed function after Program project is built
|
|
/// </summary>
|
|
internal class Program
|
|
{
|
|
private static ILogger _logger;
|
|
private static object syncObj = new object();
|
|
private static Dictionary<string, string> dllNameDict = new Dictionary<string, string>();
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
_logger = LogManager.GetCurrentClassLogger();
|
|
|
|
ModifyDllPathsInTestStandSequences();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Modify paths to DLLs in TestStand sequence files
|
|
/// </summary>
|
|
static void ModifyDllPathsInTestStandSequences()
|
|
{
|
|
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
IConfigurationFile config = new ConfigurationFile(Path.Combine(assemblyFolder, "config.ini"));
|
|
|
|
string dllPath = config.ReadValue("TESTSTAND", "DLL_PATH");
|
|
dllPath = Path.GetFullPath(Path.Combine(assemblyFolder, dllPath));
|
|
string testStandSequencePath = config.ReadValue("TESTSTAND", "TESTSTAND_SEQUENCE_PATH");
|
|
testStandSequencePath = Path.GetFullPath(Path.Combine(assemblyFolder, testStandSequencePath));
|
|
|
|
if (Directory.Exists(dllPath) && Directory.Exists(testStandSequencePath))
|
|
{
|
|
// get all DLL files
|
|
string[] dllFileArray = Directory.GetFiles(dllPath, "*.dll");
|
|
|
|
// get all teststand sequence files
|
|
List<string> seqFileList = Directory.GetFiles(testStandSequencePath, "*.seq", SearchOption.AllDirectories).ToList();
|
|
|
|
int maxTaskNum = Environment.ProcessorCount;
|
|
List<Task> taskList = new List<Task>();
|
|
|
|
while (seqFileList.Count > 0)
|
|
{
|
|
if (taskList.Count < maxTaskNum)
|
|
{
|
|
string seqFilePath = seqFileList[0];
|
|
taskList.Add(Task.Factory.StartNew(() => ModifyDllPathsInTestStandSequence(seqFilePath, dllFileArray)));
|
|
seqFileList.RemoveAt(0);
|
|
}
|
|
else
|
|
{
|
|
int index = Task.WaitAny(taskList.ToArray());
|
|
taskList.RemoveAt(index);
|
|
}
|
|
}
|
|
|
|
Task.WaitAll(taskList.ToArray());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Modify paths to DLLs in a TestStand sequence
|
|
/// </summary>
|
|
static void ModifyDllPathsInTestStandSequence(string seqFilePath, string[] dllFileArray)
|
|
{
|
|
string seqNewContent = "";
|
|
FileStream fs = new FileStream(seqFilePath, FileMode.Open, FileAccess.Read);
|
|
StreamReader reader = new StreamReader(fs, Encoding.Default);
|
|
|
|
string line;
|
|
Console.WriteLine($"Processing {seqFilePath}");
|
|
while ((line = reader.ReadLine()) != null)
|
|
{
|
|
Match regexMatch;
|
|
|
|
regexMatch = Regex.Match(line, @"<value>.+\\([^\.]+\.dll)</value>", RegexOptions.IgnoreCase);
|
|
|
|
if (regexMatch.Success)
|
|
{
|
|
string dllFile = regexMatch.Groups[1].Value;
|
|
|
|
bool dllExists = false;
|
|
string[] dllStringArray = null;
|
|
int index = 0;
|
|
string[] seqStringArray = seqFilePath.Split('\\');
|
|
|
|
lock (syncObj)
|
|
{
|
|
if (dllNameDict.ContainsKey(dllFile))
|
|
{
|
|
dllStringArray = dllNameDict[dllFile].Split('\\');
|
|
dllExists = true;
|
|
}
|
|
}
|
|
|
|
if (!dllExists)
|
|
{
|
|
foreach (string dllFilePath in dllFileArray)
|
|
{
|
|
if (Regex.IsMatch(dllFilePath, Regex.Escape(dllFile) + @"$", RegexOptions.IgnoreCase))
|
|
{
|
|
lock (syncObj)
|
|
{
|
|
dllStringArray = dllFilePath.Split('\\');
|
|
|
|
if (!dllNameDict.ContainsKey(dllFile))
|
|
{
|
|
dllNameDict[dllFile] = dllFilePath;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
while (String.Equals(dllStringArray[index], seqStringArray[index], StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
index++;
|
|
}
|
|
|
|
int numGoBacks = seqStringArray.Length - (index + 1);
|
|
|
|
string goBackString = String.Empty;
|
|
for (int i = 1; i <= numGoBacks; i++)
|
|
{
|
|
if (!String.IsNullOrEmpty(goBackString))
|
|
goBackString += @"\";
|
|
goBackString += "..";
|
|
}
|
|
|
|
string goForwardString = String.Empty;
|
|
|
|
for (int i = index; i < dllStringArray.Length; i++)
|
|
{
|
|
if (!String.IsNullOrEmpty(goForwardString))
|
|
goForwardString += @"\";
|
|
goForwardString += dllStringArray[i];
|
|
}
|
|
|
|
string newRelPath = Path.Combine(goBackString, goForwardString);
|
|
|
|
line = Regex.Replace(line, @"<value>(.+\\[^\.]+\.dll)</value>", "<value>" + newRelPath + "</value>", RegexOptions.IgnoreCase);
|
|
|
|
}
|
|
|
|
seqNewContent += line + "\r\n";
|
|
}
|
|
|
|
reader.Close();
|
|
fs.Close();
|
|
|
|
FileStream fs2 = null;
|
|
StreamWriter writer = null;
|
|
try
|
|
{
|
|
fs2 = new FileStream(seqFilePath, FileMode.Create, FileAccess.ReadWrite);
|
|
writer = new StreamWriter(fs2, Encoding.Default);
|
|
writer.Write(seqNewContent);
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
if (writer != null)
|
|
{
|
|
writer.Close();
|
|
fs2.Close();
|
|
}
|
|
}
|
|
|
|
Console.WriteLine($"Completed {seqFilePath}");
|
|
}
|
|
}
|
|
}
|