private DependencyContext BuildDependencyContextWithReferenceAssemblies(bool useCompilationOptions)
        {
            string   mainProjectName = "simple.dependencies";
            LockFile lockFile        = TestLockFiles.GetLockFile(mainProjectName);

            SingleProjectInfo mainProject = SingleProjectInfo.Create(
                "/usr/Path",
                mainProjectName,
                ".dll",
                "1.0.0",
                new ITaskItem[] { });

            ITaskItem[] referencePaths = new ITaskItem[]
            {
                new MockTaskItem(
                    "/usr/Path/System.NotConflicting.dll",
                    new Dictionary <string, string>
                {
                    { "CopyLocal", "false" },
                    { "FusionName", "System.NotConflicting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" },
                    { "Version", "" },
                }),
                new MockTaskItem(
                    "/usr/Path/System.Collections.NonGeneric.dll",
                    new Dictionary <string, string>
                {
                    { "CopyLocal", "false" },
                    { "FusionName", "System.Collections.NonGeneric, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" },
                    { "Version", "" },
                }),
                new MockTaskItem(
                    "/usr/Path/System.Collections.NonGeneric.Reference.dll",
                    new Dictionary <string, string>
                {
                    { "CopyLocal", "false" },
                    { "FusionName", "System.Collections.NonGeneric.Reference, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" },
                    { "Version", "" },
                }),
            };

            ProjectContext projectContext = lockFile.CreateProjectContext(
                FrameworkConstants.CommonFrameworks.NetCoreApp10,
                runtime: null,
                platformLibraryName: Constants.DefaultPlatformLibrary,
                isSelfContained: false);

            CompilationOptions compilationOptions =
                useCompilationOptions ? CreateCompilationOptions() :
                null;

            DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext, includeRuntimeFileVersions: false)
                                                  .WithReferenceAssemblies(ReferenceInfo.CreateReferenceInfos(referencePaths))
                                                  .WithCompilationOptions(compilationOptions)
                                                  .Build();

            // ensure the DependencyContext can be written out successfully - it has no duplicate dependency names
            Save(dependencyContext);

            return(dependencyContext);
        }
Example #2
0
        public void ItComputesExcludeFromPublishList()
        {
            LockFile       lockFile       = TestLockFiles.GetLockFile("dependencies.withgraphs");
            ProjectContext projectContext = lockFile.CreateProjectContext(
                FrameworkConstants.CommonFrameworks.NetStandard16,
                runtime: null,
                Constants.DefaultPlatformLibrary,
                runtimeFrameworks: null,
                isSelfContained: false);

            IEnumerable <string> excludeFromPublishPackageIds         = new[] { "Microsoft.Extensions.Logging.Abstractions" };
            IDictionary <string, LockFileTargetLibrary> libraryLookup =
                projectContext
                .LockFileTarget
                .Libraries
                .ToDictionary(e => e.Name, StringComparer.OrdinalIgnoreCase);

            HashSet <string> exclusionList =
                projectContext.GetExcludeFromPublishList(excludeFromPublishPackageIds, libraryLookup);

            HashSet <string> expectedExclusions = new HashSet <string>()
            {
                "Microsoft.Extensions.Logging.Abstractions",
                "System.Collections.Concurrent",
                "System.Diagnostics.Tracing",
            };

            exclusionList.Should().BeEquivalentTo(expectedExclusions);
        }
        public void ItBuildsDependencyContextsFromProjectLockFiles(
            string mainProjectName,
            string mainProjectVersion,
            CompilationOptions compilationOptions,
            string baselineFileName,
            string runtime,
            ITaskItem[] assemblySatelliteAssemblies,
            ITaskItem[] referencePaths,
            ITaskItem[] referenceSatellitePaths)
        {
            LockFile lockFile = TestLockFiles.GetLockFile(mainProjectName);

            SingleProjectInfo mainProject = SingleProjectInfo.Create(
                "/usr/Path",
                mainProjectName,
                ".dll",
                mainProjectVersion,
                assemblySatelliteAssemblies ?? new ITaskItem[] { });

            IEnumerable <ReferenceInfo> directReferences =
                ReferenceInfo.CreateDirectReferenceInfos(
                    referencePaths ?? new ITaskItem[] { },
                    referenceSatellitePaths ?? new ITaskItem[] { });

            ProjectContext projectContext = lockFile.CreateProjectContext(
                FrameworkConstants.CommonFrameworks.NetCoreApp10,
                runtime,
                Constants.DefaultPlatformLibrary,
                runtimeFrameworks: null,
                isSelfContained: !string.IsNullOrEmpty(runtime));

            DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext, includeRuntimeFileVersions: false)
                                                  .WithDirectReferences(directReferences)
                                                  .WithCompilationOptions(compilationOptions)
                                                  .Build();

            JObject result   = Save(dependencyContext);
            JObject baseline = ReadJson($"{baselineFileName}.deps.json");

            try
            {
                baseline
                .Should()
                .BeEquivalentTo(result);
            }
            catch
            {
                // write the result file out on failure for easy comparison

                using (JsonTextWriter writer = new JsonTextWriter(File.CreateText($"result-{baselineFileName}.deps.json")))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Formatting = Formatting.Indented;
                    serializer.Serialize(writer, result);
                }

                throw;
            }
        }
