Example #1
0
        public void Walk(IPackage package)
        {
            CheckPackageMinClientVersion(package);

            // Do nothing if we saw this package already
            if (Marker.IsVisited(package))
            {
                ProcessPackageTarget(package);
                return;
            }

            OnBeforePackageWalk(package);

            // Mark the package as processing
            Marker.MarkProcessing(package);

            if (!IgnoreDependencies)
            {
                foreach (var dependency in package.GetCompatiblePackageDependencies(TargetFramework))
                {
                    // Try to resolve the dependency from the visited packages first
                    IPackage resolvedDependency = DependencyResolveUtility.ResolveDependency(
                        Marker,
                        dependency, constraintProvider: null,
                        allowPrereleaseVersions: AllowPrereleaseVersions,
                        preferListedPackages: false,
                        dependencyVersion: DependencyVersion);
                    if (resolvedDependency == null)
                    {
                        resolvedDependency = ResolveDependency(dependency);
                    }

                    if (resolvedDependency == null)
                    {
                        OnDependencyResolveError(dependency);
                        // If we're skipping dependency resolve errors then move on to the next
                        // dependency
                        if (SkipDependencyResolveError)
                        {
                            continue;
                        }

                        return;
                    }

                    if (!IgnoreWalkInfo)
                    {
                        // Set the parent
                        PackageWalkInfo dependencyInfo = GetPackageInfo(resolvedDependency);
                        dependencyInfo.Parent = package;
                    }

                    Marker.AddDependent(package, resolvedDependency);

                    if (!OnAfterResolveDependency(package, resolvedDependency))
                    {
                        continue;
                    }

                    if (Marker.IsCycle(resolvedDependency) ||
                        Marker.IsVersionCycle(resolvedDependency.Id))
                    {
                        if (RaiseErrorOnCycle)
                        {
                            List <IPackage> packages = Marker.Packages.ToList();
                            packages.Add(resolvedDependency);

                            throw new InvalidOperationException(
                                      String.Format(CultureInfo.CurrentCulture,
                                                    NuGetResources.CircularDependencyDetected, String.Join(" => ",
                                                                                                           packages.Select(p => p.GetFullName()))));
                        }

                        continue;
                    }

                    Walk(resolvedDependency);
                }
            }

            // Mark the package as visited
            Marker.MarkVisited(package);

            ProcessPackageTarget(package);

            OnAfterPackageWalk(package);
        }
Example #2
0
        public void Walk(IPackage package)
        {
            // Do nothing if we saw this package already
            if (Marker.IsVisited(package))
            {
                return;
            }

            OnBeforePackageWalk(package);

            // Mark the package as processing
            Marker.MarkProcessing(package);

            if (!IgnoreDependencies)
            {
                foreach (var dependency in package.Dependencies)
                {
                    // Try to resolve the dependency from the visited packages first
                    IPackage resolvedDependency = Marker.FindDependency(dependency) ??
                                                  ResolveDependency(dependency);

                    if (resolvedDependency == null)
                    {
                        OnDependencyResolveError(dependency);
                        // If we're skipping dependency resolve errros then move on to the next
                        // dependency
                        if (SkipDependencyResolveError)
                        {
                            continue;
                        }

                        return;
                    }

                    if (!IgnoreWalkInfo)
                    {
                        // Set the parent
                        PackageWalkInfo dependencyInfo = GetPackageInfo(resolvedDependency);
                        dependencyInfo.Parent = package;
                    }

                    Marker.AddDependent(package, resolvedDependency);

                    if (!OnAfterResolveDependency(package, resolvedDependency))
                    {
                        continue;
                    }

                    if (Marker.IsCycle(resolvedDependency) ||
                        Marker.IsVersionCycle(resolvedDependency.Id))
                    {
                        if (RaiseErrorOnCycle)
                        {
                            List <IPackage> packages = Marker.Packages.ToList();
                            packages.Add(resolvedDependency);

                            throw new InvalidOperationException(
                                      String.Format(CultureInfo.CurrentCulture,
                                                    NuGetResources.CircularDependencyDetected, String.Join(" => ",
                                                                                                           packages.Select(p => p.GetFullName()))));
                        }

                        continue;
                    }

                    Walk(resolvedDependency);
                }
            }

            // Mark the package as visited
            Marker.MarkVisited(package);

            if (!IgnoreWalkInfo)
            {
                PackageWalkInfo info = GetPackageInfo(package);

                // If our parent is an unknown then we need to bubble up the type
                if (info.Parent != null)
                {
                    PackageWalkInfo parentInfo = GetPackageInfo(info.Parent);

                    Debug.Assert(parentInfo != null);

                    if (parentInfo.InitialTarget == PackageTargets.None)
                    {
                        // Update the parent target type
                        parentInfo.Target |= info.Target;

                        // If we ended up with both that means we found a dependency only packges
                        // that has a mix of solution and project level packages
                        if (parentInfo.Target == PackageTargets.Both)
                        {
                            throw new InvalidOperationException(NuGetResources.DependencyOnlyCannotMixDependencies);
                        }
                    }

                    // Solution packages can't depend on project level packages
                    if (parentInfo.Target == PackageTargets.External && info.Target.HasFlag(PackageTargets.Project))
                    {
                        throw new InvalidOperationException(NuGetResources.ExternalPackagesCannotDependOnProjectLevelPackages);
                    }
                }
            }

            OnAfterPackageWalk(package);
        }