internal static async Task TryAddFileAsync(
     IMSBuildProjectSystem projectSystem,
     string path,
     Func <Task <Stream> > streamTaskFactory,
     CancellationToken cancellationToken)
 {
     if (projectSystem.FileExistsInProject(path))
     {
         // file exists in project, ask user if he wants to overwrite or ignore
         var conflictMessage = string.Format(CultureInfo.CurrentCulture,
                                             Strings.FileConflictMessage, path, projectSystem.ProjectName);
         var fileConflictAction = projectSystem.NuGetProjectContext.ResolveFileConflict(conflictMessage);
         if (fileConflictAction == FileConflictAction.Overwrite ||
             fileConflictAction == FileConflictAction.OverwriteAll)
         {
             // overwrite
             projectSystem.NuGetProjectContext.Log(MessageLevel.Info, Strings.Info_OverwritingExistingFile, path);
             using (var stream = await streamTaskFactory())
             {
                 projectSystem.AddFile(path, stream);
             }
         }
         else
         {
             // ignore
             projectSystem.NuGetProjectContext.Log(MessageLevel.Warning, Strings.Warning_FileAlreadyExists, path);
         }
     }
     else
     {
         projectSystem.AddFile(path, await streamTaskFactory());
     }
 }
 internal static void AddFile(IMSBuildProjectSystem projectSystem, string path, Action <Stream> writeToStream)
 {
     using (var memoryStream = new MemoryStream())
     {
         writeToStream(memoryStream);
         memoryStream.Seek(0, SeekOrigin.Begin);
         projectSystem.AddFile(path, memoryStream);
     }
 }
예제 #3
0
        /// <summary>
        /// Creates a .refresh file in bin directory of the IFileSystem that points to the assembly being installed.
        /// This works around issues in DTE's AddReference method when dealing with GACed binaries.
        /// </summary>
        /// <remarks>Adds the file to disk ONLY!</remarks>
        /// <param name="root">the root path is dte full path</param>
        /// <param name="assemblyPath">The relative path to the assembly being added</param>
        public static void CreateRefreshFile(string root, string assemblyPath, IMSBuildProjectSystem msbuildNuGetProjectSystem)
        {
            string refreshFilePath = CreateRefreshFilePath(assemblyPath);

            if (!FileSystemUtility.FileExists(root, refreshFilePath))
            {
                try
                {
                    using (var stream = CreateRefreshFileStream(root, assemblyPath))
                    {
                        msbuildNuGetProjectSystem.AddFile(refreshFilePath, stream);
                    }
                }
                catch (UnauthorizedAccessException exception)
                {
                    // log IO permission error
                    ExceptionHelper.WriteErrorToActivityLog(exception);
                }
            }
        }