예제 #1
0
        public PackageDescription GetDescription(LockFilePackageLibrary package, LockFileTargetLibrary targetLibrary)
        {
            // If a NuGet dependency is supposed to provide assemblies but there is no assembly compatible with
            // current target framework, we should mark this dependency as unresolved
            var containsAssembly = package.Files
                                   .Any(x => x.StartsWith($"ref{Path.DirectorySeparatorChar}") ||
                                        x.StartsWith($"lib{Path.DirectorySeparatorChar}"));

            var compatible = targetLibrary.FrameworkAssemblies.Any() ||
                             targetLibrary.CompileTimeAssemblies.Any() ||
                             targetLibrary.RuntimeAssemblies.Any() ||
                             !containsAssembly;

            var dependencies = new List <LibraryRange>(targetLibrary.Dependencies.Count + targetLibrary.FrameworkAssemblies.Count);

            PopulateDependencies(dependencies, targetLibrary);

            var path = _packagePathResolver.GetInstallPath(package.Name, package.Version);

            var packageDescription = new PackageDescription(
                path,
                package,
                targetLibrary,
                dependencies,
                compatible);

            return(packageDescription);
        }
예제 #2
0
        public void GetDescriptionShouldGenerateHashFileName()
        {
            // Arrange
            var provider = new PackageDependencyProvider(
                NuGetPathContext.Create("/foo/packages"),
                new FrameworkReferenceResolver("/foo/references"));
            var package = new LockFilePackageLibrary();

            package.Name    = "Something";
            package.Version = NuGetVersion.Parse("1.0.0-Beta");
            package.Files.Add("lib/dotnet/_._");
            package.Files.Add("runtimes/any/native/Microsoft.CSharp.CurrentVersion.targets");
            package.Path = "SomePath";

            var target = new LockFileTargetLibrary();

            target.Name    = "Something";
            target.Version = package.Version;

            target.RuntimeAssemblies.Add("lib/dotnet/_._");
            target.CompileTimeAssemblies.Add("lib/dotnet/_._");
            target.NativeLibraries.Add("runtimes/any/native/Microsoft.CSharp.CurrentVersion.targets");

            // Act
            var p = provider.GetDescription(NuGetFramework.Parse("netcoreapp1.0"), package, target);

            // Assert
            p.PackageLibrary.Path.Should().Be("SomePath");
            p.HashPath.Should().Be("something.1.0.0-beta.nupkg.sha512");
        }
예제 #3
0
        private string GetPackageDirectoryFullPath(LockFilePackageLibrary library, string nugetPackagesRoot)
        {
            var packageDirectory = new VersionFolderPathResolver(nugetPackagesRoot)
                                   .GetInstallPath(library.Name, library.Version);

            return(packageDirectory);
        }
예제 #4
0
        public CommandSpec CreateCommandSpecFromLibrary(
            LockFilePackageLibrary library,
            string commandName,
            IEnumerable <string> commandArguments,
            IEnumerable <string> allowedExtensions,
            string nugetPackagesRoot,
            CommandResolutionStrategy commandResolutionStrategy,
            string depsFilePath)
        {
            var packageDirectory = GetPackageDirectoryFullPath(library, nugetPackagesRoot);

            if (!Directory.Exists(packageDirectory))
            {
                return(null);
            }

            var commandFile = GetCommandFileRelativePath(library, commandName, allowedExtensions);

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

            var commandPath = Path.Combine(packageDirectory, commandFile);

            return(CreateCommandSpecWrappingWithCorehostfDll(
                       commandPath,
                       commandArguments,
                       depsFilePath,
                       commandResolutionStrategy));
        }
