private static void AddPackageTargetFallbacks(PackageSpec spec, IEnumerable <IMSBuildItem> items) { foreach (var item in GetItemByType(items, "TargetFrameworkInformation")) { var frameworkString = item.GetProperty("TargetFramework"); var frameworks = new List <TargetFrameworkInformation>(); if (!string.IsNullOrEmpty(frameworkString)) { frameworks.Add(spec.GetTargetFramework(NuGetFramework.Parse(frameworkString))); } else { frameworks.AddRange(spec.TargetFrameworks); } foreach (var targetFrameworkInfo in frameworks) { var packageTargetFallback = MSBuildStringUtility.Split(item.GetProperty("PackageTargetFallback")) .Select(NuGetFramework.Parse) .ToList(); var assetTargetFallback = MSBuildStringUtility.Split(item.GetProperty(AssetTargetFallbackUtility.AssetTargetFallback)) .Select(NuGetFramework.Parse) .ToList(); // Throw if an invalid combination was used. AssetTargetFallbackUtility.EnsureValidFallback(packageTargetFallback, assetTargetFallback, spec.FilePath); // Update the framework appropriately AssetTargetFallbackUtility.ApplyFramework(targetFrameworkInfo, packageTargetFallback, assetTargetFallback); } } }
static void AddPackageTargetFallbacks(PackageSpec packageSpec, IDotNetProject project) { var packageTargetFallback = GetPackageTargetFallbackList(project) .Select(NuGetFramework.Parse) .ToList(); var assetTargetFallback = GetAssetTargetFallbackList(project) .Select(NuGetFramework.Parse) .ToList(); if (!packageTargetFallback.Any() && !assetTargetFallback.Any()) { return; } AssetTargetFallbackUtility.EnsureValidFallback( packageTargetFallback, assetTargetFallback, packageSpec.FilePath); var frameworks = GetProjectFrameworks(project); foreach (var framework in frameworks) { var frameworkInfo = packageSpec.GetTargetFramework(framework); AssetTargetFallbackUtility.ApplyFramework(frameworkInfo, packageTargetFallback, assetTargetFallback); } }
public static ISet <LibraryDependency> GetPackageDependenciesForFramework(this PackageSpec project, NuGetFramework framework) { // Remove non-package dependencies such as framework assembly references. return(new HashSet <LibraryDependency>( project.Dependencies.Concat(project.GetTargetFramework(framework).Dependencies) .Where(e => e.LibraryRange.TypeConstraintAllows(LibraryDependencyTarget.Package)))); }
private static void AddPackageTargetFallbacks(PackageSpec spec, IEnumerable <IMSBuildItem> items) { foreach (var item in GetItemByType(items, "TargetFrameworkInformation")) { var frameworkString = item.GetProperty("TargetFramework"); TargetFrameworkInformation targetFrameworkInfo = null; if (!string.IsNullOrEmpty(frameworkString)) { targetFrameworkInfo = spec.GetTargetFramework(NuGetFramework.Parse(frameworkString)); } Debug.Assert(targetFrameworkInfo != null, $"PackageSpec does not contain the target framework: {frameworkString}"); if (targetFrameworkInfo != null) { var fallbackList = MSBuildStringUtility.Split(item.GetProperty("PackageTargetFallback")) .Select(NuGetFramework.Parse) .ToList(); targetFrameworkInfo.Imports = fallbackList; // Update the PackageSpec framework to include fallback frameworks if (targetFrameworkInfo.Imports.Count > 0) { targetFrameworkInfo.FrameworkName = new FallbackFramework(targetFrameworkInfo.FrameworkName, fallbackList); } } } }
private static void AddTargetFrameworkSpecificProperties(PackageSpec spec, IEnumerable <IMSBuildItem> items) { foreach (var item in GetItemByType(items, "TargetFrameworkProperties")) { var frameworkString = item.GetProperty("TargetFramework"); var targetFrameworkInformation = spec.GetTargetFramework(NuGetFramework.Parse(frameworkString)); targetFrameworkInformation.RuntimeIdentifierGraphPath = item.GetProperty("RuntimeIdentifierGraphPath"); } }
private static void AddCentralPackageVersions(PackageSpec spec, IEnumerable <IMSBuildItem> items) { var centralVersionsDependencies = CreateCentralVersionDependencies(items, spec.TargetFrameworks); foreach (var framework in centralVersionsDependencies.Keys) { var frameworkInfo = spec.GetTargetFramework(framework); frameworkInfo.CentralPackageVersions.AddRange(centralVersionsDependencies[framework]); } }
private static bool AddDependencyIfNotExist(PackageSpec spec, NuGetFramework framework, string frameworkReference) { var frameworkInfo = spec.GetTargetFramework(framework); if (!frameworkInfo.FrameworkReferences.Contains(frameworkReference)) { frameworkInfo.FrameworkReferences.Add(frameworkReference); return(true); } return(false); }
private static bool AddDownloadDependencyIfNotExist(PackageSpec spec, NuGetFramework framework, DownloadDependency dependency) { var frameworkInfo = spec.GetTargetFramework(framework); if (!frameworkInfo.DownloadDependencies.Contains(dependency)) { frameworkInfo.DownloadDependencies.Add(dependency); return(true); } return(false); }
private static bool AddFrameworkReferenceIfNotExists(PackageSpec spec, NuGetFramework framework, string frameworkReference, string privateAssetsValue) { var frameworkInfo = spec.GetTargetFramework(framework); if (!frameworkInfo .FrameworkReferences .Select(f => f.Name) .Contains(frameworkReference, ComparisonUtility.FrameworkReferenceNameComparer)) { var privateAssets = FrameworkDependencyFlagsUtils.GetFlags(MSBuildStringUtility.Split(privateAssetsValue)); frameworkInfo.FrameworkReferences.Add(new FrameworkDependency(frameworkReference, privateAssets)); return(true); } return(false); }
private static bool AddDependencyIfNotExist(PackageSpec spec, NuGetFramework framework, LibraryDependency dependency) { var frameworkInfo = spec.GetTargetFramework(framework); if (!spec.Dependencies .Concat(frameworkInfo.Dependencies) .Select(d => d.Name) .Contains(dependency.Name, StringComparer.OrdinalIgnoreCase)) { frameworkInfo.Dependencies.Add(dependency); return(true); } return(false); }
static void AddPackageTargetFallbacks(PackageSpec packageSpec, IDotNetProject project) { var fallbackList = GetPackageTargetFallbackList(project) .Select(NuGetFramework.Parse) .ToList(); if (!fallbackList.Any()) { return; } var frameworks = GetProjectFrameworks(project); foreach (var framework in frameworks) { var frameworkInfo = packageSpec.GetTargetFramework(framework); frameworkInfo.Imports = fallbackList; frameworkInfo.FrameworkName = new FallbackFramework(frameworkInfo.FrameworkName, fallbackList); } }
/// <summary> /// Extracts PackageSpecific WarningProperties from a PackageSpec for a specific NuGetFramework /// </summary> /// <param name="packageSpec">PackageSpec containing the Dependencies with WarningProperties</param> /// <param name="framework">NuGetFramework for which the properties should be assessed.</param> /// <returns>PackageSpecific WarningProperties extracted from a PackageSpec for a specific NuGetFramework</returns> public static PackageSpecificWarningProperties CreatePackageSpecificWarningProperties(PackageSpec packageSpec, NuGetFramework framework) { // NuGetLogCode -> LibraryId -> Set of Frameworks. var warningProperties = new PackageSpecificWarningProperties(); foreach (var dependency in packageSpec.Dependencies) { warningProperties.AddRangeOfCodes(dependency.NoWarn, dependency.Name, framework); } var targetFrameworkInformation = packageSpec.GetTargetFramework(framework); foreach (var dependency in targetFrameworkInformation.Dependencies) { warningProperties.AddRangeOfCodes(dependency.NoWarn, dependency.Name, framework); } return(warningProperties); }
internal static Dictionary <string, LibraryIncludeFlags> FlattenDependencyTypes( RestoreTargetGraph targetGraph, PackageSpec spec) { var result = new Dictionary <string, LibraryIncludeFlags>(StringComparer.OrdinalIgnoreCase); // Walk dependencies FlattenDependencyTypesUnified(targetGraph, result); // Override flags for direct dependencies var directDependencies = spec.Dependencies.ToList(); // Add dependencies defined under the framework node var specFramework = spec.GetTargetFramework(targetGraph.Framework); if (specFramework?.Dependencies != null) { directDependencies.AddRange(specFramework.Dependencies); } // Override the flags for direct dependencies. This lets the // user take control when needed. foreach (var dependency in directDependencies) { if (result.ContainsKey(dependency.Name)) { result[dependency.Name] = dependency.IncludeType; } else { result.Add(dependency.Name, dependency.IncludeType); } } return(result); }
/// <summary> /// Added a workaround for netcoreapp2.0 and netstandard2.0 projects so they /// add a net461 PackageTargetFallback. This information is included in the /// .NET Core 2.0 SDK but requires an sdk resolver to work. Once the resolver /// is working this workaround can be removed. /// </summary> static void AddPackageTargetFallbacks(PackageSpec packageSpec, IDotNetProject project) { var fallbackList = GetPackageTargetFallbackList(project) .Select(NuGetFramework.Parse) .ToList(); // May need to add net461 even if the project does not have any PackageTargetFallback items so // do not return here. // if (!fallbackList.Any ()) // return; var frameworks = GetProjectFrameworks(project); foreach (var framework in frameworks) { var updatedFallbackList = ModifyFallbackList(fallbackList, framework); if (updatedFallbackList.Any()) { var frameworkInfo = packageSpec.GetTargetFramework(framework); frameworkInfo.Imports = updatedFallbackList; frameworkInfo.FrameworkName = new FallbackFramework(frameworkInfo.FrameworkName, updatedFallbackList); } } }
public LockFile CreateLockFile(LockFile previousLockFile, PackageSpec project, IEnumerable <RestoreTargetGraph> targetGraphs, IReadOnlyList <NuGetv3LocalRepository> localRepositories, RemoteWalkContext context) { var lockFile = new LockFile() { Version = _lockFileVersion }; var previousLibraries = previousLockFile?.Libraries.ToDictionary(l => Tuple.Create(l.Name, l.Version)); if (project.RestoreMetadata?.ProjectStyle == ProjectStyle.PackageReference || project.RestoreMetadata?.ProjectStyle == ProjectStyle.DotnetToolReference) { AddProjectFileDependenciesForPackageReference(project, lockFile, targetGraphs); } else { AddProjectFileDependenciesForSpec(project, lockFile); } // Record all libraries used foreach (var item in targetGraphs.SelectMany(g => g.Flattened).Distinct() .OrderBy(x => x.Data.Match.Library)) { var library = item.Data.Match.Library; if (project.Name.Equals(library.Name, StringComparison.OrdinalIgnoreCase)) { // Do not include the project itself as a library. continue; } if (library.Type == LibraryType.Project || library.Type == LibraryType.ExternalProject) { // Project var localMatch = (LocalMatch)item.Data.Match; var projectLib = new LockFileLibrary() { Name = library.Name, Version = library.Version, Type = LibraryType.Project, }; // Set the relative path if a path exists // For projects without project.json this will be empty if (!string.IsNullOrEmpty(localMatch.LocalLibrary.Path)) { projectLib.Path = PathUtility.GetRelativePath( project.FilePath, localMatch.LocalLibrary.Path, '/'); } // The msbuild project path if it exists object msbuildPath; if (localMatch.LocalLibrary.Items.TryGetValue(KnownLibraryProperties.MSBuildProjectPath, out msbuildPath)) { var msbuildRelativePath = PathUtility.GetRelativePath( project.FilePath, (string)msbuildPath, '/'); projectLib.MSBuildProject = msbuildRelativePath; } lockFile.Libraries.Add(projectLib); } else if (library.Type == LibraryType.Package) { // Packages var packageInfo = NuGetv3LocalRepositoryUtility.GetPackage(localRepositories, library.Name, library.Version); // Add the library if it was resolved, unresolved packages are not added to the assets file. if (packageInfo != null) { var package = packageInfo.Package; var resolver = packageInfo.Repository.PathResolver; var sha512 = package.Sha512; var path = PathUtility.GetPathWithForwardSlashes(resolver.GetPackageDirectory(package.Id, package.Version)); LockFileLibrary lockFileLib = null; LockFileLibrary previousLibrary = null; if (previousLibraries?.TryGetValue(Tuple.Create(package.Id, package.Version), out previousLibrary) == true) { // Check that the previous library is still valid if (previousLibrary != null && StringComparer.Ordinal.Equals(path, previousLibrary.Path) && StringComparer.Ordinal.Equals(sha512, previousLibrary.Sha512)) { // We mutate this previous library so we must take a clone of it. This is // important because later, when deciding whether the lock file has changed, // we compare the new lock file to the previous (in-memory) lock file. lockFileLib = previousLibrary.Clone(); } } // Create a new lock file library if one doesn't exist already. if (lockFileLib == null) { lockFileLib = CreateLockFileLibrary(package, sha512, path); } // Create a new lock file library lockFile.Libraries.Add(lockFileLib); } } } var libraries = lockFile.Libraries.ToDictionary(lib => Tuple.Create(lib.Name, lib.Version)); var librariesWithWarnings = new HashSet <LibraryIdentity>(); var rootProjectStyle = project.RestoreMetadata?.ProjectStyle ?? ProjectStyle.Unknown; // Cache package data and selection criteria across graphs. var builderCache = new LockFileBuilderCache(); // Add the targets foreach (var targetGraph in targetGraphs .OrderBy(graph => graph.Framework.ToString(), StringComparer.Ordinal) .ThenBy(graph => graph.RuntimeIdentifier, StringComparer.Ordinal)) { var target = new LockFileTarget { TargetFramework = targetGraph.Framework, RuntimeIdentifier = targetGraph.RuntimeIdentifier }; var flattenedFlags = IncludeFlagUtils.FlattenDependencyTypes(_includeFlagGraphs, project, targetGraph); // Check if warnings should be displayed for the current framework. var tfi = project.GetTargetFramework(targetGraph.Framework); bool warnForImportsOnGraph = tfi.Warn && (target.TargetFramework is FallbackFramework || target.TargetFramework is AssetTargetFallbackFramework); foreach (var graphItem in targetGraph.Flattened.OrderBy(x => x.Key)) { var library = graphItem.Key; // include flags LibraryIncludeFlags includeFlags; if (!flattenedFlags.TryGetValue(library.Name, out includeFlags)) { includeFlags = ~LibraryIncludeFlags.ContentFiles; } if (library.Type == LibraryType.Project || library.Type == LibraryType.ExternalProject) { if (project.Name.Equals(library.Name, StringComparison.OrdinalIgnoreCase)) { // Do not include the project itself as a library. continue; } var projectLib = LockFileUtils.CreateLockFileTargetProject( graphItem, library, includeFlags, targetGraph, rootProjectStyle); target.Libraries.Add(projectLib); continue; } else if (library.Type == LibraryType.Package) { var packageInfo = NuGetv3LocalRepositoryUtility.GetPackage(localRepositories, library.Name, library.Version); if (packageInfo == null) { continue; } var package = packageInfo.Package; var targetLibrary = LockFileUtils.CreateLockFileTargetLibrary( libraries[Tuple.Create(library.Name, library.Version)], package, targetGraph, dependencyType: includeFlags, targetFrameworkOverride: null, dependencies: graphItem.Data.Dependencies, cache: builderCache); target.Libraries.Add(targetLibrary); // Log warnings if the target library used the fallback framework if (warnForImportsOnGraph && !librariesWithWarnings.Contains(library)) { var nonFallbackFramework = new NuGetFramework(target.TargetFramework); var targetLibraryWithoutFallback = LockFileUtils.CreateLockFileTargetLibrary( libraries[Tuple.Create(library.Name, library.Version)], package, targetGraph, targetFrameworkOverride: nonFallbackFramework, dependencyType: includeFlags, dependencies: graphItem.Data.Dependencies, cache: builderCache); if (!targetLibrary.Equals(targetLibraryWithoutFallback)) { var libraryName = DiagnosticUtility.FormatIdentity(library); var message = string.Format(CultureInfo.CurrentCulture, Strings.Log_ImportsFallbackWarning, libraryName, GetFallbackFrameworkString(target.TargetFramework), nonFallbackFramework); var logMessage = RestoreLogMessage.CreateWarning( NuGetLogCode.NU1701, message, library.Name, targetGraph.TargetGraphName); _logger.Log(logMessage); // only log the warning once per library librariesWithWarnings.Add(library); } } } } lockFile.Targets.Add(target); } PopulatePackageFolders(localRepositories.Select(repo => repo.RepositoryRoot).Distinct(), lockFile); AddCentralTransitiveDependencyGroupsForPackageReference(project, lockFile, targetGraphs); // Add the original package spec to the lock file. lockFile.PackageSpec = project; return(lockFile); }