protected override void ProcessRecord()
{
JavaScriptSerializer jss = new JavaScriptSerializer();
var modules = Sdk.GetModules(GetWorkspaceSetting());
string rawJson = "";
Experiment exp = Sdk.GetExperimentById(GetWorkspaceSetting(), ExperimentId, out rawJson);
dynamic graph = jss.Deserialize<object>(rawJson);
List<string> updatedModules = new List<string>();
foreach (dynamic node in graph["Graph"]["ModuleNodes"])
{
string moduleId = node["ModuleId"];
string familyId = moduleId.Split('.')[1];
if (!modules.Any(m => m.FamilyId.Equals(familyId, StringComparison.InvariantCultureIgnoreCase)))
throw new Exception(string.Format("The module with family id \"{0}\" in the experiment cannot be found in the workspace!", moduleId));
var module = modules.SingleOrDefault(m => m.FamilyId.Equals(familyId, StringComparison.InvariantCultureIgnoreCase));
if (!module.Id.Equals(moduleId, StringComparison.InvariantCultureIgnoreCase))
{
if (string.IsNullOrEmpty(ModuleName) || module.Name.Equals(ModuleName, StringComparison.InvariantCultureIgnoreCase))
{
//needs update
WriteObject(string.Format("Module \"{0}\" with family id of \"{1}\" is updated from batch \"{2}\" to \"{3}\".", module.Name, module.FamilyId, moduleId.Split('.')[2], module.Id.Split('.')[2]));
node["ModuleId"] = module.Id;
updatedModules.Add(module.Name);
}
}
}
if (updatedModules.Count > 0)
{
rawJson = jss.Serialize(graph);
Sdk.SaveExperiment(GetWorkspaceSetting(), exp, rawJson);
}
else
WriteObject("All modules are already up-to-date, so no update is needed.");
}