lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [11/12] lucenenet git commit: Lucene.Net.Util.SPIClassIterator: Factored out code to get all non-Microsoft referenced assemblies into a new class in Support named AssemblyUtils
Date Wed, 02 Aug 2017 04:12:28 GMT
Lucene.Net.Util.SPIClassIterator: Factored out code to get all non-Microsoft referenced assemblies
into a new class in Support named AssemblyUtils


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/1e522937
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/1e522937
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/1e522937

Branch: refs/heads/master
Commit: 1e5229375d35371b732e3f1b8f105d6a6a345d0c
Parents: a11ca03
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Mon Jul 31 13:51:17 2017 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Wed Aug 2 09:54:25 2017 +0700

----------------------------------------------------------------------
 src/Lucene.Net/Lucene.Net.csproj        |   1 +
 src/Lucene.Net/Support/AssemblyUtils.cs | 138 +++++++++++++++++++++++++++
 src/Lucene.Net/Util/SPIClassIterator.cs | 115 +---------------------
 3 files changed, 140 insertions(+), 114 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1e522937/src/Lucene.Net/Lucene.Net.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Lucene.Net.csproj b/src/Lucene.Net/Lucene.Net.csproj
index 211a013..368597a 100644
--- a/src/Lucene.Net/Lucene.Net.csproj
+++ b/src/Lucene.Net/Lucene.Net.csproj
@@ -394,6 +394,7 @@
     <Compile Include="Index\TwoStoredFieldsConsumers.cs" />
     <Compile Include="Index\UpgradeIndexMergePolicy.cs" />
     <Compile Include="LucenePackage.cs" />
+    <Compile Include="Support\AssemblyUtils.cs" />
     <Compile Include="Support\Document\DocumentExtensions.cs" />
     <Compile Include="Support\IO\Compression\LZOCompressor.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1e522937/src/Lucene.Net/Support/AssemblyUtils.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/AssemblyUtils.cs b/src/Lucene.Net/Support/AssemblyUtils.cs
