118 lines
4.5 KiB
C#
118 lines
4.5 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.Windows.Forms;
|
|||
|
|
|
|||
|
|
namespace DTS.Common.Utilities
|
|||
|
|
{
|
|||
|
|
public static class TestRenamer
|
|||
|
|
{
|
|||
|
|
private static IEnumerable<string> GetAllDirectories(string dir)
|
|||
|
|
{
|
|||
|
|
return Directory.GetDirectories(dir, "*", SearchOption.AllDirectories);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//FB 29992 imported from rename tool , changed to have bothh rename ID and test setup name
|
|||
|
|
//in one method
|
|||
|
|
public static bool RenameTest(string origTestFolder, string newTestName, string oldTestName, string dtsFileFolder, string newTestId, string oldTestId, Action<int> progressFunc)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (newTestName == oldTestName && newTestId == oldTestId)
|
|||
|
|
{
|
|||
|
|
//No need to rename since it's same
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (string.IsNullOrEmpty(origTestFolder)
|
|||
|
|
|| string.IsNullOrEmpty(newTestName)) { return false; }
|
|||
|
|
|
|||
|
|
var eventPath = Directory.GetParent(dtsFileFolder);
|
|||
|
|
var eventNumber = eventPath.FullName.Split('\\').LastOrDefault();
|
|||
|
|
|
|||
|
|
var path = Directory.GetParent(Directory.GetParent(origTestFolder).FullName).FullName;
|
|||
|
|
string newTestFolder = Path.Combine(path, newTestName, newTestId);
|
|||
|
|
|
|||
|
|
if (Directory.Exists(newTestFolder))
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//Create directories
|
|||
|
|
foreach (var d in GetAllDirectories(origTestFolder))
|
|||
|
|
{
|
|||
|
|
if (d.Contains("_Event Number") && !d.Contains(eventNumber))
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
Directory.CreateDirectory(d.Replace(origTestFolder, newTestFolder));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//Copy files
|
|||
|
|
|
|||
|
|
var allFiles = Directory.GetFiles(origTestFolder, "*.*", SearchOption.AllDirectories);
|
|||
|
|
var currentFile = 0;
|
|||
|
|
|
|||
|
|
foreach (var f in allFiles)
|
|||
|
|
{
|
|||
|
|
currentFile++;
|
|||
|
|
|
|||
|
|
if (f.Contains("_Event Number") && !f.Contains(eventNumber))
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var fullPath = f.Replace(origTestFolder, newTestFolder);
|
|||
|
|
var originalFile = Path.GetFileName(fullPath);
|
|||
|
|
var updatedFile = originalFile.Replace(oldTestId, newTestId);
|
|||
|
|
fullPath = Path.Combine(fullPath.Remove(fullPath.LastIndexOf('\\')), updatedFile);
|
|||
|
|
|
|||
|
|
File.Copy(f, fullPath, true);
|
|||
|
|
|
|||
|
|
if (fullPath.EndsWith(".dts"))
|
|||
|
|
{
|
|||
|
|
FindandReplace(fullPath, string.Concat("<Test Id=\"", oldTestName, "\""), string.Concat("<Test Id=\"", newTestName, "\""));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var testIdNoEvent = oldTestId.Replace(eventNumber, "");
|
|||
|
|
|
|||
|
|
if (fullPath.EndsWith(string.Concat(oldTestId, ".xml"))
|
|||
|
|
|| fullPath.EndsWith(string.Concat(newTestId, ".xml"))
|
|||
|
|
|| fullPath.EndsWith(string.Concat(testIdNoEvent, ".xml")))
|
|||
|
|
{
|
|||
|
|
FindandReplace(fullPath, string.Concat("<SetupName>", oldTestName), string.Concat("<SetupName>", newTestName));
|
|||
|
|
var xmlFile = string.Concat(newTestId, ".xml");
|
|||
|
|
if (originalFile != xmlFile)
|
|||
|
|
{
|
|||
|
|
var newFullPath = Path.Combine(fullPath.Remove(fullPath.LastIndexOf('\\')), xmlFile);
|
|||
|
|
File.Move(fullPath, newFullPath);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var progress = currentFile * 100 / allFiles.Count();
|
|||
|
|
if (progress < 0) { progress = 0; }
|
|||
|
|
else if (progress > 100) { progress = 100; }
|
|||
|
|
progressFunc?.Invoke(progress);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static void FindandReplace(string path, string oldText, string newText)
|
|||
|
|
{
|
|||
|
|
string text = File.ReadAllText(path);
|
|||
|
|
text = text.Replace(oldText, newText);
|
|||
|
|
File.WriteAllText(path, text);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|