void TestExecuteTask(PDictionary input, PropertyListEditorAction action, string entry, string type, string value, PObject expected) { var task = CreateTask <PropertyListEditor> (); task.PropertyList = Path.Combine(Cache.CreateTemporaryDirectory(), "propertyList.plist"); task.Action = action.ToString(); task.Entry = entry; task.Type = type; task.Value = value; input.Save(task.PropertyList); if (expected == null) { Assert.IsFalse(task.Execute(), "Task was expected to fail."); return; } Assert.IsTrue(task.Execute(), "Task was expected to execute successfully."); var output = PObject.FromFile(task.PropertyList); Assert.AreEqual(expected.Type, output.Type, "Task produced the incorrect plist output."); CheckValue(output, expected); }
static void TestExecuteTask(PDictionary input, PropertyListEditorAction action, string entry, string type, string value, PObject expected) { var task = new PropertyListEditor { PropertyList = Path.GetTempFileName(), BuildEngine = new TestEngine(), Action = action.ToString(), Entry = entry, Type = type, Value = value }; input.Save(task.PropertyList); try { if (expected == null) { Assert.IsFalse(task.Execute(), "Task was expected to fail."); return; } Assert.IsTrue(task.Execute(), "Task was expected to execute successfully."); var output = PObject.FromFile(task.PropertyList); Assert.AreEqual(expected.Type, output.Type, "Task produced the incorrect plist output."); CheckValue(output, expected); } finally { File.Delete(task.PropertyList); } }
public override bool Execute() { PropertyListEditorAction action; PObject plist; bool binary; if (!Enum.TryParse(Action, out action)) { Log.LogError(7065, null, $"Unknown PropertyList editor action: {Action}"); return(false); } if (File.Exists(PropertyList)) { try { plist = PObject.FromFile(PropertyList, out binary); } catch (Exception ex) { Log.LogError(7066, PropertyList, $"Error loading '{PropertyList}': {ex.Message}", PropertyList); return(false); } } else { Log.LogMessage(MessageImportance.Low, "File Doesn't Exist, Will Create: {0}", PropertyList); plist = new PDictionary(); binary = false; } switch (action) { case PropertyListEditorAction.Add: Add(plist); break; case PropertyListEditorAction.Clear: Clear(ref plist); break; case PropertyListEditorAction.Delete: Delete(plist); break; case PropertyListEditorAction.Import: Import(plist); break; case PropertyListEditorAction.Merge: Merge(plist); break; case PropertyListEditorAction.Set: Set(plist); break; } if (!Log.HasLoggedErrors) { try { if (plist is PDictionary) { ((PDictionary)plist).Save(PropertyList, true, binary); } else { File.WriteAllText(PropertyList, plist.ToXml()); } } catch (Exception ex) { Log.LogError(7067, PropertyList, $"Error saving '{PropertyList}': {ex.Message}", PropertyList); } } return(!Log.HasLoggedErrors); }
bool Merge(PObject plist) { if (Entry != null) { var path = GetPropertyPath(); var current = plist; PDictionary dict; PObject value; PArray array; int index; int i = 0; while (i < path.Length - 1) { dict = current as PDictionary; array = current as PArray; if (array != null) { if (!int.TryParse(path[i], out index) || index < 0) { Log.LogError(7059, PropertyList, $"Merge: Entry, \"{Entry}\", Contains Invalid Array Index"); return(false); } if (index >= array.Count) { Log.LogError(7060, PropertyList, $"Merge: Entry, \"{Entry}\", Does Not Exist"); return(false); } current = array[index]; } else if (dict != null) { if (!dict.TryGetValue(path[i], out current)) { Log.LogError(7060, PropertyList, $"Merge: Entry, \"{Entry}\", Does Not Exist"); return(false); } } else { Log.LogError(7060, PropertyList, $"Merge: Entry, \"{Entry}\", Does Not Exist"); return(false); } i++; } dict = current as PDictionary; array = current as PArray; PObject root; if (array != null) { if (i > 0 || path[i].Length > 0) { if (!int.TryParse(path[i], out index) || index < 0) { Log.LogError(7059, PropertyList, $"Merge: Entry, \"{Entry}\", Contains Invalid Array Index"); return(false); } if (index >= array.Count) { Log.LogError(7060, PropertyList, $"Merge: Entry, \"{Entry}\", Does Not Exist"); return(false); } root = array[index]; } else { root = array; } try { value = PObject.FromFile(Value); } catch { Log.LogError(7061, PropertyList, $"Merge: Error Reading File: {Value}", Value); return(false); } return(Merge(root, value)); } if (dict != null) { if (i > 0 || path[i].Length > 0) { if (!dict.TryGetValue(path[i], out root)) { Log.LogError(7060, PropertyList, $"Merge: Entry, \"{Entry}\", Does Not Exist"); return(false); } } else { root = dict; } try { value = PObject.FromFile(Value); } catch { Log.LogError(7061, PropertyList, $"Merge: Error Reading File: {Value}", Value); return(false); } return(Merge(root, value)); } Log.LogError(7060, PropertyList, $"Merge: Entry, \"{Entry}\", Does Not Exist"); return(false); } else { PObject value; try { value = PObject.FromFile(Value); } catch { Log.LogError(7061, PropertyList, $"Merge: Error Reading File: {Value}", Value); return(false); } return(Merge(plist, value)); } }