예제 #5
0
        public void GetDescriptionShouldNotModifyTarget()
        {
            var provider = new PackageDependencyProvider(
                NuGetPathContext.Create("/foo/packages"),
                new FrameworkReferenceResolver("/foo/references"));
            var package = new LockFilePackageLibrary();

            package.Name    = "Something";
            package.Version = NuGetVersion.Parse("1.0.0");
            package.Files.Add("lib/dotnet/_._");
            package.Files.Add("runtimes/any/native/Microsoft.CSharp.CurrentVersion.targets");

            var target = new LockFileTargetLibrary();

            target.Name    = "Something";
            target.Version = package.Version;

            target.RuntimeAssemblies.Add("lib/dotnet/_._");
            target.CompileTimeAssemblies.Add("lib/dotnet/_._");
            target.NativeLibraries.Add("runtimes/any/native/Microsoft.CSharp.CurrentVersion.targets");

            var p1 = provider.GetDescription(NuGetFramework.Parse("netcoreapp1.0"), package, target);
            var p2 = provider.GetDescription(NuGetFramework.Parse("netcoreapp1.0"), package, target);

            Assert.True(p1.Compatible);
            Assert.True(p2.Compatible);

            Assert.Empty(p1.CompileTimeAssemblies);
            Assert.Empty(p1.RuntimeAssemblies);

            Assert.Empty(p2.CompileTimeAssemblies);
            Assert.Empty(p2.RuntimeAssemblies);
        }
예제 #6
0
        public void HasCompileTimePlaceholderChecksAllCompileTimeAssets()
        {
            var provider = new PackageDependencyProvider(
                NuGetPathContext.Create("/foo/packages"),
                new FrameworkReferenceResolver("/foo/references"));
            var package = new LockFilePackageLibrary();

            package.Name    = "Something";
            package.Version = NuGetVersion.Parse("1.0.0");
            package.Files.Add("lib/net46/_._");
            package.Files.Add("lib/net46/Something.dll");

            var target = new LockFileTargetLibrary();

            target.Name    = "Something";
            target.Version = package.Version;

            target.RuntimeAssemblies.Add("lib/net46/_._");
            target.RuntimeAssemblies.Add("lib/net46/Something.dll");
            target.CompileTimeAssemblies.Add("lib/net46/_._");
            target.CompileTimeAssemblies.Add("lib/net46/Something.dll");

            var p1 = provider.GetDescription(NuGetFramework.Parse("net46"), package, target);

            Assert.False(p1.HasCompileTimePlaceholder);
            Assert.Equal(1, p1.CompileTimeAssemblies.Count());
            Assert.Equal(1, p1.RuntimeAssemblies.Count());
            Assert.Equal("lib/net46/Something.dll", p1.CompileTimeAssemblies.First().Path);
            Assert.Equal("lib/net46/Something.dll", p1.RuntimeAssemblies.First().Path);
        }
예제 #7
0
 private string GetCommandFileRelativePath(
     LockFilePackageLibrary library,
     string commandName,
     IEnumerable <string> allowedExtensions)
 {
     // TODO: Should command names be case sensitive?
     return(library.Files
            .Where(f => Path.GetFileNameWithoutExtension(f) == commandName)
            .Where(e => allowedExtensions.Contains(Path.GetExtension(e)))
            .FirstOrDefault());
 }
예제 #8
0
 private PackageDescription CreateDescription(
     LockFileTargetLibrary target   = null,
     LockFilePackageLibrary package = null,
     string hashPath = null)
 {
     return(new PackageDescription(
                PackagePath,
                hashPath ?? HashPath,
                package ?? new LockFilePackageLibrary(),
                target ?? new LockFileTargetLibrary(),
                new List <LibraryRange>(), compatible: true, resolved: true));
 }
예제 #9
0
 public PackageInfo(
     IFileSystem repositoryRoot,
     string packageId,
     SemanticVersion version,
     string versionDir,
     LockFilePackageLibrary lockFileLibrary = null)
 {
     _repositoryRoot = repositoryRoot;
     Id              = packageId;
     Version         = version;
     _versionDir     = versionDir;
     LockFileLibrary = lockFileLibrary;
 }
 private CommandSpec ResolveFromDependencyLibrary(
     LockFilePackageLibrary dependencyLibrary,
     string depsFilePath,
     string commandName,
     IEnumerable <string> allowedExtensions,
     IEnumerable <string> commandArguments,
     ProjectContext projectContext)
 {
     return(_packagedCommandSpecFactory.CreateCommandSpecFromLibrary(
                dependencyLibrary,
                commandName,
                commandArguments,
                allowedExtensions,
                projectContext.PackagesDirectory,
                s_commandResolutionStrategy,
                depsFilePath));
 }
