public TextFile(MDTextFile file) { this.file = file; }
void UpdateWebConfigRefs() { var refs = new List <string> (); foreach (var reference in References) { //local copied assemblies are copied to the bin directory so ASP.NET references them automatically if (reference.LocalCopy && (reference.ReferenceType == ReferenceType.Project || reference.ReferenceType == ReferenceType.Assembly)) { continue; } if (string.IsNullOrEmpty(reference.Reference)) { continue; } //these assemblies are referenced automatically by ASP.NET if (IsSystemReference(reference.Reference)) { continue; } //bypass non dotnet projects if ((reference.ReferenceType == ReferenceType.Project) && (!(reference.OwnerProject.ParentSolution.FindProjectByName(reference.Reference) is DotNetProject))) { continue; } refs.Add(reference.Reference); } var webConfig = GetWebConfig(); if (webConfig == null || !File.Exists(webConfig.FilePath)) { return; } var textFile = MonoDevelop.Ide.TextFileProvider.Instance.GetEditableTextFile(webConfig.FilePath); //use textfile API because it's write safe (writes out to another file then moves) if (textFile == null) { textFile = MonoDevelop.Projects.Text.TextFile.ReadFile(webConfig.FilePath); } //can't use System.Web.Configuration.WebConfigurationManager, as it can only access virtual paths within an app //so need full manual handling try { System.Xml.XmlDocument doc = new XmlDocument(); //FIXME: PreserveWhitespace doesn't handle whitespace in attribute lists //doc.PreserveWhitespace = true; doc.LoadXml(textFile.Text); //hunt our way to the assemblies element, creating elements if necessary XmlElement configElement = doc.DocumentElement; if (configElement == null || string.Compare(configElement.Name, "configuration", StringComparison.OrdinalIgnoreCase) != 0) { configElement = (XmlElement)doc.AppendChild(doc.CreateNode(XmlNodeType.Document, "configuration", null)); } XmlElement webElement = GetNamedXmlElement(doc, configElement, "system.web"); XmlElement compilationNode = GetNamedXmlElement(doc, webElement, "compilation"); XmlElement assembliesNode = GetNamedXmlElement(doc, compilationNode, "assemblies"); List <XmlNode> existingAdds = new List <XmlNode> (); foreach (XmlNode node in assembliesNode) { if (string.Compare(node.Name, "add", StringComparison.OrdinalIgnoreCase) == 0) { existingAdds.Add(node); } } //add refs to the doc if they're not in it foreach (string reference in refs) { int index = 0; bool found = false; while (index < existingAdds.Count) { XmlNode node = existingAdds[index]; XmlAttribute att = (XmlAttribute)node.Attributes.GetNamedItem("assembly"); if (att == null) { continue; } string refAtt = att.Value; if (refAtt != null && refAtt == reference) { existingAdds.RemoveAt(index); found = true; break; } else { index++; } } if (!found) { XmlElement newAdd = doc.CreateElement("add"); XmlAttribute newAtt = doc.CreateAttribute("assembly"); newAtt.Value = reference; newAdd.Attributes.Append(newAtt); assembliesNode.AppendChild(newAdd); } } //any nodes that weren't removed from the existingAdds list are old/redundant, so remove from doc foreach (XmlNode node in existingAdds) { assembliesNode.RemoveChild(node); } StringWriter sw = new StringWriter(); XmlTextWriter tw = new XmlTextWriter(sw); tw.Formatting = Formatting.Indented; doc.WriteTo(tw); tw.Flush(); textFile.Text = sw.ToString(); MonoDevelop.Projects.Text.TextFile tf = textFile as MonoDevelop.Projects.Text.TextFile; if (tf != null) { tf.Save(); } } catch (Exception e) { LoggingService.LogWarning("Could not modify application web.config in project " + this.Name, e); } }
/// <summary> /// Reads file from given path /// </summary> /// <param name="sourcePath"></param> /// <returns></returns> public static ITextFile ReadFile(string sourcePath) { return(new TextFile(MDTextFile.ReadFile(sourcePath))); }