private JProperty WriteLibrary(LockFileLibrary library) { var json = new JObject(); WriteBool(json, "serviceable", library.IsServiceable); json["sha"] = WriteString(library.Sha); WriteObject(json, "frameworks", library.FrameworkGroups, WriteFrameworkGroup); WritePathArray(json, "files", library.Files, WriteString); return(new JProperty( library.Name + "/" + library.Version.ToString(), json)); }
public PackageInfo( IFileSystem repositoryRoot, string packageId, SemanticVersion version, string versionDir, LockFileLibrary lockFileLibrary = null) { _repositoryRoot = repositoryRoot; Id = packageId; Version = version; _versionDir = versionDir; LockFileLibrary = lockFileLibrary; }
private LockFileLibrary ReadLibrary(string property, JToken json) { var library = new LockFileLibrary(); var parts = property.Split(new[] { '/' }, 2); library.Name = parts[0]; if (parts.Length == 2) { library.Version = SemanticVersion.Parse(parts[1]); } library.IsServiceable = ReadBool(json, "serviceable", defaultValue: false); library.Sha = ReadString(json["sha"]); library.FrameworkGroups = ReadObject(json["frameworks"] as JObject, ReadFrameworkGroup); library.Files = ReadPathArray(json["files"] as JArray, ReadString); return(library); }
public static LockFileLibrary CreateLockFileLibrary(LockFileLibrary previousLibrary, IPackagePathResolver resolver, IPackage package, string correctedPackageName = null) { var lockFileLib = new LockFileLibrary(); // package.Id is read from nuspec and it might be in wrong casing. // correctedPackageName should be the package name used by dependency graph and // it has the correct casing that runtime needs during dependency resolution. lockFileLib.Name = correctedPackageName ?? package.Id; lockFileLib.Version = package.Version; lockFileLib.Sha512 = File.ReadAllText(resolver.GetHashPath(package.Id, package.Version)); // If the shas are equal then do nothing if (previousLibrary?.Sha512 == lockFileLib.Sha512) { lockFileLib.Files = previousLibrary.Files; lockFileLib.IsServiceable = previousLibrary.IsServiceable; } else { lockFileLib.Files = package.GetFiles().Select(p => p.Path).ToList(); var installPath = resolver.GetInstallPath(package.Id, package.Version); foreach (var filePath in lockFileLib.Files) { if (!string.Equals(Path.GetExtension(filePath), ".dll", StringComparison.OrdinalIgnoreCase)) { continue; } var assemblyPath = Path.Combine(installPath, filePath); try { if (IsAssemblyServiceable(assemblyPath)) { lockFileLib.IsServiceable = true; break; } } catch { // Just move on to the next file } } } return lockFileLib; }
private LockFileLibrary ReadLibrary(string property, JsonValue json) { var jobject = json as JsonObject; if (jobject == null) { throw FileFormatException.Create("The value type is not object.", json); } var library = new LockFileLibrary(); var parts = property.Split(new[] { '/' }, 2); library.Name = parts[0]; if (parts.Length == 2) { library.Version = SemanticVersion.Parse(parts[1]); } library.IsServiceable = ReadBool(jobject, "serviceable", defaultValue: false); library.Sha512 = ReadString(jobject.Value("sha512")); library.Files = ReadPathArray(jobject.Value("files"), ReadString); return(library); }
public static LockFileTargetLibrary CreateLockFileTargetLibrary(LockFileLibrary library, IPackage package, RestoreContext context, string correctedPackageName) { var lockFileLib = new LockFileTargetLibrary(); var framework = context.FrameworkName; var runtimeIdentifier = context.RuntimeName; // package.Id is read from nuspec and it might be in wrong casing. // correctedPackageName should be the package name used by dependency graph and // it has the correct casing that runtime needs during dependency resolution. lockFileLib.Name = correctedPackageName ?? package.Id; lockFileLib.Version = package.Version; var files = library.Files.Select(p => p.Replace(Path.DirectorySeparatorChar, '/')); var contentItems = new ContentItemCollection(); contentItems.Load(files); IEnumerable<PackageDependencySet> dependencySet; if (VersionUtility.GetNearest(framework, package.DependencySets, out dependencySet)) { var set = dependencySet.FirstOrDefault()?.Dependencies?.ToList(); if (set != null) { lockFileLib.Dependencies = set; } } // TODO: Remove this when we do #596 // ASP.NET Core and .NET Core 5.0 don't have framework reference assemblies if (!VersionUtility.IsPackageBased(framework)) { IEnumerable<FrameworkAssemblyReference> frameworkAssemblies; if (VersionUtility.GetNearest(framework, package.FrameworkAssemblies, out frameworkAssemblies)) { AddFrameworkReferences(lockFileLib, framework, frameworkAssemblies); } // Add framework assemblies with empty supported frameworks AddFrameworkReferences(lockFileLib, framework, package.FrameworkAssemblies.Where(f => !f.SupportedFrameworks.Any())); } var patterns = PatternDefinitions.DotNetPatterns; var criteriaBuilderWithTfm = new SelectionCriteriaBuilder(patterns.Properties.Definitions); var criteriaBuilderWithoutTfm = new SelectionCriteriaBuilder(patterns.Properties.Definitions); if (context.RuntimeSpecs != null) { foreach (var runtimeSpec in context.RuntimeSpecs) { criteriaBuilderWithTfm = criteriaBuilderWithTfm .Add["tfm", framework]["rid", runtimeSpec.Name]; criteriaBuilderWithoutTfm = criteriaBuilderWithoutTfm .Add["rid", runtimeSpec.Name]; } } criteriaBuilderWithTfm = criteriaBuilderWithTfm .Add["tfm", framework]; var criteria = criteriaBuilderWithTfm.Criteria; var compileGroup = contentItems.FindBestItemGroup(criteria, patterns.CompileTimeAssemblies, patterns.ManagedAssemblies); if (compileGroup != null) { lockFileLib.CompileTimeAssemblies = compileGroup.Items.Select(t => (LockFileItem)t.Path).ToList(); } var runtimeGroup = contentItems.FindBestItemGroup(criteria, patterns.ManagedAssemblies); if (runtimeGroup != null) { lockFileLib.RuntimeAssemblies = runtimeGroup.Items.Select(p => (LockFileItem)p.Path).ToList(); } var resourceGroup = contentItems.FindBestItemGroup(criteria, patterns.ResourceAssemblies); if (resourceGroup != null) { lockFileLib.ResourceAssemblies = resourceGroup.Items.Select(ToResourceLockFileItem).ToList(); } var nativeGroup = contentItems.FindBestItemGroup(criteriaBuilderWithoutTfm.Criteria, patterns.NativeLibraries); if (nativeGroup != null) { lockFileLib.NativeLibraries = nativeGroup.Items.Select(p => (LockFileItem)p.Path).ToList(); } // COMPAT: Support lib/contract so older packages can be consumed string contractPath = "lib/contract/" + package.Id + ".dll"; var hasContract = files.Any(path => path == contractPath); var hasLib = lockFileLib.RuntimeAssemblies.Any(); if (hasContract && hasLib && !VersionUtility.IsDesktop(framework)) { lockFileLib.CompileTimeAssemblies.Clear(); lockFileLib.CompileTimeAssemblies.Add(contractPath); } // See if there's a list of specific references defined for this target framework IEnumerable<PackageReferenceSet> referenceSets; if (VersionUtility.GetNearest(framework, package.PackageAssemblyReferences, out referenceSets)) { // Get the first compatible reference set var referenceSet = referenceSets.FirstOrDefault(); if (referenceSet != null) { // Remove all assemblies of which names do not appear in the References list lockFileLib.RuntimeAssemblies.RemoveAll(path => path.Path.StartsWith("lib/") && !referenceSet.References.Contains(Path.GetFileName(path), StringComparer.OrdinalIgnoreCase)); lockFileLib.CompileTimeAssemblies.RemoveAll(path => path.Path.StartsWith("lib/") && !referenceSet.References.Contains(Path.GetFileName(path), StringComparer.OrdinalIgnoreCase)); } } return lockFileLib; }
public void LibrariesAreResolvedCorrectly(string packageName, string framework, bool resolved) { var repo = new PackageRepository("path/to/packages"); var resolver = new NuGetDependencyResolver(repo); var net45Target = new LockFileTarget { TargetFramework = new FrameworkName(".NETFramework,Version=v4.5"), Libraries = new List<LockFileTargetLibrary> { new LockFileTargetLibrary { Name = "MetaPackage", Version = SemanticVersion.Parse("1.0.0"), }, new LockFileTargetLibrary { Name = "Net451LibPackage", Version = SemanticVersion.Parse("1.0.0"), }, new LockFileTargetLibrary { Name = "Net451RefPackage", Version = SemanticVersion.Parse("1.0.0"), }, new LockFileTargetLibrary { Name = "AssemblyPlaceholderPackage", Version = SemanticVersion.Parse("1.0.0"), CompileTimeAssemblies = new List<LockFileItem> { Path.Combine("ref", "net45", "_._") }, RuntimeAssemblies = new List<LockFileItem> { Path.Combine("lib", "net45", "_._") } } } }; var dnx451Target = new LockFileTarget { TargetFramework = new FrameworkName("DNX,Version=v4.5.1"), Libraries = new List<LockFileTargetLibrary> { new LockFileTargetLibrary { Name = "MetaPackage", Version = SemanticVersion.Parse("1.0.0") }, new LockFileTargetLibrary { Name = "Net451LibPackage", Version = SemanticVersion.Parse("1.0.0"), CompileTimeAssemblies = new List<LockFileItem> { Path.Combine("lib", "net451", "Net451LibPackage.dll") }, RuntimeAssemblies = new List<LockFileItem> { Path.Combine("lib", "net451", "Net451LibPackage.dll") } }, new LockFileTargetLibrary { Name = "Net451RefPackage", Version = SemanticVersion.Parse("1.0.0"), CompileTimeAssemblies = new List<LockFileItem> { Path.Combine("ref", "net451", "Net451LibPackage.dll") } }, new LockFileTargetLibrary { Name = "AssemblyPlaceholderPackage", Version = SemanticVersion.Parse("1.0.0") } } }; var metaPackageLibrary = new LockFileLibrary { Name = "MetaPackage", Version = SemanticVersion.Parse("1.0.0") }; var net451LibPackageLibrary = new LockFileLibrary { Name = "Net451LibPackage", Version = SemanticVersion.Parse("1.0.0"), Files = new List<string> { Path.Combine("lib", "net451", "Net451LibPackage.dll"), Path.Combine("lib", "net451", "Net451LibPackage.xml") } }; var net451RefPackageLibrary = new LockFileLibrary { Name = "Net451RefPackage", Version = SemanticVersion.Parse("1.0.0"), Files = new List<string> { Path.Combine("ref", "net451", "Net451LibPackage.dll") } }; var assemblyPlaceholderPackageLibrary = new LockFileLibrary { Name = "AssemblyPlaceholderPackage", Version = SemanticVersion.Parse("1.0.0"), Files = new List<string> { Path.Combine("lib", "net45", "_._"), Path.Combine("ref", "net45", "_._"), } }; var lockFile = new LockFile() { Targets = new List<LockFileTarget> { net45Target, dnx451Target }, Libraries = new List<LockFileLibrary> { metaPackageLibrary, net451LibPackageLibrary, net451RefPackageLibrary, assemblyPlaceholderPackageLibrary } }; resolver.ApplyLockFile(lockFile); var libToLookup = new LibraryRange(packageName, frameworkReference: false); Assert.Equal(resolved, resolver.GetDescription(libToLookup, new FrameworkName(framework)).Resolved); }
public static LockFileLibrary CreateLockFileLibraryForProject( Runtime.Project project, IPackage package, SHA512 sha512, IEnumerable<FrameworkName> frameworks, IPackagePathResolver resolver, string correctedPackageName = null) { var lockFileLib = new LockFileLibrary(); // package.Id is read from nuspec and it might be in wrong casing. // correctedPackageName should be the package name used by dependency graph and // it has the correct casing that runtime needs during dependency resolution. lockFileLib.Name = correctedPackageName ?? package.Id; lockFileLib.Version = package.Version; using (var nupkgStream = package.GetStream()) { lockFileLib.Sha = Convert.ToBase64String(sha512.ComputeHash(nupkgStream)); } lockFileLib.Files = package.GetFiles().Select(p => p.Path).ToList(); foreach (var framework in frameworks) { var group = new LockFileFrameworkGroup(); group.TargetFramework = framework; IEnumerable<PackageDependencySet> dependencySet; if (VersionUtility.TryGetCompatibleItems(framework, package.DependencySets, out dependencySet)) { var set = dependencySet.FirstOrDefault()?.Dependencies?.ToList(); if (set != null) { group.Dependencies = set; } } // TODO: Remove this when we do #596 // ASP.NET Core isn't compatible with generic PCL profiles if (!string.Equals(framework.Identifier, VersionUtility.AspNetCoreFrameworkIdentifier, StringComparison.OrdinalIgnoreCase) && !string.Equals(framework.Identifier, VersionUtility.DnxCoreFrameworkIdentifier, StringComparison.OrdinalIgnoreCase)) { IEnumerable<FrameworkAssemblyReference> frameworkAssemblies; if (VersionUtility.TryGetCompatibleItems(framework, package.FrameworkAssemblies, out frameworkAssemblies)) { foreach (var assemblyReference in frameworkAssemblies) { if (!assemblyReference.SupportedFrameworks.Any() && !VersionUtility.IsDesktop(framework)) { // REVIEW: This isn't 100% correct since none *can* mean // any in theory, but in practice it means .NET full reference assembly // If there's no supported target frameworks and we're not targeting // the desktop framework then skip it. // To do this properly we'll need all reference assemblies supported // by each supported target framework which isn't always available. continue; } group.FrameworkAssemblies.Add(assemblyReference); } } } group.RuntimeAssemblies = GetPackageAssemblies(package, framework); string contractPath = Path.Combine("lib", "contract", package.Id + ".dll"); var hasContract = lockFileLib.Files.Any(path => path == contractPath); var hasLib = group.RuntimeAssemblies.Any(); if (hasContract && hasLib && !VersionUtility.IsDesktop(framework)) { group.CompileTimeAssemblies.Add(contractPath); } else if (hasLib) { group.CompileTimeAssemblies.AddRange(group.RuntimeAssemblies); } lockFileLib.FrameworkGroups.Add(group); } var installPath = resolver.GetInstallPath(package.Id, package.Version); foreach (var assembly in lockFileLib.FrameworkGroups.SelectMany(f => f.RuntimeAssemblies)) { var assemblyPath = Path.Combine(installPath, assembly); if (IsAssemblyServiceable(assemblyPath)) { lockFileLib.IsServiceable = true; break; } } return lockFileLib; }