private static void InitializePackageDependencies( LockFile assetsFile, Dictionary <NuGetFramework, HashSet <LibraryDependency> > dependenciesByFramework, ISet <NuGetFramework> frameworkWithSuppressedDependencies) { // From the package spec, we know the direct package dependencies of this project. foreach (var framework in assetsFile.PackageSpec.TargetFrameworks) { if (frameworkWithSuppressedDependencies.Contains(framework.FrameworkName)) { continue; } // First, add each of the generic package dependencies to the framework-specific list. var packageDependencies = assetsFile .PackageSpec .Dependencies .Concat(framework.Dependencies); HashSet <LibraryDependency> dependencies; if (!dependenciesByFramework.TryGetValue(framework.FrameworkName, out dependencies)) { dependencies = new HashSet <LibraryDependency>(); dependenciesByFramework[framework.FrameworkName] = dependencies; } // Add each package dependency. foreach (var packageDependency in packageDependencies) { // If we have a floating package dependency like 1.2.3-xyz-*, we // use the version of the package that restore resolved it to. if (packageDependency.LibraryRange.VersionRange.IsFloating) { var lockFileTarget = assetsFile.GetTarget(framework.FrameworkName, runtimeIdentifier: null); var package = lockFileTarget.Libraries.First( library => string.Equals(library.Name, packageDependency.Name, StringComparison.OrdinalIgnoreCase)); if (package != null) { if (packageDependency.LibraryRange.VersionRange.HasUpperBound) { packageDependency.LibraryRange.VersionRange = new VersionRange( minVersion: package.Version, includeMinVersion: packageDependency.LibraryRange.VersionRange.IsMinInclusive, maxVersion: packageDependency.LibraryRange.VersionRange.MaxVersion, includeMaxVersion: packageDependency.LibraryRange.VersionRange.IsMaxInclusive); } else { packageDependency.LibraryRange.VersionRange = new VersionRange( minVersion: package.Version, includeMinVersion: packageDependency.LibraryRange.VersionRange.IsMinInclusive); } } } PackCommandRunner.AddLibraryDependency(packageDependency, dependencies); } } }
private static void InitializeProjectDependencies( LockFile assetsFile, IDictionary <NuGetFramework, HashSet <LibraryDependency> > dependenciesByFramework, IDictionary <string, string> projectRefToVersionMap, ISet <NuGetFramework> frameworkWithSuppressedDependencies) { // From the package spec, all we know is each absolute path to the project reference the the target // framework that project reference applies to. if (assetsFile.PackageSpec.RestoreMetadata == null) { return; } // Using the libraries section of the assets file, the library name and version for the project path. var projectPathToLibraryIdentities = assetsFile .Libraries .Where(library => library.MSBuildProject != null) .ToLookup( library => Path.GetFullPath(Path.Combine( Path.GetDirectoryName(assetsFile.PackageSpec.RestoreMetadata.ProjectPath), PathUtility.GetPathWithDirectorySeparator(library.MSBuildProject))), library => new PackageIdentity(library.Name, library.Version), PathUtility.GetStringComparerBasedOnOS()); // Consider all of the project references, grouped by target framework. foreach (var framework in assetsFile.PackageSpec.RestoreMetadata.TargetFrameworks) { var target = assetsFile.GetTarget(framework.FrameworkName, runtimeIdentifier: null); if (target == null || frameworkWithSuppressedDependencies.Contains(framework.FrameworkName)) { continue; } HashSet <LibraryDependency> dependencies; if (!dependenciesByFramework.TryGetValue(framework.FrameworkName, out dependencies)) { dependencies = new HashSet <LibraryDependency>(); dependenciesByFramework[framework.FrameworkName] = dependencies; } // For the current target framework, create a map from library identity to library model. This allows // us to be sure we have picked the correct library (name and version) for this target framework. var libraryIdentityToTargetLibrary = target .Libraries .ToLookup(library => new PackageIdentity(library.Name, library.Version)); foreach (var projectReference in framework.ProjectReferences) { var libraryIdentities = projectPathToLibraryIdentities[projectReference.ProjectPath]; var targetLibrary = libraryIdentities .Select(identity => libraryIdentityToTargetLibrary[identity].FirstOrDefault()) .FirstOrDefault(library => library != null); if (targetLibrary == null) { continue; } var versionToUse = new VersionRange(targetLibrary.Version); // Use the project reference version obtained at build time if it exists, otherwise fallback to the one in assets file. if (projectRefToVersionMap.TryGetValue(projectReference.ProjectPath, out var projectRefVersion)) { versionToUse = VersionRange.Parse(projectRefVersion, allowFloating: false); } // TODO: Implement <TreatAsPackageReference>false</TreatAsPackageReference> // https://github.com/NuGet/Home/issues/3891 // // For now, assume the project reference is a package dependency. var projectDependency = new LibraryDependency { LibraryRange = new LibraryRange( targetLibrary.Name, versionToUse, LibraryDependencyTarget.All), IncludeType = projectReference.IncludeAssets & ~projectReference.ExcludeAssets, SuppressParent = projectReference.PrivateAssets }; PackCommandRunner.AddLibraryDependency(projectDependency, dependencies); } } }
private void InitializePackageDependencies( LockFile assetsFile, Dictionary <NuGetFramework, HashSet <LibraryDependency> > dependenciesByFramework, ISet <NuGetFramework> frameworkWithSuppressedDependencies) { var packageSpecificNoWarnProperties = new Dictionary <string, HashSet <(NuGetLogCode, NuGetFramework)> >(StringComparer.OrdinalIgnoreCase); // From the package spec, we know the direct package dependencies of this project. foreach (TargetFrameworkInformation framework in assetsFile.PackageSpec.TargetFrameworks) { if (frameworkWithSuppressedDependencies.Contains(framework.FrameworkName)) { continue; } IEnumerable <LibraryDependency> centralTransitiveDependencies = assetsFile .CentralTransitiveDependencyGroups .Where(centralTDG => centralTDG.FrameworkName.Equals(framework.FrameworkName.ToString(), StringComparison.OrdinalIgnoreCase)) .SelectMany(centralTDG => centralTDG.TransitiveDependencies); // First, add each of the generic package dependencies to the framework-specific list. var packageDependencies = assetsFile .PackageSpec .Dependencies .Concat(framework.Dependencies) .Concat(centralTransitiveDependencies); HashSet <LibraryDependency> dependencies; if (!dependenciesByFramework.TryGetValue(framework.FrameworkName, out dependencies)) { dependencies = new HashSet <LibraryDependency>(); dependenciesByFramework[framework.FrameworkName] = dependencies; } // Add each package dependency. foreach (var packageDependency in packageDependencies) { // If we have a floating package dependency like 1.2.3-xyz-*, we // use the version of the package that restore resolved it to. if (packageDependency.LibraryRange.VersionRange.IsFloating) { var lockFileTarget = assetsFile.GetTarget(framework.FrameworkName, runtimeIdentifier: null); var package = lockFileTarget.Libraries.First( library => string.Equals(library.Name, packageDependency.Name, StringComparison.OrdinalIgnoreCase)); if (package != null) { if (packageDependency.LibraryRange.VersionRange.HasUpperBound) { packageDependency.LibraryRange.VersionRange = new VersionRange( minVersion: package.Version, includeMinVersion: packageDependency.LibraryRange.VersionRange.IsMinInclusive, maxVersion: packageDependency.LibraryRange.VersionRange.MaxVersion, includeMaxVersion: packageDependency.LibraryRange.VersionRange.IsMaxInclusive); } else { packageDependency.LibraryRange.VersionRange = new VersionRange( minVersion: package.Version, includeMinVersion: packageDependency.LibraryRange.VersionRange.IsMinInclusive); } } } if (packageDependency.NoWarn.Count > 0) { HashSet <(NuGetLogCode, NuGetFramework)> nowarnProperties = null; if (!packageSpecificNoWarnProperties.TryGetValue(packageDependency.Name, out nowarnProperties)) { nowarnProperties = new HashSet <(NuGetLogCode, NuGetFramework)>(); } nowarnProperties.AddRange(packageDependency.NoWarn.Select(n => (n, framework.FrameworkName))); packageSpecificNoWarnProperties[packageDependency.Name] = nowarnProperties; } PackCommandRunner.AddLibraryDependency(packageDependency, dependencies); } } if (packageSpecificNoWarnProperties.Keys.Count > 0) { _packageSpecificWarningProperties = PackageSpecificWarningProperties.CreatePackageSpecificWarningProperties(packageSpecificNoWarnProperties); } }