/// <summary> /// Merge a sequence of DefaultStyles into a single style. /// </summary> /// <param name="styles">Sequence of DefaultStyles.</param> /// <returns>Merged DefaultStyle.</returns> public static DefaultStyle Merge(IEnumerable <DefaultStyle> styles) { var combined = new DefaultStyle(); if (styles != null) { foreach (DefaultStyle style in styles) { combined.Merge(style); } } return(combined); }
public override bool Execute() { Order = 0; Log.LogMessage(MessageImportance.Low, "Merging default styles into generic.xaml."); // Get the original generic.xaml string originalPath = Path.Combine(ProjectDirectory, Path.Combine("themes", "generic.xaml")); if (!File.Exists(originalPath)) { Log.LogError("{0} does not exist!", originalPath); return(false); } Log.LogMessage(MessageImportance.Low, "Found original generic.xaml at {0}.", originalPath); string original = null; Encoding encoding = Encoding.Default; try { using (StreamReader reader = new StreamReader(File.Open(originalPath, FileMode.Open, FileAccess.Read))) { original = reader.ReadToEnd(); encoding = reader.CurrentEncoding; } } catch (Exception ex) { Log.LogErrorFromException(ex); return(false); } // Create the merged generic.xaml List <DefaultStyle> styles = new List <DefaultStyle>(); foreach (ITaskItem item in DefaultStyles) { string path = Path.Combine(ProjectDirectory, item.ItemSpec); if (!File.Exists(path)) { Log.LogWarning("Ignoring missing DefaultStyle {0}.", path); continue; } try { Log.LogMessage(MessageImportance.Low, "Processing file {0}.", item.ItemSpec); styles.Add(DefaultStyle.Load(path)); } catch (Exception ex) { Log.LogErrorFromException(ex); } } string merged = null; try { merged = DefaultStyle.Merge(styles).GenerateXaml(); } catch (InvalidOperationException ex) { Log.LogErrorFromException(ex); return(false); } // Write the new generic.xaml if (original != merged) { Log.LogMessage(MessageImportance.Low, "Writing merged generic.xaml."); try { // Could interact with the source control system / TFS here File.SetAttributes(originalPath, FileAttributes.Normal); Log.LogMessage("Removed any read-only flag for generic.xaml."); File.WriteAllText(originalPath, merged, encoding); Log.LogMessage("Successfully merged generic.xaml."); } catch (Exception ex) { Log.LogErrorFromException(ex); return(false); } } else { Log.LogMessage("Existing generic.xaml was up to date."); } return(true); }