public void UpdateResultsJson(
MgaFCO singleFco,
string OutputSubDir)
{
string jsonFile = Path.Combine(this.GetResultsFolder(), "results.metaresults.json");
AVM.DDP.MetaResults results = null;
bool createdNew;
using (System.Threading.Mutex jsonFileMutex = new System.Threading.Mutex(false, "results_metaresults_mutex", out createdNew))
{
jsonFileMutex.WaitOne();
try
{
if (File.Exists(jsonFile))
{
string content = "";
using (StreamReader reader = new StreamReader(jsonFile))
{
content = reader.ReadToEnd();
}
try
{
results = JsonConvert.DeserializeObject<AVM.DDP.MetaResults>(content);
}
catch (Newtonsoft.Json.JsonReaderException ex)
{
// Will this cause problems if we run it from the job manager??? Should we only log it istead?
throw new Exception(string.Format("{0} file is probably malformed. Not a valid json. {1}{2}", Path.GetFullPath(jsonFile), Environment.NewLine, ex.Message));
}
// TODO: remove broken links
}
else
{
results = new AVM.DDP.MetaResults();
}
AVM.DDP.MetaResults.Result thisResult = new AVM.DDP.MetaResults.Result();
thisResult.Summary = MakeRelativePath(
Path.GetDirectoryName(jsonFile),
Path.Combine(OutputSubDir, AVM.DDP.MetaTBManifest.TESTBENCH_FILENAME)).Replace('\\', '/');
thisResult.Time = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
thisResult.TestBench = singleFco.RegistryValue["TestBenchUniqueName"] + ".testbench.json";
Func<MgaFCO, CyPhy.TestBenchType> cast;
if (TestbenchAndCompositeTypes.TryGetValue(singleFco.Meta.Name, out cast))
{
CyPhy.TestBenchType testBench = cast(singleFco);
var tlsut = testBench.Children.TopLevelSystemUnderTestCollection.FirstOrDefault();
if (tlsut != null)
{
if (tlsut.AllReferred is CyPhy.ComponentAssembly)
{
var cfg = tlsut.Referred.ComponentAssembly;
var cid = cfg.Attributes.ConfigurationUniqueID;
//this.ConfigurationUniqueID = cid;
if (string.IsNullOrWhiteSpace(cid))
{
cid = Guid.NewGuid().ToString("B");
cfg.Attributes.ConfigurationUniqueID = cid;
}
if (!string.IsNullOrEmpty(cid))
{
try
{
Guid guid = new Guid(cid);
thisResult.DesignID = guid.ToString("B");
}
catch (System.FormatException ex)
{
Trace.TraceError("{0} is not a vaild GUID.", cid);
Trace.TraceError(ex.ToString());
}
}
}
}
}
results.Results.Add(thisResult);
var dirname = Path.GetDirectoryName(jsonFile);
if (Directory.Exists(dirname) == false)
{
Directory.CreateDirectory(dirname);
}
using (StreamWriter writer = new StreamWriter(jsonFile))
{
writer.WriteLine(JsonConvert.SerializeObject(results, Newtonsoft.Json.Formatting.Indented));
}
}
finally
{
jsonFileMutex.ReleaseMutex();
jsonFileMutex.Dispose();
}
}
}