private void ProcessElement(ApplyElementSubNode applyElementSubNode, bool result) { if (result) { foreach (var child in new Queue <XNode>(applyElementSubNode.Element.Nodes())) { child.Remove(); applyElementSubNode.ParentApplyElement.AddBeforeSelf(child); } } else { switch (applyElementSubNode.OnNotAppliedAction) { case OnNotAppliedAction.Remove: applyElementSubNode.Element.Remove(); break; case OnNotAppliedAction.CommentOut: using (var ms = new MemoryStream()) using (var writer = new XmlTextWriter(ms, Encoding.UTF8)) using (var reader = new StreamReader(ms)) { foreach (var child in new Queue <XNode>(applyElementSubNode.Element.Nodes())) { child.WriteTo(writer); child.Remove(); } writer.Flush(); ms.Position = 0; var commentedOutNode = new XComment(reader.ReadToEnd().Trim()); applyElementSubNode.ParentApplyElement.AddBeforeSelf(commentedOutNode); if (!applyElementSubNode.OnCommentedOutComment.IsNullOrEmpty()) { var commentForCommentedOutNode = new XComment(" " + applyElementSubNode.OnCommentedOutComment + " "); commentedOutNode.AddBeforeSelf(commentForCommentedOutNode); commentForCommentedOutNode.AddAfterSelf(new XText(Environment.NewLine)); commentedOutNode.AddAfterSelf(new XText(Environment.NewLine)); commentedOutNode.AddAfterSelf(new XText(Environment.NewLine)); } } break; default: throw new NotSupportedException("OnNotAppliedAction not supported: " + applyElementSubNode.OnNotAppliedAction); } } }
/// <summary> Saves the Project and also preserves any <c> Keepers </c> - code that needs rescuing from the Link Zone. </summary> internal void Save() { string docString = DestProjXdoc.ToString().ToLower(); if (Keepers.Any()) { var newItemGroup = new XElement(Settings.MSBuild + "ItemGroup"); foreach (XElement keeper in Keepers) { string keeperString = keeper.FirstAttribute.Value.ToLower(); if (!docString.Contains(keeperString)) { newItemGroup.Add(keeper); Log.WriteLine("Rescued: " + keeper.ToString().Replace("xmlns=\"" + Settings.MSBuild + "\"", ""), ConsoleColor.White, ConsoleColor.DarkBlue); //Log.WriteLine("keeperString: "+ keeperString); } else { Log.WriteLine("Skipped duplicating: " + keeperString, ConsoleColor.White, ConsoleColor.DarkCyan); } } // Log.WriteLine("DocString" +docString); EndPlaceHolder.AddAfterSelf(newItemGroup); // move the keepers out of the Link zone. } DestProjXdoc.Save(DestProjAbsolutePath); Log.WriteLine("Saved: " + DestProjAbsolutePath, ConsoleColor.Green); }
/// <summary> Preserves any <c> Keepers </c> - code that needs rescuing from the Link Zone. </summary> /// <param name="exclusionsList"> </param> internal void PreserveKeepersAndReport(List <string> exclusionsList) { string docString = DestProjXdoc.ToString().ToLower(); if (Keepers?.Any() ?? false) { var newItemGroup = new XElement(Settings.MSBuild + "ItemGroup"); newItemGroup?.Add(new XComment("Code Linker moved these here from inside the link zone because they were not re-linked")); newItemGroup?.Add(new XComment("You may wish to delete or un-comment these")); foreach (XElement keeper in Keepers) { if (keeper?.FirstAttribute == null) { continue; } string keeperString = keeper.FirstAttribute.Value.ToLower(); string keeperCommentString = keeper.ToString().Replace("xmlns=\"" + Settings.MSBuild + "\"", ""); if (!docString.Contains(keeperString) && (exclusionsList == null || !exclusionsList.Any(e => e?.Contains(keeperString) ?? false))) { XAttribute attrib = keeper.Attribute("Include") ?? keeper.Attribute("Exclude"); if (attrib == null) { continue; // these are not the droids } string originalSourcePath = attrib.Value; if (originalSourcePath.StartsWith("..", StringComparison.Ordinal)) // the file is outside the project tree, it is most likely an intruder { newItemGroup.Add(new XComment(keeperCommentString)); Log.WriteLine("[Warning] Rescued this from inside the link zone. Review it, you may want to uncomment it or delete it. ", ConsoleColor.Red, ConsoleColor.DarkBlue); Log.WriteLine(keeperCommentString, ConsoleColor.Red, ConsoleColor.DarkBlue); } else // the file is inside the project tree, it is most likely an keeper { newItemGroup.Add(keeper); Log.WriteLine("[Warning] Rescued this from inside the link zone. Review it, you may want to delete it " + keeper.ToString().Replace("xmlns=\"" + Settings.MSBuild + "\"", ""), ConsoleColor.Red, ConsoleColor.DarkBlue); } } else { Log.WriteLine("Skipped duplicating: " + keeper.FirstAttribute.Value, ConsoleColor.DarkGray); } } // Log.WriteLine("DocString" +docString); EndPlaceHolder.AddAfterSelf(newItemGroup); // move the keepers out of the Link zone. } }
/// <summary> Saves the Project and also preserves any <c>Keepers</c> - code that needs rescuing from the Link Zone. </summary> internal void Save() { if (Keepers.Any()) { XElement newItemGroup = new XElement(Settings.MSBuild + "ItemGroup"); foreach (XElement keeper in Keepers) { newItemGroup.Add(keeper); Log.WriteLine("Rescued: " + keeper.ToString().Replace("xmlns=\"" + Settings.MSBuild.ToString() + "\"", "")); } EndPlaceHolder.AddAfterSelf(newItemGroup); // move the keepers out of the Link zone. } DestProjXdoc.Save(DestProjAbsolutePath); Log.WriteLine("Saved: " + DestProjAbsolutePath); }