예제 #11
0
        public static LockFilePackageLibrary CreateLockFilePackageLibrary(LockFilePackageLibrary previousLibrary, IPackagePathResolver resolver, IPackage package, string correctedPackageName = null)
        {
            var lockFileLib = new LockFilePackageLibrary();

            // 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);
        }
예제 #12
0
        public PackageDescription GetDescription(NuGetFramework targetFramework, LockFilePackageLibrary package, LockFileTargetLibrary targetLibrary)
        {
            // If a NuGet dependency is supposed to provide assemblies but there is no assembly compatible with
            // current target framework, we should mark this dependency as unresolved
            var containsAssembly = package.Files
                                   .Any(x => x.StartsWith($"ref{Path.DirectorySeparatorChar}") ||
                                        x.StartsWith($"lib{Path.DirectorySeparatorChar}"));

            var compatible = targetLibrary.FrameworkAssemblies.Any() ||
                             targetLibrary.CompileTimeAssemblies.Any() ||
                             targetLibrary.RuntimeAssemblies.Any() ||
                             !containsAssembly;

            var dependencies = new List <LibraryRange>(targetLibrary.Dependencies.Count + targetLibrary.FrameworkAssemblies.Count);

            PopulateDependencies(dependencies, targetLibrary, targetFramework);

            var  path   = _packagePathResolver?.GetPackageDirectory(package.Name, package.Version);
            bool exists = path != null;

            string hashPath = null;

            if (_versionFolderPathResolver != null)
            {
                hashPath = _versionFolderPathResolver.GetHashFileName(package.Name, package.Version);
            }

            if (exists)
            {
                // If the package's compile time assemblies is for a portable profile then, read the assembly metadata
                // and turn System.* references into reference assembly dependencies
                PopulateLegacyPortableDependencies(targetFramework, dependencies, path, targetLibrary);
            }

            var packageDescription = new PackageDescription(
                path,
                hashPath,
                package,
                targetLibrary,
                dependencies,
                compatible,
                resolved: compatible && exists);

            return(packageDescription);
        }
예제 #13
0
 public PackageDescription(
     string path,
     LockFilePackageLibrary package,
     LockFileTargetLibrary lockFileLibrary,
     IEnumerable<LibraryRange> dependencies,
     bool compatible)
     : base(
           new LibraryIdentity(package.Name, package.Version, LibraryType.Package),
           "sha512-" + package.Sha512,
           path,
           dependencies: dependencies,
           framework: null,
           resolved: compatible,
           compatible: compatible)
 {
     Library = package;
     Target = lockFileLibrary;
 }
예제 #14
0
        public void HasCompileTimePlaceholderReturnsFalseIfEmpty()
        {
            var provider = new PackageDependencyProvider("/foo/packages", new FrameworkReferenceResolver("/foo/references"));
            var package  = new LockFilePackageLibrary();

            package.Name    = "Something";
            package.Version = NuGetVersion.Parse("1.0.0");

            var target = new LockFileTargetLibrary();

            target.Name    = "Something";
            target.Version = package.Version;

            var p1 = provider.GetDescription(NuGetFramework.Parse("net46"), package, target);

            Assert.False(p1.HasCompileTimePlaceholder);
            Assert.Equal(0, p1.CompileTimeAssemblies.Count());
            Assert.Equal(0, p1.RuntimeAssemblies.Count());
        }
예제 #15
0
 public PackageDescription(
     string path,
     string hashPath,
     LockFilePackageLibrary package,
     LockFileTargetLibrary lockFileLibrary,
     IEnumerable <LibraryRange> dependencies,
     bool compatible,
     bool resolved)
     : base(
         new LibraryIdentity(package.Name, package.Version, LibraryType.Package),
         "sha512-" + package.Sha512,
         path,
         lockFileLibrary,
         dependencies,
         resolved: resolved,
         compatible: compatible,
         framework: null)
 {
     HashPath       = hashPath;
     PackageLibrary = package;
 }
