using System.Collections.Generic;
namespace XLinked
{
class Program
{
#region class variables
private static AcDepots _depots;
private static Dictionary<AcDepot, HashSet<XElement>> _map = new Dictionary<AcDepot, HashSet<XElement>>();
private static XNodeEqualityComparer _comparer = new XNodeEqualityComparer();
private static readonly object _locker = new object();
#endregion
static int Main(string[] args)
{
if (!init()) return 1;
Task<bool> xini = xlinkedAsync();
if (!xini.Result) return 1;
return report() ? 0 : 1;
}
private static async Task<bool> xlinkedAsync()
{
List<Task<bool>> tasks = new List<Task<bool>>(_depots.Count);
foreach (AcDepot
depot in _depots)
tasks.Add(initAsync(depot));
bool[] arr = await Task.WhenAll(tasks);
return (arr != null && arr.All(n => n == true));
}
private static async Task<bool> initAsync(AcDepot depot)
{
bool ret = false;
try
{
int num = depot.Streams.Count();
List<Task<AcResult>> tasks = new List<Task<AcResult>>(num);
foreach (AcStream
stream in depot.Streams)
tasks.Add(AcCommand.runAsync($@"stat -s ""{stream}"" -a -fkvx"));
HashSet<XElement> hset = new HashSet<XElement>(_comparer);
while (tasks.Count > 0)
{
Task<AcResult> r = await Task.WhenAny(tasks);
tasks.Remove(r);
if (r == null || r.Result.RetVal != 0) return false;
XElement xml = XElement.Parse(r.Result.CmdResult);
foreach (XElement e in xml.Elements("element")
.Where(n => (string)n.Attribute("xlinked") != null &&
_etypes.Any(t => t == n.acxType("elemType"))))
{
hset.Add(e);
}
}
lock (_locker) { _map.Add(depot, hset); }
ret = true;
}
catch (AcUtilsException exc)
{
AcDebug.Log($"AcUtilsException caught and logged in Program.initAsync{Environment.NewLine}{exc.Message}");
}
catch (Exception ecx)
{
AcDebug.Log($"Exception caught and logged in Program.initAsync{Environment.NewLine}{ecx.Message}");
}
return ret;
}
private static bool report()
{
bool ret = false;
try
{
foreach (KeyValuePair<AcDepot, HashSet<XElement>> pair in _map.OrderBy(n => n.Key))
{
Console.WriteLine(pair.Key);
foreach (XElement e in pair.Value
.OrderBy(n => ((string)n.Attribute("namedVersion"))
.Substring(0, ((string)n.Attribute("namedVersion")).IndexOf('\\')))
.ThenBy(n => (string)n.Attribute("location")))
{
string mtime = e.acxTime("modTime") == null ? String.Empty :
"Last modified: " + e.acxTime("modTime").ToString() + " ";
Console.WriteLine($"\tEID: {(string)e.Attribute("id")} {{{(string)e.Attribute("elemType")}}} " +
$"{(string)e.Attribute("location")}{Environment.NewLine}" +
$"\t\t{mtime}{(string)e.Attribute("namedVersion")} {(string)e.Attribute("status")}");
}
}
ret = true;
}
catch (Exception ecx)
{
AcDebug.Log($"Exception caught and logged in Program.report{Environment.NewLine}{ecx.Message}");
}
return ret;
}
private static bool init()
{
if (!AcDebug.initAcLogging())
{
Console.WriteLine("Logging support initialization failed.");
return false;
}
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AcDebug.unhandledException);
Task<string> prncpl = AcQuery.getPrincipalAsync();
if (String.IsNullOrEmpty(prncpl.Result))
{
AcDebug.Log($"Not logged into AccuRev.{Environment.NewLine}Please login and try again.");
return false;
}
if (!initAppConfigData()) return false;
_depots = new AcDepots(dynamicOnly: true);
Task<bool> dini = _depots.initAsync();
if (!dini.Result)
{
AcDebug.Log($"Depots list initialization failed. See log file:{Environment.NewLine}" +
$"{AcDebug.getLogFile()}");
return false;
}
return true;
}
private static bool initAppConfigData()
{
bool ret = false;
try
{
string[] arr = AcQuery.getAppConfigSetting<string>("ElementTypes")
.Split(',').Select(s => s.Trim()).ToArray();
_etypes = Array.ConvertAll(arr, new Converter<string, ElementType>(n =>
ret = true;
}
catch (ConfigurationErrorsException exc)
{
Process currentProcess = Process.GetCurrentProcess();
ProcessModule pm = currentProcess.MainModule;
AcDebug.Log($"Invalid data in {pm.ModuleName}.config{Environment.NewLine}{exc.Message}");
}
return ret;
}
}
}