|
| 1 | +// SPDX-FileCopyrightText: 2021 smdn <smdn@smdn.jp> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +#pragma warning disable CA1848 |
| 4 | + |
| 5 | +using System; |
| 6 | +#if !SYSTEM_CONVERT_TOHEXSTRING |
| 7 | +using System.Linq; |
| 8 | +#endif |
| 9 | +using System.Reflection; |
| 10 | + |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | + |
| 13 | +namespace Smdn.Reflection.ReverseGenerating.ListApi; |
| 14 | + |
| 15 | +internal static class RuntimeAssemblyName { |
| 16 | + public static void WarnNotToBeAbleToResolve(ILogger? logger, AssemblyName name) |
| 17 | + { |
| 18 | + if (ShouldWarnIfNotToBeAbleToResolve(name)) |
| 19 | + logger?.LogWarning("could not resolve assembly '{AssemblyName}'", name); |
| 20 | + else |
| 21 | + logger?.LogDebug("could not resolve assembly '{AssemblyName}'", name); |
| 22 | + } |
| 23 | + |
| 24 | + private static bool ShouldWarnIfNotToBeAbleToResolve(AssemblyName name) |
| 25 | + { |
| 26 | + const bool shouldNotWarn = false; |
| 27 | + const bool shouldWarn = true; |
| 28 | + |
| 29 | + if (name.Name is null) |
| 30 | + return shouldWarn; |
| 31 | + |
| 32 | + var publicKeyTokenBytes = name.GetPublicKeyToken(); |
| 33 | + var publicKeyTokenString = publicKeyTokenBytes is null |
| 34 | + ? null |
| 35 | +#if SYSTEM_CONVERT_TOHEXSTRING |
| 36 | + : Convert.ToHexString(publicKeyTokenBytes); // ToHexString() returns upper case string |
| 37 | +#else |
| 38 | + : string.Concat(publicKeyTokenBytes.Select(static b => b.ToString("x2", provider: null))); |
| 39 | +#endif |
| 40 | + |
| 41 | + // ref: https://github.com/dotnet/source-build-reference-packages/blob/main/src/referencePackageSourceGenerator/Tasks/GenerateProjects.cs |
| 42 | + const string PublicKeyTokenMicrosoft = "b03f5f7f11d50a3a"; |
| 43 | + const string PublicKeyTokenDotnetOpenSource = "cc7b13ffcd2ddd51"; |
| 44 | + const string PublicKeyTokenEcma = "b77a5c561934e089"; |
| 45 | + |
| 46 | + if (string.Equals(name.Name, "mscorlib", StringComparison.Ordinal)) { |
| 47 | + if (string.Equals(publicKeyTokenString, PublicKeyTokenEcma, StringComparison.OrdinalIgnoreCase)) |
| 48 | + return shouldNotWarn; |
| 49 | + } |
| 50 | + |
| 51 | + if (string.Equals(name.Name, "netstandard", StringComparison.Ordinal)) { |
| 52 | + if (string.Equals(publicKeyTokenString, PublicKeyTokenDotnetOpenSource, StringComparison.OrdinalIgnoreCase)) |
| 53 | + return shouldNotWarn; |
| 54 | + } |
| 55 | + |
| 56 | + if (string.Equals(name.Name, "System", StringComparison.Ordinal) || name.Name.StartsWith("System.", StringComparison.Ordinal)) { |
| 57 | + if ( |
| 58 | + string.Equals(publicKeyTokenString, PublicKeyTokenMicrosoft, StringComparison.OrdinalIgnoreCase) || |
| 59 | + string.Equals(publicKeyTokenString, PublicKeyTokenEcma, StringComparison.OrdinalIgnoreCase) || |
| 60 | + string.Equals(publicKeyTokenString, PublicKeyTokenDotnetOpenSource, StringComparison.OrdinalIgnoreCase) |
| 61 | + ) { |
| 62 | + return shouldNotWarn; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return shouldWarn; |
| 67 | + } |
| 68 | +} |
0 commit comments