예제 #16
0
        public PackageDescription GetDescription(NuGetFramework targetFramework, LockFilePackageLibrary package, LockFileTargetLibrary targetLibrary)
        {
            // If a NuGet dependency is supposed to provide assemblies but there is no assembly compatible with
            // current target framework, we should mark this dependency as unresolved
            var containsAssembly = package.Files
                                   .Any(x => x.StartsWith($"ref{Path.DirectorySeparatorChar}") ||
                                        x.StartsWith($"lib{Path.DirectorySeparatorChar}"));

            var compatible = targetLibrary.FrameworkAssemblies.Any() ||
                             targetLibrary.CompileTimeAssemblies.Any() ||
                             targetLibrary.RuntimeAssemblies.Any() ||
                             !containsAssembly;

            var dependencies = new List <LibraryRange>(targetLibrary.Dependencies.Count + targetLibrary.FrameworkAssemblies.Count);

            PopulateDependencies(dependencies, targetLibrary, targetFramework);

            var path = _packagePathResolver.GetInstallPath(package.Name, package.Version);

            // Remove place holders
            targetLibrary.CompileTimeAssemblies = targetLibrary.CompileTimeAssemblies.Where(item => !IsPlaceholderFile(item.Path)).ToList();
            targetLibrary.RuntimeAssemblies     = targetLibrary.RuntimeAssemblies.Where(item => !IsPlaceholderFile(item.Path)).ToList();

            // If the package's compile time assemblies is for a portable profile then, read the assembly metadata
            // and turn System.* references into reference assembly dependencies
            PopulateLegacyPortableDependencies(targetFramework, dependencies, path, targetLibrary);

            var packageDescription = new PackageDescription(
                path,
                package,
                targetLibrary,
                dependencies,
                compatible);

            return(packageDescription);
        }
예제 #17
0
        public void LibrariesAreResolvedCorrectly(string packageName, string framework, bool resolved)
        {
            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 LockFilePackageLibrary
            {
                Name    = "MetaPackage",
                Version = SemanticVersion.Parse("1.0.0")
            };

            var net451LibPackageLibrary = new LockFilePackageLibrary
            {
                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 LockFilePackageLibrary
            {
                Name    = "Net451RefPackage",
                Version = SemanticVersion.Parse("1.0.0"),
                Files   = new List <string>
                {
                    Path.Combine("ref", "net451", "Net451LibPackage.dll")
                }
            };

            var assemblyPlaceholderPackageLibrary = new LockFilePackageLibrary
            {
                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
                },
                PackageLibraries = new List <LockFilePackageLibrary>
                {
                    metaPackageLibrary,
                    net451LibPackageLibrary,
                    net451RefPackageLibrary,
                    assemblyPlaceholderPackageLibrary
                }
            };

            var resolver       = new PackageDependencyProvider("/path/to/packages");
            var lockFileLookup = new LockFileLookup(lockFile);

            var targetFramework = new FrameworkName(framework);
            var libToLookup     = lockFile.Targets.First(t => t.TargetFramework == targetFramework)
                                  .Libraries
                                  .First(l => l.Name == packageName);

            var description = resolver.GetDescription(lockFileLookup.GetPackage(libToLookup.Name, libToLookup.Version), libToLookup);

            Assert.Equal(resolved, description.Compatible);
            Assert.NotNull(description);
        }