Example #4
0
        protected override void ExecuteCore()
        {
            var                       lockFileCache = new LockFileCache(BuildEngine4);
            LockFile                  lockFile      = lockFileCache.GetLockFile(AssetsFilePath);
            IEnumerable <string>      excludeFromPublishPackageIds = PackageReferenceConverter.GetPackageIds(ExcludeFromPublishPackageReferences);
            IPackageResolver          packageResolver      = NuGetPackageResolver.CreateResolver(lockFile, ProjectPath);
            HashSet <PackageIdentity> packagestoBeFiltered = null;

            if (TargetManifestFiles != null && TargetManifestFiles.Length > 0)
            {
                packagestoBeFiltered = new HashSet <PackageIdentity>();
                foreach (var manifestFile in TargetManifestFiles)
                {
                    Log.LogMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, Strings.ParsingFiles, manifestFile));
                    var packagesSpecified = StoreArtifactParser.Parse(manifestFile);

                    foreach (var pkg in packagesSpecified)
                    {
                        Log.LogMessage(MessageImportance.Low, string.Format(CultureInfo.CurrentCulture, Strings.PackageInfoLog, pkg.Id, pkg.Version));
                    }
                    packagestoBeFiltered.UnionWith(packagesSpecified);
                }
            }

            ProjectContext projectContext = lockFile.CreateProjectContext(
                NuGetUtils.ParseFrameworkName(TargetFramework),
                RuntimeIdentifier,
                PlatformLibraryName,
                IsSelfContained);

            projectContext.PackagesToBeFiltered = packagestoBeFiltered;

            var assemblyResolver =
                new PublishAssembliesResolver(packageResolver)
                .WithExcludeFromPublish(excludeFromPublishPackageIds)
                .WithPreserveStoreLayout(PreserveStoreLayout);

            IEnumerable <ResolvedFile> resolvedAssemblies = assemblyResolver.Resolve(projectContext);

            foreach (ResolvedFile resolvedAssembly in resolvedAssemblies)
            {
                TaskItem item = new TaskItem(resolvedAssembly.SourcePath);
                item.SetMetadata("DestinationSubPath", resolvedAssembly.DestinationSubPath);
                item.SetMetadata("AssetType", resolvedAssembly.Asset.ToString().ToLower());
                _assembliesToPublish.Add(item);
            }

            foreach (var resolvedPkg in assemblyResolver.GetResolvedPackages())
            {
                TaskItem item = new TaskItem(resolvedPkg.Id);
                item.SetMetadata("Version", resolvedPkg.Version.ToString());
                _packagesResolved.Add(item);
            }
        }
        public void ItCanGenerateTheRuntimeFallbackGraph()
        {
            string   mainProjectName = "simple.dependencies";
            LockFile lockFile        = TestLockFiles.GetLockFile(mainProjectName);

            SingleProjectInfo mainProject = SingleProjectInfo.Create(
                "/usr/Path",
                mainProjectName,
                ".dll",
                "1.0.0",
                new ITaskItem[] { });

            ProjectContext projectContext = lockFile.CreateProjectContext(
                FrameworkConstants.CommonFrameworks.NetCoreApp10.GetShortFolderName(),
                runtime: null,
                platformLibraryName: Constants.DefaultPlatformLibrary,
                runtimeFrameworks: null,
                isSelfContained: true);

            var runtimeGraph = new RuntimeGraph(
                new RuntimeDescription []
            {
                new RuntimeDescription("os-arch", new string [] { "os", "base" }),
                new RuntimeDescription("new_os-arch", new string [] { "os-arch", "os", "base" }),
                new RuntimeDescription("os-new_arch", new string [] { "os-arch", "os", "base" }),
                new RuntimeDescription("new_os-new_arch", new string [] { "new_os-arch", "os-new_arch", "os-arch", "os", "base" }),
                new RuntimeDescription("os-another_arch", new string [] { "os", "base" })
            });

            void CheckRuntimeFallbacks(string runtimeIdentifier, int fallbackCount)
            {
                projectContext.LockFileTarget.RuntimeIdentifier = runtimeIdentifier;
                var dependencyContextBuilder = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph, projectContext);
                var runtimeFallbacks         = dependencyContextBuilder.Build().RuntimeGraph;

                runtimeFallbacks
                .Count()
                .Should()
                .Be(fallbackCount);

                runtimeFallbacks
                .Any(runtimeFallback => !runtimeFallback.Runtime.Equals(runtimeIdentifier) && !runtimeFallback.Fallbacks.Contains(runtimeIdentifier))
                .Should()
                .BeFalse();
            }

            CheckRuntimeFallbacks("os-arch", 4);
            CheckRuntimeFallbacks("new_os-arch", 2);
            CheckRuntimeFallbacks("os-new_arch", 2);
            CheckRuntimeFallbacks("new_os-new_arch", 1);
            CheckRuntimeFallbacks("unrelated_os-unknown_arch", 0);
        }
