internal static bool IsNuGetAssembly(AssemblyReference assemblyReference)
        {
            var assemblyName = assemblyReference.GetAssemblyName();

            var isNuGetAssembly = assemblyName.Name == null || NuGetAssembly.MatchesName(assemblyName.Name);

            // Originally I wanted to also check the strong name token, to reduce the risk that someone compiled their
            // code against a custom NuGet assembly with APIs that we don't ship. However, JetBrains compiled their
            // products against custom NuGet assemblies with a different public token, so we can't do that check if we
            // want to find out which APIs they're using.

            return(isNuGetAssembly);
        }
Beispiel #2
0
        private static UsageInformation?GetApisUsedByAssembly(string file)
        {
            // Skip NuGet's own assemblies
            string filename = Path.GetFileNameWithoutExtension(file);

            if (NuGetAssembly.MatchesName(filename) ||
                filename.Equals("NuGet.Core", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            if (filename.StartsWith("NuGet.", StringComparison.OrdinalIgnoreCase))
            {
                Console.Error.WriteLine("Suspicious assembly name: " + file);
            }

            using var fileStream = File.OpenRead(file);
            using var peReader   = new PEReader(fileStream);

            MetadataReader metadata;

            try
            {
                metadata = peReader.GetMetadataReader();
            }
            catch (InvalidOperationException)
            {
                // Not a .NET assembly
                return(null);
            }

            if (!AssemblyAnalyser.HasReferenceToNuGetAssembly(metadata))
            {
                return(null);
            }

            UsageInformation usedNuGetApis = AssemblyAnalyser.FindUsedNuGetApis(metadata);

            return(usedNuGetApis);
        }