<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

#if NETSTANDARD2_0 || NETSTANDARD1_6
using System.Runtime.Loader;
#endif

using Microsoft.Extensions.DependencyModel;

namespace Cogito.Reflection
{

    public static class SafeAssemblyLoader
    {

#if NETSTANDARD2_0 || NETSTANDARD1_6

        /// <summary>
        /// Loads an <see cref="Assembly"/> from a path, ignoring failures.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Assembly Load(string path, AssemblyLoadContext loadContext)
        {
            if (path == null)
                throw new ArgumentNullException(nameof(path));
            if (loadContext is null)
                throw new ArgumentNullException(nameof(loadContext));
            if (string.IsNullOrWhiteSpace(path))
                throw new ArgumentOutOfRangeException(nameof(path));

            try
            {
                return loadContext.LoadFromAssemblyPath(path);
            }
            catch (Exception)
            {
                // ignore
            }

            return null;
        }

#endif

        /// <summary>
        /// Loads an <see cref="Assembly"/> from a path, ignoring failures.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Assembly Load(string path)
        {
            if (path == null)
                throw new ArgumentNullException(nameof(path));
            if (string.IsNullOrWhiteSpace(path))
                throw new ArgumentOutOfRangeException(nameof(path));

#if NETSTANDARD2_0 || NETSTANDARD1_6
            return Load(path, AssemblyLoadContext.Default);
#else

            try
            {
                return Assembly.LoadFrom(path);
            }
            catch (Exception)
            {
                // ignore
            }

            return null;
#endif
        }

#if NETSTANDARD2_0 || NETSTANDARD1_6

        /// <summary>
        /// Load all assemblies.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Assembly> LoadAll(DependencyContext dependencyContext, AssemblyLoadContext assemblyLoadContext)
        {
            if (dependencyContext is null)
                throw new ArgumentNullException(nameof(dependencyContext));
            if (assemblyLoadContext is null)
                throw new ArgumentNullException(nameof(assemblyLoadContext));

            return dependencyContext.RuntimeLibraries
                .SelectMany(i => i.GetDefaultAssemblyNames(dependencyContext))
                .Select(i => LoadFromAssemblyName(i, assemblyLoadContext))
                .Where(i => i != null);
        }

        /// <summary>
        /// Load all assemblies.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Assembly> LoadAll(DependencyContext dependencyContext)
        {
            if (dependencyContext is null)
                throw new ArgumentNullException(nameof(dependencyContext));

            return LoadAll(dependencyContext, AssemblyLoadContext.Default);
        }

        /// <summary>
        /// Load all assemblies.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Assembly> LoadAll(AssemblyLoadContext assemblyLoadContext)
        {
            if (assemblyLoadContext is null)
                throw new ArgumentNullException(nameof(assemblyLoadContext));

            return LoadAll(DependencyContext.Default, assemblyLoadContext);
        }

        /// <summary>
        /// Load all assemblies.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Assembly> LoadAll()
        {
            return LoadAll(DependencyContext.Default);
        }

        /// <summary>
        /// Safely loads the specified <see cref="Assembly"/>.
        /// </summary>
        /// <param name="assemblyName"></param>
        static Assembly LoadFromAssemblyName(AssemblyName assemblyName, AssemblyLoadContext assemblyLoadContext)
        {
            if (assemblyName is null)
                throw new ArgumentNullException(nameof(assemblyName));
            if (assemblyLoadContext is null)
                throw new ArgumentNullException(nameof(assemblyLoadContext));

            try
            {
                return assemblyLoadContext.LoadFromAssemblyName(assemblyName);
            }
            catch (Exception)
            {
                return null;
            }
        }

#else

        /// <summary>
        /// Load all assemblies.
        /// </summary><commit_msg>Use other method.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

#if NETSTANDARD2_0 || NETSTANDARD1_6
using System.Runtime.Loader;
#endif

using Microsoft.Extensions.DependencyModel;

namespace Cogito.Reflection
{

    public static class SafeAssemblyLoader
    {

#if NETSTANDARD2_0 || NETSTANDARD1_6

        /// <summary>
        /// Loads an <see cref="Assembly"/> from a path, ignoring failures.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Assembly Load(string path, AssemblyLoadContext loadContext)
        {
            if (path == null)
                throw new ArgumentNullException(nameof(path));
            if (loadContext is null)
                throw new ArgumentNullException(nameof(loadContext));
            if (string.IsNullOrWhiteSpace(path))
                throw new ArgumentOutOfRangeException(nameof(path));

            try
            {
                return loadContext.LoadFromAssemblyPath(path);
            }
            catch (Exception)
            {
                // ignore
            }

            return null;
        }

#endif

        /// <summary>
        /// Loads an <see cref="Assembly"/> from a path, ignoring failures.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Assembly Load(string path)
        {
            if (path == null)
                throw new ArgumentNullException(nameof(path));
            if (string.IsNullOrWhiteSpace(path))
                throw new ArgumentOutOfRangeException(nameof(path));

#if NETSTANDARD2_0 || NETSTANDARD1_6
            return Load(path, AssemblyLoadContext.Default);
#else

            try
            {
                return Assembly.LoadFrom(path);
            }
            catch (Exception)
            {
                // ignore
            }

            return null;
#endif
        }

#if NETSTANDARD2_0 || NETSTANDARD1_6

        /// <summary>
        /// Load all assemblies.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Assembly> LoadAll(DependencyContext dependencyContext, AssemblyLoadContext assemblyLoadContext)
        {
            if (dependencyContext is null)
                throw new ArgumentNullException(nameof(dependencyContext));
            if (assemblyLoadContext is null)
                throw new ArgumentNullException(nameof(assemblyLoadContext));

            return dependencyContext.RuntimeLibraries
                .SelectMany(i => i.GetDefaultAssemblyNames(dependencyContext))
                .Select(i => LoadFromAssemblyName(i, assemblyLoadContext))
                .Where(i => i != null);
        }

        /// <summary>
        /// Load all assemblies.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Assembly> LoadAll(DependencyContext dependencyContext)
        {
            if (dependencyContext is null)
                throw new ArgumentNullException(nameof(dependencyContext));

            return LoadAll(dependencyContext, AssemblyLoadContext.Default);
        }

        /// <summary>
        /// Load all assemblies.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Assembly> LoadAll(AssemblyLoadContext assemblyLoadContext)
        {
            if (assemblyLoadContext is null)
                throw new ArgumentNullException(nameof(assemblyLoadContext));

            return LoadAll(DependencyContext.Default, assemblyLoadContext);
        }

        /// <summary>
        /// Load all assemblies.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Assembly> LoadAll()
        {
            return LoadAll(DependencyContext.Default, AssemblyLoadContext.Default);
        }

        /// <summary>
        /// Safely loads the specified <see cref="Assembly"/>.
        /// </summary>
        /// <param name="assemblyName"></param>
        static Assembly LoadFromAssemblyName(AssemblyName assemblyName, AssemblyLoadContext assemblyLoadContext)
        {
            if (assemblyName is null)
                throw new ArgumentNullException(nameof(assemblyName));
            if (assemblyLoadContext is null)
                throw new ArgumentNullException(nameof(assemblyLoadContext));

            try
            {
                return assemblyLoadContext.LoadFromAssemblyName(assemblyName);
            }
            catch (Exception)
            {
                return null;
            }
        }

#else

        /// <summary>
        /// Load all assemblies.
        /// </summary>
{'fraction_non_alphanumeric': 0.0997344662772172, 'fraction_numerical': 0.0033988316516197557, 'mean_word_length': 1.761290322580645, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '923380', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2189, 'n_words': 518}