new file mode 100644
index 0000000..48c7439
--- /dev/null
+++ b/src/Lucene.Net/Support/AssemblyUtils.cs
@@ -0,0 +1,138 @@
+´╗┐using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+#if NETSTANDARD
+using Microsoft.Extensions.DependencyModel;
+#endif
+
+namespace Lucene.Net.Support
+{
+    /// <summary>
+    /// Methods for working with Assemblies.
+    /// </summary>
+    public class AssemblyUtils
+    {
+        /// <summary>
+        /// Gets a list of the host assembly's referenced assemblies excluding 
+        /// any Microsoft, System, or Mono prefixed assemblies or assemblies with
+        /// official Microsoft key hashes. Essentially, we get a list of all non
+        /// Microsoft assemblies here.
+        /// </summary>
+        /// <returns></returns>
+        public static IList<Assembly> GetReferencedAssemblies()
+        {
+            // .NET Port Hack: We do a 2-level deep check here because if the assembly you're
+            // hoping would be loaded hasn't been loaded yet into the app domain,
+            // it is unavailable. So we go to the next level on each and check each referenced
+            // assembly.
+#if NETSTANDARD
+            var dependencyContext = DependencyContext.Default;
+            var assemblyNames = dependencyContext.RuntimeLibraries
+                .SelectMany(lib => lib.GetDefaultAssemblyNames(dependencyContext))
+                .Where(x => !DotNetFrameworkFilter.IsFrameworkAssembly(x))
+                .Distinct();
+            var assembliesLoaded = LoadAssemblyFromName(assemblyNames);
+#else
+            var assembliesLoaded = AppDomain.CurrentDomain.GetAssemblies();
+#endif
+            assembliesLoaded = assembliesLoaded.Where(x => !DotNetFrameworkFilter.IsFrameworkAssembly(x)).ToArray();
+
+            var referencedAssemblies = assembliesLoaded
+                .SelectMany(assembly =>
+                {
+                    return assembly
+                        .GetReferencedAssemblies()
+                        .Where(reference => !DotNetFrameworkFilter.IsFrameworkAssembly(reference))
+                        .Select(assemblyName => LoadAssemblyFromName(assemblyName));
+                })
+                .Where(x => x != null)
+                .Distinct();
+
+            return assembliesLoaded.Concat(referencedAssemblies).Distinct().ToList();
+        }
+
+        private static IEnumerable<Assembly> LoadAssemblyFromName(IEnumerable<AssemblyName>
assemblyNames)
+        {
+            return assemblyNames.Select(x => LoadAssemblyFromName(x)).Where(x => x
!= null);
+        }
+
+        private static Assembly LoadAssemblyFromName(AssemblyName assemblyName)
+        {
+            try
+            {
+                return Assembly.Load(assemblyName);
+            }
+            catch
+            {
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// Assembly filter logic from:
+        /// https://raw.githubusercontent.com/Microsoft/dotnet-apiport/master/src/Microsoft.Fx.Portability/Analyzer/DotNetFrameworkFilter.cs
+        /// </summary>
+        public static class DotNetFrameworkFilter
+        {
+            /// <summary>
+            /// These keys are a collection of public key tokens derived from all the reference
assemblies in
+            /// "%ProgramFiles%\Reference Assemblies\Microsoft" on a Windows 10 machine with
VS 2015 installed
+            /// </summary>
+            private static readonly ICollection<string> s_microsoftKeys = new HashSet<string>(new[]
+            {
+                "b77a5c561934e089", // ECMA
+                "b03f5f7f11d50a3a", // DEVDIV
+                "7cec85d7bea7798e", // SLPLAT
+                "31bf3856ad364e35", // Windows
+                "24eec0d8c86cda1e", // Phone
+                "0738eb9f132ed756", // Mono
+                "ddd0da4d3e678217", // Component model
+                "84e04ff9cfb79065", // Mono Android
+                "842cf8be1de50553"  // Xamarin.iOS
+            }, StringComparer.OrdinalIgnoreCase);
+
+            private static readonly IEnumerable<string> s_frameworkAssemblyNamePrefixes
= new[]
+            {
+                "System.",
+                "Microsoft.",
+                "Mono."
+            };
+
+            /// <summary>
+            /// Gets a best guess as to whether this assembly is a .NET Framework assembly
or not.
+            /// </summary>
+            public static bool IsFrameworkAssembly(Assembly assembly)
+            {
+                return assembly != null && IsFrameworkAssembly(assembly.GetName());
+            }
+
+            /// <summary>
+            /// Gets a best guess as to whether this assembly is a .NET Framework assembly
or not.
+            /// </summary>
+            public static bool IsFrameworkAssembly(AssemblyName assembly)
+            {
+                if (assembly == null)
+                {
+                    return false;
+                }
+
+                if (s_frameworkAssemblyNamePrefixes.Any(p => assembly.Name.StartsWith(p,
StringComparison.OrdinalIgnoreCase)))
+                {
+                    return true;
+                }
+
+                var publicKey = assembly.GetPublicKeyToken();
+
+                if (publicKey == default(byte[]))
+                {
+                    return false;
+                }
+
+                var publicKeyToken = string.Concat(publicKey.Select(i => i.ToString("x2")));
+
+                return s_microsoftKeys.Contains(publicKeyToken);
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1e522937/src/Lucene.Net/Util/SPIClassIterator.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Util/SPIClassIterator.cs b/src/Lucene.Net/Util/SPIClassIterator.cs
index 8a75309..f3c08f0 100644
--- a/src/Lucene.Net/Util/SPIClassIterator.cs
+++ b/src/Lucene.Net/Util/SPIClassIterator.cs
@@ -2,9 +2,6 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
-#if NETSTANDARD
-using Microsoft.Extensions.DependencyModel;
-#endif
 
 namespace Lucene.Net.Util
 {
@@ -41,34 +38,7 @@ namespace Lucene.Net.Util
         {
             types = new HashSet<Type>();
 
-            // .NET Port Hack: We do a 2-level deep check here because if the assembly you're
-            // hoping would be loaded hasn't been loaded yet into the app domain,
-            // it is unavailable. So we go to the next level on each and check each referenced
-            // assembly.
-#if NETSTANDARD
-            var dependencyContext = DependencyContext.Default;
-            var assemblyNames = dependencyContext.RuntimeLibraries
-                .SelectMany(lib => lib.GetDefaultAssemblyNames(dependencyContext))
-                .Where(x => !DotNetFrameworkFilter.IsFrameworkAssembly(x))
-                .Distinct();
-            var assembliesLoaded = LoadAssemblyFromName(assemblyNames);
-#else
-            var assembliesLoaded = AppDomain.CurrentDomain.GetAssemblies();
-#endif
-            assembliesLoaded = assembliesLoaded.Where(x => !DotNetFrameworkFilter.IsFrameworkAssembly(x)).ToArray();
-
-            var referencedAssemblies = assembliesLoaded
-                .SelectMany(assembly =>
-                {
-                    return assembly
-                        .GetReferencedAssemblies()
-                        .Where(reference => !DotNetFrameworkFilter.IsFrameworkAssembly(reference))
-                        .Select(assemblyName => LoadAssemblyFromName(assemblyName));
-                })
-                .Where(x => x != null)
-                .Distinct();
-
-            var assembliesToExamine = assembliesLoaded.Concat(referencedAssemblies).Distinct().ToList();
+            var assembliesToExamine = Support.AssemblyUtils.GetReferencedAssemblies();
 
             // LUCENENET HACK:
             // Tests such as TestImpersonation.cs expect that the assemblies
@@ -154,89 +124,6 @@ namespace Lucene.Net.Util
         {
             return GetEnumerator();
         }
-
-        private static IEnumerable<Assembly> LoadAssemblyFromName(IEnumerable<AssemblyName>
assemblyNames)
-        {
-            return assemblyNames.Select(x => LoadAssemblyFromName(x)).Where(x => x
!= null);
-        }
-
-        private static Assembly LoadAssemblyFromName(AssemblyName assemblyName)
-        {
-            try
-            {
-                return Assembly.Load(assemblyName);
-            }
-            catch
-            {
-                return null;
-            }
-        }
-
-        /// <summary>
-        /// Assembly filter logic from:
-        /// https://raw.githubusercontent.com/Microsoft/dotnet-apiport/master/src/Microsoft.Fx.Portability/Analyzer/DotNetFrameworkFilter.cs
-        /// </summary>
-        private static class DotNetFrameworkFilter
-        {
-            /// <summary>
-            /// These keys are a collection of public key tokens derived from all the reference
assemblies in
-            /// "%ProgramFiles%\Reference Assemblies\Microsoft" on a Windows 10 machine with
VS 2015 installed
-            /// </summary>
-            private static readonly ICollection<string> s_microsoftKeys = new HashSet<string>(new[]
-            {
-                "b77a5c561934e089", // ECMA
-                "b03f5f7f11d50a3a", // DEVDIV
-                "7cec85d7bea7798e", // SLPLAT
-                "31bf3856ad364e35", // Windows
-                "24eec0d8c86cda1e", // Phone
-                "0738eb9f132ed756", // Mono
-                "ddd0da4d3e678217", // Component model
-                "84e04ff9cfb79065", // Mono Android
-                "842cf8be1de50553"  // Xamarin.iOS
-            }, StringComparer.OrdinalIgnoreCase);
-
-            private static readonly IEnumerable<string> s_frameworkAssemblyNamePrefixes
= new[]
-            {
-                "System.",
-                "Microsoft.",
-                "Mono."
-            };
-
-            /// <summary>
-            /// Gets a best guess as to whether this assembly is a .NET Framework assembly
or not.
-            /// </summary>
-            public static bool IsFrameworkAssembly(Assembly assembly)
-            {
-                return assembly != null && IsFrameworkAssembly(assembly.GetName());
-            }
-
-            /// <summary>
-            /// Gets a best guess as to whether this assembly is a .NET Framework assembly
or not.
-            /// </summary>
-            public static bool IsFrameworkAssembly(AssemblyName assembly)
-            {
-                if (assembly == null)
-                {
-                    return false;
-                }
-
-                if (s_frameworkAssemblyNamePrefixes.Any(p => assembly.Name.StartsWith(p,
StringComparison.OrdinalIgnoreCase)))
-                {
-                    return true;
-                }
-
-                var publicKey = assembly.GetPublicKeyToken();
-
-                if (publicKey == default(byte[]))
-                {
-                    return false;
-                }
-
-                var publicKeyToken = string.Concat(publicKey.Select(i => i.ToString("x2")));
-
-                return s_microsoftKeys.Contains(publicKeyToken);
-            }
-        }
     }
 
     /* Being Re-written


Mime
View raw message