public static void DeleteFiles(this IProjectSystem project,
                                       IEnumerable <IPackageFile> files,
                                       IEnumerable <IPackage> otherPackages,
                                       IDictionary <FileTransformExtensions, IPackageFileTransformer> fileTransformers)
        {
            IPackageFileTransformer transformer;
            // First get all directories that contain files
            var directoryLookup = files.ToLookup(
                p => Path.GetDirectoryName(ResolveTargetPath(project, fileTransformers, fte => fte.UninstallExtension, p.EffectivePath, out transformer)));

            // Get all directories that this package may have added
            var directories = from grouping in directoryLookup
                              from directory in FileSystemExtensions.GetDirectories(grouping.Key)
                              orderby directory.Length descending
                              select directory;

            // Remove files from every directory
            foreach (var directory in directories)
            {
                var directoryFiles = directoryLookup.Contains(directory) ? directoryLookup[directory] : Enumerable.Empty <IPackageFile>();

                if (!project.DirectoryExists(directory))
                {
                    continue;
                }
                var batchProcessor = project as IBatchProcessor <string>;

                try
                {
                    if (batchProcessor != null)
                    {
                        var paths = directoryFiles.Select(file => ResolvePath(fileTransformers, fte => fte.UninstallExtension, file.EffectivePath));
                        batchProcessor.BeginProcessing(paths, PackageAction.Uninstall);
                    }

                    foreach (var file in directoryFiles)
                    {
                        if (file.IsEmptyFolder())
                        {
                            continue;
                        }

                        // Resolve the path
                        string path = ResolveTargetPath(project,
                                                        fileTransformers,
                                                        fte => fte.UninstallExtension,
                                                        file.EffectivePath,
                                                        out transformer);

                        if (project.IsSupportedFile(path))
                        {
                            if (transformer != null)
                            {
                                var matchingFiles = from p in otherPackages
                                                    from otherFile in project.GetCompatibleItemsCore(p.GetContentFiles())
                                                    where otherFile.EffectivePath.Equals(file.EffectivePath, StringComparison.OrdinalIgnoreCase)
                                                    select otherFile;

                                try
                                {
                                    transformer.RevertFile(file, path, matchingFiles, project);
                                }
                                catch (Exception e)
                                {
                                    // Report a warning and move on
                                    project.Logger.Log(MessageLevel.Warning, e.Message);
                                }
                            }
                            else
                            {
                                project.DeleteFileSafe(path, file.GetStream);
                            }
                        }
                    }

                    // If the directory is empty then delete it
                    if (!project.GetFilesSafe(directory).Any() &&
                        !project.GetDirectoriesSafe(directory).Any())
                    {
                        project.DeleteDirectorySafe(directory, recursive: false);
                    }
                }
                finally
                {
                    if (batchProcessor != null)
                    {
                        batchProcessor.EndProcessing();
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void DeleteFiles(this IProjectSystem project, IEnumerable <IPackageFile> files, IEnumerable <IPackage> otherPackages, IDictionary <FileTransformExtensions, IPackageFileTransformer> fileTransformers)
        {
            IPackageFileTransformer        transformer;
            ILookup <string, IPackageFile> lookup = Enumerable.ToLookup <IPackageFile, string>(files, p => Path.GetDirectoryName(ResolveTargetPath(project, fileTransformers, fte => fte.UninstallExtension, p.EffectivePath, out transformer)));

            foreach (string str in from grouping in lookup
                     from directory in FileSystemExtensions.GetDirectories(grouping.Key)
                     orderby directory.Length descending
                     select directory)
            {
                IEnumerable <IPackageFile> enumerable = lookup.Contains(str) ? lookup[str] : Enumerable.Empty <IPackageFile>();
                if (project.DirectoryExists(str))
                {
                    IBatchProcessor <string> processor = project as IBatchProcessor <string>;
                    try
                    {
                        if (processor != null)
                        {
                            Func <IPackageFile, string> < > 9__6;
                            Func <IPackageFile, string> func5 = < > 9__6;
                            if (< > 9__6 == null)
                            {
                                Func <IPackageFile, string> local5 = < > 9__6;
                                func5 = < > 9__6 = file => ResolvePath(fileTransformers, fte => fte.UninstallExtension, file.EffectivePath);
                            }
                            processor.BeginProcessing(Enumerable.Select <IPackageFile, string>(enumerable, func5), PackageAction.Uninstall);
                        }
                        foreach (IPackageFile file in enumerable)
                        {
                            if (!file.IsEmptyFolder())
                            {
                                string path = ResolveTargetPath(project, fileTransformers, fte => fte.UninstallExtension, file.EffectivePath, out transformer);
                                if (project.IsSupportedFile(path))
                                {
                                    Func <IPackage, IEnumerable <IPackageFile> > < > 9__9;
                                    if (transformer == null)
                                    {
                                        project.DeleteFileSafe(path, new Func <Stream>(file.GetStream));
                                        continue;
                                    }
                                    Func <IPackage, IEnumerable <IPackageFile> > func4 = < > 9__9;
                                    if (< > 9__9 == null)
                                    {
                                        Func <IPackage, IEnumerable <IPackageFile> > local7 = < > 9__9;
                                        func4 = < > 9__9 = p => project.GetCompatibleItemsCore <IPackageFile>(p.GetContentFiles());
                                    }
                                    IEnumerable <IPackageFile> matchingFiles = from <> h__TransparentIdentifier0 in Enumerable.SelectMany(otherPackages, func4, (p, otherFile) => new {
                                        p         = p,
                                        otherFile = otherFile
                                    })
                                                                               where < > h__TransparentIdentifier0.otherFile.EffectivePath.Equals(file.EffectivePath, StringComparison.OrdinalIgnoreCase)
                                                                               select <> h__TransparentIdentifier0.otherFile;
                                    try
                                    {
                                        transformer.RevertFile(file, path, matchingFiles, project);
                                    }
                                    catch (Exception exception)
                                    {
                                        project.Logger.Log(MessageLevel.Warning, exception.Message, new object[0]);
                                    }
                                }
                            }
                        }
                        if (!project.GetFilesSafe(str).Any <string>() && !project.GetDirectoriesSafe(str).Any <string>())
                        {
                            project.DeleteDirectorySafe(str, false);
                        }
                    }
                    finally
                    {
                        if (processor != null)
                        {
                            processor.EndProcessing();
                        }
                    }
                }
            }
        }