Example #6
0
        protected override void ExecuteCore()
        {
            var                       lockFileCache = new LockFileCache(this);
            LockFile                  lockFile      = lockFileCache.GetLockFile(AssetsFilePath);
            IEnumerable <string>      excludeFromPublishPackageIds = PackageReferenceConverter.GetPackageIds(ExcludeFromPublishPackageReferences);
            IPackageResolver          packageResolver      = NuGetPackageResolver.CreateResolver(lockFile, ProjectPath);
            HashSet <PackageIdentity> packagestoBeFiltered = null;

            if (RuntimeStorePackages != null && RuntimeStorePackages.Length > 0)
            {
                packagestoBeFiltered = new HashSet <PackageIdentity>();
                foreach (var package in RuntimeStorePackages)
                {
                    packagestoBeFiltered.Add(ItemUtilities.GetPackageIdentity(package));
                }
            }

            ProjectContext projectContext = lockFile.CreateProjectContext(
                NuGetUtils.ParseFrameworkName(TargetFramework),
                RuntimeIdentifier,
                PlatformLibraryName,
                RuntimeFrameworks,
                IsSelfContained);

            projectContext.PackagesToBeFiltered = packagestoBeFiltered;

            var assemblyResolver =
                new PublishAssembliesResolver(packageResolver)
                .WithExcludeFromPublish(excludeFromPublishPackageIds)
                .WithPreserveStoreLayout(PreserveStoreLayout);

            IEnumerable <ResolvedFile> resolvedAssemblies = assemblyResolver.Resolve(projectContext);

            foreach (ResolvedFile resolvedAssembly in resolvedAssemblies)
            {
                TaskItem item = new TaskItem(resolvedAssembly.SourcePath);
                item.SetMetadata("DestinationSubPath", resolvedAssembly.DestinationSubPath);
                item.SetMetadata("AssetType", resolvedAssembly.Asset.ToString().ToLower());
                item.SetMetadata(MetadataKeys.PackageName, resolvedAssembly.Package.Id.ToString());
                item.SetMetadata(MetadataKeys.PackageVersion, resolvedAssembly.Package.Version.ToString().ToLower());

                if (resolvedAssembly.Asset == AssetType.Resources)
                {
                    //  For resources, the DestinationSubDirectory is set to the locale.  Set the Culture
                    //  metadata on the generated item to this value so that the satellite assemblies can
                    //  be filtered by culture.
                    item.SetMetadata(MetadataKeys.Culture, resolvedAssembly.DestinationSubDirectory);
                }

                _assembliesToPublish.Add(item);
            }
        }
