/// <summary>
 /// Try to add the specified the project with the target path. If there's an existing file in the project with the same name,
 /// it will ask the logger for the resolution, which has 4 choices: Overwrite|Ignore|Overwrite All|Ignore All
 /// </summary>
 internal static void TryAddFile(IProjectSystem project, string path, Func <Stream> content)
 {
     if (project.FileExists(path) && project.FileExistsInProject(path))
     {
         // file exists in project, ask user if he wants to overwrite or ignore
         string conflictMessage            = String.Format(CultureInfo.CurrentCulture, NuGetResources.FileConflictMessage, path, project.ProjectName);
         FileConflictResolution resolution = project.Logger.ResolveFileConflict(conflictMessage);
         if (resolution == FileConflictResolution.Overwrite || resolution == FileConflictResolution.OverwriteAll)
         {
             // overwrite
             project.Logger.Log(MessageLevel.Info, NuGetResources.Info_OverwriteExistingFile, path);
             using (Stream stream = content())
             {
                 project.AddFile(path, stream);
             }
         }
         else
         {
             // ignore
             project.Logger.Log(MessageLevel.Info, NuGetResources.Warning_FileAlreadyExists, path);
         }
     }
     else
     {
         using (Stream stream = content())
         {
             project.AddFile(path, stream);
         }
     }
 }
Пример #2
0
 internal static void TryAddFile(IProjectSystem project, string path, Func <Stream> content)
 {
     if (!project.FileExists(path) || !project.FileExistsInProject(path))
     {
         using (Stream stream2 = content())
         {
             project.AddFile(path, stream2);
         }
     }
     else
     {
         object[] args    = new object[] { path, project.ProjectName };
         string   message = string.Format(CultureInfo.CurrentCulture, NuGetResources.FileConflictMessage, args);
         FileConflictResolution resolution = project.Logger.ResolveFileConflict(message);
         if ((resolution != FileConflictResolution.Overwrite) && (resolution != FileConflictResolution.OverwriteAll))
         {
             object[] objArray3 = new object[] { path };
             project.Logger.Log(MessageLevel.Info, NuGetResources.Warning_FileAlreadyExists, objArray3);
         }
         else
         {
             object[] objArray2 = new object[] { path };
             project.Logger.Log(MessageLevel.Info, NuGetResources.Info_OverwriteExistingFile, objArray2);
             using (Stream stream = content())
             {
                 project.AddFile(path, stream);
             }
         }
     }
 }
Пример #3
0
 public void TransformFile(IPackageFile file, string targetPath, IProjectSystem projectSystem)
 {
     if (!projectSystem.FileExists(targetPath)) {
         using (Stream stream = Process(file, projectSystem).AsStream()) {
             projectSystem.AddFile(targetPath, stream);
         }
     }
 }
Пример #4
0
 public void TransformFile(IPackageFile file, string targetPath, IProjectSystem projectSystem)
 {
     if (!projectSystem.FileExists(targetPath))
     {
         using (Stream stream = Process(file, projectSystem).AsStream()) {
             projectSystem.AddFile(targetPath, stream);
         }
     }
 }
Пример #5
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);
                }
            }
        }
Пример #6
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);
                }
            }
        }
Пример #7
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);
         }
     }
 }