예제 #18
0
        public static LockFileTargetLibrary CreateLockFileTargetLibrary(LockFilePackageLibrary library,
                                                                        IPackage package,
                                                                        RestoreContext context,
                                                                        string correctedPackageName)
        {
            var lockFileLib = new LockFileTargetLibrary {
                Type = "package"
            };

            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 compile-time assemblies of which names do not appear in the References list
                    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 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 LockFilePackageLibrary
            {
                Name = "MetaPackage",
                Version = SemanticVersion.Parse("1.0.0")
            };

            var net451LibPackageLibrary = new LockFilePackageLibrary
            {
                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 LockFilePackageLibrary
            {
                Name = "Net451RefPackage",
                Version = SemanticVersion.Parse("1.0.0"),
                Files = new List<string>
                {
                    Path.Combine("ref", "net451", "Net451LibPackage.dll")
                }
            };

            var assemblyPlaceholderPackageLibrary = new LockFilePackageLibrary
            {
                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 },
                PackageLibraries = new List<LockFilePackageLibrary>
                {
                    metaPackageLibrary,
                    net451LibPackageLibrary,
                    net451RefPackageLibrary,
                    assemblyPlaceholderPackageLibrary
                }
            };

            var resolver = new PackageDependencyProvider("/path/to/packages");
            var lockFileLookup = new LockFileLookup(lockFile);

            var targetFramework = new FrameworkName(framework);
            var libToLookup = lockFile.Targets.First(t => t.TargetFramework == targetFramework)
                                              .Libraries
                                              .First(l => l.Name == packageName);

            var description = resolver.GetDescription(lockFileLookup.GetPackage(libToLookup.Name, libToLookup.Version), libToLookup);
            Assert.Equal(resolved, description.Compatible);
            Assert.NotNull(description);
        }
예제 #20
0
 private PackageDescription CreateDescription(LockFileTargetLibrary target = null, LockFilePackageLibrary package = null)
 {
     return(new PackageDescription(PackagePath,
                                   package ?? new LockFilePackageLibrary(),
                                   target ?? new LockFileTargetLibrary(),
                                   new List <LibraryRange>(), true));
 }
예제 #21
0
        private void WriteLockFile(LockFile previousLockFile,
                                   string projectLockFilePath,
                                   Runtime.Project project,
                                   List <GraphItem> graphItems,
                                   PackageRepository repository,
                                   IProjectResolver projectResolver,
                                   IEnumerable <TargetContext> contexts)
        {
            var resolver = new DefaultPackagePathResolver(repository.RepositoryRoot.Root);
            var previousPackageLibraries = previousLockFile?.PackageLibraries.ToDictionary(l => Tuple.Create(l.Name, l.Version));

            var lockFile = new LockFile();

            lockFile.Islocked = Lock;

            // Use empty string as the key of dependencies shared by all frameworks
            lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                                                         string.Empty,
                                                         project.Dependencies.Select(x => x.LibraryRange.ToString())));

            foreach (var frameworkInfo in project.GetTargetFrameworks())
            {
                lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                                                             frameworkInfo.FrameworkName.ToString(),
                                                             frameworkInfo.Dependencies.Select(x => x.LibraryRange.ToString())));
            }

            // Record all libraries used
            foreach (var item in graphItems.OrderBy(x => x.Match.Library, new LibraryComparer()))
            {
                if (item.Match.LibraryType.Equals(Runtime.LibraryTypes.Implicit))
                {
                    continue;
                }

                var library = item.Match.Library;
                if (library.Name == project.Name)
                {
                    continue;
                }

                if (item.Match.LibraryType.Equals(Runtime.LibraryTypes.Project))
                {
                    var projectDependency = projectResolver.FindProject(library.Name);
                    var projectLibrary    = LockFileUtils.CreateLockFileProjectLibrary(project, projectDependency);

                    lockFile.ProjectLibraries.Add(projectLibrary);
                }
                else if (item.Match.LibraryType.Equals(Runtime.LibraryTypes.Package))
                {
                    var packageInfo = repository.FindPackagesById(library.Name)
                                      .FirstOrDefault(p => p.Version == library.Version);

                    if (packageInfo == null)
                    {
                        throw new InvalidOperationException($"Unresolved package: {library.Name}");
                    }

                    LockFilePackageLibrary previousLibrary = null;
                    previousPackageLibraries?.TryGetValue(Tuple.Create(library.Name, library.Version), out previousLibrary);

                    var package = packageInfo.Package;

                    // The previousLibrary can't be a project, otherwise exception has been thrown.
                    lockFile.PackageLibraries.Add(LockFileUtils.CreateLockFilePackageLibrary(
                                                      previousLibrary,
                                                      resolver,
                                                      package,
                                                      correctedPackageName: library.Name));
                }
                else
                {
                    throw new InvalidOperationException($"Unresolved library: {library.Name}");
                }
            }

            var packageLibraries = lockFile.PackageLibraries.ToDictionary(lib => Tuple.Create(lib.Name, lib.Version));

            // Add the contexts
            foreach (var context in contexts)
            {
                var target = new LockFileTarget();
                target.TargetFramework   = context.RestoreContext.FrameworkName;
                target.RuntimeIdentifier = context.RestoreContext.RuntimeName;

                foreach (var match in context.Matches.OrderBy(x => x.Library, new LibraryComparer()))
                {
                    if (match.Library.Name == project.Name)
                    {
                        continue;
                    }

                    if (match.LibraryType.Equals(Runtime.LibraryTypes.Project))
                    {
                        var projectDependency    = projectResolver.FindProject(match.Library.Name);
                        var projectTargetLibrary = LockFileUtils.CreateLockFileTargetLibrary(projectDependency, context.RestoreContext);
                        target.Libraries.Add(projectTargetLibrary);
                    }
                    else if (match.LibraryType.Equals(Runtime.LibraryTypes.Package))
                    {
                        var packageInfo = repository.FindPackagesById(match.Library.Name)
                                          .FirstOrDefault(p => p.Version == match.Library.Version);

                        var package = packageInfo.Package;

                        target.Libraries.Add(LockFileUtils.CreateLockFileTargetLibrary(
                                                 packageLibraries[Tuple.Create(match.Library.Name, match.Library.Version)],
                                                 package,
                                                 context.RestoreContext,
                                                 correctedPackageName: match.Library.Name));
                    }
                }

                lockFile.Targets.Add(target);
            }

            var lockFileFormat = new LockFileFormat();

            lockFileFormat.Write(projectLockFilePath, lockFile);
        }
