Beispiel #1
0
        private static PackageInput GetPackageInput(string file, ILogger log)
        {
            // Validate package
            log.LogInformation($"Reading {file}");

            PackageIdentity identity           = null;
            var             isSymbolsPackage   = false;
            var             hasNuspec          = false;
            var             hasMultipleNuspecs = false;
            var             nuspecName         = string.Empty;

            try
            {
                // Read basic info from the package and verify that it isn't broken.
                using (var zip = new ZipArchive(File.OpenRead(file), ZipArchiveMode.Read, leaveOpen: false))
                    using (var package = new PackageArchiveReader(zip))
                    {
                        identity         = package.GetIdentity();
                        isSymbolsPackage = SymbolsUtility.IsSymbolsPackage(zip, file);

                        // Check for correct nuspec name
                        nuspecName = identity.Id + ".nuspec";
                        var nuspecs = zip.Entries.Where(entry => entry.FullName.EndsWith(".nuspec", StringComparison.OrdinalIgnoreCase)).ToList();

                        hasNuspec          = nuspecs.Count == 1;
                        hasMultipleNuspecs = nuspecs.Count > 1;
                    }
            }
            catch
            {
                log.LogError($"Invalid package '{file}'.");
                throw;
            }

            var packageInput = new PackageInput(file, identity, isSymbolsPackage);

            // Display a message for non-normalized packages
            if (packageInput.Identity.Version.ToString() != packageInput.Identity.Version.ToNormalizedString())
            {
                var message = $"Package '{packageInput.PackagePath}' does not contain a normalized version. Normalized: '{packageInput.Identity.Version.ToNormalizedString()}' Nuspec version: '{packageInput.Identity.Version.ToString()}'. See https://semver.org/ for details.";
                log.LogVerbose(message);
            }

            // Check for correct nuspec name
            if (!hasNuspec)
            {
                throw new InvalidDataException($"'{packageInput.PackagePath}' does not contain '{nuspecName}'.");
            }

            // Check for multiple nuspec files
            if (hasMultipleNuspecs)
            {
                throw new InvalidDataException($"'{packageInput.PackagePath}' contains multiple nuspecs and cannot be consumed.");
            }

            return(packageInput);
        }
Beispiel #2
0
        /// <summary>
        /// Create a package input from the given file path.
        /// </summary>
        public static PackageInput Create(string file)
        {
            PackageInput result = null;

            using (var zip = new ZipArchive(File.OpenRead(file), ZipArchiveMode.Read, leaveOpen: false))
                using (var reader = new PackageArchiveReader(file))
                {
                    var isSymbolsPackage = SymbolsUtility.IsSymbolsPackage(zip, file);
                    result = new PackageInput(file, isSymbolsPackage, reader.NuspecReader);
                }

            return(result);
        }
Beispiel #3
0
        private async Task <List <PackageFile> > GetAssembliesAsync(PackageInput packageInput)
        {
            var result = new List <PackageFile>();
            var seen   = new HashSet <ISleetFile>();

            var assemblyFiles = await packageInput.RunWithLockAsync(p => Task.FromResult(p.Zip.Entries
                                                                                         .Where(e => e.FullName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
                                                                                         .ToList()));

            var pdbFiles = await packageInput.RunWithLockAsync(p => Task.FromResult(p.Zip.Entries
                                                                                    .Where(e => e.FullName.EndsWith(".pdb", StringComparison.OrdinalIgnoreCase))
                                                                                    .ToList()));

            foreach (var assembly in assemblyFiles)
            {
                string          assemblyHash = null;
                string          pdbHash      = null;
                ZipArchiveEntry pdbEntry     = null;
                var             valid        = false;

                try
                {
                    using (var stream = await packageInput.GetEntryStreamWithLockAsync(assembly))
                        using (var reader = new PEReader(stream))
                        {
                            assemblyHash = SymbolsUtility.GetSymbolHashFromAssembly(reader);
                            pdbHash      = SymbolsUtility.GetPDBHashFromAssembly(reader);
                        }

                    var assemblyWithoutExt = SleetLib.PathUtility.GetFullPathWithoutExtension(assembly.FullName);

                    pdbEntry = pdbFiles.FirstOrDefault(e =>
                                                       StringComparer.OrdinalIgnoreCase.Equals(
                                                           SleetLib.PathUtility.GetFullPathWithoutExtension(e.FullName), assemblyWithoutExt));

                    valid = true;
                }
                catch
                {
                    // Ignore bad assemblies
                    var message = LogMessage.Create(LogLevel.Warning, $"Unable add symbols for {assembly.FullName}, this file will not be present in the symbol server.");
                    await _context.Log.LogAsync(message);
                }

                if (valid)
                {
                    // Add .dll
                    var fileInfo  = new FileInfo(assembly.FullName);
                    var dllFile   = _context.Source.Get(SymbolsIndexUtility.GetAssemblyFilePath(fileInfo.Name, assemblyHash));
                    var indexFile = _context.Source.Get(SymbolsIndexUtility.GetAssemblyToPackageIndexPath(fileInfo.Name, assemblyHash));

                    // Avoid duplicates
                    if (seen.Add(dllFile))
                    {
                        result.Add(new PackageFile(fileInfo.Name, assemblyHash, assembly, dllFile, indexFile));
                    }

                    // Add .pdb
                    if (pdbEntry != null)
                    {
                        var pdbFileInfo  = new FileInfo(pdbEntry.FullName);
                        var pdbFile      = _context.Source.Get(SymbolsIndexUtility.GetAssemblyFilePath(pdbFileInfo.Name, pdbHash));
                        var pdbIndexFile = _context.Source.Get(SymbolsIndexUtility.GetAssemblyToPackageIndexPath(pdbFileInfo.Name, pdbHash));

                        // Avoid duplicates
                        if (seen.Add(pdbFile))
                        {
                            result.Add(new PackageFile(pdbFileInfo.Name, pdbHash, pdbEntry, pdbFile, pdbIndexFile));
                        }
                    }
                }
            }

            return(result);
        }
Beispiel #4
0
        private ISleetFile GetFile(string fileName, string hash)
        {
            var symbolsPath = SymbolsUtility.GetSymbolsServerPath(fileName, hash);

            return(_context.Source.Get($"/symbols/{symbolsPath}"));
        }
Beispiel #5
0
        /// <summary>
        /// Package index for a dll or pdb.
        /// Symbols/file/hash/packages.json
        /// </summary>
        public static string GetAssemblyToPackageIndexPath(string fileName, string hash)
        {
            var symbolsPath = SymbolsUtility.GetSymbolsServerDirectoryPath(fileName, hash);

            return($"/symbols/{symbolsPath}packages.json");
        }
Beispiel #6
0
        /// <summary>
        /// Path to dll or pdb
        /// Symbols/file/hash/file.dll
        /// </summary>
        public static string GetAssemblyFilePath(string fileName, string hash)
        {
            var symbolsPath = SymbolsUtility.GetSymbolsServerPath(fileName, hash);

            return($"/symbols/{symbolsPath}");
        }