Example #1
0
        private async Task <GraphItem> FindLibraryEntry(RestoreContext context, LibraryRange libraryRange)
        {
            _report.WriteLine(string.Format("Attempting to resolve dependency {0} {1}", libraryRange.Name.Bold(), libraryRange.VersionRange));

            Task <WalkProviderMatch> task;

            lock (context.MatchCache)
            {
                if (!context.MatchCache.TryGetValue(libraryRange, out task))
                {
                    task = FindLibraryMatch(context, libraryRange);
                    context.MatchCache[libraryRange] = task;
                }
            }

            var match = await task;

            if (match == null)
            {
                return(null);
            }

            var dependencies = await match.Provider.GetDependencies(match, context.FrameworkName);

            return(new GraphItem
            {
                Match = match,
                Dependencies = dependencies,
            });
        }
Example #2
0
        private async Task <WalkProviderMatch> FindProjectMatch(RestoreContext context, LibraryRange libraryRange)
        {
            foreach (var provider in context.ProjectLibraryProviders)
            {
                var match = await provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted : false);

                if (match != null)
                {
                    return(match);
                }
            }

            return(null);
        }
Example #3
0
        public Task <GraphItem> FindLibraryCached(RestoreContext context, LibraryRange libraryRange)
        {
            lock (context.GraphItemCache)
            {
                Task <GraphItem> task;
                if (!context.GraphItemCache.TryGetValue(libraryRange, out task))
                {
                    task = FindLibraryEntry(context, libraryRange);
                    context.GraphItemCache[libraryRange] = task;
                }

                return(task);
            }
        }
Example #4
0
        private async Task <WalkProviderMatch> FindLibraryByVersion(RestoreContext context, LibraryRange libraryRange, IEnumerable <IWalkProvider> providers)
        {
            if (libraryRange.VersionRange.VersionFloatBehavior != SemanticVersionFloatBehavior.None)
            {
                // Don't optimize the non http path for floating versions or we'll miss things
                return(await FindLibrary(libraryRange, providers, provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: false)));
            }

            // Try the non http sources first
            // non-http sources don't support list/unlist so set includeUnlisted to true
            var nonHttpMatch = await FindLibrary(libraryRange, providers.Where(p => !p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: true));

            // If we found an exact match then use it
            if (nonHttpMatch != null && nonHttpMatch.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
            {
                return(nonHttpMatch);
            }

            // Otherwise try listed packages on http sources
            var httpMatch = await FindLibrary(libraryRange, providers.Where(p => p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: false));

            // If the http sources failed to find a listed package that matched, try unlisted packages
            if (httpMatch == null)
            {
                httpMatch = await FindLibrary(libraryRange, providers.Where(p => p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: true));
            }

            // Pick the best match of the 2
            if (VersionUtility.ShouldUseConsidering(
                    nonHttpMatch?.Library?.Version,
                    httpMatch?.Library.Version,
                    libraryRange.VersionRange))
            {
                return(httpMatch);
            }

            return(nonHttpMatch);
        }
Example #5
0
        public async Task<GraphNode> CreateGraphNode(RestoreContext context, LibraryRange libraryRange, Func<string, bool> predicate)
        {
            var dependencies = new List<LibraryDependency>();

            if (context.RuntimeDependencies != null)
            {
                DependencySpec dependencyMapping;
                if (context.RuntimeDependencies.TryGetValue(libraryRange.Name, out dependencyMapping))
                {
                    foreach (var runtimeDependency in dependencyMapping.Implementations.Values)
                    {
                        var libraryDependency = new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(runtimeDependency.Name, frameworkReference: false)
                            {
                                VersionRange = VersionUtility.ParseVersionRange(runtimeDependency.Version)
                            }
                        };

                        // HACK(davidfowl): This is making runtime.json support package redirects
                        if (libraryDependency.LibraryRange.Name == libraryRange.Name)
                        {
                            // It's replacing the current version, we need to override rather than adding a (potentially circular) dependency
                            if (libraryRange.VersionRange != null &&
                                libraryDependency.LibraryRange.VersionRange != null &&
                                libraryRange.VersionRange.MinVersion < libraryDependency.LibraryRange.VersionRange.MinVersion)
                            {
                                libraryRange = libraryDependency.LibraryRange;
                            }
                        }
                        else
                        {
                            // Otherwise it's a dependency of this node
                            dependencies.Add(libraryDependency);
                        }
                    }
                }
            }

            var node = new GraphNode
            {
                LibraryRange = libraryRange,
                Item = await FindLibraryCached(context, libraryRange),
            };

            if (node.Item != null)
            {
                if (node.LibraryRange.VersionRange != null &&
                    node.LibraryRange.VersionRange.VersionFloatBehavior != SemanticVersionFloatBehavior.None)
                {
                    lock (context.GraphItemCache)
                    {
                        if (!context.GraphItemCache.ContainsKey(node.LibraryRange))
                        {
                            context.GraphItemCache[node.LibraryRange] = Task.FromResult(node.Item);
                        }
                    }
                }

                dependencies.AddRange(node.Item.Dependencies);

                var tasks = new List<Task<GraphNode>>();
                foreach (var dependency in dependencies)
                {
                    if (predicate(dependency.Name))
                    {
                        tasks.Add(CreateGraphNode(context, dependency.LibraryRange, ChainPredicate(predicate, node.Item, dependency)));
                    }
                }

                while (tasks.Any())
                {
                    var task = await Task.WhenAny(tasks);
                    tasks.Remove(task);
                    var dependency = await task;
                    node.Dependencies.Add(dependency);
                }
            }
            return node;
        }
