MoveTo is a console program that moves elements in the folder at the command line (workspace tree source location), to the folder given as the command line argument (workspace tree destination location). Features include:
using System.Collections.Generic;
namespace MoveTo
{
class Program
{
#region class variables
private static string[] _skipOver;
#endregion
static int Main(string[] args)
{
bool ret = false;
if (args.Length != 1)
{
Console.WriteLine(@"Example usage: C:\Workspaces\MARS_DEV2\Foo>moveto ..\Bar");
return 1;
}
if (!init()) return 1;
string destfolder = args[0];
string tempfile;
if (store(destfolder, out tempfile))
{
if (ready(destfolder))
{
try
{
AcResult r = AcCommand.run($@"move -l ""{tempfile}"" ""{destfolder}""");
ret = (r.RetVal == 0);
}
catch (AcUtilsException ecx)
{
Console.WriteLine($"AcUtilsException caught in Program.Main{Environment.NewLine}{ecx.Message}");
}
}
}
if (tempfile != null) File.Delete(tempfile);
return ret ? 0 : 1;
}
private static bool store(string destfolder, out string tempfile)
{
tempfile = null;
bool ret = false;
try
{
AcResult r = AcCommand.run("stat -fax *");
if (r.RetVal == 0)
{
string fullpath = Path.GetFullPath(destfolder);
XElement xml = XElement.Parse(r.CmdResult);
IEnumerable<XElement> filter =
from e in xml.Elements(
"element")
where !_skipOver.Any(s => e.Attribute("status").Value.Contains(s)) &&
!fullpath.Equals((string)e.Attribute("location"), StringComparison.OrdinalIgnoreCase)
select e;
tempfile = Path.GetTempFileName();
using (StreamWriter sw = new StreamWriter(tempfile))
{
foreach (XElement e in filter)
sw.WriteLine((string)e.Attribute("location"));
}
FileInfo fi = new FileInfo(tempfile);
ret = fi.Length > 0;
}
}
catch (AcUtilsException ecx)
{
Console.WriteLine($"AcUtilsException caught in Program.store{Environment.NewLine}{ecx.Message}");
}
catch (Exception ecx)
{
Console.WriteLine($"Exception caught in Program.store{Environment.NewLine}{ecx.Message}");
}
return ret;
}
private static bool ready(string dest)
{
bool ret = false;
try
{
if (!Directory.Exists(dest))
{
Directory.CreateDirectory(dest);
AcCommand.run($@"add ""{dest}""");
}
else
{
AcResult r = AcCommand.run($@"stat -fx ""{dest}""");
if (r.RetVal == 0)
{
XElement xml = XElement.Parse(r.CmdResult);
string status = (string)xml.Element("element").Attribute("status");
if (status == "(external)")
AcCommand.run($@"add ""{dest}""");
}
}
ret = true;
}
catch (AcUtilsException ecx)
{
Console.WriteLine($"AcUtilsException caught in Program.ready{Environment.NewLine}{ecx.Message}");
}
catch (Exception ecx)
{
Console.WriteLine($"Exception caught in Program.ready{Environment.NewLine}{ecx.Message}");
}
return ret;
}
private static bool init()
{
try
{
Task<string> prncpl = AcQuery.getPrincipalAsync();
if (String.IsNullOrEmpty(prncpl.Result))
{
Console.WriteLine($"Not logged into AccuRev.{Environment.NewLine}Please login and try again.");
return false;
}
if (!isCurrDirInWSpace())
{
Console.WriteLine($"No workspace found for location {Environment.CurrentDirectory}");
return false;
}
char[] sep = new char[] { };
string temp = AcQuery.getAppConfigSetting<string>("SkipOver").Trim();
if (!String.IsNullOrEmpty(temp))
_skipOver = temp.Split(sep);
else
_skipOver = new string[] { };
}
catch (ConfigurationErrorsException exc)
{
Process currentProcess = Process.GetCurrentProcess();
ProcessModule pm = currentProcess.MainModule;
Console.WriteLine($"Invalid data in {pm.ModuleName}.config{Environment.NewLine}{exc.Message}");
}
catch (Exception ecx)
{
Console.WriteLine($"Exception caught in Program.init{Environment.NewLine}{ecx.Message}");
}
return true;
}
private static bool isCurrDirInWSpace()
{
bool found = false;
try
{
AcResult r = AcCommand.run("info");
if (r.RetVal == 0)
{
using (StringReader sr = new StringReader(r.CmdResult))
{
string line;
char[] sep = new char[] { ':' };
while ((line = sr.ReadLine()) != null)
{
string[] arr = line.Split(sep);
if (arr.Length == 2)
{
if (String.Equals(arr[0], "Workspace/ref"))
{
found = true;
break;
}
}
}
}
}
}
catch (AcUtilsException ecx)
{
Console.WriteLine($"AcUtilsException caught in Program.isCurrDirInWSpace{Environment.NewLine}{ecx.Message}");
}
catch (Exception ecx)
{
Console.WriteLine($"Exception caught in Program.isCurrDirInWSpace{Environment.NewLine}{ecx.Message}");
}
return found;
}
}
}