private static MemoryStream LoadFileFromPackage(IPackageCoreReader package, string filePath) { try { var cleanPath = filePath.Replace('/', '\\'); if (cleanPath.IndexOf('%') > -1) { cleanPath = Uri.UnescapeDataString(cleanPath); } using var fileStream = package.GetStream(cleanPath); var decompressedStream = new MemoryStream(); fileStream.CopyTo(decompressedStream); decompressedStream.Position = 0; return(decompressedStream); } catch (FileLoadException e) { // Note: intentionally discarding the original exception because I want to ensure the following message is displayed in the 'Exceptions' report throw new FileLoadException($"An error occured while loading {Path.GetFileName(filePath)} from the Nuget package. {e.Message}"); } }
internal static void AddFiles(IMSBuildNuGetProjectSystem msBuildNuGetProjectSystem, IPackageCoreReader packageReader, FrameworkSpecificGroup frameworkSpecificGroup, IDictionary <FileTransformExtensions, IPackageFileTransformer> fileTransformers) { var packageTargetFramework = frameworkSpecificGroup.TargetFramework; var packageItemListAsArchiveEntryNames = frameworkSpecificGroup.Items.ToList(); packageItemListAsArchiveEntryNames.Sort(new PackageItemComparer()); try { try { var paths = packageItemListAsArchiveEntryNames.Select(file => ResolvePath(fileTransformers, fte => fte.InstallExtension, GetEffectivePathForContentFile(packageTargetFramework, file))); paths = paths.Where(p => !string.IsNullOrEmpty(p)); msBuildNuGetProjectSystem.BeginProcessing(); msBuildNuGetProjectSystem.RegisterProcessedFiles(paths); } catch (Exception) { // Ignore all exceptions for now } foreach (var file in packageItemListAsArchiveEntryNames) { if (IsEmptyFolder(file)) { continue; } var effectivePathForContentFile = GetEffectivePathForContentFile(packageTargetFramework, file); // Resolve the target path IPackageFileTransformer installTransformer; var path = ResolveTargetPath(msBuildNuGetProjectSystem, fileTransformers, fte => fte.InstallExtension, effectivePathForContentFile, out installTransformer); if (msBuildNuGetProjectSystem.IsSupportedFile(path)) { if (installTransformer != null) { installTransformer.TransformFile(() => packageReader.GetStream(file), path, msBuildNuGetProjectSystem); } else { // Ignore uninstall transform file during installation string truncatedPath; var uninstallTransformer = FindFileTransformer(fileTransformers, fte => fte.UninstallExtension, effectivePathForContentFile, out truncatedPath); if (uninstallTransformer != null) { continue; } TryAddFile(msBuildNuGetProjectSystem, path, () => packageReader.GetStream(file)); } } } } finally { msBuildNuGetProjectSystem.EndProcessing(); } }
public Stream GetStream(string path) => _reader.GetStream(path);