Example #6
0
        private async Task<WalkProviderMatch> FindLibraryMatch(RestoreContext context, LibraryRange libraryRange)
        {
            var projectMatch = await FindProjectMatch(context, libraryRange);

            if (projectMatch != null)
            {
                return projectMatch;
            }

            if (libraryRange.VersionRange == null)
            {
                return null;
            }

            if (libraryRange.IsGacOrFrameworkReference)
            {
                return null;
            }

            if (libraryRange.VersionRange.VersionFloatBehavior != SemanticVersionFloatBehavior.None)
            {
                // For snapshot dependencies, get the version remotely first.
                var remoteMatch = await FindLibraryByVersion(context, libraryRange, context.RemoteLibraryProviders);
                if (remoteMatch == null)
                {
                    // If there was nothing remotely, use the local match (if any)
                    var localMatch = await FindLibraryByVersion(context, libraryRange, context.LocalLibraryProviders);
                    return localMatch;
                }
                else
                {
                    // Now check the local repository
                    var localMatch = await FindLibraryByVersion(context, libraryRange, context.LocalLibraryProviders);

                    if (localMatch != null && remoteMatch != null)
                    {
                        // We found a match locally and remotely, so pick the better version
                        // in relation to the specified version.
                        if (VersionUtility.ShouldUseConsidering(
                            current: remoteMatch.Library.Version,
                            considering: localMatch.Library.Version,
                            ideal: libraryRange.VersionRange))
                        {
                            return localMatch;
                        }

                        // The remote match is better
                    }

                    // Try to see if the specific version found on the remote exists locally. This avoids any unnecessary
                    // remote access incase we already have it in the cache/local packages folder.
                    localMatch = await FindLibraryByVersion(context, remoteMatch.Library, context.LocalLibraryProviders);

                    if (localMatch != null && localMatch.Library.Version.Equals(remoteMatch.Library.Version))
                    {
                        // If we have a local match, and it matches the version *exactly* then use it.
                        return localMatch;
                    }

                    // We found something locally, but it wasn't an exact match
                    // for the resolved remote match.
                    return remoteMatch;
                }
            }
            else
            {
                // Check for the specific version locally.
                var localMatch = await FindLibraryByVersion(context, libraryRange, context.LocalLibraryProviders);

                if (localMatch != null && localMatch.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
                {
                    // We have an exact match so use it.
                    return localMatch;
                }

                // Either we found a local match but it wasn't the exact version, or 
                // we didn't find a local match.
                var remoteMatch = await FindLibraryByVersion(context, libraryRange, context.RemoteLibraryProviders);

                if (remoteMatch != null && localMatch == null)
                {
                    // There wasn't any local match for the specified version but there was a remote match.
                    // See if that version exists locally.
                    localMatch = await FindLibraryByVersion(context, remoteMatch.Library, context.LocalLibraryProviders);
                }

                if (localMatch != null && remoteMatch != null)
                {
                    // We found a match locally and remotely, so pick the better version
                    // in relation to the specified version.
                    if (VersionUtility.ShouldUseConsidering(
                        current: localMatch.Library.Version,
                        considering: remoteMatch.Library.Version,
                        ideal: libraryRange.VersionRange))
                    {
                        return remoteMatch;
                    }
                    else
                    {
                        return localMatch;
                    }
                }

                // Prefer local over remote generally.
                return localMatch ?? remoteMatch;
            }
        }
Example #7
0
        private async Task<GraphItem> FindLibraryEntry(RestoreContext context, LibraryRange libraryRange)
        {
            _report.WriteLine(string.Format("Attempting to resolve dependency {0} {1}", libraryRange.Name.Bold(), libraryRange.VersionRange));

            Task<WalkProviderMatch> task;
            lock (context.MatchCache)
            {
                if (!context.MatchCache.TryGetValue(libraryRange, out task))
                {
                    task = FindLibraryMatch(context, libraryRange);
                    context.MatchCache[libraryRange] = task;
                }
            }

            var match = await task;

            if (match == null)
            {
                return null;
            }

            var dependencies = await match.Provider.GetDependencies(match, context.FrameworkName);

            return new GraphItem
            {
                Match = match,
                Dependencies = dependencies,
            };
        }
Example #8
0
        public Task<GraphItem> FindLibraryCached(RestoreContext context, LibraryRange libraryRange)
        {
            lock (context.GraphItemCache)
            {
                Task<GraphItem> task;
                if (!context.GraphItemCache.TryGetValue(libraryRange, out task))
                {
                    task = FindLibraryEntry(context, libraryRange);
                    context.GraphItemCache[libraryRange] = task;
                }

                return task;
            }
        }
Example #9
0
        private async Task <TargetContext> CreateGraphNode(RestoreOperations restoreOperations, RestoreContext context, LibraryRange libraryRange, Func <object, bool> predicate)
        {
            var walkStopwatch = Stopwatch.StartNew();
            var node          = await restoreOperations.CreateGraphNode(context, libraryRange, predicate);

            Reports.WriteVerbose($" Walked graph for {context.FrameworkName}/{context.RuntimeName} in {walkStopwatch.ElapsedMilliseconds:0.00}ms");
            walkStopwatch.Stop();
            return(new TargetContext
            {
                RestoreContext = context,
                Root = node
            });
        }
Example #10
0
        private async Task <bool> RestoreForProject(string projectJsonPath, string rootDirectory, string packagesDirectory, IList <IWalkProvider> remoteProviders, SummaryContext summary)
        {
            var success = true;

            Reports.Information.WriteLine(string.Format("Restoring packages for {0}", projectJsonPath.Bold()));

            var sw = new Stopwatch();

            sw.Start();

            var projectFolder       = Path.GetDirectoryName(projectJsonPath);
            var projectLockFilePath = Path.Combine(projectFolder, LockFileFormat.LockFileName);

            Runtime.Project project;
            var             diagnostics = new List <DiagnosticMessage>();

            if (!Runtime.Project.TryGetProject(projectJsonPath, out project, diagnostics))
            {
                var errorMessages = diagnostics
                                    .Where(x => x.Severity == DiagnosticMessageSeverity.Error)
                                    .Select(x => x.Message);

                throw new InvalidOperationException(errorMessages.Any() ?
                                                    $"Errors occurred when while parsing project.json:{Environment.NewLine}{string.Join(Environment.NewLine, errorMessages)}" :
                                                    "Invalid project.json");
            }

            if (diagnostics.HasErrors())
            {
                var errorMessages = diagnostics
                                    .Where(x => x.Severity == DiagnosticMessageSeverity.Error)
                                    .Select(x => x.Message);
                summary.ErrorMessages.GetOrAdd(projectJsonPath, _ => new List <string>()).AddRange(errorMessages);
            }

            var lockFile = await ReadLockFile(projectLockFilePath);

            var useLockFile = false;

            if (Lock == false &&
                Unlock == false &&
                lockFile != null &&
                lockFile.Islocked)
            {
                useLockFile = true;
            }

            if (useLockFile && !lockFile.IsValidForProject(project))
            {
                // Exhibit the same behavior as if it has been run with "dnu restore --lock"
                Reports.Information.WriteLine("Updating the invalid lock file with {0}",
                                              "dnu restore --lock".Yellow().Bold());
                useLockFile = false;
                Lock        = true;
            }

            Func <string, string> getVariable = key =>
            {
                return(null);
            };

            if (!SkipRestoreEvents)
            {
                if (!ScriptExecutor.Execute(project, "prerestore", getVariable))
                {
                    summary.ErrorMessages.GetOrAdd("prerestore", _ => new List <string>()).Add(ScriptExecutor.ErrorMessage);
                    Reports.Error.WriteLine(ScriptExecutor.ErrorMessage);
                    return(false);
                }
            }

            var projectDirectory  = project.ProjectDirectory;
            var projectResolver   = new ProjectResolver(projectDirectory, rootDirectory);
            var packageRepository = new PackageRepository(packagesDirectory)
            {
                CheckHashFile = CheckHashFile
            };
            var restoreOperations = new RestoreOperations(Reports.Verbose);
            var projectProviders  = new List <IWalkProvider>();
            var localProviders    = new List <IWalkProvider>();
            var contexts          = new List <RestoreContext>();
            var cache             = new Dictionary <LibraryRange, Task <WalkProviderMatch> >();

            projectProviders.Add(
                new LocalWalkProvider(
                    new ProjectReferenceDependencyProvider(
                        projectResolver)));

            localProviders.Add(
                new LocalWalkProvider(
                    new NuGetDependencyResolver(packageRepository)));

            var tasks = new List <Task <TargetContext> >();

            if (useLockFile)
            {
                Reports.Information.WriteLine(string.Format("Following lock file {0}", projectLockFilePath.White().Bold()));

                var context = new RestoreContext
                {
                    FrameworkName           = FallbackFramework,
                    ProjectLibraryProviders = projectProviders,
                    LocalLibraryProviders   = localProviders,
                    RemoteLibraryProviders  = remoteProviders,
                    MatchCache = cache
                };

                contexts.Add(context);

                foreach (var lockFileLibrary in lockFile.PackageLibraries)
                {
                    var projectLibrary = new LibraryRange(lockFileLibrary.Name, frameworkReference: false)
                    {
                        VersionRange = new SemanticVersionRange
                        {
                            MinVersion           = lockFileLibrary.Version,
                            MaxVersion           = lockFileLibrary.Version,
                            IsMaxInclusive       = true,
                            VersionFloatBehavior = SemanticVersionFloatBehavior.None,
                        }
                    };

                    tasks.Add(CreateGraphNode(restoreOperations, context, projectLibrary, _ => false));
                }
            }
            else
            {
                var frameworks = TargetFrameworks.Count == 0 ? project.GetTargetFrameworks().Select(f => f.FrameworkName) : TargetFrameworks;

                foreach (var frameworkName in frameworks)
                {
                    var context = new RestoreContext
                    {
                        FrameworkName           = frameworkName,
                        ProjectLibraryProviders = projectProviders,
                        LocalLibraryProviders   = localProviders,
                        RemoteLibraryProviders  = remoteProviders,
                        MatchCache = cache
                    };
                    contexts.Add(context);
                }

                if (!contexts.Any())
                {
                    contexts.Add(new RestoreContext
                    {
                        FrameworkName           = FallbackFramework,
                        ProjectLibraryProviders = projectProviders,
                        LocalLibraryProviders   = localProviders,
                        RemoteLibraryProviders  = remoteProviders,
                        MatchCache = cache
                    });
                }

                foreach (var context in contexts)
                {
                    var projectLibrary = new LibraryRange(project.Name, frameworkReference: false)
                    {
                        VersionRange = new SemanticVersionRange(project.Version)
                    };

                    tasks.Add(CreateGraphNode(restoreOperations, context, projectLibrary, _ => true));
                }
            }

            var targetContexts = await Task.WhenAll(tasks);

            foreach (var targetContext in targetContexts)
            {
                Reduce(targetContext.Root);
            }

            if (!useLockFile)
            {
                var projectRuntimeFile = RuntimeFile.ParseFromProject(project);
                var restoreRuntimes    = GetRestoreRuntimes(projectRuntimeFile.Runtimes.Keys).ToList();
                if (restoreRuntimes.Any())
                {
                    var runtimeTasks = new List <Task <TargetContext> >();

                    foreach (var pair in contexts.Zip(targetContexts, (context, graph) => new { context, graph }))
                    {
                        var runtimeFileTasks = new List <Task <RuntimeFile> >();
                        ForEach(pair.graph.Root, node =>
                        {
                            var match = node?.Item?.Match;
                            if (match == null)
                            {
                                return;
                            }
                            runtimeFileTasks.Add(match.Provider.GetRuntimes(node.Item.Match, pair.context.FrameworkName));
                        });

                        var libraryRuntimeFiles = await Task.WhenAll(runtimeFileTasks);

                        var runtimeFiles = new List <RuntimeFile> {
                            projectRuntimeFile
                        };
                        runtimeFiles.AddRange(libraryRuntimeFiles.Where(file => file != null));

                        foreach (var runtimeName in restoreRuntimes)
                        {
                            Reports.WriteVerbose($"Restoring packages for {pair.context.FrameworkName} on {runtimeName}...");
                            var runtimeDependencies = new Dictionary <string, DependencySpec>();
                            var runtimeNames        = new HashSet <string>();
                            var runtimeStopwatch    = Stopwatch.StartNew();
                            FindRuntimeDependencies(
                                runtimeName,
                                runtimeFiles,
                                runtimeDependencies,
                                runtimeNames);
                            runtimeStopwatch.Stop();
                            Reports.WriteVerbose($" Scanned Runtime graph in {runtimeStopwatch.ElapsedMilliseconds:0.00}ms");

                            // If there are no runtime specs in the graph, we still want to restore for the specified runtime, so synthesize one
                            if (!runtimeNames.Any(r => r.Equals(runtimeName)))
                            {
                                runtimeNames.Add(runtimeName);
                            }

                            var runtimeContext = new RestoreContext
                            {
                                FrameworkName           = pair.context.FrameworkName,
                                ProjectLibraryProviders = pair.context.ProjectLibraryProviders,
                                LocalLibraryProviders   = pair.context.LocalLibraryProviders,
                                RemoteLibraryProviders  = pair.context.RemoteLibraryProviders,
                                RuntimeName             = runtimeName,
                                AllRuntimeNames         = runtimeNames,
                                RuntimeDependencies     = runtimeDependencies,
                                MatchCache = cache
                            };
                            var projectLibrary = new LibraryRange(project.Name, frameworkReference: false)
                            {
                                VersionRange = new SemanticVersionRange(project.Version)
                            };

                            runtimeTasks.Add(CreateGraphNode(restoreOperations, runtimeContext, projectLibrary, _ => true));
                        }
                    }

                    var runtimeTragetContexts = await Task.WhenAll(runtimeTasks);

                    foreach (var runtimeTargetContext in runtimeTragetContexts)
                    {
                        Reduce(runtimeTargetContext.Root);
                    }

                    targetContexts = targetContexts.Concat(runtimeTragetContexts).ToArray();
                }
            }

            var graphItems   = new List <GraphItem>();
            var installItems = new List <GraphItem>();
            var missingItems = new HashSet <LibraryRange>();

            foreach (var context in targetContexts)
            {
                ForEach(context.Root, node =>
                {
                    if (node == null || node.LibraryRange == null)
                    {
                        return;
                    }

                    if (node.Item == null || node.Item.Match == null)
                    {
                        // This is a workaround for #1322. Since we use restore to generate the lock file
                        // after publish, it's possible to fail restore after copying the closure
                        if (!IgnoreMissingDependencies)
                        {
                            if (!node.LibraryRange.IsGacOrFrameworkReference &&
                                missingItems.Add(node.LibraryRange))
                            {
                                var versionString = node.LibraryRange.VersionRange == null ?
                                                    string.Empty :
                                                    (" " + node.LibraryRange.VersionRange.ToString());
                                var errorMessage =
                                    $"Unable to locate {DependencyTargets.GetDisplayForTarget(node.LibraryRange.Target)} " +
                                    $"{node.LibraryRange.Name.Red().Bold()}{versionString}";
                                summary.ErrorMessages.GetOrAdd(projectJsonPath, _ => new List <string>()).Add(errorMessage);
                                Reports.Error.WriteLine(errorMessage);
                                success = false;
                            }
                        }

                        return;
                    }

                    if (!string.Equals(node.Item.Match.Library.Name, node.LibraryRange.Name, StringComparison.Ordinal))
                    {
                        // Fix casing of the library name to be installed
                        node.Item.Match.Library = node.Item.Match.Library.ChangeName(node.LibraryRange.Name);
                    }

                    var isRemote      = remoteProviders.Contains(node.Item.Match.Provider);
                    var isInstallItem = installItems.Any(item => item.Match.Library == node.Item.Match.Library);

                    if (!isInstallItem && isRemote)
                    {
                        // It's ok to download rejected nodes so we avoid downloading them in the future
                        // The trade off is that subsequent restores avoid going to any remotes
                        installItems.Add(node.Item);
                    }

                    // Don't add rejected nodes since we only want to write reduced nodes
                    // to the lock file
                    if (node.Disposition != GraphNode.DispositionType.Rejected)
                    {
                        var isGraphItem = graphItems.Any(item => item.Match.Library == node.Item.Match.Library);

                        if (!isGraphItem)
                        {
                            graphItems.Add(node.Item);
                        }

                        context.Matches.Add(node.Item.Match);
                    }
                });
            }

            if (!SkipInstall)
            {
                await InstallPackages(installItems, packagesDirectory);

                summary.InstallCount += installItems.Count;
            }

            if (!useLockFile)
            {
                Reports.Information.WriteLine(string.Format("Writing lock file {0}", projectLockFilePath.White().Bold()));

                var repository = new PackageRepository(packagesDirectory);

                WriteLockFile(lockFile,
                              projectLockFilePath,
                              project,
                              graphItems,
                              repository,
                              projectResolver,
                              targetContexts);
            }

            if (!SkipRestoreEvents)
            {
                if (!ScriptExecutor.Execute(project, "postrestore", getVariable))
                {
                    summary.ErrorMessages.GetOrAdd("postrestore", _ => new List <string>()).Add(ScriptExecutor.ErrorMessage);
                    Reports.Error.WriteLine(ScriptExecutor.ErrorMessage);
                    return(false);
                }

                if (!ScriptExecutor.Execute(project, "prepare", getVariable))
                {
                    summary.ErrorMessages.GetOrAdd("prepare", _ => new List <string>()).Add(ScriptExecutor.ErrorMessage);
                    Reports.Error.WriteLine(ScriptExecutor.ErrorMessage);
                    return(false);
                }
            }

            Reports.Information.WriteLine(string.Format("{0}, {1}ms elapsed", "Restore complete".Green().Bold(), sw.ElapsedMilliseconds));

            return(success);
        }
Example #11
0
        public async Task<GraphNode> CreateGraphNode(RestoreContext context, LibraryRange libraryRange, Func<string, bool> predicate)
        {
            var sw = new Stopwatch();
            sw.Start();

            var node = new GraphNode
            {
                LibraryRange = libraryRange,
                Item = await FindLibraryCached(context, libraryRange),
            };

            if (node.Item != null)
            {
                if (node.LibraryRange.VersionRange != null &&
                    node.LibraryRange.VersionRange.VersionFloatBehavior != SemanticVersionFloatBehavior.None)
                {
                    lock (context.GraphItemCache)
                    {
                        if (!context.GraphItemCache.ContainsKey(node.LibraryRange))
                        {
                            context.GraphItemCache[node.LibraryRange] = Task.FromResult(node.Item);
                        }
                    }
                }

                var tasks = new List<Task<GraphNode>>();
                var dependencies = node.Item.Dependencies ?? Enumerable.Empty<LibraryDependency>();
                foreach (var dependency in dependencies)
                {
                    if (predicate(dependency.Name))
                    {
                        tasks.Add(CreateGraphNode(context, dependency.LibraryRange, ChainPredicate(predicate, node.Item, dependency)));

                        if (context.RuntimeSpecs != null)
                        {
                            foreach (var runtimeSpec in context.RuntimeSpecs)
                            {
                                DependencySpec dependencyMapping;
                                if (runtimeSpec.Dependencies.TryGetValue(dependency.Name, out dependencyMapping))
                                {
                                    foreach (var dependencyImplementation in dependencyMapping.Implementations.Values)
                                    {
                                        tasks.Add(CreateGraphNode(
                                            context,
                                            new LibraryRange(dependencyImplementation.Name, frameworkReference: false)
                                            {
                                                VersionRange = VersionUtility.ParseVersionRange(dependencyImplementation.Version)
                                            },
                                            ChainPredicate(predicate, node.Item, dependency)));
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }

                while (tasks.Any())
                {
                    var task = await Task.WhenAny(tasks);
                    tasks.Remove(task);
                    var dependency = await task;
                    node.Dependencies.Add(dependency);
                }
            }
            return node;
        }
Example #12
0
        public async Task <GraphNode> CreateGraphNode(RestoreContext context, LibraryRange libraryRange, Func <string, bool> predicate)
        {
            var sw = new Stopwatch();

            sw.Start();

            var node = new GraphNode
            {
                LibraryRange = libraryRange,
                Item         = await FindLibraryCached(context, libraryRange),
            };

            if (node.Item != null)
            {
                if (node.LibraryRange.VersionRange != null &&
                    node.LibraryRange.VersionRange.VersionFloatBehavior != SemanticVersionFloatBehavior.None)
                {
                    lock (context.GraphItemCache)
                    {
                        if (!context.GraphItemCache.ContainsKey(node.LibraryRange))
                        {
                            context.GraphItemCache[node.LibraryRange] = Task.FromResult(node.Item);
                        }
                    }
                }

                var tasks        = new List <Task <GraphNode> >();
                var dependencies = node.Item.Dependencies ?? Enumerable.Empty <LibraryDependency>();
                foreach (var dependency in dependencies)
                {
                    if (predicate(dependency.Name))
                    {
                        tasks.Add(CreateGraphNode(context, dependency.LibraryRange, ChainPredicate(predicate, node.Item, dependency)));

                        if (context.RuntimeSpecs != null)
                        {
                            foreach (var runtimeSpec in context.RuntimeSpecs)
                            {
                                DependencySpec dependencyMapping;
                                if (runtimeSpec.Dependencies.TryGetValue(dependency.Name, out dependencyMapping))
                                {
                                    foreach (var dependencyImplementation in dependencyMapping.Implementations.Values)
                                    {
                                        tasks.Add(CreateGraphNode(
                                                      context,
                                                      new LibraryRange(dependencyImplementation.Name, frameworkReference: false)
                                        {
                                            VersionRange = VersionUtility.ParseVersionRange(dependencyImplementation.Version)
                                        },
                                                      ChainPredicate(predicate, node.Item, dependency)));
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }

                while (tasks.Any())
                {
                    var task = await Task.WhenAny(tasks);

                    tasks.Remove(task);
                    var dependency = await task;
                    node.Dependencies.Add(dependency);
                }
            }
            return(node);
        }
Example #13
0
        private async Task <WalkProviderMatch> FindLibraryMatch(RestoreContext context, LibraryRange libraryRange)
        {
            var projectMatch = await FindProjectMatch(context, libraryRange.Name);

            if (projectMatch != null)
            {
                return(projectMatch);
            }

            if (libraryRange.VersionRange == null)
            {
                return(null);
            }

            if (libraryRange.IsGacOrFrameworkReference)
            {
                return(null);
            }

            if (libraryRange.VersionRange.VersionFloatBehavior != SemanticVersionFloatBehavior.None)
            {
                // For snapshot dependencies, get the version remotely first.
                var remoteMatch = await FindLibraryByVersion(context, libraryRange, context.RemoteLibraryProviders);

                if (remoteMatch == null)
                {
                    // If there was nothing remotely, use the local match (if any)
                    var localMatch = await FindLibraryByVersion(context, libraryRange, context.LocalLibraryProviders);

                    return(localMatch);
                }
                else
                {
                    // Now check the local repository
                    var localMatch = await FindLibraryByVersion(context, libraryRange, context.LocalLibraryProviders);

                    if (localMatch != null && remoteMatch != null)
                    {
                        // We found a match locally and remotely, so pick the better version
                        // in relation to the specified version.
                        if (VersionUtility.ShouldUseConsidering(
                                current: remoteMatch.Library.Version,
                                considering: localMatch.Library.Version,
                                ideal: libraryRange.VersionRange))
                        {
                            return(localMatch);
                        }

                        // The remote match is better
                    }

                    // Try to see if the specific version found on the remote exists locally. This avoids any unnecessary
                    // remote access incase we already have it in the cache/local packages folder.
                    localMatch = await FindLibraryByVersion(context, remoteMatch.Library, context.LocalLibraryProviders);

                    if (localMatch != null && localMatch.Library.Version.Equals(remoteMatch.Library.Version))
                    {
                        // If we have a local match, and it matches the version *exactly* then use it.
                        return(localMatch);
                    }

                    // We found something locally, but it wasn't an exact match
                    // for the resolved remote match.
                    return(remoteMatch);
                }
            }
            else
            {
                // Check for the specific version locally.
                var localMatch = await FindLibraryByVersion(context, libraryRange, context.LocalLibraryProviders);

                if (localMatch != null && localMatch.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
                {
                    // We have an exact match so use it.
                    return(localMatch);
                }

                // Either we found a local match but it wasn't the exact version, or
                // we didn't find a local match.
                var remoteMatch = await FindLibraryByVersion(context, libraryRange, context.RemoteLibraryProviders);

                if (remoteMatch != null && localMatch == null)
                {
                    // There wasn't any local match for the specified version but there was a remote match.
                    // See if that version exists locally.
                    localMatch = await FindLibraryByVersion(context, remoteMatch.Library, context.LocalLibraryProviders);
                }

                if (localMatch != null && remoteMatch != null)
                {
                    // We found a match locally and remotely, so pick the better version
                    // in relation to the specified version.
                    if (VersionUtility.ShouldUseConsidering(
                            current: localMatch.Library.Version,
                            considering: remoteMatch.Library.Version,
                            ideal: libraryRange.VersionRange))
                    {
                        return(remoteMatch);
                    }
                    else
                    {
                        return(localMatch);
                    }
                }

                // Prefer local over remote generally.
                return(localMatch ?? remoteMatch);
            }
        }
Example #14
0
        private async Task<WalkProviderMatch> FindProjectMatch(RestoreContext context, LibraryRange libraryRange)
        {
            foreach (var provider in context.ProjectLibraryProviders)
            {
                var match = await provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: false);
                if (match != null)
                {
                    return match;
                }
            }

            return null;
        }
Example #15
0
        private async Task <TargetContext> CreateGraphNode(RestoreOperations restoreOperations, RestoreContext context, LibraryRange libraryRange, Func <object, bool> predicate)
        {
            var node = await restoreOperations.CreateGraphNode(context, libraryRange, predicate);

            return(new TargetContext
            {
                RestoreContext = context,
                Root = node
            });
        }
Example #16
0
        private async Task<WalkProviderMatch> FindLibraryByVersion(RestoreContext context, LibraryRange libraryRange, IEnumerable<IWalkProvider> providers)
        {
            if (libraryRange.VersionRange.VersionFloatBehavior != SemanticVersionFloatBehavior.None)
            {
                // Don't optimize the non http path for floating versions or we'll miss things
                return await FindLibrary(libraryRange, providers, provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: false));
            }

            // Try the non http sources first
            // non-http sources don't support list/unlist so set includeUnlisted to true
            var nonHttpMatch = await FindLibrary(libraryRange, providers.Where(p => !p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: true));

            // If we found an exact match then use it
            if (nonHttpMatch != null && nonHttpMatch.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
            {
                return nonHttpMatch;
            }

            // Otherwise try listed packages on http sources
            var httpMatch = await FindLibrary(libraryRange, providers.Where(p => p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: false));

            // If the http sources failed to find a listed package that matched, try unlisted packages
            if (httpMatch == null)
            {
                httpMatch = await FindLibrary(libraryRange, providers.Where(p => p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: true));
            }

            // Pick the best match of the 2
            if (VersionUtility.ShouldUseConsidering(
                nonHttpMatch?.Library?.Version,
                httpMatch?.Library.Version,
                libraryRange.VersionRange))
            {
                return httpMatch;
            }

            return nonHttpMatch;
        }
Example #17
0
        public async Task <GraphNode> CreateGraphNode(RestoreContext context, LibraryRange libraryRange, Func <string, bool> predicate)
        {
            var dependencies = new List <LibraryDependency>();

            if (context.RuntimeDependencies != null)
            {
                DependencySpec dependencyMapping;
                if (context.RuntimeDependencies.TryGetValue(libraryRange.Name, out dependencyMapping))
                {
                    foreach (var runtimeDependency in dependencyMapping.Implementations.Values)
                    {
                        var libraryDependency = new LibraryDependency
                        {
                            LibraryRange = new LibraryRange(runtimeDependency.Name, frameworkReference: false)
                            {
                                VersionRange = VersionUtility.ParseVersionRange(runtimeDependency.Version)
                            }
                        };

                        // HACK(davidfowl): This is making runtime.json support package redirects
                        if (libraryDependency.LibraryRange.Name == libraryRange.Name)
                        {
                            // It's replacing the current version, we need to override rather than adding a (potentially circular) dependency
                            if (libraryRange.VersionRange != null &&
                                libraryDependency.LibraryRange.VersionRange != null &&
                                libraryRange.VersionRange.MinVersion < libraryDependency.LibraryRange.VersionRange.MinVersion)
                            {
                                libraryRange = libraryDependency.LibraryRange;
                            }
                        }
                        else
                        {
                            // Otherwise it's a dependency of this node
                            dependencies.Add(libraryDependency);
                        }
                    }
                }
            }

            var node = new GraphNode
            {
                LibraryRange = libraryRange,
                Item         = await FindLibraryCached(context, libraryRange),
            };

            if (node.Item != null)
            {
                if (node.LibraryRange.VersionRange != null &&
                    node.LibraryRange.VersionRange.VersionFloatBehavior != SemanticVersionFloatBehavior.None)
                {
                    lock (context.GraphItemCache)
                    {
                        if (!context.GraphItemCache.ContainsKey(node.LibraryRange))
                        {
                            context.GraphItemCache[node.LibraryRange] = Task.FromResult(node.Item);
                        }
                    }
                }

                dependencies.AddRange(node.Item.Dependencies);

                var tasks = new List <Task <GraphNode> >();
                foreach (var dependency in dependencies)
                {
                    if (predicate(dependency.Name))
                    {
                        tasks.Add(CreateGraphNode(context, dependency.LibraryRange, ChainPredicate(predicate, node.Item, dependency)));
                    }
                }

                while (tasks.Any())
                {
                    var task = await Task.WhenAny(tasks);

                    tasks.Remove(task);
                    var dependency = await task;
                    node.Dependencies.Add(dependency);
                }
            }
            return(node);
        }