Exemplo n.º 1
0
        public void SetFileCopyToOutputDirectory(ProjectFilePath projectFilePath, FilePath filePath)
        {
            var fileProjectFileRelativePath = PathUtilities.GetRelativePath(projectFilePath.Value, filePath.Value);

            this.Logger.LogDebug($"{projectFilePath} - Setting copy to output directory for file:\n{fileProjectFileRelativePath}");

            // Read the project file path in as XML.
            var xmlDoc = new XmlDocument();

            xmlDoc.Load(projectFilePath.Value);

            // Create the new node.
            var copyToOutputDirectoryNode = xmlDoc.CreateElement("CopyToOutputDirectory");

            copyToOutputDirectoryNode.InnerText = "PreserveNewest";

            var noneNode = xmlDoc.CreateElement("None");

            noneNode.AppendChild(copyToOutputDirectoryNode);

            var updateAttributeNode = xmlDoc.CreateAttribute("Update");

            updateAttributeNode.Value = fileProjectFileRelativePath;
            noneNode.Attributes.Append(updateAttributeNode);

            var itemGroupNode = xmlDoc.CreateElement("ItemGroup");

            itemGroupNode.AppendChild(noneNode);

            var projectNode = xmlDoc.ChildNodes[0];

            var firstItemGroup = projectNode.ChildNodes[0];

            projectNode.InsertAfter(itemGroupNode, firstItemGroup);

            xmlDoc.Save(projectFilePath.Value);

            this.Logger.LogInformation($"{projectFilePath} - Set copy to output directory for file:\n{fileProjectFileRelativePath}");
        }