예제 #22
0
        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 LockFilePackageLibrary
            {
                Name    = "MetaPackage",
                Version = SemanticVersion.Parse("1.0.0")
            };

            var net451LibPackageLibrary = new LockFilePackageLibrary
            {
                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 LockFilePackageLibrary
            {
                Name    = "Net451RefPackage",
                Version = SemanticVersion.Parse("1.0.0"),
                Files   = new List <string>
                {
                    Path.Combine("ref", "net451", "Net451LibPackage.dll")
                }
            };

            var assemblyPlaceholderPackageLibrary = new LockFilePackageLibrary
            {
                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
                },
                PackageLibraries = new List <LockFilePackageLibrary>
                {
                    metaPackageLibrary,
                    net451LibPackageLibrary,
                    net451RefPackageLibrary,
                    assemblyPlaceholderPackageLibrary
                }
            };

            resolver.ApplyLockFile(lockFile);

            var libToLookup = new LibraryRange(packageName, frameworkReference: false);

            Assert.Equal(resolved, resolver.GetDescription(libToLookup, new FrameworkName(framework)).Resolved);
        }
예제 #23
0
        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 LockFilePackageLibrary
            {
                Name = "MetaPackage",
                Version = SemanticVersion.Parse("1.0.0")
            };

            var net451LibPackageLibrary = new LockFilePackageLibrary
            {
                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 LockFilePackageLibrary
            {
                Name = "Net451RefPackage",
                Version = SemanticVersion.Parse("1.0.0"),
                Files = new List<string>
                {
                    Path.Combine("ref", "net451", "Net451LibPackage.dll")
                }
            };

            var assemblyPlaceholderPackageLibrary = new LockFilePackageLibrary
            {
                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 },
                PackageLibraries = new List<LockFilePackageLibrary>
                {
                    metaPackageLibrary,
                    net451LibPackageLibrary,
                    net451RefPackageLibrary,
                    assemblyPlaceholderPackageLibrary
                }
            };

            resolver.ApplyLockFile(lockFile);

            var libToLookup = new LibraryRange(packageName, frameworkReference: false);
            Assert.Equal(resolved, resolver.GetDescription(libToLookup, new FrameworkName(framework)).Resolved);
        }