コード例 #1
0
        /// <summary>
        /// Add a pre-parsed <see cref="DependencyContext" /> to the load context.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="dependencyContext">The dependency context.</param>
        /// <returns>The builder.</returns>
        public static AssemblyLoadContextBuilder AddDependencyContext(this AssemblyLoadContextBuilder builder, DependencyContext dependencyContext)
        {
            IReadOnlyList <RuntimeFallbacks> ridGraph = dependencyContext.RuntimeGraph.Any() || DependencyContext.Default == null
               ? dependencyContext.RuntimeGraph
               : DependencyContext.Default.RuntimeGraph;

            string           rid           = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.GetRuntimeIdentifier();
            string           fallbackRid   = GetFallbackRid();
            RuntimeFallbacks fallbackGraph = ridGraph.FirstOrDefault(g => g.Runtime == rid)
                                             ?? ridGraph.FirstOrDefault(g => g.Runtime == fallbackRid)
                                             ?? new RuntimeFallbacks("any");

            foreach (ManagedLibrary managed in dependencyContext.ResolveRuntimeAssemblies(fallbackGraph))
            {
                builder.AddManagedLibrary(managed);
            }

            foreach (RuntimeLibrary library in dependencyContext.ResolveResourceAssemblies())
            {
                foreach (ResourceAssembly resource in library.ResourceAssemblies)
                {
                    /*
                     * For resource assemblies, look in $packageRoot/$packageId/$version/$resourceGrandparent
                     *
                     * For example, a deps file may contain
                     *
                     * "Example/1.0.0": {
                     *    "runtime": {
                     *         "lib/netcoreapp2.0/Example.dll": { }
                     *     },
                     *     "resources": {
                     *         "lib/netcoreapp2.0/es/Example.resources.dll": {
                     *           "locale": "es"
                     *         }
                     *     }
                     * }
                     *
                     * In this case, probing should happen in $packageRoot/example/1.0.0/lib/netcoreapp2.0
                     */

                    string resourceDir = Path.GetDirectoryName(Path.GetDirectoryName(resource.Path));

                    if (resourceDir != null)
                    {
                        string path = Path.Combine(library.Name.ToLowerInvariant(),
                                                   library.Version,
                                                   resourceDir);

                        builder.AddResourceProbingSubpath(path);
                    }
                }
            }

            foreach (NativeLibrary native in dependencyContext.ResolveNativeAssets(fallbackGraph))
            {
                builder.AddNativeLibrary(native);
            }

            return(builder);
        }
コード例 #2
0
ファイル: FrameworkDependencyFile.cs プロジェクト: 3F/IeXod
        internal static bool TryGetMostFitRuntimeIdentifier(
            string currentRuntimeIdentifier,
            string alternativeCurrentRuntimeIdentifier,
            IReadOnlyList <RuntimeFallbacks> runtimeGraph,
            string[] candidateRuntimeIdentifiers,
            out string mostFitRuntimeIdentifier)
        {
            mostFitRuntimeIdentifier = null;
            RuntimeFallbacks[] runtimeFallbacksCandidates;

            if (!string.IsNullOrEmpty(currentRuntimeIdentifier))
            {
                runtimeFallbacksCandidates =
                    runtimeGraph
                    .Where(g => string.Equals(g.Runtime, currentRuntimeIdentifier, StringComparison.OrdinalIgnoreCase))
                    .ToArray();
            }
            else
            {
                runtimeFallbacksCandidates = Array.Empty <RuntimeFallbacks>();
            }

            if (runtimeFallbacksCandidates.Length == 0 && !string.IsNullOrEmpty(alternativeCurrentRuntimeIdentifier))
            {
                runtimeFallbacksCandidates =
                    runtimeGraph
                    .Where(g => string.Equals(g.Runtime, alternativeCurrentRuntimeIdentifier, StringComparison.OrdinalIgnoreCase))
                    .ToArray();
            }

            if (runtimeFallbacksCandidates.Length == 0)
            {
                return(false);
            }

            RuntimeFallbacks runtimeFallbacks = runtimeFallbacksCandidates[0];

            var runtimeFallbacksIncludesRuntime = new List <string>();

            runtimeFallbacksIncludesRuntime.Add(runtimeFallbacks.Runtime);
            runtimeFallbacksIncludesRuntime.AddRange(runtimeFallbacks.Fallbacks);


            var candidateMap = candidateRuntimeIdentifiers
                               .Distinct(comparer: StringComparer.OrdinalIgnoreCase)
                               .ToDictionary(x => x, StringComparer.OrdinalIgnoreCase);

            foreach (var fallback in runtimeFallbacksIncludesRuntime)
            {
                if (candidateMap.TryGetValue(fallback, out string match))
                {
                    mostFitRuntimeIdentifier = match;

                    return(true);
                }
            }

            return(false);
        }
