private static void AddPackageReferences(MsBuildProject packagesProject, IEnumerable <PackageReference> packageReferences)
        {
            var itemGroup = packagesProject.GetItemGroupWithItemOfTypeOrCreateNew(PackageReferenceTypeTag);

            foreach (var packageReference in packageReferences)
            {
                Action action = CheckForConflict(packagesProject, packageReference) switch
                {
                    PackageReferenceConflictResult.NoExisting _ => () => AddPackageReference(itemGroup, packageReference),
                    PackageReferenceConflictResult.ExistingIsCompatible _ => Functional.NoOperation,
                    PackageReferenceConflictResult.ExistingIsIncompatible result => throw new ConflictingPackageReferenceException(packageReference, result.Version),
                          var result => throw new InvalidOperationException($"Enum variant {result.GetType().Name} is not supported"),
                };
                action();
            }
        }
Esempio n. 2
0
        private void AddPackageReferencesToProjectWithoutGuard(MsBuildProject project, IEnumerable <PackageReference> packageReferences)
        {
            var usesCentralPackages      = _centralPackageVersionsManipulator.ProjectUsesCentralPackageVersionsSdk(project);
            var addVersionMetadataToItem = usesCentralPackages
                ? (AddVersionMetadata)NoOperation
                : AddVersionMetadataToItem;

            var itemGroup = project.GetItemGroupWithItemOfTypeOrCreateNew(PackageReferenceTypeTag);

            void AddPackageReferenceCurried(PackageReference packageReference) => AddPackageReference(itemGroup, packageReference, addVersionMetadataToItem);

            foreach (var packageReference in packageReferences)
            {
                AddPackageReferenceWithValidation(project, packageReference, AddPackageReferenceCurried);
            }

            if (usesCentralPackages)
            {
                _centralPackageVersionsManipulator.AddPackageReferencesToGlobalPackages(project, packageReferences);
            }
        }