public static void SetContent(this EnvDTE.ProjectItem projectItem, string text) { if (!projectItem.IsOpen) { projectItem.Open(EnvDTE.Constants.vsViewKindCode); } var document = projectItem.Document; if (document != null) { SetContent(document, text); } else { var fileName = projectItem.TryGetFileName(); if (string.IsNullOrEmpty(fileName)) { return; } File.WriteAllText(fileName, text); } }
public static string GetContent(this EnvDTE.ProjectItem projectItem) { try { if (!projectItem.IsOpen) { projectItem.Open(); } var document = projectItem.Document; if (document != null) { return(GetContent((EnvDTE.TextDocument)document.Object("TextDocument"))); } var fileName = projectItem.TryGetFileName(); if (string.IsNullOrEmpty(fileName)) { return(string.Empty); } return(File.ReadAllText(fileName)); } catch (Exception) { return(string.Empty); } }
public WixSourceFile([NotNull] WixProject project, [NotNull] EnvDTE.ProjectItem projectItem) { Project = project; _projectItem = projectItem; try { _xmlFile = _projectItem.GetXmlContent(LoadOptions.PreserveWhitespace); _rawXmlFile = _projectItem.GetXmlContent(LoadOptions.None); } catch { var placeholder = @"<?xml version=""1.0"" encoding=""utf-8""?><Include />"; _xmlFile = XDocument.Parse(placeholder); _rawXmlFile = XDocument.Parse(placeholder); } var root = _xmlFile.Root; _root = root ?? throw new InvalidDataException("Invalid source file: " + projectItem.TryGetFileName()); WixNames = new WixNames(root.GetDefaultNamespace().NamespaceName); _defines = root.Nodes().OfType <XProcessingInstruction>() .Where(p => p.Target.Equals(WixNames.Define, StringComparison.Ordinal)) .Select(p => new WixDefine(this, p)) .ToList(); _componentGroupNodes = root.Descendants(WixNames.ComponentGroupNode) .Select(node => new WixComponentGroupNode(this, node)) .ToList(); _componentNodes = root.Descendants(WixNames.ComponentNode) .Select(node => new WixComponentNode(this, node)) .ToList(); _fileNodes = new List <WixFileNode>(); _fileNodes.AddRange(root .Descendants(WixNames.FileNode) .Select(node => new WixFileNode(this, node, _fileNodes))); _directoryNodes = root .Descendants(WixNames.DirectoryNode) .Select(node => new WixDirectoryNode(this, node)) .Where(node => node.Id != "TARGETDIR") .ToList(); _featureNodes = root .Descendants(WixNames.FeatureNode) .Select(node => new WixFeatureNode(this, node)) .ToList(); var featureNodesLookup = _featureNodes.ToDictionary(item => item.Id); foreach (var featureNode in _featureNodes) { featureNode.BuildTree(featureNodesLookup); } }
public static string GetContent([NotNull] this EnvDTE.ProjectItem projectItem) { Contract.Requires(projectItem != null); Contract.Ensures(Contract.Result <string>() != null); try { if (!projectItem.IsOpen) { projectItem.Open(); } var document = projectItem.Document; if (document != null) { // ReSharper disable once AssignNullToNotNullAttribute return(GetContent((EnvDTE.TextDocument)document.Object("TextDocument"))); } var fileName = projectItem.TryGetFileName(); if (string.IsNullOrEmpty(fileName)) { return(string.Empty); } return(File.ReadAllText(fileName)); } catch (Exception) { return(string.Empty); } }
public WixSourceFile([NotNull] WixProject project, [NotNull] EnvDTE.ProjectItem projectItem) { Project = project; _projectItem = projectItem; _xmlFile = _projectItem.GetXmlContent(LoadOptions.PreserveWhitespace); _rawXmlFile = _projectItem.GetXmlContent(LoadOptions.None); var root = _xmlFile.Root; _root = root ?? throw new InvalidDataException("Invalid source file: " + projectItem.TryGetFileName()); WixNames = new WixNames(root.GetDefaultNamespace().NamespaceName); _defines = root.Nodes().OfType <XProcessingInstruction>() .Where(p => p.Target.Equals(WixNames.Define, StringComparison.Ordinal)) .Select(p => new WixDefine(this, p)) .ToList(); _componentGroupNodes = root.Descendants(WixNames.ComponentGroupNode) // ReSharper disable once AssignNullToNotNullAttribute .Select(node => new WixComponentGroupNode(this, node)) .ToList(); _componentNodes = root.Descendants(WixNames.ComponentNode) // ReSharper disable once AssignNullToNotNullAttribute .Select(node => new WixComponentNode(this, node)) .ToList(); _fileNodes = new List <WixFileNode>(); _fileNodes.AddRange(root .Descendants(WixNames.FileNode) // ReSharper disable once AssignNullToNotNullAttribute .Select(node => new WixFileNode(this, node, _fileNodes))); _directoryNodes = root .Descendants(WixNames.DirectoryNode) // ReSharper disable once AssignNullToNotNullAttribute .Select(node => new WixDirectoryNode(this, node)) .Where(node => node.Id != "TARGETDIR") .ToList(); _featureNodes = root .Descendants(WixNames.FeatureNode) // ReSharper disable once AssignNullToNotNullAttribute .Select(node => new WixFeatureNode(this, node)) .ToList(); // ReSharper disable once PossibleNullReferenceException var featureNodesLookup = _featureNodes.ToDictionary(item => item.Id); foreach (var featureNode in _featureNodes) { featureNode.BuildTree(featureNodesLookup); } }
public WixSourceFile([NotNull] WixProject project, [NotNull] EnvDTE.ProjectItem projectItem) { Contract.Requires(project != null); Contract.Requires(projectItem != null); _project = project; _projectItem = projectItem; _xmlFile = _projectItem.GetXmlContent(LoadOptions.PreserveWhitespace); _rawXmlFile = _projectItem.GetXmlContent(LoadOptions.None); var root = _xmlFile.Root; if (root == null) { throw new InvalidDataException("Invalid source file: " + projectItem.TryGetFileName()); } _root = root; WixNames = new WixNames(_root.GetDefaultNamespace().NamespaceName); _defines = _root.Nodes().OfType <XProcessingInstruction>() .Where(p => p.Target.Equals(WixNames.Define, StringComparison.Ordinal)) .Select(p => new WixDefine(this, p)) .ToList(); _componentGroups = _root.Descendants(WixNames.ComponentGroupNode) .Select(node => new WixComponentGroupNode(this, node)) .ToList(); _fileNodes = new List <WixFileNode>(); _fileNodes.AddRange(_root .Descendants(WixNames.FileNode) .Select(node => new WixFileNode(this, node, _fileNodes))); _directoryNodes = _root .Descendants(WixNames.DirectoryNode) .Select(node => new WixDirectoryNode(this, node)) .Where(node => node.Id != "TARGETDIR") .ToList(); _featureNodes = _root .Descendants(WixNames.FeatureNode) .Select(node => new WixFeatureNode(this, node)) .ToList(); }