Example #7
0
        protected override void ExecuteCore()
        {
            var      lockFileCache = new LockFileCache(this);
            LockFile lockFile      = lockFileCache.GetLockFile(AssetsFilePath);
            HashSet <PackageIdentity> packagestoBeFiltered = null;

            if (RuntimeStorePackages != null && RuntimeStorePackages.Length > 0)
            {
                packagestoBeFiltered = new HashSet <PackageIdentity>();
                foreach (var package in RuntimeStorePackages)
                {
                    packagestoBeFiltered.Add(ItemUtilities.GetPackageIdentity(package));
                }
            }

            ProjectContext projectContext = lockFile.CreateProjectContext(
                NuGetUtils.ParseFrameworkName(TargetFramework),
                RuntimeIdentifier,
                PlatformLibraryName,
                RuntimeFrameworks,
                IsSelfContained);

            projectContext.PackagesToBeFiltered = packagestoBeFiltered;

            var assetsFileResolver =
                new AssetsFileResolver(NuGetPackageResolver.CreateResolver(lockFile))
                .WithExcludedPackages(PackageReferenceConverter.GetPackageIds(ExcludedPackageReferences))
                .WithPreserveStoreLayout(PreserveStoreLayout);

            foreach (var resolvedFile in assetsFileResolver.Resolve(projectContext))
            {
                TaskItem item = new TaskItem(resolvedFile.SourcePath);

                item.SetMetadata(MetadataKeys.DestinationSubPath, resolvedFile.DestinationSubPath);
                item.SetMetadata(MetadataKeys.DestinationSubDirectory, resolvedFile.DestinationSubDirectory);
                item.SetMetadata(MetadataKeys.AssetType, resolvedFile.Asset.ToString().ToLowerInvariant());
                item.SetMetadata(MetadataKeys.PackageName, resolvedFile.Package.Id.ToString());
                item.SetMetadata(MetadataKeys.PackageVersion, resolvedFile.Package.Version.ToString().ToLowerInvariant());

                if (resolvedFile.Asset == AssetType.Resources)
                {
                    //  For resources, the DestinationSubDirectory is set to the locale.  Set the Culture
                    //  metadata on the generated item to this value so that the satellite assemblies can
                    //  be filtered by culture.
                    item.SetMetadata(MetadataKeys.Culture, resolvedFile.DestinationSubDirectory.TrimEnd(Path.DirectorySeparatorChar));
                }

                _resolvedAssets.Add(item);
            }
        }
        public void ItResolvesAssembliesFromProjectLockFiles(string projectName, string runtime, object[] expectedResolvedFiles)
        {
            LockFile       lockFile       = TestLockFiles.GetLockFile(projectName);
            ProjectContext projectContext = lockFile.CreateProjectContext(
                FrameworkConstants.CommonFrameworks.NetCoreApp10,
                runtime,
                Constants.DefaultPlatformLibrary);

            IEnumerable <ResolvedFile> resolvedFiles = new PublishAssembliesResolver(new MockPackageResolver())
                                                       .Resolve(projectContext);

            resolvedFiles
            .Should()
            .BeEquivalentTo(expectedResolvedFiles);
        }
Example #9
0
        protected override void ExecuteCore()
        {
            var      lockFileCache = new LockFileCache(this);
            LockFile lockFile      = lockFileCache.GetLockFile(AssetsFilePath);

            ProjectContext projectContext = lockFile.CreateProjectContext(
                NuGetUtils.ParseFrameworkName(TargetFramework),
                RuntimeIdentifier,
                PlatformLibraryName,
                runtimeFrameworks: null,
                IsSelfContained);

            var packageClosure = new HashSet <PackageIdentity>();

            foreach (var packageItem in PackagesToPrune)
            {
                var pkgName = packageItem.ItemSpec;
                if (!string.IsNullOrEmpty(pkgName))
                {
                    packageClosure.UnionWith(projectContext.GetTransitiveList(pkgName));
                }
            }

            var packagesToPublish = new HashSet <PackageIdentity>();

            foreach (var resolvedFile in ResolvedFiles)
            {
                var resolvedPkg = ItemUtilities.GetPackageIdentity(resolvedFile);

                if (resolvedPkg != null && !packageClosure.Contains(resolvedPkg))
                {
                    _assembliesToPublish.Add(resolvedFile);
                    packagesToPublish.Add(resolvedPkg);
                }
            }
            AssembliesToPublish = _assembliesToPublish.ToArray();

            foreach (var resolvedPkg in packagesToPublish)
            {
                TaskItem item = new TaskItem(resolvedPkg.Id);
                item.SetMetadata("Version", resolvedPkg.Version.ToString());
                _packagesResolved.Add(item);
            }
            PublishedPackages = _packagesResolved.ToArray();
        }
        public void ItResolvesAssembliesFromProjectLockFilesWithStoreLayout(string projectName, string runtime, object[] expectedResolvedFiles)
        {
            LockFile       lockFile       = TestLockFiles.GetLockFile(projectName);
            ProjectContext projectContext = lockFile.CreateProjectContext(
                FrameworkConstants.CommonFrameworks.NetCoreApp10,
                runtime,
                Constants.DefaultPlatformLibrary,
                runtimeFrameworks: null,
                isSelfContained: false);

            IEnumerable <ResolvedFile> resolvedFiles = new AssetsFileResolver(new MockPackageResolver())
                                                       .WithPreserveStoreLayout(true)
                                                       .Resolve(projectContext);

            resolvedFiles
            .Should()
            .BeEquivalentTo(expectedResolvedFiles);
        }