コード例 #3
0
        public void GetDefaultRuntimeFallbacks_MatchesCurrentRuntimeFallbacks()
        {
            foreach (var fallback in DependencyContext.Default.RuntimeGraph)
            {
                RuntimeFallbacks result = DependencyHelper.GetDefaultRuntimeFallbacks(fallback.Runtime);

                bool match = result.Fallbacks
                             .Zip(fallback.Fallbacks, (s1, s2) => string.Equals(s1, s2, StringComparison.Ordinal))
                             .All(r => r);

                Assert.True(match, $"Mismatched fallbacks for RID '{fallback.Runtime}'");
            }
        }
コード例 #4
0
        private static List <string> GetRuntimeFallbacks()
        {
            string currentRuntimeIdentifier = GetRuntimeIdentifier();

            RuntimeFallbacks fallbacks = DependencyContext.Default
                                         .RuntimeGraph
                                         .FirstOrDefault(f => string.Equals(f.Runtime, currentRuntimeIdentifier, StringComparison.OrdinalIgnoreCase))
                                         ?? DependencyHelper.GetDefaultRuntimeFallbacks(currentRuntimeIdentifier)
                                         ?? new RuntimeFallbacks("any");

            var rids = new List <string> {
                fallbacks.Runtime
            };

            rids.AddRange(fallbacks.Fallbacks);
            return(rids);
        }
コード例 #5
0
        public static List <string> GetRuntimeFallbacks(string rid)
        {
            if (rid == null)
            {
                throw new ArgumentNullException(nameof(rid));
            }

            RuntimeFallbacks fallbacks = DependencyContext.Default
                                         .RuntimeGraph
                                         .FirstOrDefault(f => string.Equals(f.Runtime, rid, StringComparison.OrdinalIgnoreCase))
                                         ?? GetDefaultRuntimeFallbacks(rid)
                                         ?? new RuntimeFallbacks("any");

            var rids = new List <string> {
                fallbacks.Runtime
            };

            rids.AddRange(fallbacks.Fallbacks);
            return(rids);
        }
コード例 #6
0
        private string GetRuntimeAssetPath(string assetFileName, bool isNativeAsset)
        {
            string basePath     = _probingPaths[0];
            string ridSubFolder = isNativeAsset ? "native" : string.Empty;
            string runtimesPath = Path.Combine(basePath, "runtimes");

            string           currentRuntimeIdentifier = GetRuntimeIdentifier();
            RuntimeFallbacks fallbacks = DependencyContext.Default
                                         .RuntimeGraph
                                         .FirstOrDefault(f => string.Equals(f.Runtime, currentRuntimeIdentifier, StringComparison.OrdinalIgnoreCase));

            var rids = new List <string> {
                fallbacks.Runtime
            };

            rids.AddRange(fallbacks.Fallbacks);

            return(rids.Select(r => Path.Combine(runtimesPath, r, ridSubFolder, assetFileName))
                   .FirstOrDefault(p => File.Exists(p)));
        }
コード例 #7
0
        /// <summary>
        /// Gets the default runtime fallback RIDs for a given RID.
        /// The graph used to build the fallback list is static and
        /// useful in self-contained scenarios, where this information
        /// is not available at runtime
        /// </summary>
        /// <param name="rid">The runtime identifier to lookup.</param>
        /// <returns>The runtime fallbacks for the provided identifier.</returns>
        public static RuntimeFallbacks GetDefaultRuntimeFallbacks(string rid)
        {
            var ridGraph = _ridGraph.Value;

            var runtimeFallbacks = new RuntimeFallbacks(rid);
            var fallbacks        = new List <string>();

            if (!ridGraph.ContainsKey(rid))
            {
                rid = GetDefaultPlatformRid();
                fallbacks.Add(rid);
            }

            var queue = new Queue <string>(ridGraph[rid]);

            while (queue.Count > 0)
            {
                var currentRid = queue.Dequeue();

                if (fallbacks.Contains(currentRid))
                {
                    continue;
                }

                fallbacks.Add(currentRid);

                foreach (var fallbackRid in ridGraph[currentRid])
                {
                    if (!fallbacks.Contains(fallbackRid, StringComparer.OrdinalIgnoreCase))
                    {
                        queue.Enqueue(fallbackRid);
                    }
                }
            }

            runtimeFallbacks.Fallbacks = fallbacks.AsReadOnly();
            return(runtimeFallbacks);
        }
