Exemplo n.º 1
0
        public virtual void RevertFile(IPackageFile file, string targetPath, IEnumerable <IPackageFile> matchingFiles, IProjectSystem projectSystem)
        {
            XElement  xml      = GetXml(file, projectSystem);
            XDocument document = XmlUtility.GetOrCreateDocument(xml.Name, projectSystem, targetPath);

            document.Root.Except(xml.Except(Enumerable.Aggregate <XElement, XElement>(from f in matchingFiles select GetXml(f, projectSystem), new XElement(xml.Name), (left, right) => left.MergeWith(right, this._nodeActions))));
            using (Stream stream = projectSystem.CreateFile(targetPath))
            {
                document.Save(stream);
            }
        }
Exemplo n.º 2
0
        private static void PerformXdtTransform(IPackageFile file, string targetPath, IProjectSystem projectSystem)
        {
            if (projectSystem.FileExists(targetPath))
            {
                string content = Preprocessor.Process(file, projectSystem);

                try
                {
                    using (var transformation = new XmlTransformation(content, isTransformAFile: false, logger: null))
                    {
                        using (var document = new XmlTransformableDocument())
                        {
                            document.PreserveWhitespace = true;

                            // make sure we close the input stream immediately so that we can override
                            // the file below when we save to it.
                            using (var inputStream = projectSystem.OpenFile(targetPath))
                            {
                                document.Load(inputStream);
                            }

                            bool succeeded = transformation.Apply(document);
                            if (succeeded)
                            {
                                using (var memoryStream = new MemoryStream())
                                {
                                    // save the result into a memoryStream first so that if there is any
                                    // exception during document.Save(), the original file won't be truncated.
                                    document.Save(memoryStream);
                                    memoryStream.Seek(0, SeekOrigin.Begin);
                                    using (var fileStream = projectSystem.CreateFile(targetPath))
                                    {
                                        memoryStream.CopyTo(fileStream);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw new InvalidDataException(
                              String.Format(
                                  CultureInfo.CurrentCulture,
                                  NuGetResources.XdtError + " " + exception.Message,
                                  targetPath,
                                  projectSystem.ProjectName),
                              exception);
                }
            }
        }
Exemplo n.º 3
0
        private static void PerformXdtTransform(IPackageFile file, string targetPath, IProjectSystem projectSystem)
        {
            if (projectSystem.FileExists(targetPath))
            {
                string content = Preprocessor.Process(file, projectSystem);

                try
                {
                    using (var transformation = new XmlTransformation(content, isTransformAFile: false, logger: null))
                    {
                        using (var document = new XmlTransformableDocument())
                        {
                            document.PreserveWhitespace = true;

                            // make sure we close the input stream immediately so that we can override 
                            // the file below when we save to it.
                            using (var inputStream = projectSystem.OpenFile(targetPath))
                            {
                                document.Load(inputStream);
                            }

                            bool succeeded = transformation.Apply(document);
                            if (succeeded)
                            {
                                using (var memoryStream = new MemoryStream())
                                {
                                    // save the result into a memoryStream first so that if there is any
                                    // exception during document.Save(), the original file won't be truncated.
                                    document.Save(memoryStream);
                                    memoryStream.Seek(0, SeekOrigin.Begin);
                                    using (var fileStream = projectSystem.CreateFile(targetPath))
                                    {
                                        memoryStream.CopyTo(fileStream);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw new InvalidDataException(
                        String.Format(
                            CultureInfo.CurrentCulture, 
                            NuGetResources.XdtError + " " + exception.Message,
                            targetPath, 
                            projectSystem.ProjectName), 
                        exception);
                }
            }
        }
Exemplo n.º 4
0
        public virtual void RevertFile(IPackageFile file, string targetPath, IEnumerable<IPackageFile> matchingFiles, IProjectSystem projectSystem)
        {
            // Get the xml snippet
            XElement xmlFragment = GetXml(file, projectSystem);

            XDocument document = XmlUtility.GetOrCreateDocument(xmlFragment.Name, projectSystem, targetPath);

            // Merge the other xml elements into one element within this xml hierarchy (matching the config file path)
            var mergedFragments = matchingFiles.Select(f => GetXml(f, projectSystem))
                                               .Aggregate(new XElement(xmlFragment.Name), (left, right) => left.MergeWith(right, _nodeActions));

            // Take the difference of the xml and remove it from the main xml file
            document.Root.Except(xmlFragment.Except(mergedFragments));

            // Save the new content to the file system
            using (var fileStream = projectSystem.CreateFile(targetPath))
            {
                document.Save(fileStream);
            }
        }
        public virtual void RevertFile(IPackageFile file, string targetPath, IEnumerable <IPackageFile> matchingFiles, IProjectSystem projectSystem)
        {
            // Get the xml snippet
            XElement xmlFragment = GetXml(file, projectSystem);

            XDocument document = XmlUtility.GetOrCreateDocument(xmlFragment.Name, projectSystem, targetPath);

            // Merge the other xml elements into one element within this xml hierarchy (matching the config file path)
            var mergedFragments = matchingFiles.Select(f => GetXml(f, projectSystem))
                                  .Aggregate(new XElement(xmlFragment.Name), (left, right) => left.MergeWith(right, _nodeActions));

            // Take the difference of the xml and remove it from the main xml file
            document.Root.Except(xmlFragment.Except(mergedFragments));

            // Save the new content to the file system
            using (var fileStream = projectSystem.CreateFile(targetPath))
            {
                document.Save(fileStream);
            }
        }
Exemplo n.º 6
0
 private static void PerformXdtTransform(IPackageFile file, string targetPath, IProjectSystem projectSystem)
 {
     if (projectSystem.FileExists(targetPath))
     {
         string transform = Preprocessor.Process(file, projectSystem);
         try
         {
             using (XmlTransformation transformation = new XmlTransformation(transform, false, null))
             {
                 using (XmlTransformableDocument document = new XmlTransformableDocument())
                 {
                     document.PreserveWhitespace = true;
                     using (Stream stream = projectSystem.OpenFile(targetPath))
                     {
                         document.Load(stream);
                     }
                     if (transformation.Apply(document))
                     {
                         using (MemoryStream stream2 = new MemoryStream())
                         {
                             document.Save(stream2);
                             stream2.Seek(0L, SeekOrigin.Begin);
                             using (Stream stream3 = projectSystem.CreateFile(targetPath))
                             {
                                 stream2.CopyTo(stream3);
                             }
                         }
                     }
                 }
             }
         }
         catch (Exception exception)
         {
             object[] args = new object[] { targetPath, projectSystem.ProjectName };
             throw new InvalidDataException(string.Format(CultureInfo.CurrentCulture, NuGetResources.XdtError + " " + exception.Message, args), exception);
         }
     }
 }