コード例 #8
0
 private static IEnumerable <string> GetRids(RuntimeFallbacks runtimeGraph)
 {
     return(new[] { runtimeGraph.Runtime }.Concat(runtimeGraph?.Fallbacks ?? Enumerable.Empty <string>()));
 }
コード例 #9
0
        private static IEnumerable <NativeLibrary> ResolveNativeAssets(this DependencyContext depContext, RuntimeFallbacks runtimeGraph)
        {
            var rids = GetRids(runtimeGraph);

            return(from library in depContext.RuntimeLibraries
                   from assetPath in SelectAssets(rids, library.NativeLibraryGroups)
                   // some packages include symbols alongside native assets, such as System.Native.a or pwshplugin.pdb
                   where PlatformInformation.NativeLibraryExtensions.Contains(Path.GetExtension(assetPath), StringComparer.OrdinalIgnoreCase)
                   select NativeLibrary.CreateFromPackage(library.Name, library.Version, assetPath));
        }
コード例 #10
0
        private static IEnumerable <ManagedLibrary> ResolveRuntimeAssemblies(this DependencyContext depContext, RuntimeFallbacks runtimeGraph)
        {
            var rids = GetRids(runtimeGraph);

            return(from library in depContext.RuntimeLibraries
                   from assetPath in SelectAssets(rids, library.RuntimeAssemblyGroups)
                   select ManagedLibrary.CreateFromPackage(library.Name, library.Version, assetPath));
        }
コード例 #11
0
        static IEnumerable <NativeLibrary> ResolveNativeAssets(this DependencyContext depContext, RuntimeFallbacks runtimeGraph)
        {
            var rids = GetRids(runtimeGraph);

            return(from library in depContext.RuntimeLibraries
                   from assetPath in SelectAssets(rids, library.NativeLibraryGroups)
                   where !assetPath.EndsWith(".a", StringComparison.Ordinal)
                   select NativeLibrary.CreateFromPackage(assetPath));
        }
コード例 #12
0
        private static IEnumerable <NativeLibrary> ResolveNativeAssets(this DependencyContext depContext, RuntimeFallbacks runtimeGraph)
        {
            var rids = GetRids(runtimeGraph);

            return(from library in depContext.RuntimeLibraries
                   from assetPath in SelectAssets(rids, library.NativeLibraryGroups)
                   // workaround for System.Native.a being included in the deps.json file for Microsoft.NETCore.App
                   where !assetPath.EndsWith(".a", StringComparison.Ordinal)
                   select NativeLibrary.CreateFromPackage(library.Name, library.Version, assetPath));
        }
コード例 #13
0
        private static LibraryAssetGroup GetGroup(IEnumerable <LibraryAssetGroup> groups, string runtimeIdentifier, RuntimeFallbacks fallbacks)
        {
            IEnumerable <string> rids = new[] { runtimeIdentifier };

            if (fallbacks != null)
            {
                rids = Enumerable.Concat(rids, fallbacks.Fallbacks);
            }

            foreach (var rid in rids)
            {
                var group = groups.GetRuntimeGroup(rid);
                if (group != null)
                {
                    return(group);
                }
            }
            return(null);
        }
コード例 #14
0
        private static IEnumerable <ManagedLibrary> ResolveRuntimeAssemblies(DependencyContext depContext, RuntimeFallbacks runtimeGraph)
        {
            var rids = GetRids(runtimeGraph);

            return(depContext.RuntimeLibraries.SelectMany(x => SelectAssets(rids, x.RuntimeAssemblyGroups).Select(assetPath => ManagedLibrary.CreateFromPackage(x.Name, x.Version, assetPath))));
        }
コード例 #15
0
 private static LibraryAssetGroup SelectGroup(IEnumerable <LibraryAssetGroup> groups, RuntimeFallbacks fallbacks)
 {
     foreach (var runtime in fallbacks.AllRuntimes())
     {
         var group = groups.GetRuntimeGroup(runtime);
         if (group != null)
         {
             return(group);
         }
     }
     return(groups.GetDefaultGroup());
 }
コード例 #16
0
 public static IEnumerable <string> AllRuntimes(this RuntimeFallbacks fallback)
 => Enumerable.Concat(new[] { fallback.Runtime }, fallback.Fallbacks);